BlueGit

ict rusty-teams

Files

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

prejoin_view.rs

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