BlueGit

ict rusty-teams

Files

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

view_actions.rs

9ee83b18 History
view_actions.rs 10.3 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 Some(call) = self.call.as_mut() else {
101 return;
102 };
103 let wants = video_wants(&call.model, measured_stage(call));
104 if wants != call.sent_wants {
105 let _ = call.commands.send(CallCommand::SetVideoWants(wants.clone()));
106 call.sent_wants = wants;
107 }
108 }
109
110 pub fn toggle_call_fullscreen(&mut self, cx: &mut Context<Self>) {
111 let Some(call) = self.call.as_mut().filter(|call| call.viewing) else {
112 return;
113 };
114 call.fullscreen = match call.fullscreen {
115 Some(_) => None,
116 None => Some(FullscreenState::new(Instant::now())),
117 };
118 self.watch_fullscreen_chrome(cx);
119 cx.emit(AppEvent::Call);
120 cx.notify();
121 }
122
123 pub fn escape_call_fullscreen(&mut self, cx: &mut Context<Self>) {
124 if self.stage_fullscreen() {
125 self.leave_stage_fullscreen(cx);
126 return;
127 }
128 let Some(call) = self.call.as_mut().filter(|call| call.fullscreen.is_some()) else {
129 return;
130 };
131 call.fullscreen = None;
132 cx.emit(AppEvent::Call);
133 cx.notify();
134 }
135
136 pub fn call_fullscreen(&self) -> bool {
137 self.call
138 .as_ref()
139 .is_some_and(|call| call.viewing && call.fullscreen.is_some())
140 }
141
142 pub fn touch_call_chrome(&mut self, cx: &mut Context<Self>) {
143 let now = Instant::now();
144 let Some(fullscreen) = self.call.as_mut().and_then(|call| call.fullscreen.as_mut()) else {
145 return;
146 };
147 let was_visible = fullscreen.chrome_visible(now);
148 if was_visible
149 && now.saturating_duration_since(fullscreen.active_at) < CHROME_TOUCH_THROTTLE
150 {
151 return;
152 }
153 fullscreen.active_at = now;
154 if !was_visible {
155 self.watch_fullscreen_chrome(cx);
156 cx.notify();
157 }
158 }
159
160 pub fn set_call_chrome_hovered(&mut self, hovered: bool, cx: &mut Context<Self>) {
161 let Some(fullscreen) = self
162 .call
163 .as_mut()
164 .and_then(|call| call.fullscreen.as_mut())
165 .filter(|fullscreen| fullscreen.hovered != hovered)
166 else {
167 return;
168 };
169 fullscreen.hovered = hovered;
170 fullscreen.active_at = Instant::now();
171 self.watch_fullscreen_chrome(cx);
172 cx.notify();
173 }
174
175 fn watch_fullscreen_chrome(&mut self, cx: &mut Context<Self>) {
176 let Some(call) = self.call.as_mut() else {
177 return;
178 };
179 let call_id = call.id;
180 let Some(fullscreen) = call
181 .fullscreen
182 .as_mut()
183 .filter(|fullscreen| !fullscreen.timer_running)
184 else {
185 return;
186 };
187 fullscreen.timer_running = true;
188 cx.spawn(async move |this, cx| {
189 while let Ok(Some(wait)) =
190 this.update(cx, |state, cx| state.fullscreen_tick(call_id, cx))
191 {
192 cx.background_executor().timer(wait).await;
193 }
194 })
195 .detach();
196 }
197
198 fn fullscreen_tick(&mut self, call_id: u64, cx: &mut Context<Self>) -> Option<Duration> {
199 cx.notify();
200 let now = Instant::now();
201 let fullscreen = self
202 .call
203 .as_mut()
204 .filter(|call| call.id == call_id)
205 .and_then(|call| call.fullscreen.as_mut())?;
206 let hint_left =
207 HINT_SHOWN.saturating_sub(now.saturating_duration_since(fullscreen.entered));
208 let next = [
209 fullscreen.next_change(now),
210 (!hint_left.is_zero()).then_some(hint_left),
211 ]
212 .into_iter()
213 .flatten()
214 .min();
215 if next.is_none() {
216 fullscreen.timer_running = false;
217 }
218 next.map(|wait| wait + TIMER_SLACK)
219 }
220
221 pub fn note_call_chat_message(
222 &mut self,
223 conversation_id: &str,
224 message_id: &str,
225 sender_id: Option<String>,
226 sender_name: &str,
227 text: &str,
228 cx: &mut Context<Self>,
229 ) {
230 let Some(call) = self.call.as_mut().filter(|call| {
231 !call.chat_open
232 && call.model.view.chat_bubbles
233 && call.chat_thread() == Some(conversation_id)
234 }) else {
235 return;
236 };
237 push_bubble(
238 &mut call.bubbles,
239 ChatBubble {
240 id: message_id.to_owned(),
241 sender_id,
242 name: sender_name.to_owned(),
243 text: text.to_owned(),
244 at: Instant::now(),
245 },
246 );
247 cx.spawn(async move |this, cx| {
248 cx.background_executor()
249 .timer(BUBBLE_SHOWN + TIMER_SLACK)
250 .await;
251 this.update(cx, |_, cx| cx.notify()).ok();
252 })
253 .detach();
254 cx.notify();
255 }
256
257 pub fn set_call_share_menu(&mut self, open: bool, cx: &mut Context<Self>) {
258 if let Some(call) = self.call.as_mut() {
259 call.share_menu_open = open;
260 cx.notify();
261 }
262 }
263
264 pub fn set_call_lobby_list(&mut self, open: bool, cx: &mut Context<Self>) {
265 if let Some(call) = self.call.as_mut() {
266 call.lobby_list_open = open;
267 cx.notify();
268 }
269 }
270
271 pub fn toggle_call_share(&mut self, cx: &mut Context<Self>) {
272 let Some(call) = self.call.as_ref() else {
273 return;
274 };
275 if call.model.local_share.is_some() {
276 self.stop_call_share(cx);
277 return;
278 }
279 if call.model.can_share() {
280 self.show_call(true, cx);
281 self.set_call_share_menu(true, cx);
282 }
283 }
284
285 pub fn open_call_lobby_list(&mut self, cx: &mut Context<Self>) {
286 let waiting = self
287 .call
288 .as_ref()
289 .is_some_and(|call| call.model.can_admit() && !call.model.lobby_guests().is_empty());
290 if waiting {
291 self.show_call(true, cx);
292 self.set_call_lobby_list(true, cx);
293 }
294 }
295
296 pub(super) fn queue_demo_bubbles(&mut self, thread: &str, cx: &mut Context<Self>) {
297 let thread = thread.to_owned();
298 cx.spawn(async move |this, cx| {
299 cx.background_executor().timer(DEMO_BUBBLE_START).await;
300 for (index, (name, text)) in DEMO_BUBBLES.iter().enumerate() {
301 let sent = this.update(cx, |state, cx| {
302 state.note_call_chat_message(
303 &thread,
304 &format!("demo-bubble-{index}"),
305 Some(format!("demo-guest-{index}")),
306 name,
307 text,
308 cx,
309 )
310 });
311 if sent.is_err() {
312 return;
313 }
314 cx.background_executor().timer(DEMO_BUBBLE_EVERY).await;
315 }
316 })
317 .detach();
318 }
319}