BlueGit

ict rusty-teams

Files

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

view_actions.rs

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