BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / call / view_actions.rs

view_actions.rs

view_actions.rs 9.8 KiB Download Raw
1use std::time::{Duration, Instant};
2
3use calling::CallCommand;
4use gpui_kit::*;
5
6use super::caption_style::{CaptionPosition, CaptionSize};
7use super::bubbles::{BUBBLE_SHOWN, ChatBubble, push_bubble};
8use super::fullscreen::{FullscreenState, HINT_SHOWN};
9use super::view_settings::{CallLayout, ViewSettings};
10use crate::app_state::{AppEvent, AppState};
11use crate::views::call_layouts::{measured_stage, video_wants};
12
13const CHROME_TOUCH_THROTTLE: Duration = Duration::from_millis(250);
14const DEMO_BUBBLE_START: Duration = Duration::from_secs(8);
15const DEMO_BUBBLE_EVERY: Duration = Duration::from_secs(7);
16const DEMO_BUBBLES: [(&str, &str); 4] = [
17 ("Jonas Ortega", "Slides are in the channel now"),
18 (
19 "Priya Nair",
20 "Can you share the release notes with us before we start the demo?",
21 ),
22 ("Lea Schneider", "Looks good to me, thanks"),
23 ("Tobias Klein", "I will join the retro in five minutes"),
24];
25const TIMER_SLACK: Duration = Duration::from_millis(30);
26
27impl AppState {
28 fn change_call_view(&mut self, change: impl FnOnce(&mut ViewSettings), cx: &mut Context<Self>) {
29 change(&mut self.call_view);
30 self.call_view.hide_me = false;
31 self.call_view.save(&self.store);
32 if let Some(call) = self.call.as_mut() {
33 let hide_me = call.model.view.hide_me;
34 call.model.view = self.call_view;
35 call.model.view.hide_me = hide_me;
36 call.model.refresh_layout(Instant::now());
37 }
38 cx.notify();
39 }
40
41 pub fn set_call_layout(&mut self, layout: CallLayout, cx: &mut Context<Self>) {
42 self.change_call_view(|view| view.choose(layout), cx);
43 }
44
45 pub fn toggle_call_hide_no_video(&mut self, cx: &mut Context<Self>) {
46 self.change_call_view(|view| view.hide_no_video = !view.hide_no_video, cx);
47 }
48
49 pub fn set_call_caption_size(&mut self, size: CaptionSize, cx: &mut Context<Self>) {
50 self.change_call_view(|view| view.caption_size = size, cx);
51 }
52
53 pub fn set_call_caption_position(&mut self, position: CaptionPosition, cx: &mut Context<Self>) {
54 self.change_call_view(|view| view.caption_position = position, cx);
55 }
56
57 pub fn toggle_call_chat_bubbles(&mut self, cx: &mut Context<Self>) {
58 self.change_call_view(|view| view.chat_bubbles = !view.chat_bubbles, cx);
59 }
60
61 pub fn set_call_hide_me(&mut self, hidden: bool, cx: &mut Context<Self>) {
62 if let Some(call) = self.call.as_mut() {
63 call.model.view.hide_me = hidden;
64 call.model.refresh_layout(Instant::now());
65 cx.notify();
66 }
67 }
68
69 pub fn tick_call_layout(&mut self, cx: &mut Context<Self>) {
70 let changed = self
71 .call
72 .as_mut()
73 .is_some_and(|call| call.model.refresh_layout(Instant::now()));
74 self.sync_video_wants();
75 if changed {
76 cx.notify();
77 }
78 }
79
80 fn sync_video_wants(&mut self) {
81 let strip_width = self.call_strip_width;
82 let Some(call) = self.call.as_mut() else {
83 return;
84 };
85 let wants = video_wants(&call.model, measured_stage(call), strip_width);
86 if wants != call.sent_wants {
87 let _ = call.commands.send(CallCommand::SetVideoWants(wants.clone()));
88 call.sent_wants = wants;
89 }
90 }
91
92 pub fn toggle_call_fullscreen(&mut self, cx: &mut Context<Self>) {
93 let Some(call) = self.call.as_mut().filter(|call| call.viewing) else {
94 return;
95 };
96 call.fullscreen = match call.fullscreen {
97 Some(_) => None,
98 None => Some(FullscreenState::new(Instant::now())),
99 };
100 self.watch_fullscreen_chrome(cx);
101 cx.emit(AppEvent::Call);
102 cx.notify();
103 }
104
105 pub fn escape_call_fullscreen(&mut self, cx: &mut Context<Self>) {
106 if self.stage_fullscreen() {
107 self.leave_stage_fullscreen(cx);
108 return;
109 }
110 let Some(call) = self.call.as_mut().filter(|call| call.fullscreen.is_some()) else {
111 return;
112 };
113 call.fullscreen = None;
114 cx.emit(AppEvent::Call);
115 cx.notify();
116 }
117
118 pub fn call_fullscreen(&self) -> bool {
119 self.call
120 .as_ref()
121 .is_some_and(|call| call.viewing && call.fullscreen.is_some())
122 }
123
124 pub fn touch_call_chrome(&mut self, cx: &mut Context<Self>) {
125 let now = Instant::now();
126 let Some(fullscreen) = self.call.as_mut().and_then(|call| call.fullscreen.as_mut()) else {
127 return;
128 };
129 let was_visible = fullscreen.chrome_visible(now);
130 if was_visible
131 && now.saturating_duration_since(fullscreen.active_at) < CHROME_TOUCH_THROTTLE
132 {
133 return;
134 }
135 fullscreen.active_at = now;
136 if !was_visible {
137 self.watch_fullscreen_chrome(cx);
138 cx.notify();
139 }
140 }
141
142 pub fn set_call_chrome_hovered(&mut self, hovered: bool, cx: &mut Context<Self>) {
143 let Some(fullscreen) = self
144 .call
145 .as_mut()
146 .and_then(|call| call.fullscreen.as_mut())
147 .filter(|fullscreen| fullscreen.hovered != hovered)
148 else {
149 return;
150 };
151 fullscreen.hovered = hovered;
152 fullscreen.active_at = Instant::now();
153 self.watch_fullscreen_chrome(cx);
154 cx.notify();
155 }
156
157 fn watch_fullscreen_chrome(&mut self, cx: &mut Context<Self>) {
158 let Some(call) = self.call.as_mut() else {
159 return;
160 };
161 let call_id = call.id;
162 let Some(fullscreen) = call
163 .fullscreen
164 .as_mut()
165 .filter(|fullscreen| !fullscreen.timer_running)
166 else {
167 return;
168 };
169 fullscreen.timer_running = true;
170 cx.spawn(async move |this, cx| {
171 while let Ok(Some(wait)) =
172 this.update(cx, |state, cx| state.fullscreen_tick(call_id, cx))
173 {
174 cx.background_executor().timer(wait).await;
175 }
176 })
177 .detach();
178 }
179
180 fn fullscreen_tick(&mut self, call_id: u64, cx: &mut Context<Self>) -> Option<Duration> {
181 cx.notify();
182 let now = Instant::now();
183 let fullscreen = self
184 .call
185 .as_mut()
186 .filter(|call| call.id == call_id)
187 .and_then(|call| call.fullscreen.as_mut())?;
188 let hint_left =
189 HINT_SHOWN.saturating_sub(now.saturating_duration_since(fullscreen.entered));
190 let next = [
191 fullscreen.next_change(now),
192 (!hint_left.is_zero()).then_some(hint_left),
193 ]
194 .into_iter()
195 .flatten()
196 .min();
197 if next.is_none() {
198 fullscreen.timer_running = false;
199 }
200 next.map(|wait| wait + TIMER_SLACK)
201 }
202
203 pub fn note_call_chat_message(
204 &mut self,
205 conversation_id: &str,
206 message_id: &str,
207 sender_id: Option<String>,
208 sender_name: &str,
209 text: &str,
210 cx: &mut Context<Self>,
211 ) {
212 let Some(call) = self.call.as_mut().filter(|call| {
213 !call.chat_open
214 && call.model.view.chat_bubbles
215 && call.chat_thread() == Some(conversation_id)
216 }) else {
217 return;
218 };
219 push_bubble(
220 &mut call.bubbles,
221 ChatBubble {
222 id: message_id.to_owned(),
223 sender_id,
224 name: sender_name.to_owned(),
225 text: text.to_owned(),
226 at: Instant::now(),
227 },
228 );
229 cx.spawn(async move |this, cx| {
230 cx.background_executor()
231 .timer(BUBBLE_SHOWN + TIMER_SLACK)
232 .await;
233 this.update(cx, |_, cx| cx.notify()).ok();
234 })
235 .detach();
236 cx.notify();
237 }
238
239 pub fn set_call_share_menu(&mut self, open: bool, cx: &mut Context<Self>) {
240 if let Some(call) = self.call.as_mut() {
241 call.share_menu_open = open;
242 cx.notify();
243 }
244 }
245
246 pub fn set_call_lobby_list(&mut self, open: bool, cx: &mut Context<Self>) {
247 if let Some(call) = self.call.as_mut() {
248 call.lobby_list_open = open;
249 cx.notify();
250 }
251 }
252
253 pub fn toggle_call_share(&mut self, cx: &mut Context<Self>) {
254 let Some(call) = self.call.as_ref() else {
255 return;
256 };
257 if call.model.local_share.is_some() {
258 self.stop_call_share(cx);
259 return;
260 }
261 if call.model.can_share() {
262 self.show_call(true, cx);
263 self.set_call_share_menu(true, cx);
264 }
265 }
266
267 pub fn open_call_lobby_list(&mut self, cx: &mut Context<Self>) {
268 let waiting = self
269 .call
270 .as_ref()
271 .is_some_and(|call| call.model.can_admit() && !call.model.lobby_guests().is_empty());
272 if waiting {
273 self.show_call(true, cx);
274 self.set_call_lobby_list(true, cx);
275 }
276 }
277
278 pub(super) fn queue_demo_bubbles(&mut self, thread: &str, cx: &mut Context<Self>) {
279 let thread = thread.to_owned();
280 cx.spawn(async move |this, cx| {
281 cx.background_executor().timer(DEMO_BUBBLE_START).await;
282 for (index, (name, text)) in DEMO_BUBBLES.iter().enumerate() {
283 let sent = this.update(cx, |state, cx| {
284 state.note_call_chat_message(
285 &thread,
286 &format!("demo-bubble-{index}"),
287 Some(format!("demo-guest-{index}")),
288 name,
289 text,
290 cx,
291 )
292 });
293 if sent.is_err() {
294 return;
295 }
296 cx.background_executor().timer(DEMO_BUBBLE_EVERY).await;
297 }
298 })
299 .detach();
300 }
301}