BlueGit

ict rusty-teams

Files

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

prejoin_view.rs

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