BlueGit

ict rusty-teams

Files

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

view_actions.rs

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