BlueGit

ict rusty-teams

Files

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

view_actions.rs

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