From b1334127000f02d570de5646c4ad9b8b92a532ba Mon Sep 17 00:00:00 2001 From: faumaray Date: Tue, 27 Oct 2026 23:37:37 +0300 Subject: [PATCH] voelin-gpui: Update.refresh as per-area signals, the shell's panes cached The "for the next slices" design of the shell's review. The root state turns the flags of an update into entities with a generation, one per area (servers, toolbar, tree, member card; crate::signals), raised once per batch of engine events and once per transition, and the rail, the top bar, the sidebar and the main area listen to their areas instead of asking the root whether anything changed for them. The rail, the top bar and the main area are embedded with Entity::cached and a size of their own, so gpui-pre renders them again only when they were notified, and the meter counts the renders of each pane (rail, topbar, sidebar, tree, main, members) beside the window's frames. voelin-ui-vm: Refresh::merge, for the flags of a batch. Tests: a burst raises each area once per batch; a change of the voice card renders the sidebar with every frame and none of the cached panes. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-gpui/src/app.rs | 84 +++++++++--- crates/voelin-gpui/src/lib.rs | 1 + crates/voelin-gpui/src/meter.rs | 46 ++++++- crates/voelin-gpui/src/shell/main_panel.rs | 17 ++- crates/voelin-gpui/src/shell/mod.rs | 10 +- crates/voelin-gpui/src/shell/rail.rs | 22 ++- crates/voelin-gpui/src/shell/sidebar.rs | 31 +++-- crates/voelin-gpui/src/shell/topbar.rs | 17 ++- crates/voelin-gpui/src/signals.rs | 150 +++++++++++++++++++++ crates/voelin-gpui/src/view.rs | 64 +++++++-- crates/voelin-ui-vm/src/host.rs | 50 +++++++ 11 files changed, 434 insertions(+), 58 deletions(-) create mode 100644 crates/voelin-gpui/src/signals.rs diff --git a/crates/voelin-gpui/src/app.rs b/crates/voelin-gpui/src/app.rs index a342c8f..1e3570b 100644 --- a/crates/voelin-gpui/src/app.rs +++ b/crates/voelin-gpui/src/app.rs @@ -19,15 +19,18 @@ //! channel of jobs a third foreground task runs on GPUI's thread, as //! `later(|app| ...)` does in the Slint front end. //! -//! The root notifies on every batch of events and on every move of the user's; -//! it is the views (`crate::shell`) that decide whether what they show changed -//! and tell the window only then (the window draws every part). What a -//! transition asks for besides the state (`Update`) is carried out as far as -//! this front end has the parts: the engine's commands go out, the status line -//! is set, what is to be kept goes to the store, a setting the theme follows -//! is applied and the warnings are logged; the redraw flags, the notices, the -//! pictures to decode and the streams' capture and decoder are for the slices -//! that have those parts. +//! The root notifies on every batch of events and on every move of the user's +//! (the window's title and the toast follow it). What a transition's +//! `refresh` flags name is raised as per-area signals ([`crate::signals`], +//! once per batch of events) that the parts of the window observe, and it is +//! the parts (`crate::shell`) that decide whether what they show changed and +//! tell the window only then. What a transition asks for besides the state +//! (`Update`) is carried out as far as this front end has the parts: the +//! engine's commands go out, the status line is set, what is to be kept goes +//! to the store, a setting the theme follows is applied, the warnings are +//! logged and the flags of the areas that have a part are raised; the other +//! flags, the notices, the pictures to decode and the streams' capture and +//! decoder are for the slices that have those parts. use std::task::Poll; use std::time::Duration; @@ -40,7 +43,7 @@ use voelin_core::{Command, Engine, Event, VoiceState}; use voelin_store::{Bookmark, ChatRead, ChatTarget as StoredTarget, Store}; use voelin_ui_vm::bridge::{BookmarkForm, ChatLine}; use voelin_ui_vm::events; -use voelin_ui_vm::host::{Cx, Host, Persist, Update}; +use voelin_ui_vm::host::{Cx, Host, Persist, Refresh, Update}; use voelin_ui_vm::image::ImageRef; use voelin_ui_vm::servers::logic; use voelin_ui_vm::servers::sequence::{self, Backing}; @@ -53,6 +56,7 @@ use voelin_ui_vm::social::logic::Social; use crate::backend::Backend; use crate::dev::Switches; use crate::meter::Counters; +use crate::signals::Signals; use crate::theme::{Choice, VoelinTheme}; /// The toolkit-free state of the client, with the chat lines of a tab as plain @@ -230,6 +234,7 @@ pub struct AppState { switches: Switches, window: WindowKeeper, counters: Counters, + signals: Signals, later: Later, /// What went to the engine (tests look at it: an engine of the test's /// own would have to be asked what it was sent). @@ -311,6 +316,7 @@ impl AppState { opening_reports: 0, }, counters, + signals: Signals::new(cx), later: Later(later), #[cfg(test)] sent: Default::default(), @@ -362,6 +368,17 @@ impl AppState { &self.backend } + /// What the meter counts. + pub fn counters(&self) -> &Counters { + &self.counters + } + + /// The per-area signals the parts of the window observe + /// ([`crate::signals`]). + pub fn signals(&self) -> &Signals { + &self.signals + } + /// What an engine event does to the state ([`events::handle`], the one /// dispatch of both front ends), and what asks to be done of it. fn handle(&mut self, event: Event) -> Update { @@ -380,13 +397,16 @@ impl AppState { events: impl IntoIterator, cx: &mut Context, ) { + let mut refresh = Refresh::default(); for event in events { self.events_handled += 1; self.counters.engine_events.incr(); let update = self.handle(event); - self.perform(update, cx); + refresh.merge(self.perform(update, cx)); } self.counters.connections.set(self.connections() as u64); + // Each area once for the whole batch. + self.signals.raise(&refresh, cx); cx.notify(); } @@ -427,7 +447,8 @@ impl AppState { let mut transition = Cx::new(&host); let result = f(&mut self.vm, &mut transition, &backing); let update = transition.update; - self.perform(update, cx); + let refresh = self.perform(update, cx); + self.signals.raise(&refresh, cx); result } @@ -463,9 +484,14 @@ impl AppState { } } - /// Do what an update asks that this front end has the parts for. - fn perform(&mut self, update: Update, cx: &mut Context) { - let Update { status, commands, persist, warnings, rest, password, discover, .. } = update; + /// Do what an update asks that this front end has the parts for, and say + /// which areas of the window it asks to draw again (`refresh`, and what + /// the front end's own settings change): the caller raises them + /// ([`Signals::raise`]) once for as many updates as it has handled. + fn perform(&mut self, update: Update, cx: &mut Context) -> Refresh { + let Update { + refresh, status, commands, persist, warnings, rest, password, discover, .. + } = update; self.log_warnings(warnings); for command in commands { self.send(command); @@ -486,6 +512,7 @@ impl AppState { if let Some(Event::SettingChanged { key }) = rest { self.setting_changed(&key, cx); } + refresh } /// The status line says this from now on. @@ -861,6 +888,7 @@ mod tests { use super::*; use crate::backend::testing::TempDir; use crate::engine::HostedEngine; + use crate::signals::Area; fn voice(voice: VoiceState) -> Event { Event::State { session: 1, state: SessionState { voice, ..Default::default() } } @@ -976,6 +1004,32 @@ mod tests { assert_eq!(counters.engine_events.get(), total as u64); } + /// A batch of events raises each area its updates name once, not once per + /// event: the parts that listen to an area are told once per batch. + #[gpui::test] + fn a_burst_raises_each_area_once_per_batch(cx: &mut TestAppContext) { + let (app, tx, _) = entity(cx); + let signals = app.read_with(cx, |app, _| app.signals().clone()); + let generation = |cx: &mut TestAppContext, area: Area| { + signals.of(area).read_with(cx, |signal, _| signal.generation()) + }; + for i in 0..BATCH * 2 { + let state = SessionState { + voice: VoiceState::Connected, + input_muted: i % 2 == 0, + ..Default::default() + }; + tx.send(Event::State { session: 1, state }).unwrap(); + } + cx.run_until_parked(); + // A session's state names the servers, the toolbar and the tree; none + // names a member card. + for area in [Area::Servers, Area::Toolbar, Area::Tree] { + assert_eq!(generation(cx, area), 2, "{area:?}: two batches of {BATCH}"); + } + assert_eq!(generation(cx, Area::MemberCard), 0); + } + /// The tick counts and keeps what is due, and draws nothing: a window left /// alone is not redrawn once a second. #[gpui::test] diff --git a/crates/voelin-gpui/src/lib.rs b/crates/voelin-gpui/src/lib.rs index 3186e8f..71d454a 100644 --- a/crates/voelin-gpui/src/lib.rs +++ b/crates/voelin-gpui/src/lib.rs @@ -13,6 +13,7 @@ pub mod engine; pub mod fonts; pub mod meter; pub mod shell; +pub mod signals; pub mod swatch; pub mod theme; pub mod view; diff --git a/crates/voelin-gpui/src/meter.rs b/crates/voelin-gpui/src/meter.rs index 10c1775..8a2e9ba 100644 --- a/crates/voelin-gpui/src/meter.rs +++ b/crates/voelin-gpui/src/meter.rs @@ -41,6 +41,41 @@ impl Counter { } } +/// How many times each pane of the window was rendered (per second). A pane +/// is an entity of its own that the window draws again only when it was +/// notified (`Entity::cached`), or when something below it was; the figures +/// say which panes a change of the state cost. +#[derive(Clone, Default, Debug)] +pub struct Panes { + /// The server rail. + pub rail: Counter, + /// The top bar. + pub topbar: Counter, + /// The sidebar (the server card, the voice and user cards; the tree is + /// inside it). + pub sidebar: Counter, + /// The channel tree. + pub tree: Counter, + /// The main area. + pub main: Counter, + /// The members panel. + pub members: Counter, +} + +impl Panes { + /// The counters with the names the meter reports them under. + pub fn named(&self) -> [(&'static str, Counter); 6] { + [ + ("rail", self.rail.clone()), + ("topbar", self.topbar.clone()), + ("sidebar", self.sidebar.clone()), + ("tree", self.tree.clone()), + ("main", self.main.clone()), + ("members", self.members.clone()), + ] + } +} + /// The app's own numbers the meter reports besides the process's. #[derive(Clone, Default, Debug)] pub struct Counters { @@ -50,6 +85,8 @@ pub struct Counters { pub ticks: Counter, /// Windows drawn (frames, per second). pub renders: Counter, + /// Renders of each pane of the window (per second). + pub panes: Panes, /// Jobs other threads handed to the foreground through /// [`crate::app::Later`] (per second). pub later_jobs: Counter, @@ -194,12 +231,15 @@ pub fn start(counters: Counters) { tracing::warn!(target: "voelin_gpui::meter", "the meter reads /proc/self; there is none here"); return; }; - let rated = [ + let rated: Vec<(&str, Counter)> = [ ("engine_events", counters.engine_events.clone()), ("ticks", counters.ticks.clone()), ("renders", counters.renders.clone()), - ("later_jobs", counters.later_jobs.clone()), - ]; + ] + .into_iter() + .chain(counters.panes.named()) + .chain([("later_jobs", counters.later_jobs.clone())]) + .collect(); let mut last_counts: Vec = rated.iter().map(|(_, c)| c.get()).collect(); tracing::info!(target: "voelin_gpui::meter", "meter on: one line a second (VOELIN_METER)"); loop { diff --git a/crates/voelin-gpui/src/shell/main_panel.rs b/crates/voelin-gpui/src/shell/main_panel.rs index 7c1c15a..4d90ed1 100644 --- a/crates/voelin-gpui/src/shell/main_panel.rs +++ b/crates/voelin-gpui/src/shell/main_panel.rs @@ -16,6 +16,8 @@ use voelin_ui_vm::servers::logic; use super::actions::{AddServer, ConnectVoice, DisconnectVoice, EditServer}; use super::widgets::{Avatar, Presence, icon, panel, tagged}; use crate::app::AppState; +use crate::meter::Counters; +use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// What the main area shows. @@ -51,21 +53,25 @@ fn state_words(status: &str) -> &'static str { /// The main area. pub struct MainPanel { state: MainState, - _subscription: Subscription, + counters: Counters, + _subscriptions: Vec, } impl MainPanel { - /// A main area that follows `app`. + /// A main area that follows the server on screen in `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { - let state = MainState::of(app.read(cx)); - let subscription = cx.observe(app, |this: &mut Self, app, cx| { + let (state, counters, signals) = { + let shown = app.read(cx); + (MainState::of(shown), shown.counters().clone(), shown.signals().clone()) + }; + let subscriptions = signals.watch(app, &[Area::Servers], cx, |this: &mut Self, app, cx| { let state = MainState::of(app.read(cx)); if state != this.state { this.state = state; cx.notify(); } }); - Self { state, _subscription: subscription } + Self { state, counters, _subscriptions: subscriptions } } /// What it shows now. @@ -76,6 +82,7 @@ impl MainPanel { impl Render for MainPanel { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.counters.panes.main.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); let body = match &self.state { diff --git a/crates/voelin-gpui/src/shell/mod.rs b/crates/voelin-gpui/src/shell/mod.rs index 541a092..f389cf9 100644 --- a/crates/voelin-gpui/src/shell/mod.rs +++ b/crates/voelin-gpui/src/shell/mod.rs @@ -1,10 +1,12 @@ //! 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 -//! that observes the root state ([`crate::app::AppState`]) and tells the window -//! only when what it shows changed (the window then draws every part), and the -//! actions the user's moves are ([`actions`]). [`crate::view::MainView`] puts -//! them in the window. +//! 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. pub mod actions; mod form; diff --git a/crates/voelin-gpui/src/shell/rail.rs b/crates/voelin-gpui/src/shell/rail.rs index 12eaf40..ffaa807 100644 --- a/crates/voelin-gpui/src/shell/rail.rs +++ b/crates/voelin-gpui/src/shell/rail.rs @@ -1,7 +1,8 @@ //! The server rail: the saved servers as avatars with their state, the //! selection, the add-server button and the settings button (`ServerRail` of -//! `ui/shells/desktop.slint`). An entity of its own: it observes the root -//! state and tells the window only when the servers it lists changed. +//! `ui/shells/desktop.slint`). An entity of its own, drawn as a cached pane: +//! it listens to the servers' signal ([`crate::signals`]) and tells the window +//! only when the servers it lists changed. use gpui_kit::prelude::FluentBuilder as _; use gpui_kit::{ @@ -14,6 +15,8 @@ use voelin_ui_vm::servers::logic; use super::actions::{AddServer, EditServer, OpenSettings, SelectServer}; use super::widgets::{Avatar, IconButton, Presence}; use crate::app::AppState; +use crate::meter::Counters; +use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// What the rail shows: the servers in order, and which is on screen. @@ -38,21 +41,25 @@ impl RailView { /// The rail. pub struct ServerRail { view: RailView, - _subscription: Subscription, + counters: Counters, + _subscriptions: Vec, } impl ServerRail { - /// A rail that follows `app`. + /// A rail that follows the servers of `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { - let view = RailView::of(app.read(cx)); - let subscription = cx.observe(app, |this: &mut Self, app, cx| { + let (view, counters, signals) = { + let state = app.read(cx); + (RailView::of(state), state.counters().clone(), state.signals().clone()) + }; + let subscriptions = signals.watch(app, &[Area::Servers], cx, |this: &mut Self, app, cx| { let view = RailView::of(app.read(cx)); if view != this.view { this.view = view; cx.notify(); } }); - Self { view, _subscription: subscription } + Self { view, counters, _subscriptions: subscriptions } } /// What it shows now. @@ -160,6 +167,7 @@ fn server(theme: &VoelinTheme, item: &ServerItem, selected: bool) -> impl IntoEl impl Render for ServerRail { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.counters.panes.rail.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); let current = self.view.current; diff --git a/crates/voelin-gpui/src/shell/sidebar.rs b/crates/voelin-gpui/src/shell/sidebar.rs index 73587f9..7e1cba7 100644 --- a/crates/voelin-gpui/src/shell/sidebar.rs +++ b/crates/voelin-gpui/src/shell/sidebar.rs @@ -21,6 +21,8 @@ use super::actions::{ }; use super::widgets::{Avatar, IconButton, Presence, card, icon, panel}; use crate::app::AppState; +use crate::meter::Counters; +use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// What the sidebar shows. @@ -83,21 +85,27 @@ pub struct Sidebar { view: SidebarView, /// The hold-to-talk button is held down. holding: bool, - _subscription: Subscription, + counters: Counters, + _subscriptions: Vec, } impl Sidebar { - /// A sidebar that follows `app`. + /// A sidebar that follows the server on screen and the voice state in + /// `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { - let view = SidebarView::of(app.read(cx)); - let subscription = cx.observe(app, |this: &mut Self, app, cx| { - let view = SidebarView::of(app.read(cx)); - if view != this.view { - this.view = view; - cx.notify(); - } - }); - Self { view, holding: false, _subscription: subscription } + let (view, counters, signals) = { + let state = app.read(cx); + (SidebarView::of(state), state.counters().clone(), state.signals().clone()) + }; + let subscriptions = + signals.watch(app, &[Area::Servers, Area::Toolbar], cx, |this: &mut Self, app, cx| { + let view = SidebarView::of(app.read(cx)); + if view != this.view { + this.view = view; + cx.notify(); + } + }); + Self { view, holding: false, counters, _subscriptions: subscriptions } } /// What it shows now. @@ -400,6 +408,7 @@ fn name_initials(name: &str) -> String { impl Render for Sidebar { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.counters.panes.sidebar.incr(); let theme = VoelinTheme::global(cx).clone(); let m = &theme.metrics; let view = &self.view; diff --git a/crates/voelin-gpui/src/shell/topbar.rs b/crates/voelin-gpui/src/shell/topbar.rs index 6a15ed5..11c6ac7 100644 --- a/crates/voelin-gpui/src/shell/topbar.rs +++ b/crates/voelin-gpui/src/shell/topbar.rs @@ -11,6 +11,8 @@ use gpui_kit::{ use super::actions::{OpenSearch, OpenSettings}; use super::widgets::{IconButton, icon}; use crate::app::AppState; +use crate::meter::Counters; +use crate::signals::Area; use crate::theme::{VoelinTheme, hsla}; /// The page title: the server on screen, else the app's name. @@ -24,21 +26,25 @@ pub fn title(app: &AppState) -> String { /// The top bar. pub struct TopBar { title: String, - _subscription: Subscription, + counters: Counters, + _subscriptions: Vec, } impl TopBar { - /// A top bar that follows `app`. + /// A top bar that follows the server on screen in `app`. pub fn new(app: &Entity, cx: &mut Context) -> Self { - let shown = title(app.read(cx)); - let subscription = cx.observe(app, |this: &mut Self, app, cx| { + let (shown, counters, signals) = { + let state = app.read(cx); + (title(state), state.counters().clone(), state.signals().clone()) + }; + let subscriptions = signals.watch(app, &[Area::Servers], cx, |this: &mut Self, app, cx| { let shown = title(app.read(cx)); if shown != this.title { this.title = shown; cx.notify(); } }); - Self { title: shown, _subscription: subscription } + Self { title: shown, counters, _subscriptions: subscriptions } } /// The title it shows now. @@ -49,6 +55,7 @@ impl TopBar { impl Render for TopBar { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.counters.panes.topbar.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); div() diff --git a/crates/voelin-gpui/src/signals.rs b/crates/voelin-gpui/src/signals.rs new file mode 100644 index 0000000..370d151 --- /dev/null +++ b/crates/voelin-gpui/src/signals.rs @@ -0,0 +1,150 @@ +//! Per-area signals: `Update.refresh` of the transitions +//! ([`voelin_ui_vm::host::Refresh`]) turned into what the window's parts +//! listen to. +//! +//! The Slint front end draws again what a transition's `refresh` flags name. +//! Here a part of the window is an entity, and gpui-pre 0.3.10 draws a part +//! again when its entity is notified, so each area has an entity of its own, +//! a [`Signal`] with a generation, that the root state raises for the flags +//! of an update ([`Signals::raise`]) and that the parts observe +//! ([`Signals::watch`]): a change of the channel tree notifies the tree's +//! listeners, and the rail, the top bar and the main area are not asked +//! whether anything changed for them. A part still compares what it would show +//! with what it showed before it tells the window, for the many events that +//! set a flag and change nothing it shows. +//! +//! Only the flags of areas this front end has parts for are signals; the +//! others (`chat`, `streams`, ...) are dropped until their slices come, as +//! `perform` dropped all of them before. + +use gpui_kit::{App, AppContext as _, Context, Entity, Subscription}; +use voelin_ui_vm::host::Refresh; + +use crate::app::AppState; + +/// An area of the window that a change of the state can concern. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Area { + /// The servers: the rail, the top bar's title, the main area, the server + /// card (`Refresh::servers`). + Servers, + /// The voice and user cards of the sidebar (`Refresh::toolbar`). + Toolbar, + /// The channel tree and the members panel (`Refresh::tree`). + Tree, + /// The card over a member row (`Refresh::member_card`). + MemberCard, +} + +impl Area { + /// Every area. + pub const ALL: [Area; 4] = [Area::Servers, Area::Toolbar, Area::Tree, Area::MemberCard]; +} + +/// The entity of an area: it has nothing but a count of how many times it was +/// raised, so that what observes it is told. +#[derive(Debug, Default)] +pub struct Signal { + generation: u64, +} + +impl Signal { + /// How many times it was raised. + pub fn generation(&self) -> u64 { + self.generation + } +} + +/// The signals of the areas, owned by the root state. +#[derive(Clone, Debug)] +pub struct Signals { + servers: Entity, + toolbar: Entity, + tree: Entity, + member_card: Entity, +} + +impl Signals { + /// One signal per area, made in the entity `cx` belongs to. + pub fn new(cx: &mut Context) -> Self { + Self { + servers: cx.new(|_| Signal::default()), + toolbar: cx.new(|_| Signal::default()), + tree: cx.new(|_| Signal::default()), + member_card: cx.new(|_| Signal::default()), + } + } + + /// The signal of an area. + pub fn of(&self, area: Area) -> &Entity { + match area { + Area::Servers => &self.servers, + Area::Toolbar => &self.toolbar, + Area::Tree => &self.tree, + Area::MemberCard => &self.member_card, + } + } + + /// The areas an update's flags name. + pub fn areas(refresh: &Refresh) -> Vec { + [ + (refresh.servers, Area::Servers), + (refresh.toolbar, Area::Toolbar), + (refresh.tree, Area::Tree), + (refresh.member_card, Area::MemberCard), + ] + .into_iter() + .filter_map(|(flagged, area)| flagged.then_some(area)) + .collect() + } + + /// Raise one area: whatever observes it is told. + pub fn raise_area(&self, area: Area, cx: &mut App) { + self.of(area).update(cx, |signal, cx| { + signal.generation += 1; + cx.notify(); + }); + } + + /// Raise the areas `refresh` names, each once however many flags a batch + /// of events merged into it. + pub fn raise(&self, refresh: &Refresh, cx: &mut App) { + for area in Self::areas(refresh) { + self.raise_area(area, cx); + } + } + + /// Run `on` whenever one of `areas` is raised, with the root state; the + /// subscriptions end with the part that holds them. `on` reads the state + /// outside of the part's render: a part that read the root entity while it + /// renders would be drawn again with every change of the root, which is + /// what the signals are for not doing. + pub fn watch( + &self, + app: &Entity, + areas: &[Area], + cx: &mut Context, + on: impl Fn(&mut T, &Entity, &mut Context) + Clone + 'static, + ) -> Vec { + areas + .iter() + .map(|&area| { + let (app, on) = (app.clone(), on.clone()); + cx.observe(self.of(area), move |this, _, cx| on(this, &app, cx)) + }) + .collect() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn the_flags_of_an_update_name_the_areas_this_front_end_has() { + assert!(Signals::areas(&Refresh::default()).is_empty()); + let refresh = Refresh { tree: true, servers: true, chat: true, ..Default::default() }; + assert_eq!(Signals::areas(&refresh), [Area::Servers, Area::Tree], "chat has no part yet"); + assert_eq!(Signals::areas(&Refresh::ALL), Area::ALL); + } +} diff --git a/crates/voelin-gpui/src/view.rs b/crates/voelin-gpui/src/view.rs index a5d97ab..463dae1 100644 --- a/crates/voelin-gpui/src/view.rs +++ b/crates/voelin-gpui/src/view.rs @@ -7,7 +7,8 @@ use gpui_kit::{ App, AppContext as _, Context, Entity, FocusHandle, InteractiveElement as _, IntoElement, - ParentElement as _, Render, Styled as _, Subscription, Window, WindowBounds, div, px, + ParentElement as _, Render, StyleRefinement, Styled as _, Subscription, Window, WindowBounds, + div, px, }; use voelin_ui_vm::settings::WindowState; @@ -262,10 +263,12 @@ fn report(app: &Entity, window: &Window, cx: &mut App) { impl Render for MainView { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { - // One render of the window is one frame. A part that has something new - // tells the window, and the window then renders every part (gpui-pre - // renders each uncached child view on every frame); a window nobody - // tells is not drawn. + // One render of the window is one frame. The panes that have a size of + // their own (the rail, the top bar, the main area) are embedded cached: + // gpui-pre 0.3.10 renders them again only when their entity was + // notified, so a change in one of them is not paid for by the others. + // The sidebar and the toast are rendered with every frame. A window + // nobody tells is not drawn. self.counters.renders.incr(); let theme = VoelinTheme::global(cx).clone(); let (c, m) = (&theme.colors, &theme.metrics); @@ -280,7 +283,11 @@ impl Render for MainView { } let main = match &self.form { Some(form) => form.clone().into_any_element(), - None => self.main.clone().into_any_element(), + None => self + .main + .clone() + .cached(StyleRefinement::default().flex_1().min_w_0().h_full()) + .into_any_element(), }; frame .key_context("Shell") @@ -303,7 +310,11 @@ impl Render for MainView { .on_action(cx.listener(Self::delete_server)) .on_action(cx.listener(Self::open_search)) .on_action(cx.listener(Self::open_settings)) - .child(self.rail.clone()) + .child( + self.rail + .clone() + .cached(StyleRefinement::default().w(px(m.rail_width)).h_full().flex_none()), + ) .child( div() .flex_1() @@ -311,7 +322,9 @@ impl Render for MainView { .h_full() .flex() .flex_col() - .child(self.topbar.clone()) + .child(self.topbar.clone().cached( + StyleRefinement::default().w_full().h(px(m.topbar_height)).flex_none(), + )) .child( div() .flex_1() @@ -653,6 +666,41 @@ mod tests { assert!(parts_told.get() >= 2, "the rail and the sidebar at least: {}", parts_told.get()); } + /// The panes that were rendered: a pane drawn cached renders again only + /// when its entity was told something changed, so what changes the voice + /// card costs the sidebar (rendered with every frame) and not the rail, the + /// top bar or the main area. + #[gpui::test] + fn a_change_of_the_voice_card_renders_the_sidebar_and_no_cached_pane(cx: &mut TestAppContext) { + let (shell, cx) = + open(cx, backend_with(&[("Nightfall", "ts.example")]), Switches::default()); + let id = ids(&shell, cx)[0]; + shell.events.send(connected(id)).unwrap(); + frame(cx); + let panes = &shell.counters.panes; + let seen = || { + [&shell.counters.renders, &panes.rail, &panes.topbar, &panes.sidebar, &panes.main] + .map(|pane| pane.get()) + }; + let before = seen(); + assert!(before.iter().all(|&n| n >= 1), "every pane has been drawn: {before:?}"); + + // Our microphone muted: the voice card changes, nothing else does. + let state = + SessionState { voice: VoiceState::Connected, input_muted: true, ..Default::default() }; + shell.events.send(Event::State { session: id as u64, state }).unwrap(); + frame(cx); + let after = seen(); + let frames = after[0] - before[0]; + assert!(frames >= 1, "the window was drawn"); + assert_eq!(after[3] - before[3], frames, "the sidebar with every frame"); + assert_eq!( + [after[1], after[2], after[4]], + [before[1], before[2], before[4]], + "the rail, the top bar and the main area stay as they were" + ); + } + /// The plus of the rail opens the form; Save with an address adds the /// server, selects it and closes the form. #[gpui::test] diff --git a/crates/voelin-ui-vm/src/host.rs b/crates/voelin-ui-vm/src/host.rs index a3822a8..b950918 100644 --- a/crates/voelin-ui-vm/src/host.rs +++ b/crates/voelin-ui-vm/src/host.rs @@ -119,6 +119,56 @@ impl Refresh { pub fn is_empty(&self) -> bool { *self == Self::default() } + + /// Every part either of the two names: what a front end that handles a + /// batch of events draws once, however many events set a part. + pub fn merge(&mut self, other: Self) { + let Self { + servers, + toolbar, + tree, + chat, + streams, + viewer, + member_card, + home, + events, + gateways, + people, + conversations, + dm, + } = other; + self.servers |= servers; + self.toolbar |= toolbar; + self.tree |= tree; + self.chat |= chat; + self.streams |= streams; + self.viewer |= viewer; + self.member_card |= member_card; + self.home |= home; + self.events |= events; + self.gateways |= gateways; + self.people |= people; + self.conversations |= conversations; + self.dm |= dm; + } +} + +#[cfg(test)] +mod refresh_tests { + use super::Refresh; + + #[test] + fn merging_names_the_parts_of_both() { + let mut a = Refresh { tree: true, chat: true, ..Default::default() }; + a.merge(Refresh { toolbar: true, tree: true, ..Default::default() }); + assert_eq!(a, Refresh { toolbar: true, tree: true, chat: true, ..Default::default() }); + let mut none = Refresh::default(); + none.merge(Refresh::default()); + assert!(none.is_empty()); + none.merge(Refresh::ALL); + assert_eq!(none, Refresh::ALL); + } } /// What a transition wants kept.