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