BlueGit

ict rusty-teams

Files

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

prejoin_view.rs

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