Files
VoelinClient/crates/voelin-gpui/src/signals.rs
T
faumarayandClaude Sonnet 5.5 b133412700 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 <noreply@anthropic.com>
2026-10-27 23:37:37 +03:00

151 lines
4.7 KiB
Rust

//! 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<Signal>,
toolbar: Entity<Signal>,
tree: Entity<Signal>,
member_card: Entity<Signal>,
}
impl Signals {
/// One signal per area, made in the entity `cx` belongs to.
pub fn new<T: 'static>(cx: &mut Context<T>) -> 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<Signal> {
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<Area> {
[
(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<T: 'static>(
&self,
app: &Entity<AppState>,
areas: &[Area],
cx: &mut Context<T>,
on: impl Fn(&mut T, &Entity<AppState>, &mut Context<T>) + Clone + 'static,
) -> Vec<Subscription> {
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);
}
}