From ba4fe3c8ca6723c21e42a08f3cd2b4ae2d0c3244 Mon Sep 17 00:00:00 2001 From: faumaray Date: Wed, 28 Oct 2026 10:02:35 +0300 Subject: [PATCH] voelin-gpui: the voice channel page and its call bar Slice 3 of plan section 6 step 3, first part. VoicePage is a cached pane of its own, a sibling of the server page that MainView puts in the main area while the page is asked for (show_voice_view, through voelin-ui-vm's logic) and voice is connected, as the Slint VoiceView is. It shows the channel (name, topic, banner under the surface veil, "N in voice / limit total"), the people of our channel as TreeView::members gives them through the tree model's Voice event (the ring that is green while they talk and blue while they stream, the bars or the microphone and sound marks in the corner, the crown, "Speaking", "Streaming", "Away"; a click opens the card), and the call bar: mute, deafen, Share and the voice settings (both logged until their slices), Leave. The smaller stage, one row of small avatars, is ui.voice_compact and always in a window under 800 pixels tall, which MainView pushes into the pane (a cached pane does not see a resize); VOELIN_OPEN=voice[:compact] asks for it, the sample servers keep nothing. The server page gets a Voice channel button for the way in, and the meter counts the page's renders. Tests on the test platform: the page takes the main area and gives it back, the people and their marks, talking and mute events redraw the page once and the rail, top bar and main area not at all (one model build for three lists), Leave reaches the engine, the call bar's buttons, the stage kept in the settings and forced short, the banner. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-gpui/src/app.rs | 51 +- crates/voelin-gpui/src/dev.rs | 24 +- crates/voelin-gpui/src/meter.rs | 9 +- crates/voelin-gpui/src/shell/actions.rs | 28 +- crates/voelin-gpui/src/shell/main_panel.rs | 18 +- crates/voelin-gpui/src/shell/mod.rs | 13 +- crates/voelin-gpui/src/shell/voice.rs | 650 +++++++++++++++++++++ crates/voelin-gpui/src/shell/widgets.rs | 13 +- crates/voelin-gpui/src/view.rs | 146 ++++- crates/voelin-gpui/src/view/voice_tests.rs | 337 +++++++++++ 10 files changed, 1242 insertions(+), 47 deletions(-) create mode 100644 crates/voelin-gpui/src/shell/voice.rs create mode 100644 crates/voelin-gpui/src/view/voice_tests.rs diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index 415c690..8ff616b 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -52,7 +52,7 @@ use voelin_ui_vm::servers::logic; use voelin_ui_vm::servers::sequence::{self, Backing}; use voelin_ui_vm::settings::{ CLIENT_PLAYBACK, ThemeChoice, TreeStates, UI, UI_FONT_SCALE, UI_IMAGE_PREVIEW_KB, UI_THEME, - UI_TREE, UI_WINDOW, WindowState, + UI_TREE, UI_VOICE_COMPACT, UI_WINDOW, WindowState, }; use voelin_ui_vm::social::logic::Social; use voelin_ui_vm::tree::ChannelId; @@ -251,6 +251,9 @@ pub struct AppState { tree_states: TreeStates, /// The servers whose remembered folds are in their sessions. tree_restored: HashSet, + /// The voice page shows the smaller stage (`ui.voice_compact`, or the + /// development switch that asks for it). + voice_compact: bool, /// Where the sample servers' pictures were written (`VOELIN_DEMO_UI`), /// removed with the state. demo_dir: Option, @@ -364,17 +367,23 @@ impl AppState { password: None, tree_states, tree_restored: HashSet::new(), + voice_compact: false, demo_dir: None, later: Later(later), #[cfg(test)] sent: Default::default(), }; state.window.opening_reports = if state.wants_maximized() { OPENING_REPORTS } else { 0 }; + state.voice_compact = + state.switches.voice_compact || state.backend.prefs.get(&UI_VOICE_COMPACT); if state.switches.demo_ui { state.load_demo(); } else if let Some(id) = state.vm.current { state.restore_tree(id); } + if state.switches.voice { + state.transition(cx, |state, cx| logic::show_voice_view(state, cx, true)); + } state } @@ -856,6 +865,46 @@ impl AppState { cx.notify(); } + /// The voice channel page on or off (`show_voice_view` of + /// `bind/servers.rs`, through [`logic::show_voice_view`]): the channel's + /// chat is the one shown, and the people of our channel lead the members + /// panel while the page is. + pub fn show_voice_view(&mut self, on: bool, cx: &mut Context) { + self.transition(cx, |state, cx| logic::show_voice_view(state, cx, on)); + cx.notify(); + } + + /// Whether the main area is the voice page: it was asked for, and voice is + /// connected on the server on screen (the Slint front end's + /// `Nav.voice-view && Bridge.voice-connected`; the page is asked for again + /// by a reconnect, as there). + pub fn voice_page_shown(&self) -> bool { + self.vm.voice_view + && self + .vm + .current + .and_then(|id| self.vm.sessions.get(&id)) + .is_some_and(|view| view.state.voice == VoiceState::Connected) + } + + /// Whether the voice page is on its smaller stage by choice (a window under + /// 800 pixels tall has it whatever this says). + pub fn voice_compact(&self) -> bool { + self.voice_compact + } + + /// The voice page's Smaller stage / Larger stage: kept in `ui.voice_compact` + /// (nothing is kept with the sample servers). + pub fn toggle_voice_compact(&mut self, cx: &mut Context) { + self.voice_compact = !self.voice_compact; + if !self.switches.demo_ui + && let Err(e) = self.backend.prefs.set(&UI_VOICE_COMPACT, self.voice_compact) + { + warn!(%e, "could not store the stage size"); + } + cx.notify(); + } + /// A link to the voice channel we are in, copied to the clipboard with a /// note in the status line; the reason in the status line without one. pub fn invite(&mut self, cx: &mut Context) -> Option { diff --git a/crates/voelin-gpui/src/dev.rs b/crates/voelin-gpui/src/dev.rs index 00c3cca..8df11a1 100644 --- a/crates/voelin-gpui/src/dev.rs +++ b/crates/voelin-gpui/src/dev.rs @@ -7,6 +7,9 @@ //! components, the icons) in place of the shell. The Slint front end's //! other pages do not exist here yet; they are named in a warning and //! skipped. +//! - `VOELIN_OPEN=voice` (`voice:compact`): the voice channel page on screen +//! once voice is connected, in place of the server page; `voice:compact` +//! with the smaller stage, which is not stored. //! - `VOELIN_DEMO_UI=1`: the Slint front end's sample servers, shown through //! the app's own events, with their pictures as files of a folder in the //! temporary directory (removed when the window closes); nothing is sent to @@ -42,6 +45,12 @@ pub enum Page { pub struct Switches { /// The page to open on. pub open: Page, + /// The voice channel page is asked for (`VOELIN_OPEN=voice`): it shows + /// once voice is connected. + pub voice: bool, + /// With the voice page, the smaller stage, as the switch says (it is not + /// stored, as the Slint front end's `voice:compact`). + pub voice_compact: bool, /// Show the sample servers instead of the stored ones. pub demo_ui: bool, /// Write the sample servers' pictures to a folder of the temporary @@ -69,6 +78,8 @@ impl Default for Switches { fn default() -> Self { Self { open: Page::Shell, + voice: false, + voice_compact: false, demo_ui: false, demo_pictures: false, demo_server: None, @@ -95,8 +106,10 @@ impl Switches { for what in list.split(',').map(str::trim).filter(|w| !w.is_empty()) { match what { "swatch" => switches.open = Page::Swatch, + "voice" => switches.voice = true, + "voice:compact" => (switches.voice, switches.voice_compact) = (true, true), other => tracing::warn!( - "VOELIN_OPEN={other}: this page is not in the GPUI front end yet (swatch is)" + "VOELIN_OPEN={other}: this page is not in the GPUI front end yet (swatch and voice are)" ), } } @@ -166,7 +179,7 @@ mod tests { #[test] fn every_switch_is_read() { let s = env(&[ - ("VOELIN_OPEN", "swatch"), + ("VOELIN_OPEN", "swatch,voice:compact"), ("VOELIN_DEMO_UI", "1"), ("VOELIN_DEMO_SERVER", "2"), ("VOELIN_DEMO_TALK", "10"), @@ -176,6 +189,7 @@ mod tests { ("VOELIN_METER", "1"), ]); assert_eq!(s.open, Page::Swatch); + assert!(s.voice && s.voice_compact, "the voice page, with the smaller stage"); assert!(s.demo_ui && s.demo_pictures, "the sample servers come with their pictures"); assert_eq!(s.demo_server, Some(2)); assert_eq!(s.demo_talk, Some(10.0)); @@ -185,6 +199,12 @@ mod tests { assert!(s.meter); } + #[test] + fn the_voice_page_alone_has_the_larger_stage() { + let s = env(&[("VOELIN_OPEN", "voice")]); + assert_eq!((s.open, s.voice, s.voice_compact), (Page::Shell, true, false)); + } + #[test] fn what_makes_no_sense_is_left_out() { let s = env(&[ diff --git a/crates/voelin-gpui/src/meter.rs b/crates/voelin-gpui/src/meter.rs index 7b79432..8c76186 100644 --- a/crates/voelin-gpui/src/meter.rs +++ b/crates/voelin-gpui/src/meter.rs @@ -60,11 +60,13 @@ pub struct Panes { pub main: Counter, /// The members panel. pub members: Counter, + /// The voice channel page. + pub voice: Counter, } impl Panes { /// The counters with the names the meter reports them under. - pub fn named(&self) -> [(&'static str, Counter); 6] { + pub fn named(&self) -> [(&'static str, Counter); 7] { [ ("rail", self.rail.clone()), ("topbar", self.topbar.clone()), @@ -72,6 +74,7 @@ impl Panes { ("tree", self.tree.clone()), ("main", self.main.clone()), ("members", self.members.clone()), + ("voice", self.voice.clone()), ] } } @@ -278,9 +281,9 @@ mod tests { panes.members.incr(); let named = panes.named(); let names: Vec<&str> = named.iter().map(|(name, _)| *name).collect(); - assert_eq!(names, ["rail", "topbar", "sidebar", "tree", "main", "members"]); + assert_eq!(names, ["rail", "topbar", "sidebar", "tree", "main", "members", "voice"]); let seen: Vec = named.iter().map(|(_, counter)| counter.get()).collect(); - assert_eq!(seen, [0, 0, 0, 0, 0, 1], "the counters are the panes' own"); + assert_eq!(seen, [0, 0, 0, 0, 0, 1, 0], "the counters are the panes' own"); } #[test] diff --git a/crates/voelin-gpui/src/shell/actions.rs b/crates/voelin-gpui/src/shell/actions.rs index 89ee31c..434e73c 100644 --- a/crates/voelin-gpui/src/shell/actions.rs +++ b/crates/voelin-gpui/src/shell/actions.rs @@ -6,13 +6,18 @@ //! ([`crate::app::AppState`]) for what the callback did in Slint: key bindings //! and menus come to the same handlers later. //! +//! The voice page and the call bar add `show_voice_view`, the page's own +//! buttons (the smaller stage, Leave) and the two affordances of the call bar +//! whose pages come with later slices (Share, the voice settings), which are +//! logged. +//! //! The channel tree and the members panel add `join_channel`, //! `join_with_password`, `toggle_collapse`, `search_members`, `open_member` and //! `open_client` (the last one only logs: the client's volume dialog is a //! dialog of its own slice). The callbacks of `bind/servers.rs` that are -//! still not here belong to the slices after: `show_voice_view` to the voice -//! page, `client_audio_changed` to the dialogs, and `copied` (the toast that -//! follows a copy) to whichever copies: [`Invite`] sets it itself. +//! still not here belong to the slices after: `client_audio_changed` to the +//! dialogs, and `copied` (the toast that follows a copy) to whichever copies: +//! [`Invite`] sets it itself. use gpui_kit::{Action, actions}; @@ -41,9 +46,26 @@ actions!( SubmitChannelPassword, /// Close the password prompt without joining. CancelChannelPassword, + /// The voice page's Leave (`Nav.show-voice(false)` then `disconnect_voice`). + LeaveVoice, + /// The voice page's Smaller stage / Larger stage (`ui.voice_compact`). + ToggleVoiceCompact, + /// The call bar's Share: the share menu comes with the stream slices, today it is logged. + OpenShare, + /// The call bar's voice settings: the settings come with their slice, today it is logged. + OpenVoiceSettings, ] ); +/// `show_voice_view`: the voice channel page on (the main area) or off (back to +/// the server page, where the chat will be). +#[derive(Clone, Debug, PartialEq, Action)] +#[action(namespace = voelin, no_json)] +pub struct ShowVoiceView { + /// Shown from now on. + pub on: bool, +} + /// `select_server`: show a server (and observe it). #[derive(Clone, Debug, PartialEq, Action)] #[action(namespace = voelin, no_json)] diff --git a/crates/voelin-gpui/src/shell/main_panel.rs b/crates/voelin-gpui/src/shell/main_panel.rs index 596844b..3cfb5eb 100644 --- a/crates/voelin-gpui/src/shell/main_panel.rs +++ b/crates/voelin-gpui/src/shell/main_panel.rs @@ -14,7 +14,7 @@ use gpui_kit::{ use voelin_ui_vm::bridge::ServerItem; use voelin_ui_vm::servers::logic; -use super::actions::{AddServer, ConnectVoice, DisconnectVoice, EditServer}; +use super::actions::{AddServer, ConnectVoice, DisconnectVoice, EditServer, ShowVoiceView}; use super::widgets::{Avatar, Presence, icon, panel, tagged}; use crate::app::AppState; use crate::meter::Counters; @@ -173,7 +173,7 @@ impl Render for MainPanel { .text_center() .text_size(px(m.font_body)) .text_color(hsla(c.text_muted)) - .child("Its chat and its voice page come with the next slices."), + .child("Its chat comes with a later slice."), ) .child( div() @@ -202,6 +202,20 @@ impl Render for MainPanel { }), )) }) + .when(connected, |row| { + row.child(tagged( + "main-voice", + Button::new("main-voice") + .primary() + .label("Voice channel") + .on_click(|_, window, cx| { + window.dispatch_action( + Box::new(ShowVoiceView { on: true }), + cx, + ); + }), + )) + }) .child(tagged( "main-edit", Button::new("main-edit").label("Edit").on_click( diff --git a/crates/voelin-gpui/src/shell/mod.rs b/crates/voelin-gpui/src/shell/mod.rs index 56778ef..417ec23 100644 --- a/crates/voelin-gpui/src/shell/mod.rs +++ b/crates/voelin-gpui/src/shell/mod.rs @@ -1,12 +1,13 @@ //! The desktop shell of the GPUI front end (`ui/shells/desktop.slint` and //! the server callbacks of `bind/servers.rs`): the server rail, the top bar, -//! the sidebar, the main area, the server form and the toast, each an entity +//! the sidebar, the main area, the voice channel page that takes its place +//! while voice is on screen, the server form and the toast, each an entity //! that listens to the per-area signals of the root state //! ([`crate::signals`]) and tells the window only when what it shows changed, -//! and the actions the user's moves are ([`actions`]). The rail, the top bar -//! and the main area are embedded cached (they have a size of their own), so -//! gpui-pre 0.3.10 renders them again only when they were told something -//! changed; [`crate::view::MainView`] puts them in the window. +//! and the actions the user's moves are ([`actions`]). The rail, the top bar, +//! the main area and the voice page are embedded cached (they have a size of +//! their own), so gpui-pre 0.3.10 renders them again only when they were told +//! something changed; [`crate::view::MainView`] puts them in the window. pub mod actions; mod channels; @@ -19,6 +20,7 @@ mod sidebar; mod toast; mod topbar; mod tree_model; +mod voice; pub mod widgets; pub use channels::ChannelTree; @@ -37,3 +39,4 @@ pub use sidebar::{Sidebar, SidebarView}; pub use toast::{TOAST_TIME, Toast}; pub use topbar::TopBar; pub use tree_model::{Emptiness, TreeEvent, TreeModel}; +pub use voice::{SHORT_WINDOW, VoiceHead, VoicePage}; diff --git a/crates/voelin-gpui/src/shell/voice.rs b/crates/voelin-gpui/src/shell/voice.rs new file mode 100644 index 0000000..1ac5ebf --- /dev/null +++ b/crates/voelin-gpui/src/shell/voice.rs @@ -0,0 +1,650 @@ +//! The voice channel page (`VoiceView` of `screens/voice.slint`, with its +//! `Participant` of `voice-parts.slint` and the `CallBar`): the channel we are +//! in, who is there and who talks, and the call's controls under them. +//! +//! [`VoicePage`] is a cached pane of its own, a sibling of the server page +//! ([`super::MainPanel`]) that [`crate::view::MainView`] puts in the main +//! area in its place while the page is asked for and voice is connected +//! ([`crate::app::AppState::voice_page_shown`]). It takes its people from the +//! tree model ([`TreeEvent::Voice`]: the rows `TreeView::members` gave the one +//! build the tree and the members panel come from, so the page builds nothing +//! of its own) and the header and the call bar from the servers' and the +//! toolbar's signals, compares what it would show with what it showed, and +//! tells the window only when they differ. A person who starts or stops +//! talking, or mutes, is one row of the page and one render of this pane. +//! +//! The larger stage lays the people out as large avatars in a wrapping row; +//! the smaller stage (`ui.voice_compact`, and always in a window under 800 +//! pixels tall) is one row of small avatars that scrolls sideways. The streams +//! of the Slint page come with the stream slices and its chat with the chat's. + +use std::sync::Arc; + +use gpui_kit::prelude::FluentBuilder as _; +use gpui_kit::{ + AnyElement, Context, Entity, FontWeight, InteractiveElement as _, IntoElement, ObjectFit, + ParentElement as _, Render, StatefulInteractiveElement as _, Styled as _, StyledImage as _, + Subscription, Window, div, px, +}; +use voelin_ui_vm::bridge::MemberItem; +use voelin_ui_vm::image::ImageRef; +use voelin_ui_vm::servers::logic; + +use super::actions::{ + Invite, LeaveVoice, OpenMember, OpenShare, OpenVoiceSettings, SetInputMuted, SetOutputMuted, + ShowVoiceView, ToggleVoiceCompact, +}; +use super::tree_model::{TreeEvent, TreeModel}; +use super::widgets::{Avatar, IconButton, banner_scrim, icon, live_badge, panel, speaking_bars_in}; +use crate::app::AppState; +use crate::meter::Counters; +use crate::pictures::{PictureKey, picture}; +use crate::signals::Area; +use crate::theme::tokens::Rgba; +use crate::theme::{VoelinTheme, hsla}; + +/// A window under this tall (pixels) has the smaller stage whatever the +/// setting says (`Nav.short` of the Slint front end). +pub const SHORT_WINDOW: f32 = 800.0; + +/// What the page's header and call bar show, from the state. +#[derive(Clone, Debug, PartialEq)] +pub struct VoiceHead { + /// The channel we are in, as the tree shows it. + pub channel: String, + /// Its topic. + pub topic: String, + /// Its banner, empty without one. + pub banner: ImageRef, + /// Its client limit, -1 without one. + pub limit: i32, + /// Voice is connected. + pub connected: bool, + /// Our microphone is muted. + pub input_muted: bool, + /// Our sound is muted. + pub output_muted: bool, + /// The server can stream (TeamSpeak 6 with voice): the call bar has Share. + pub can_share: bool, +} + +impl VoiceHead { + /// The header and the call bar for the state. + pub fn of(app: &AppState) -> Self { + let card = logic::toolbar(&app.vm, |_| ImageRef::default()); + let can_share = app + .vm + .current + .and_then(|id| app.vm.sessions.get(&id)) + .is_some_and(|view| view.streams_available()); + Self { + channel: card.own_channel, + topic: card.own_channel_topic, + banner: card.own_channel_banner, + limit: card.own_channel_limit, + connected: card.voice_connected, + input_muted: card.input_muted, + output_muted: card.output_muted, + can_share, + } + } +} + +/// The voice channel page. +pub struct VoicePage { + head: VoiceHead, + people: Arc>, + /// The smaller stage by choice. + compact: bool, + /// The window is too short for the larger stage. + short: bool, + counters: Counters, + _subscriptions: Vec, +} + +impl VoicePage { + /// A page that follows `app` for the header and the call bar and `model` for + /// the people, on the stage the setting says (`compact`) in a window that is + /// `short` or not. + pub fn new( + app: &Entity, + model: &Entity, + compact: bool, + short: bool, + cx: &mut Context, + ) -> Self { + let (head, counters, signals) = { + let state = app.read(cx); + (VoiceHead::of(state), state.counters().clone(), state.signals().clone()) + }; + let mut subscriptions = + signals.watch(app, &[Area::Servers, Area::Toolbar], cx, |this: &mut Self, app, cx| { + let head = VoiceHead::of(app.read(cx)); + if head != this.head { + this.head = head; + cx.notify(); + } + }); + subscriptions.push(cx.subscribe(model, |this: &mut Self, _, event: &TreeEvent, cx| { + if let TreeEvent::Voice(people) = event { + this.people = people.clone(); + cx.notify(); + } + })); + let people = model.read(cx).voice().clone(); + Self { head, people, compact, short, counters, _subscriptions: subscriptions } + } + + /// What the header and the call bar show. + pub fn head(&self) -> &VoiceHead { + &self.head + } + + /// The people it shows. + pub fn people(&self) -> &[MemberItem] { + &self.people + } + + /// Whether the page is on the smaller stage now: chosen, or the window is too + /// short for the larger. + pub fn compact_shown(&self) -> bool { + self.compact || self.short + } + + /// Whether the window is too short for the larger stage (the stage button is + /// off then). + pub fn short(&self) -> bool { + self.short + } + + /// The setting `ui.voice_compact` is `compact`: tells the window if the stage + /// changes. + pub fn set_compact(&mut self, compact: bool, cx: &mut Context) { + if self.compact != compact { + self.compact = compact; + cx.notify(); + } + } + + /// The window is `short` (under [`SHORT_WINDOW`] tall) or not. + pub fn set_short(&mut self, short: bool, cx: &mut Context) { + if self.short != short { + self.short = short; + cx.notify(); + } + } +} + +/// The ring around a person's avatar: green while they talk, blue while they +/// stream (3 pixels, 2 on the smaller stage), else the quiet border of the +/// larger stage (2 pixels; the smaller has none). +fn ring(theme: &VoelinTheme, person: &MemberItem, compact: bool) -> (u8, Rgba) { + let c = &theme.colors; + let loud = if compact { 2 } else { 3 }; + if person.talking { + (loud, c.success) + } else if person.streaming { + (loud, c.accent) + } else { + (if compact { 0 } else { 2 }, c.border_strong) + } +} + +/// What the corner of a person's avatar says: the bars while they talk, else +/// that their sound or their microphone is off. +fn corner(theme: &VoelinTheme, person: &MemberItem, compact: bool) -> Option { + let (c, bars) = (&theme.colors, if compact { 12.0 } else { 14.0 }); + let size = if compact { 22.0 } else { 26.0 }; + if !(person.talking || person.muted || person.sound_off) { + return None; + } + let (face, inside) = if person.talking { + if compact { + (c.accent, speaking_bars_in(theme, bars, c.on_accent).into_any_element()) + } else { + (c.surface, speaking_bars_in(theme, bars, c.success).into_any_element()) + } + } else { + let name = if person.sound_off { "headphone-off" } else { "mic-off" }; + (c.surface, icon(name, if compact { 12.0 } else { 15.0 }, c.danger).into_any_element()) + }; + let (id, talking) = (person.id, person.talking); + let inset = if compact { 0.0 } else { 2.0 }; + Some( + div() + .absolute() + .right(px(inset)) + .bottom(px(inset)) + .debug_selector(move || { + format!("voice-{}-{id}", if talking { "speaking" } else { "off" }) + }) + .size(px(size)) + .rounded(px(size / 2.0)) + .border_2() + .border_color(hsla(c.surface)) + .bg(hsla(face)) + .flex() + .items_center() + .justify_center() + .child(inside) + .into_any_element(), + ) +} + +/// One person: an avatar with its ring, the mark in its corner, the name with +/// the crown of an admin, and what they do. A click opens their card. +fn participant(theme: &VoelinTheme, person: &MemberItem, compact: bool) -> AnyElement { + let (c, m) = (&theme.colors, &theme.metrics); + let size = if compact { 54.0 } else { 80.0 }; + let width = if compact { 68.0 } else { 120.0 }; + let outer = size + 8.0; + let id = person.id; + let (ring_width, ring_color) = ring(theme, person, compact); + let client = u16::try_from(id).unwrap_or_default(); + let name_width = width - if person.admin { 16.0 } else { 0.0 }; + let words = if person.talking { + "Speaking" + } else if person.streaming { + "Streaming" + } else if person.away { + "Away" + } else { + "" + }; + let mut face = div() + .relative() + .size(px(outer)) + .flex_none() + .rounded(px(outer / 2.0)) + .border_color(hsla(ring_color)) + .flex() + .items_center() + .justify_center(); + face = match ring_width { + 3 => face.border_3(), + 2 => face.border_2(), + _ => face, + }; + let mut face = face.child( + Avatar::new(("voice-avatar", id as u64), size, &person.initials, person.tint) + .image(person.avatar.clone()), + ); + if let Some(mark) = corner(theme, person, compact) { + face = face.child(mark); + } + div() + .id(("voice-member", id as u64)) + .debug_selector(move || format!("voice-member-{id}")) + .w(px(width)) + .flex_none() + .flex() + .flex_col() + .items_center() + .gap(px(if compact { m.space_1 } else { m.space_2 })) + .cursor_pointer() + .on_click(move |_, window, cx| { + window.dispatch_action(Box::new(OpenMember { client: i32::from(client) }), cx) + }) + .child(face) + .child( + div() + .flex() + .flex_row() + .items_center() + .justify_center() + .gap(px(if compact { m.space_1 } else { m.space_2 })) + .child( + div() + .max_w(px(name_width)) + .truncate() + .text_size(px(if compact { m.font_sm } else { m.font_md })) + .font_weight(if compact { FontWeight::NORMAL } else { FontWeight::MEDIUM }) + .text_color(hsla(if person.myself { c.accent_text } else { c.text })) + .child(person.name.to_string()), + ) + .when(person.admin, |row| { + row.child(icon("crown", if compact { 13.0 } else { 15.0 }, c.gold)) + }), + ) + .when(!compact, |column| { + column.child( + div() + .h(px(m.font_sm + 4.0)) + .flex() + .flex_row() + .items_center() + .justify_center() + .gap(px(m.space_2)) + .when(person.talking, |row| row.child(speaking_bars_in(theme, 12.0, c.success))) + .child( + div() + .truncate() + .text_size(px(m.font_sm)) + .text_color(hsla(if person.talking { + c.success + } else { + c.text_secondary + })) + .child(words), + ) + .when(person.streaming && !person.talking, |row| { + row.child(icon("monitor", 14.0, c.text_secondary)) + }), + ) + }) + .when(compact && person.streaming, |column| column.child(live_badge(theme))) + .into_any_element() +} + +/// The channel's name, its topic and how many are in voice, over its banner. +fn header( + theme: &VoelinTheme, + head: &VoiceHead, + people: usize, + page: &VoicePage, +) -> impl IntoElement { + let (c, m) = (&theme.colors, &theme.metrics); + let banner = PictureKey::of(&head.banner); + let ink = if banner.is_some() { c.text } else { c.text_secondary }; + let compact = page.compact_shown(); + let count = if head.limit >= 0 { + format!("{people} in voice / {} total", head.limit) + } else { + format!("{people} in voice") + }; + let stage = if page.short() { + "The window is too short for the larger stage" + } else if compact { + "Larger stage" + } else { + "Smaller stage" + }; + div() + .id("voice-header") + .debug_selector(|| "voice-header".into()) + .relative() + .w_full() + .flex_none() + .overflow_hidden() + .when_some(banner, |header, key| { + header + .child( + div() + .absolute() + .top(px(0.0)) + .left(px(0.0)) + .size_full() + .child(picture(key).size_full().object_fit(ObjectFit::Cover)), + ) + .child( + div() + .id("voice-banner") + .debug_selector(|| "voice-banner".into()) + .absolute() + .top(px(0.0)) + .left(px(0.0)) + .size_full() + .bg(banner_scrim(theme)), + ) + }) + .child( + div() + .relative() + .flex() + .flex_row() + .gap(px(m.space_5)) + .pl(px(m.space_7)) + .pr(px(m.space_5)) + .pt(px(m.space_5)) + .pb(px(m.space_3)) + .child(div().pt(px(m.space_1)).child(icon("volume-2", 40.0, c.text_secondary))) + .child( + div() + .flex_1() + .min_w_0() + .flex() + .flex_col() + .gap(px(m.space_1)) + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(m.space_3)) + .child( + div() + .id("voice-channel") + .debug_selector(|| "voice-channel".into()) + .flex_1() + .min_w_0() + .truncate() + .text_size(px(m.font_3xl)) + .font_weight(FontWeight::SEMIBOLD) + .text_color(hsla(c.text)) + .child(head.channel.clone()), + ) + .child( + IconButton::new( + "voice-invite", + "user-plus", + "Copy invite link", + ) + .on_click(|_, window, cx| { + window.dispatch_action(Box::new(Invite), cx) + }), + ) + .child( + IconButton::new( + "voice-stage", + if compact { "chevron-down" } else { "chevron-up" }, + stage, + ) + .enabled(!page.short()) + .on_click(|_, window, cx| { + window.dispatch_action(Box::new(ToggleVoiceCompact), cx) + }), + ) + .child( + IconButton::new( + "voice-chat-only", + "message-square", + "Chat only", + ) + .on_click(|_, window, cx| { + window.dispatch_action( + Box::new(ShowVoiceView { on: false }), + cx, + ) + }), + ), + ) + .when(!head.topic.is_empty(), |column| { + column.child( + div() + .truncate() + .text_size(px(m.font_md)) + .text_color(hsla(ink)) + .child(head.topic.clone()), + ) + }) + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(m.space_2)) + .child(icon("users", 16.0, c.text_secondary)) + .child( + div() + .id("voice-count") + .debug_selector(|| "voice-count".into()) + .text_size(px(m.font_body)) + .text_color(hsla(ink)) + .child(count), + ), + ), + ), + ) +} + +/// Mute, deafen, share, the voice settings and Leave, under the people. +fn call_bar(theme: &VoelinTheme, head: &VoiceHead) -> impl IntoElement { + let (c, m) = (&theme.colors, &theme.metrics); + let (input_muted, output_muted) = (head.input_muted, head.output_muted); + let leave = c.danger; + div() + .id("voice-callbar") + .debug_selector(|| "voice-callbar".into()) + .w_full() + .flex_none() + .flex() + .flex_row() + .justify_center() + .items_center() + .gap(px(m.space_3)) + .px(px(m.space_6)) + .pb(px(m.space_3)) + .child( + IconButton::new( + "call-mic", + if input_muted { "mic-off" } else { "mic" }, + if input_muted { "Microphone off" } else { "Microphone on" }, + ) + .size(44.0) + .round() + .filled() + .danger() + .checked(input_muted) + .enabled(head.connected) + .on_click(move |_, window, cx| { + window.dispatch_action(Box::new(SetInputMuted { muted: !input_muted }), cx) + }), + ) + .child( + IconButton::new( + "call-sound", + if output_muted { "headphone-off" } else { "headphones" }, + if output_muted { "Sound off" } else { "Sound on" }, + ) + .size(44.0) + .round() + .filled() + .danger() + .checked(output_muted) + .enabled(head.connected) + .on_click(move |_, window, cx| { + window.dispatch_action(Box::new(SetOutputMuted { muted: !output_muted }), cx) + }), + ) + .when(head.can_share, |bar| { + bar.child( + IconButton::new("call-share", "monitor-up", "Share or go live") + .size(44.0) + .round() + .filled() + .on_click(|_, window, cx| window.dispatch_action(Box::new(OpenShare), cx)), + ) + }) + .child( + IconButton::new("call-settings", "settings", "Voice Settings") + .size(44.0) + .round() + .filled() + .on_click(|_, window, cx| window.dispatch_action(Box::new(OpenVoiceSettings), cx)), + ) + .child( + div() + .id("call-leave") + .debug_selector(|| "call-leave".into()) + .h(px(m.control_height)) + .px(px(m.space_5)) + .flex_none() + .flex() + .flex_row() + .items_center() + .justify_center() + .gap(px(m.space_3)) + .rounded(px(m.radius_md)) + .border_1() + .border_color(hsla(leave.with_alpha(0x73))) + .bg(hsla(c.danger_soft)) + .cursor_pointer() + .hover(move |style| style.bg(hsla(c.danger_soft.mix(leave, 0.25)))) + .on_click(|_, window, cx| window.dispatch_action(Box::new(LeaveVoice), cx)) + .child(icon("phone-off", m.icon_md, leave)) + .child( + div() + .text_size(px(m.font_body)) + .font_weight(FontWeight::MEDIUM) + .text_color(hsla(leave)) + .child("Leave"), + ), + ) +} + +impl Render for VoicePage { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.counters.panes.voice.incr(); + let theme = VoelinTheme::global(cx).clone(); + let (c, m) = (&theme.colors, &theme.metrics); + let compact = self.compact_shown(); + let stage = if compact { + // One row, scrolling sideways when there are many; centred while it fits. + div() + .id("voice-people") + .debug_selector(|| "voice-people".into()) + .w_full() + .h(px(112.0)) + .flex_none() + .overflow_x_scroll() + .child( + div() + .h_full() + .mx_auto() + .flex() + .flex_row() + .gap(px(m.space_3)) + .px(px(m.space_6)) + .pt(px(m.space_2)) + .children( + self.people.iter().map(|person| participant(&theme, person, true)), + ), + ) + } else { + // Centred, wrapping into more rows when there are many. + div() + .id("voice-people") + .debug_selector(|| "voice-people".into()) + .w_full() + .flex_none() + .flex() + .flex_row() + .flex_wrap() + .justify_center() + .gap_x(px(m.space_6)) + .gap_y(px(m.space_4)) + .px(px(m.space_6)) + .pt(px(m.space_3)) + .pb(px(m.space_4)) + .children(self.people.iter().map(|person| participant(&theme, person, false))) + }; + // A cached pane's root takes the size its place gives it. + panel(&theme) + .id("voice-page") + .debug_selector(|| "voice-page".into()) + .size_full() + .child(header(&theme, &self.head, self.people.len(), self)) + .child(stage) + .child(call_bar(&theme, &self.head)) + .child( + div() + .flex_1() + .flex() + .items_center() + .justify_center() + .text_size(px(m.font_body)) + .text_color(hsla(c.text_muted)) + .child("The channel's chat comes with a later slice."), + ) + } +} diff --git a/crates/voelin-gpui/src/shell/widgets.rs b/crates/voelin-gpui/src/shell/widgets.rs index 487ba5c..e1ade04 100644 --- a/crates/voelin-gpui/src/shell/widgets.rs +++ b/crates/voelin-gpui/src/shell/widgets.rs @@ -435,13 +435,14 @@ pub fn live_badge(theme: &VoelinTheme) -> impl IntoElement { /// Three bars: someone speaks. (The Slint bars move; these stand still, as /// nothing here may draw the window anew with the clock.) pub fn speaking_bars(theme: &VoelinTheme, size: f32) -> impl IntoElement { - let c = &theme.colors; + speaking_bars_in(theme, size, theme.colors.success) +} + +/// The speaking bars in `color`, for a ground the green does not stand out on +/// (the accent of the voice page's corner badge). +pub fn speaking_bars_in(_theme: &VoelinTheme, size: f32, color: Rgba) -> impl IntoElement { let bar = move |height: f32| { - div() - .w(px(size * 3.0 / 14.0)) - .h(px(size * height)) - .rounded(px(size / 9.0)) - .bg(hsla(c.success)) + div().w(px(size * 3.0 / 14.0)).h(px(size * height)).rounded(px(size / 9.0)).bg(hsla(color)) }; div() .w(px(size)) diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index 30869e1..e903117 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -18,14 +18,14 @@ use crate::dev::Page; use crate::meter::Counters; use crate::shell::actions::{ AddServer, CancelChannelPassword, CancelServerForm, ConnectVoice, DeleteServer, - DisconnectVoice, EditChannel, EditServer, Invite, JoinChannel, MoveClient, OpenClient, - OpenMember, OpenSearch, OpenSettings, PushToTalk, SaveAndConnect, SaveServer, SearchMembers, - SelectChannel, SelectServer, SetInputMuted, SetOutputMuted, SubmitChannelPassword, - ToggleChannel, + DisconnectVoice, EditChannel, EditServer, Invite, JoinChannel, LeaveVoice, MoveClient, + OpenClient, OpenMember, OpenSearch, OpenSettings, OpenShare, OpenVoiceSettings, PushToTalk, + SaveAndConnect, SaveServer, SearchMembers, SelectChannel, SelectServer, SetInputMuted, + SetOutputMuted, ShowVoiceView, SubmitChannelPassword, ToggleChannel, ToggleVoiceCompact, }; use crate::shell::{ - ChannelPassword, ChannelTree, Emptiness, MainPanel, MemberCardView, MembersPane, ServerForm, - ServerRail, Sidebar, Toast, TopBar, TreeEvent, TreeModel, + ChannelPassword, ChannelTree, Emptiness, MainPanel, MemberCardView, MembersPane, SHORT_WINDOW, + ServerForm, ServerRail, Sidebar, Toast, TopBar, TreeEvent, TreeModel, VoicePage, }; use crate::swatch::swatch; use crate::theme::{VoelinTheme, hsla}; @@ -66,6 +66,10 @@ pub struct MainView { /// How wide the members panel is (`ui.members_width`, read at start). members_width: f32, main: Entity, + /// The voice channel page, which the main area is while voice is on screen. + voice: Entity, + /// The main area is the voice page. + voice_shown: bool, toast: Entity, form: Option>, /// The prompt for a locked channel's password, when one is open, and how @@ -115,6 +119,18 @@ impl MainView { this.form = None; cx.notify(); } + // The voice page takes the main area while it is asked for and voice + // is connected, and keeps the stage the setting says: an observer of + // the root state reads it and sets its own fields. + let (shown, compact) = { + let app = app.read(cx); + (app.voice_page_shown(), app.voice_compact()) + }; + if shown != this.voice_shown { + this.voice_shown = shown; + cx.notify(); + } + this.voice.update(cx, |page, cx| page.set_compact(compact, cx)); // A channel's password was asked for. let (generation, ask) = { let app = app.read(cx); @@ -127,7 +143,13 @@ impl MainView { cx.notify(); } }), - cx.observe_window_bounds(window, |this, window, cx| report(&this.app, window, cx)), + cx.observe_window_bounds(window, |this, window, cx| { + report(&this.app, window, cx); + // A cached pane renders only when told: it is told when the window + // becomes short or tall. + let short = is_short(window); + this.voice.update(cx, |page, cx| page.set_short(short, cx)); + }), ]; report(&app, window, cx); // The window is mapped by now: a window the last run left maximized @@ -141,6 +163,12 @@ impl MainView { let tree = cx.new(|cx| ChannelTree::new(&model, counters.clone(), cx)); let members = cx.new(|cx| MembersPane::new(&model, counters.clone(), window, cx)); let card = cx.new(|cx| MemberCardView::new(&model, members_width, cx)); + let (voice_shown, compact) = { + let app = app.read(cx); + (app.voice_page_shown(), app.voice_compact()) + }; + let short = is_short(window); + let voice = cx.new(|cx| VoicePage::new(&app, &model, compact, short, cx)); // The members panel comes with the first server on screen. subscriptions.push(cx.subscribe(&model, |_, _, event: &TreeEvent, cx| { if matches!(event, TreeEvent::Lists) { @@ -157,6 +185,8 @@ impl MainView { card, members_width, main: cx.new(|cx| MainPanel::new(&app, cx)), + voice, + voice_shown, toast: cx.new(|cx| Toast::new(&app, cx)), app, page, @@ -195,6 +225,16 @@ impl MainView { &self.card } + /// The voice channel page. + pub fn voice_page(&self) -> &Entity { + &self.voice + } + + /// Whether the main area is the voice page now. + pub fn voice_page_shown(&self) -> bool { + self.voice_shown + } + /// Open the form with `form`'s values. fn open_form( &mut self, @@ -241,6 +281,45 @@ impl MainView { self.app.update(cx, |app, cx| app.invite(cx)); } + fn show_voice_view(&mut self, action: &ShowVoiceView, _: &mut Window, cx: &mut Context) { + self.app.update(cx, |app, cx| app.show_voice_view(action.on, cx)); + } + + /// The voice page's Leave: the page goes (the chat it stands for is what + /// the main area shows next time), then voice is left. + fn leave_voice(&mut self, _: &LeaveVoice, _: &mut Window, cx: &mut Context) { + self.app.update(cx, |app, cx| { + app.show_voice_view(false, cx); + app.disconnect_voice(cx); + }); + } + + fn toggle_voice_compact( + &mut self, + _: &ToggleVoiceCompact, + _: &mut Window, + cx: &mut Context, + ) { + self.app.update(cx, |app, cx| app.toggle_voice_compact(cx)); + } + + fn open_share(&mut self, _: &OpenShare, _: &mut Window, _: &mut Context) { + tracing::info!( + "sharing a screen comes with the stream slices (docs/ui.md, \"What is next\")" + ); + } + + fn open_voice_settings( + &mut self, + _: &OpenVoiceSettings, + _: &mut Window, + _: &mut Context, + ) { + tracing::info!( + "the voice settings come with the settings slice (docs/ui.md, \"What is next\")" + ); + } + fn add_server(&mut self, _: &AddServer, window: &mut Window, cx: &mut Context) { let form = self.app.read(cx).bookmark_form(-1); self.open_form(form, window, cx); @@ -397,6 +476,11 @@ fn zoom(_: &Window) { tests::ZOOMS.with(|zooms| zooms.set(zooms.get() + 1)); } +/// Whether `window` is too short for the voice page's larger stage. +fn is_short(window: &Window) -> bool { + f32::from(window.viewport_size().height) < SHORT_WINDOW +} + /// Tell `app` where `window` is: it keeps the place at the next tick. fn report(app: &Entity, window: &Window, cx: &mut App) { if let Some(state) = window_state(window, cx) { @@ -428,13 +512,14 @@ impl Render for MainView { // says and not narrower than the least a panel can be. let people = self.model.read(cx).emptiness() != Emptiness::NoServer; let members_width = self.members_width.max(m.right_panel_min); + // The main area is the server form, else the voice page while voice is + // asked for and connected, else the server page. A cached pane renders + // only when it was told, and the one not chosen is not in the tree. + let place = StyleRefinement::default().flex_1().min_w_0().h_full(); let main = match &self.form { Some(form) => form.clone().into_any_element(), - None => self - .main - .clone() - .cached(StyleRefinement::default().flex_1().min_w_0().h_full()) - .into_any_element(), + None if self.voice_shown => self.voice.clone().cached(place).into_any_element(), + None => self.main.clone().cached(place).into_any_element(), }; frame .key_context("Shell") @@ -449,6 +534,11 @@ impl Render for MainView { .on_action(cx.listener(Self::set_output_muted)) .on_action(cx.listener(Self::push_to_talk)) .on_action(cx.listener(Self::invite)) + .on_action(cx.listener(Self::show_voice_view)) + .on_action(cx.listener(Self::leave_voice)) + .on_action(cx.listener(Self::toggle_voice_compact)) + .on_action(cx.listener(Self::open_share)) + .on_action(cx.listener(Self::open_voice_settings)) .on_action(cx.listener(Self::add_server)) .on_action(cx.listener(Self::edit_server)) .on_action(cx.listener(Self::save_server)) @@ -553,11 +643,11 @@ mod tests { /// What a test of the shell holds: the root state, the window's view, the /// engine's events and the meter's counters. - struct Shell { - app: Entity, - view: Entity, - events: UnboundedSender, - counters: Counters, + pub(super) struct Shell { + pub(super) app: Entity, + pub(super) view: Entity, + pub(super) events: UnboundedSender, + pub(super) counters: Counters, } fn bookmark(name: &str, address: &str) -> Bookmark { @@ -569,7 +659,7 @@ mod tests { } } - fn backend_with(servers: &[(&str, &str)]) -> Backend { + pub(super) fn backend_with(servers: &[(&str, &str)]) -> Backend { let backend = Backend::in_memory().unwrap(); for (name, address) in servers { backend.store.add_bookmark(&bookmark(name, address)).unwrap(); @@ -579,7 +669,7 @@ mod tests { /// Open the window of the shell on `backend`, with the theme and the /// components set up as `run` sets them. - fn open( + pub(super) fn open( cx: &mut TestAppContext, backend: Backend, switches: Switches, @@ -605,23 +695,26 @@ mod tests { } /// Run what is queued and draw the window. - fn frame(cx: &mut VisualTestContext) { + pub(super) fn frame(cx: &mut VisualTestContext) { cx.run_until_parked(); cx.update(|window, cx| window.draw(cx).clear(cx)); } /// Where the element with this debug selector is on screen. - fn at(cx: &mut VisualTestContext, selector: &str) -> Option> { + pub(super) fn at( + cx: &mut VisualTestContext, + selector: &str, + ) -> Option> { cx.debug_bounds(Box::leak(selector.to_owned().into_boxed_str())) } - fn click(cx: &mut VisualTestContext, selector: &str) { + pub(super) fn click(cx: &mut VisualTestContext, selector: &str) { let bounds = at(cx, selector).unwrap_or_else(|| panic!("{selector} is not on screen")); cx.simulate_click(bounds.center(), Default::default()); frame(cx); } - fn ids(shell: &Shell, cx: &mut VisualTestContext) -> Vec { + pub(super) fn ids(shell: &Shell, cx: &mut VisualTestContext) -> Vec { shell.app.read_with(cx, |app, _| app.vm.bookmarks.iter().map(|b| b.id).collect()) } @@ -633,7 +726,7 @@ mod tests { BookmarkForm { id: -1, address: address.into(), ..Default::default() } } - fn connected(id: i64) -> Event { + pub(super) fn connected(id: i64) -> Event { Event::State { session: id as u64, state: SessionState { voice: VoiceState::Connected, ..Default::default() }, @@ -881,7 +974,7 @@ mod tests { /// A server as an engine reports one: the sample server's events (nested /// channels, spacers, a locked channel, people with groups) played into the /// window, its bookmark the one shown. Voice is up and we are in Chill Zone. - fn sample_server(shell: &Shell, cx: &mut VisualTestContext) { + pub(super) fn sample_server(shell: &Shell, cx: &mut VisualTestContext) { shell.app.update(cx, |app, _| { app.vm.bookmarks = sample::bookmarks(); app.vm.current = Some(sample::DEMO); @@ -2157,3 +2250,6 @@ mod tests { shell.view.read_with(cx, |view, _| assert!(view.form().is_none())); } } + +#[cfg(test)] +mod voice_tests; diff --git a/crates/voelin-gpui/src/view/voice_tests.rs b/crates/voelin-gpui/src/view/voice_tests.rs new file mode 100644 index 0000000..0998d6e --- /dev/null +++ b/crates/voelin-gpui/src/view/voice_tests.rs @@ -0,0 +1,337 @@ +//! The voice page and the call bar on a drawn window (`crate::shell::VoicePage`): +//! the page takes the main area while voice is connected and asked for, its +//! people carry their marks, a talker or a mute redraws it and no pane that has +//! nothing to show for it, the call bar's buttons reach the engine, and the +//! smaller stage is kept. + +use std::sync::Arc; + +use gpui::{TestAppContext, VisualTestContext}; +use gpui_kit::{px, size}; +use voelin_core::{Command, Event, SessionState, VoiceState}; +use voelin_ui_vm::bridge::MemberItem; +use voelin_ui_vm::dev::logic as sample; +use voelin_ui_vm::settings::UI_VOICE_COMPACT; + +use super::tests::{Shell, at, click, frame, open, sample_server}; +use crate::backend::Backend; +use crate::dev::Switches; + +/// The sample server's presence with `$body` applied to it, named `$p`, as an +/// event (a macro: `voelin_core` does not name its presence type). +macro_rules! sample_presence { + ($p:ident => $body:expr) => {{ + let Some(Event::Presence { session, mut presence }) = + sample::server_events().into_iter().find(|e| matches!(e, Event::Presence { .. })) + else { + panic!("the sample server has a presence") + }; + { + let $p = Arc::make_mut(&mut presence); + $body; + } + Event::Presence { session, presence } + }}; +} + +/// The window on the sample server, tall enough for the larger stage (the test +/// platform's window is 1024 by 768). +fn tall(cx: &mut TestAppContext, switches: Switches) -> (Shell, &mut VisualTestContext) { + let (shell, vcx) = open(cx, Backend::in_memory().unwrap(), switches); + vcx.simulate_resize(size(px(1440.0), px(960.0))); + sample_server(&shell, vcx); + (shell, vcx) +} + +/// The sample server's window with the voice page asked for. +fn on_the_voice_page( + cx: &mut TestAppContext, + switches: Switches, +) -> (Shell, &mut VisualTestContext) { + let (shell, vcx) = tall(cx, switches); + click(vcx, "main-voice"); + (shell, vcx) +} + +/// The people the page shows. +fn people(shell: &Shell, cx: &mut VisualTestContext) -> Vec { + shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).people().to_vec()) +} + +/// How many times each pane was rendered: the rail, the top bar, the main +/// area, the tree, the members panel and the voice page. +fn drawn(shell: &Shell) -> [u64; 6] { + let panes = &shell.counters.panes; + [&panes.rail, &panes.topbar, &panes.main, &panes.tree, &panes.members, &panes.voice] + .map(|pane| pane.get()) +} + +fn session(voice: VoiceState) -> Event { + Event::State { + session: sample::DEMO as u64, + state: SessionState { voice, ..Default::default() }, + } +} + +/// The page takes the main area when asked for with voice connected, in the +/// server page's place, and gives it back on Chat only, on a lost connection +/// and on Leave; a reconnect asks for it again, as the Slint page does. +#[gpui::test] +fn the_voice_page_takes_the_main_area_while_voice_is_connected_and_asked_for( + cx: &mut TestAppContext, +) { + let (shell, cx) = tall(cx, Switches::default()); + assert!(at(cx, "main-server").is_some() && at(cx, "voice-page").is_none()); + assert!(at(cx, "main-voice").is_some(), "the server page offers the voice channel"); + + click(cx, "main-voice"); + assert!(at(cx, "voice-page").is_some(), "the page"); + assert!(at(cx, "main-server").is_none(), "in the server page's place"); + assert!(shell.view.read_with(cx, |view, _| view.voice_page_shown())); + assert!(shell.app.read_with(cx, |app, _| app.vm.voice_view), "the view model knows"); + + click(cx, "voice-chat-only"); + assert!(at(cx, "voice-page").is_none() && at(cx, "main-server").is_some()); + assert!(!shell.app.read_with(cx, |app, _| app.vm.voice_view)); + + click(cx, "main-voice"); + shell.events.send(session(VoiceState::Disconnected)).unwrap(); + frame(cx); + assert!(at(cx, "voice-page").is_none(), "no page without voice"); + assert!(at(cx, "main-server").is_some()); + shell + .events + .send(Event::State { + session: sample::DEMO as u64, + state: SessionState { + voice: VoiceState::Connected, + own_channel: Some(2), + own_client: Some(1), + ..Default::default() + }, + }) + .unwrap(); + frame(cx); + assert!(at(cx, "voice-page").is_some(), "asked for still: back with the connection"); +} + +/// The people of our channel are on the page, each with the marks the Slint +/// page gives them, the channel's name, its topic and the count. +#[gpui::test] +fn the_page_shows_the_people_of_our_channel_with_their_marks(cx: &mut TestAppContext) { + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + let names: Vec = people(&shell, cx).iter().map(|m| m.name.to_string()).collect(); + assert_eq!(names, ["Lumen", "Kairo", "Nova", "dex", "Mira"]); + for id in [1, 2, 3, 4, 5] { + assert!(at(cx, &format!("voice-member-{id}")).is_some(), "client {id} is on the stage"); + } + assert!(at(cx, "voice-member-6").is_none(), "Talon is in another channel"); + assert!(at(cx, "voice-speaking-3").is_some(), "Kairo talks: the bars in the corner"); + assert!(at(cx, "voice-off-5").is_some(), "dex's microphone is off: its mark"); + assert!(at(cx, "voice-off-3").is_none() && at(cx, "voice-speaking-5").is_none()); + let head = shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).head().clone()); + assert_eq!( + (head.channel.as_str(), head.topic.as_str(), head.limit), + ("Chill Zone", "Hang out, chat, and explore the worlds beyond.", 50) + ); + assert!(head.can_share && head.connected && !head.input_muted && !head.output_muted); + assert!(at(cx, "voice-banner").is_none(), "no picture has arrived: no banner"); + assert!(at(cx, "voice-count").is_some() && at(cx, "voice-channel").is_some()); +} + +/// The channel's banner is behind the header, under the surface veil, once its +/// picture is in the cache (the sample server's, as files of a folder of the +/// state's own). +#[gpui::test] +fn the_channels_banner_is_behind_the_header_under_a_veil(cx: &mut TestAppContext) { + let switches = + Switches { demo_ui: true, demo_pictures: true, voice: true, ..Default::default() }; + let (shell, cx) = open(cx, Backend::in_memory().unwrap(), switches); + cx.simulate_resize(size(px(1440.0), px(960.0))); + frame(cx); + assert!(at(cx, "voice-banner").is_some(), "the veil over Chill Zone's picture"); + let head = shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).head().clone()); + assert!(head.banner.size().width > 0, "the picture is decoded"); + // The sample pictures' folder goes with the close. + let folder = shell.app.read_with(cx, |app, _| app.demo_dir().cloned()); + shell.app.update(cx, |app, cx| app.shutdown(cx)); + assert!(!folder.is_some_and(|folder| folder.exists()), "the close removes it"); +} + +/// Someone who starts or stops talking, or mutes, is one row of the page and +/// one render of it: the model builds the rows once for the tree, the panel and +/// the page, and the rail, the top bar and the main area are not drawn. A change +/// that is not in our channel does not draw the page at all. +#[gpui::test] +fn talking_and_mute_events_update_the_voice_page_rows_and_nothing_else(cx: &mut TestAppContext) { + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + let model = shell.view.read_with(cx, |view, _| view.tree_model().clone()); + let row = |shell: &Shell, cx: &mut VisualTestContext, id: i32| { + people(shell, cx).into_iter().find(|m| m.id == id).unwrap() + }; + assert!(row(&shell, cx, 3).talking && row(&shell, cx, 5).muted); + + let (before, builds) = (drawn(&shell), model.read_with(cx, |m, _| m.refreshes())); + shell + .events + .send(Event::Talking { session: sample::DEMO as u64, client: 3, talking: false }) + .unwrap(); + frame(cx); + let after = drawn(&shell); + assert!(!row(&shell, cx, 3).talking, "Kairo stopped"); + assert!(at(cx, "voice-speaking-3").is_none(), "and the bars are gone"); + assert_eq!(after[5], before[5] + 1, "the page was drawn once"); + assert_eq!(model.read_with(cx, |m, _| m.refreshes()), builds + 1, "one build for all three"); + assert_eq!( + [after[0], after[1], after[2]], + [before[0], before[1], before[2]], + "the rail, the top bar and the main area were not drawn" + ); + + // dex unmutes: the presence says so. + let before = drawn(&shell); + shell + .events + .send(sample_presence!(p => p.clients.get_mut(&5).unwrap().input_muted = false)) + .unwrap(); + frame(cx); + let after = drawn(&shell); + assert!(!row(&shell, cx, 5).muted && at(cx, "voice-off-5").is_none(), "dex's mark is gone"); + assert_eq!(after[5], before[5] + 1, "the page was drawn once"); + assert_eq!([after[0], after[1], after[2]], [before[0], before[1], before[2]]); + + // Someone in another channel talks: the tree has a row to change, the page + // has nothing new. + let before = drawn(&shell); + shell + .events + .send(Event::Talking { session: sample::DEMO as u64, client: 6, talking: true }) + .unwrap(); + frame(cx); + let after = drawn(&shell); + assert_eq!(after[5], before[5], "the page was not drawn"); + assert!(after[3] > before[3], "though the tree was"); +} + +/// Leave reaches the engine: the voice session is left, and the page goes. +#[gpui::test] +fn leave_reaches_the_engine(cx: &mut TestAppContext) { + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + shell.app.read_with(cx, |app, _| app.take_sent()); + click(cx, "call-leave"); + let sent = shell.app.read_with(cx, |app, _| app.take_sent()); + assert!( + matches!(sent.as_slice(), [Command::DisconnectVoice { session }] if *session == sample::DEMO as u64), + "{sent:?}" + ); + assert!(!shell.app.read_with(cx, |app, _| app.vm.voice_view), "and the page is not asked for"); +} + +/// The call bar's mute and deafen buttons send their commands; Share and the +/// voice settings are logged and send nothing; the muted state shows on the +/// buttons once the engine says it. +#[gpui::test] +fn the_call_bars_buttons_send_their_commands_and_the_placeholders_nothing(cx: &mut TestAppContext) { + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + shell.app.read_with(cx, |app, _| app.take_sent()); + for button in ["call-mic", "call-sound", "call-share", "call-settings"] { + click(cx, button); + } + let sent = shell.app.read_with(cx, |app, _| app.take_sent()); + let id = sample::DEMO as u64; + assert!( + matches!( + sent.as_slice(), + [ + Command::SetInputMuted { session: a, muted: true }, + Command::SetOutputMuted { session: b, muted: true }, + ] if *a == id && *b == id + ), + "{sent:?}" + ); + let state = SessionState { + voice: VoiceState::Connected, + own_channel: Some(2), + own_client: Some(1), + input_muted: true, + ..Default::default() + }; + shell.events.send(Event::State { session: id, state }).unwrap(); + frame(cx); + let head = shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).head().clone()); + assert!(head.input_muted && !head.output_muted); + click(cx, "call-mic"); + let sent = shell.app.read_with(cx, |app, _| app.take_sent()); + assert!( + matches!(sent.as_slice(), [Command::SetInputMuted { muted: false, .. }]), + "muted: the next click unmutes {sent:?}" + ); +} + +/// The smaller stage is a row of small avatars, chosen with the button and kept +/// in `ui.voice_compact`; a window under 800 pixels tall has it whatever the +/// setting says, and the button is off there. +#[gpui::test] +fn the_smaller_stage_is_remembered_and_a_short_window_has_it_anyway(cx: &mut TestAppContext) { + let (shell, cx) = on_the_voice_page(cx, Switches::default()); + let compact = |shell: &Shell, cx: &mut VisualTestContext| { + shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).compact_shown()) + }; + let stored = |shell: &Shell, cx: &mut VisualTestContext| { + shell.app.read_with(cx, |app, _| app.backend().prefs.get(&UI_VOICE_COMPACT)) + }; + assert!(!compact(&shell, cx) && !stored(&shell, cx)); + let tall_stage = at(cx, "voice-people").expect("the people").size.height; + click(cx, "voice-stage"); + assert!(compact(&shell, cx) && stored(&shell, cx), "chosen and kept"); + let row = at(cx, "voice-people").expect("the people").size.height; + assert_eq!(row, px(112.0), "one row of small avatars"); + assert!(row < tall_stage, "{row:?} against {tall_stage:?}"); + click(cx, "voice-stage"); + assert!(!compact(&shell, cx) && !stored(&shell, cx), "and back"); + + // A short window: the smaller stage, and its button does nothing. + cx.simulate_resize(size(px(1440.0), px(700.0))); + frame(cx); + assert!(compact(&shell, cx), "under 800 pixels tall"); + click(cx, "voice-stage"); + assert!(compact(&shell, cx) && !stored(&shell, cx), "the button is off"); + cx.simulate_resize(size(px(1440.0), px(960.0))); + frame(cx); + assert!(!compact(&shell, cx), "tall again"); +} + +/// The setting is read at start: a run that left the smaller stage opens on it. +#[gpui::test] +fn the_stage_comes_back_as_the_last_run_left_it(cx: &mut TestAppContext) { + let backend = Backend::in_memory().unwrap(); + backend.prefs.set(&UI_VOICE_COMPACT, true).unwrap(); + let (shell, vcx) = open(cx, backend, Switches::default()); + vcx.simulate_resize(size(px(1440.0), px(960.0))); + frame(vcx); + assert!(shell.app.read_with(vcx, |app, _| app.voice_compact())); + assert!(shell.view.read_with(vcx, |view, cx| view.voice_page().read(cx).compact_shown())); +} + +/// `VOELIN_OPEN=voice:compact` on the sample servers: the page, on the smaller +/// stage; the sample servers keep nothing, so the stage the button then picks +/// is not stored either. +#[gpui::test] +fn a_development_switch_asks_for_the_voice_page_and_its_stage(cx: &mut TestAppContext) { + let switches = Switches { + demo_ui: true, + demo_pictures: false, + voice: true, + voice_compact: true, + ..Default::default() + }; + let (shell, cx) = open(cx, Backend::in_memory().unwrap(), switches); + cx.simulate_resize(size(px(1440.0), px(960.0))); + frame(cx); + assert!(at(cx, "voice-page").is_some(), "the sample server is connected: its voice page"); + assert!(shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).compact_shown())); + // The sample servers keep nothing: the stage is the switch's only. + click(cx, "voice-stage"); + assert!(!shell.app.read_with(cx, |app, _| app.backend().prefs.get(&UI_VOICE_COMPACT))); + assert!(!shell.view.read_with(cx, |view, cx| view.voice_page().read(cx).compact_shown())); +}