1use crate::{
2 item::{Item, ItemEvent},
3 ItemNavHistory, WorkspaceId,
4};
5use call::participant::{Frame, RemoteVideoTrack};
6use client::{proto::PeerId, User};
7use futures::StreamExt;
8use gpui::{
9 elements::*,
10 geometry::{rect::RectF, vector::vec2f},
11 platform::MouseButton,
12 AppContext, Entity, RenderContext, Task, View, ViewContext,
13};
14use settings::Settings;
15use smallvec::SmallVec;
16use std::sync::{Arc, Weak};
17
18pub enum Event {
19 Close,
20}
21
22pub struct SharedScreen {
23 track: Weak<RemoteVideoTrack>,
24 frame: Option<Frame>,
25 pub peer_id: PeerId,
26 user: Arc<User>,
27 nav_history: Option<ItemNavHistory>,
28 _maintain_frame: Task<()>,
29}
30
31impl SharedScreen {
32 pub fn new(
33 track: &Arc<RemoteVideoTrack>,
34 peer_id: PeerId,
35 user: Arc<User>,
36 cx: &mut ViewContext<Self>,
37 ) -> Self {
38 let mut frames = track.frames();
39 Self {
40 track: Arc::downgrade(track),
41 frame: None,
42 peer_id,
43 user,
44 nav_history: Default::default(),
45 _maintain_frame: cx.spawn(|this, mut cx| async move {
46 while let Some(frame) = frames.next().await {
47 this.update(&mut cx, |this, cx| {
48 this.frame = Some(frame);
49 cx.notify();
50 })
51 }
52 this.update(&mut cx, |_, cx| cx.emit(Event::Close));
53 }),
54 }
55 }
56}
57
58impl Entity for SharedScreen {
59 type Event = Event;
60}
61
62impl View for SharedScreen {
63 fn ui_name() -> &'static str {
64 "SharedScreen"
65 }
66
67 fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
68 enum Focus {}
69
70 let frame = self.frame.clone();
71 MouseEventHandler::<Focus>::new(0, cx, |_, cx| {
72 Canvas::new(move |bounds, _, cx| {
73 if let Some(frame) = frame.clone() {
74 let size = constrain_size_preserving_aspect_ratio(
75 bounds.size(),
76 vec2f(frame.width() as f32, frame.height() as f32),
77 );
78 let origin = bounds.origin() + (bounds.size() / 2.) - size / 2.;
79 cx.scene.push_surface(gpui::platform::mac::Surface {
80 bounds: RectF::new(origin, size),
81 image_buffer: frame.image(),
82 });
83 }
84 })
85 .contained()
86 .with_style(cx.global::<Settings>().theme.shared_screen)
87 .boxed()
88 })
89 .on_down(MouseButton::Left, |_, cx| cx.focus_parent_view())
90 .boxed()
91 }
92}
93
94impl Item for SharedScreen {
95 fn deactivated(&mut self, cx: &mut ViewContext<Self>) {
96 if let Some(nav_history) = self.nav_history.as_ref() {
97 nav_history.push::<()>(None, cx);
98 }
99 }
100
101 fn tab_content(
102 &self,
103 _: Option<usize>,
104 style: &theme::Tab,
105 _: &AppContext,
106 ) -> gpui::ElementBox {
107 Flex::row()
108 .with_child(
109 Svg::new("icons/disable_screen_sharing_12.svg")
110 .with_color(style.label.text.color)
111 .constrained()
112 .with_width(style.type_icon_width)
113 .aligned()
114 .contained()
115 .with_margin_right(style.spacing)
116 .boxed(),
117 )
118 .with_child(
119 Label::new(
120 format!("{}'s screen", self.user.github_login),
121 style.label.clone(),
122 )
123 .aligned()
124 .boxed(),
125 )
126 .boxed()
127 }
128
129 fn set_nav_history(&mut self, history: ItemNavHistory, _: &mut ViewContext<Self>) {
130 self.nav_history = Some(history);
131 }
132
133 fn clone_on_split(
134 &self,
135 _workspace_id: WorkspaceId,
136 cx: &mut ViewContext<Self>,
137 ) -> Option<Self> {
138 let track = self.track.upgrade()?;
139 Some(Self::new(&track, self.peer_id, self.user.clone(), cx))
140 }
141
142 fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
143 match event {
144 Event::Close => smallvec::smallvec!(ItemEvent::CloseItem),
145 }
146 }
147}