BlueGit

ict rusty-teams

Files

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

prejoin_view.rs

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