//! 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); } }