BlueGit

ict rusty-teams

Files

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

view_actions.rs

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