BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / prejoin_view.rs

prejoin_view.rs

prejoin_view.rs 19.4 KiB Download Raw
1use std::time::{Duration, Instant};
2
3use calling::camera::list_cameras;
4use calling::devices::{AudioDevice, DeviceLists, list_devices, shared_factory};
5use calling::{DeviceChoice, DeviceEntry, camera_entries};
6use gpui_kit::assets::IconName;
7use gpui_kit::component::Disableable as _;
8use gpui_kit::component::button::{Button, ButtonVariants as _};
9use gpui_kit::component::menu::{DropdownMenu as _, PopupMenu};
10use gpui_kit::component::{h_flex, v_flex};
11use gpui_kit::prelude::FluentBuilder as _;
12use gpui_kit::*;
13
14use super::avatar::person_avatar;
15use super::call_extras::{CameraMenu, background_items};
16use super::call_view::{device_items, round_control};
17use crate::app_state::AppState;
18use crate::call::{AudioChoice, CameraPreview, JoinOptions, PendingJoin};
19use crate::theme;
20
21const CONTENT_WIDTH: f32 = 560.;
22const PREVIEW_HEIGHT: f32 = 315.;
23const PREVIEW_RADIUS: f32 = 12.;
24const AVATAR_SIZE: f32 = 96.;
25const SELECT_WIDTH: f32 = 176.;
26const SECTION_GAP: f32 = 12.;
27const PANE_PADDING: f32 = 12.;
28const PREVIEW_MIN_HEIGHT: f32 = 60.;
29const FRAME_TICK: Duration = Duration::from_millis(100);
30const FPS_TICK: Duration = Duration::from_millis(500);
31const CAMERA_OFF_TEXT: &str = "Your camera is off";
32const DEFAULT_LABEL: &str = "Default device";
33const DEMO_MICROPHONES: [&str; 2] = ["Headset microphone", "Laptop microphone"];
34const DEMO_SPEAKERS: [&str; 2] = ["Headset earphones", "Laptop speakers"];
35
36pub struct PrejoinView {
37 app: Entity<AppState>,
38 pending_id: u64,
39 demo: bool,
40 preview: CameraPreview,
41 options: JoinOptions,
42 cameras: Vec<DeviceEntry>,
43 devices: DeviceLists,
44 camera: DeviceChoice,
45 input: DeviceChoice,
46 output: DeviceChoice,
47 applied_background: crate::call::BackgroundPick,
48 tick: u32,
49 _subscription: Subscription,
50 _release: Subscription,
51}
52
53struct DeviceSelect {
54 id: &'static str,
55 label: &'static str,
56 entries: Vec<DeviceEntry>,
57 current: DeviceChoice,
58 enabled: bool,
59}
60
61pub(crate) fn demo_devices() -> DeviceLists {
62 let device = |index: u16, name: &str| AudioDevice {
63 index,
64 guid: format!("demo-{name}"),
65 name: name.to_owned(),
66 };
67 DeviceLists {
68 inputs: DEMO_MICROPHONES
69 .iter()
70 .enumerate()
71 .map(|(index, name)| device(index as u16, name))
72 .collect(),
73 outputs: DEMO_SPEAKERS
74 .iter()
75 .enumerate()
76 .map(|(index, name)| device(index as u16, name))
77 .collect(),
78 }
79}
80
81fn label_of(entries: &[DeviceEntry], choice: &DeviceChoice) -> String {
82 entries
83 .iter()
84 .find(|entry| &entry.choice == choice)
85 .map_or_else(|| DEFAULT_LABEL.to_owned(), |entry| entry.label.clone())
86}
87
88impl PrejoinView {
89 pub fn new(
90 app: Entity<AppState>,
91 pending: &PendingJoin,
92 _window: &mut Window,
93 cx: &mut Context<Self>,
94 ) -> Self {
95 let subscription = cx.observe(&app, |view, _, cx| view.sync_background(cx));
96 let release = cx.on_release(|view, cx| view.preview.stop(cx));
97 let (preview, mut updates) = CameraPreview::new();
98 let state = app.read(cx);
99 preview.set_effects(state.call_effects);
100 let demo = state.mode.demo;
101 let mut options = state.join_options.clone();
102 options.camera_on = options.camera_on && !state.mode.read_only;
103 let camera = state.call_camera.clone();
104 let (input, output) = (state.call_input.clone(), state.call_output.clone());
105 let mut view = PrejoinView {
106 pending_id: pending.id,
107 demo,
108 preview,
109 options,
110 cameras: camera_entries(&[]),
111 devices: if demo {
112 demo_devices()
113 } else {
114 DeviceLists::default()
115 },
116 camera,
117 input,
118 output,
119 applied_background: crate::call::BackgroundPick::None,
120 tick: 0,
121 _subscription: subscription,
122 _release: release,
123 app,
124 };
125 cx.spawn(async move |this, cx| {
126 while updates.recv().await.is_some() {
127 if this.update(cx, |view, cx| view.take_frames(cx)).is_err() {
128 return;
129 }
130 }
131 })
132 .detach();
133 cx.spawn(async move |this, cx| {
134 loop {
135 cx.background_executor().timer(FPS_TICK).await;
136 if this
137 .update(cx, |view, cx| {
138 view.preview.tick(Instant::now());
139 cx.notify();
140 })
141 .is_err()
142 {
143 break;
144 }
145 }
146 })
147 .detach();
148 if demo {
149 cx.spawn(async move |this, cx| {
150 loop {
151 cx.background_executor().timer(FRAME_TICK).await;
152 if this.update(cx, |view, cx| view.demo_frame(cx)).is_err() {
153 break;
154 }
155 }
156 })
157 .detach();
158 } else {
159 view.load_devices(cx);
160 }
161 view.sync_background(cx);
162 view.restart_preview();
163 view
164 }
165
166 pub fn pending_id(&self) -> u64 {
167 self.pending_id
168 }
169
170 fn take_frames(&mut self, cx: &mut Context<Self>) {
171 self.preview.take_frames(cx);
172 cx.notify();
173 }
174
175 fn demo_frame(&mut self, cx: &mut Context<Self>) {
176 if self.options.camera_on {
177 self.tick = self.tick.wrapping_add(1);
178 self.preview.publish_demo_frame(self.tick);
179 self.take_frames(cx);
180 }
181 }
182
183 fn load_devices(&mut self, cx: &mut Context<Self>) {
184 cx.spawn(async move |this, cx| {
185 let (cameras, devices) = cx
186 .background_executor()
187 .spawn(async { (list_cameras(), list_devices(&shared_factory())) })
188 .await;
189 this.update(cx, |view, cx| {
190 view.cameras = camera_entries(&cameras);
191 view.devices = devices;
192 cx.notify();
193 })
194 .ok();
195 })
196 .detach();
197 }
198
199 fn restart_preview(&mut self) {
200 if self.options.camera_on && !self.demo {
201 self.preview.start(&self.camera);
202 }
203 }
204
205 fn sync_background(&mut self, cx: &mut Context<Self>) {
206 let state = self.app.read(cx);
207 let pick = state.call_background.clone();
208 if pick == self.applied_background {
209 return;
210 }
211 if let Some(choice) = state.call_backgrounds.choice_for(&pick) {
212 self.preview.set_background(choice);
213 self.applied_background = pick;
214 }
215 cx.notify();
216 }
217
218 fn toggle_camera(&mut self, cx: &mut Context<Self>) {
219 self.options.camera_on = !self.options.camera_on;
220 if self.options.camera_on {
221 self.restart_preview();
222 } else {
223 self.preview.stop(cx);
224 }
225 cx.notify();
226 }
227
228 fn toggle_mic(&mut self, cx: &mut Context<Self>) {
229 self.options.mic_on = !self.options.mic_on;
230 cx.notify();
231 }
232
233 fn set_audio(&mut self, audio: AudioChoice, cx: &mut Context<Self>) {
234 self.options.audio = audio;
235 cx.notify();
236 }
237
238 fn pick_camera(&mut self, choice: DeviceChoice, cx: &mut Context<Self>) {
239 self.camera = choice.clone();
240 self.app
241 .update(cx, |state, cx| state.select_call_camera(choice, cx));
242 self.restart_preview();
243 cx.notify();
244 }
245
246 fn pick_input(&mut self, choice: DeviceChoice, cx: &mut Context<Self>) {
247 self.input = choice.clone();
248 self.app
249 .update(cx, |state, cx| state.select_call_input(choice, cx));
250 cx.notify();
251 }
252
253 fn pick_output(&mut self, choice: DeviceChoice, cx: &mut Context<Self>) {
254 self.output = choice.clone();
255 self.app
256 .update(cx, |state, cx| state.select_call_output(choice, cx));
257 cx.notify();
258 }
259
260 fn join(&mut self, cx: &mut Context<Self>) {
261 self.preview.stop(cx);
262 let options = self.options.clone();
263 self.app
264 .update(cx, |state, cx| state.confirm_prejoin(options, cx));
265 }
266
267 fn cancel(&mut self, cx: &mut Context<Self>) {
268 self.app.update(cx, |state, cx| state.cancel_prejoin(cx));
269 }
270
271 fn preview_status(&self) -> String {
272 match self.preview.failure() {
273 Some(reason) => format!("Camera could not start: {reason}"),
274 None => "Starting camera...".to_owned(),
275 }
276 }
277
278 fn preview_box(&self, cx: &mut Context<Self>) -> Div {
279 let state = self.app.read(cx);
280 let image = self
281 .options
282 .camera_on
283 .then(|| self.preview.image(Instant::now()))
284 .flatten();
285 let me = state.directory.me.clone();
286 let face = match (&image, self.options.camera_on) {
287 (Some(image), _) => img(ImageSource::Render(image.clone()))
288 .size_full()
289 .object_fit(ObjectFit::Contain)
290 .into_any_element(),
291 (None, true) => centered_note(&self.preview_status()),
292 (None, false) => v_flex()
293 .items_center()
294 .gap(px(10.))
295 .child(person_avatar(
296 &state.directory,
297 me.as_ref().map(|me| me.user_id.as_str()),
298 me.as_ref().map_or("You", |me| me.display_name.as_str()),
299 AVATAR_SIZE,
300 ))
301 .child(
302 div()
303 .text_size(px(13.))
304 .text_color(theme::text_muted())
305 .child(CAMERA_OFF_TEXT),
306 )
307 .into_any_element(),
308 };
309 div()
310 .debug_selector(|| "shell/islands/main/prejoin/preview".into())
311 .relative()
312 .w_full()
313 .h(px(PREVIEW_HEIGHT))
314 .flex_shrink(1.)
315 .min_h(px(PREVIEW_MIN_HEIGHT))
316 .flex()
317 .items_center()
318 .justify_center()
319 .overflow_hidden()
320 .rounded(px(PREVIEW_RADIUS))
321 .bg(theme::background())
322 .child(face)
323 }
324
325 fn controls(&self, cx: &mut Context<Self>) -> impl IntoElement {
326 let view = cx.entity();
327 let camera_view = view.clone();
328 let mic_view = view.clone();
329 let (camera_glyph, camera_bg, camera_fg) = if self.options.camera_on {
330 (IconName::Video, theme::accent(), theme::on_accent())
331 } else {
332 (IconName::VideoOff, theme::surface_raised(), theme::text())
333 };
334 let (mic_glyph, mic_bg, mic_fg) = if self.options.mic_on {
335 (IconName::Mic, theme::surface_raised(), theme::text())
336 } else {
337 (IconName::MicOff, theme::red(), theme::white())
338 };
339 let audio_on = self.options.audio == AudioChoice::Computer;
340 let backgrounds = CameraMenu::of(self.app.read(cx));
341 let background_app = self.app.clone();
342 h_flex()
343 .debug_selector(|| "shell/islands/main/prejoin/controls".into())
344 .flex_none()
345 .gap(px(10.))
346 .items_center()
347 .justify_center()
348 .child(
349 round_control("prejoin-camera", camera_glyph, camera_bg, camera_fg).on_click(
350 move |_, _, cx| camera_view.update(cx, |view, cx| view.toggle_camera(cx)),
351 ),
352 )
353 .child(
354 round_control("prejoin-mic", mic_glyph, mic_bg, mic_fg)
355 .when(!audio_on, |button| button.opacity(0.4).cursor_default())
356 .when(audio_on, |button| {
357 button.on_click(move |_, _, cx| {
358 mic_view.update(cx, |view, cx| view.toggle_mic(cx))
359 })
360 }),
361 )
362 .child(
363 Button::new("prejoin-background")
364 .outline()
365 .label("Background")
366 .icon(IconName::ChevronDown)
367 .dropdown_menu(move |menu: PopupMenu, _, _| {
368 background_items(menu, &background_app, &backgrounds)
369 }),
370 )
371 }
372
373 fn select(
374 &self,
375 spec: DeviceSelect,
376 pick: impl Fn(&mut PrejoinView, DeviceChoice, &mut Context<PrejoinView>) + Clone + 'static,
377 cx: &mut Context<Self>,
378 ) -> Div {
379 let DeviceSelect {
380 id,
381 label,
382 entries,
383 current,
384 enabled,
385 } = spec;
386 let view = cx.entity();
387 let face = label_of(&entries, &current);
388 let items_entries = entries;
389 let items_current = current;
390 let button = Button::new(id)
391 .outline()
392 .w_full()
393 .label(face)
394 .icon(IconName::ChevronDown)
395 .disabled(!enabled)
396 .dropdown_menu(move |menu, _, _| {
397 let view = view.clone();
398 let pick = pick.clone();
399 device_items(items_entries.clone(), &items_current, move |choice, cx| {
400 let pick = pick.clone();
401 view.update(cx, |view, cx| pick(view, choice, cx));
402 })
403 .into_iter()
404 .fold(menu, |menu, item| menu.item(item))
405 });
406 v_flex()
407 .flex_1()
408 .min_w_0()
409 .max_w(px(SELECT_WIDTH))
410 .gap(px(4.))
411 .child(
412 div()
413 .text_size(px(12.))
414 .text_color(theme::text_muted())
415 .child(label),
416 )
417 .child(div().w_full().overflow_hidden().child(button))
418 }
419
420 fn audio_choice(&self, choice: AudioChoice, cx: &mut Context<Self>) -> Stateful<Div> {
421 let selected = self.options.audio == choice;
422 let view = cx.entity();
423 h_flex()
424 .id(match choice {
425 AudioChoice::Computer => "prejoin-audio-computer",
426 AudioChoice::NoAudio => "prejoin-audio-none",
427 })
428 .h(px(32.))
429 .px(px(14.))
430 .items_center()
431 .rounded(px(8.))
432 .border_1()
433 .border_color(if selected {
434 theme::accent()
435 } else {
436 theme::border_strong()
437 })
438 .when(selected, |option| option.bg(theme::surface_raised()))
439 .cursor_pointer()
440 .text_size(px(13.))
441 .text_color(theme::text())
442 .child(choice.label())
443 .on_click(move |_, _, cx| view.update(cx, |view, cx| view.set_audio(choice, cx)))
444 }
445}
446
447fn centered_note(text: &str) -> AnyElement {
448 div()
449 .text_size(px(13.))
450 .text_color(theme::text_muted())
451 .child(text.to_owned())
452 .into_any_element()
453}
454
455impl Render for PrejoinView {
456 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
457 let title = self
458 .app
459 .read(cx)
460 .prejoin
461 .as_ref()
462 .map(|pending| pending.join.title.clone())
463 .unwrap_or_default();
464 let audio_on = self.options.audio == AudioChoice::Computer;
465 let (cameras, camera) = (self.cameras.clone(), self.camera.clone());
466 let (inputs, input) = (self.devices.input_entries(), self.input.clone());
467 let (outputs, output) = (self.devices.output_entries(), self.output.clone());
468 let devices = h_flex()
469 .debug_selector(|| "shell/islands/main/prejoin/devices".into())
470 .flex_none()
471 .w_full()
472 .gap(px(10.))
473 .child(self.select(
474 DeviceSelect {
475 id: "prejoin-camera-select",
476 label: "Camera",
477 entries: cameras,
478 current: camera,
479 enabled: true,
480 },
481 |view, choice, cx| view.pick_camera(choice, cx),
482 cx,
483 ))
484 .child(self.select(
485 DeviceSelect {
486 id: "prejoin-mic-select",
487 label: "Microphone",
488 entries: inputs,
489 current: input,
490 enabled: audio_on,
491 },
492 |view, choice, cx| view.pick_input(choice, cx),
493 cx,
494 ))
495 .child(self.select(
496 DeviceSelect {
497 id: "prejoin-speaker-select",
498 label: "Speaker",
499 entries: outputs,
500 current: output,
501 enabled: audio_on,
502 },
503 |view, choice, cx| view.pick_output(choice, cx),
504 cx,
505 ));
506 let audio_row = h_flex()
507 .debug_selector(|| "shell/islands/main/prejoin/audio".into())
508 .flex_none()
509 .flex_wrap()
510 .gap(px(8.))
511 .child(self.audio_choice(AudioChoice::Computer, cx))
512 .child(self.audio_choice(AudioChoice::NoAudio, cx));
513 let cancel_view = cx.entity();
514 let join_view = cx.entity();
515 let actions = h_flex()
516 .debug_selector(|| "shell/islands/main/prejoin/actions".into())
517 .flex_none()
518 .gap(px(8.))
519 .justify_end()
520 .child(
521 Button::new("prejoin-cancel")
522 .ghost()
523 .label("Cancel")
524 .on_click(move |_, _, cx| cancel_view.update(cx, |view, cx| view.cancel(cx))),
525 )
526 .child(
527 Button::new("prejoin-join")
528 .primary()
529 .label("Join now")
530 .on_click(move |_, _, cx| join_view.update(cx, |view, cx| view.join(cx))),
531 );
532 v_flex()
533 .debug_selector(|| "shell/islands/main".into())
534 .size_full()
535 .p(px(PANE_PADDING))
536 .items_center()
537 .justify_center()
538 .rounded(px(10.))
539 .bg(theme::surface())
540 .overflow_hidden()
541 .child(
542 v_flex()
543 .debug_selector(|| "shell/islands/main/prejoin".into())
544 .w(px(CONTENT_WIDTH))
545 .max_w_full()
546 .max_h_full()
547 .min_h_0()
548 .gap(px(SECTION_GAP))
549 .child(
550 v_flex()
551 .flex_none()
552 .gap(px(2.))
553 .child(
554 div()
555 .truncate()
556 .text_size(px(20.))
557 .font_weight(FontWeight::SEMIBOLD)
558 .text_color(theme::text_strong())
559 .child(title),
560 )
561 .child(
562 div()
563 .truncate()
564 .text_size(px(13.))
565 .text_color(theme::text_muted())
566 .child("Choose your video and audio settings, then join"),
567 ),
568 )
569 .child(self.preview_box(cx))
570 .child(self.controls(cx))
571 .child(devices)
572 .child(audio_row)
573 .child(actions),
574 )
575 }
576}