diff --git a/crates/color-ui/src/ui/swatches/panel.rs b/crates/color-ui/src/ui/swatches/panel.rs index 69c854b2aa..b890470ab8 100644 --- a/crates/color-ui/src/ui/swatches/panel.rs +++ b/crates/color-ui/src/ui/swatches/panel.rs @@ -101,6 +101,7 @@ pub struct SwatchesPanel<'a> { report_invalid_renames: bool, payloads: Vec>, row_items: Option>, + host_version: Option, document: Option, select_groups: bool, } @@ -135,6 +136,7 @@ impl<'a> SwatchesPanel<'a> { report_invalid_renames: false, payloads: Vec::new(), row_items: None, + host_version: None, document: None, select_groups: false, } @@ -240,6 +242,14 @@ impl<'a> SwatchesPanel<'a> { self } + /// The version of everything the host's `row_items`, `takes_drop` and `drag_payload` closures read, as + /// `PresetBrowser::host_version` takes it: while it holds and nothing reaches the swatches, their view repaints its + /// last run instead of running in every pass. + pub fn host_version(mut self, version: u64) -> Self { + self.host_version = Some(version); + self + } + /// The document whose swatches the panel shows. The browser keeps its own state (an open rename field, the scroll /// position) for each document, so a rename left open in one document does not open on another document's /// swatch of the same name. Without it the browser keeps that state for the `Ui` it is shown in. @@ -274,6 +284,7 @@ impl<'a> SwatchesPanel<'a> { report_invalid_renames, payloads, row_items, + host_version, document, select_groups, } = self; @@ -316,6 +327,9 @@ impl<'a> SwatchesPanel<'a> { if let Some(id) = document_id { browser = browser.browser_id(id); } + if let Some(version) = host_version { + browser = browser.host_version(version); + } if let Some(options) = footer_options { browser = browser.footer(options); } diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index f1f73887e5..92754f95e2 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -361,6 +361,8 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 's drops: Vec>, /// The host's payloads a preset drag carries beside its `ListDrag`. payloads: Vec, + /// The version of what the host's closures read ([`host_version`](Self::host_version)). + host_version: Option, /// The search field is its scope's default stop (`Initial::Default`): a picker's popover opens with the focus in /// it, so typing filters the presets. search_first: bool, @@ -417,6 +419,7 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser tip: None, drops: Vec::new(), payloads: Vec::new(), + host_version: None, search_first: false, footer: None, max_rows: None, @@ -439,6 +442,15 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser self } + /// The version of everything the host's closures read: the tip painter (`tip`), the row menu's rows (`row_items`), + /// the drop rules (`takes_drop`) and the payload maps (`drag_payload`). The host gives a new version whenever one + /// of them would answer differently. While the version holds and nothing reaches the presets, their view repaints + /// its last run; without a version a browser with any of those closures runs its view in every pass. + pub fn host_version(mut self, version: u64) -> Self { + self.host_version = Some(version); + self + } + /// The id of the browser's list, the `ListDrag::list` of its drags: a footer action accepts only drags from its own /// browser. pub fn list_id(browser: Id) -> Id { @@ -756,6 +768,7 @@ impl PresetBrowser<'_, tip: tip_painter, drops, payloads, + host_version, search_first, footer, max_rows, @@ -857,11 +870,12 @@ impl PresetBrowser<'_, row_menu, drags, group_heads, - host: (tip_painter.is_some() || row_items.is_some() || !drops.is_empty() || !payloads.is_empty()).then(|| HostFns { - tip: tip_painter.as_ref(), - row_items: row_items.as_ref(), - drops: &drops, - payloads: &payloads, + host: (tip_painter.is_some() || row_items.is_some() || !drops.is_empty() || !payloads.is_empty()).then(|| { + let fns = HostFns { tip: tip_painter.as_ref(), row_items: row_items.as_ref(), drops: &drops, payloads: &payloads }; + match host_version { + Some(version) => Host::Versioned { version, fns }, + None => Host::EveryPass(fns), + } }), empty: empty_text.clone(), sent: Outbox::new(&mut sent), @@ -932,6 +946,26 @@ struct HostFns<'p, 'a, K> { payloads: &'p [DragPayload], } +/// The host's closures as the presets view keys them: by the version the host gives for what they read +/// ([`PresetBrowser::host_version`]), else in every pass. +#[derive(crate::contract::MemoProps)] +enum Host<'p, 'a, K> { + Versioned { + version: u64, + #[memo_props(covered_by = "version", reason = "the host's version stands for everything its closures read")] + fns: HostFns<'p, 'a, K>, + }, + EveryPass(HostFns<'p, 'a, K>), +} + +impl<'p, 'a, K> Host<'p, 'a, K> { + fn fns(&self) -> &HostFns<'p, 'a, K> { + match self { + Host::Versioned { fns, .. } | Host::EveryPass(fns) => fns, + } + } +} + /// Every input of the browser's presets view: the source, the state the toolbar left (the open groups, the search, /// the view and the zoom), the host's selection, the row menu's target, the browser's plain settings and the host's /// closures, and an outbox for what a run reports. @@ -961,7 +995,7 @@ struct ListProps<'p, 'a, K: 'static> { drags: bool, group_heads: GroupHeads, /// The host's closures; `None` without any, so the view keys on its plain props alone. - host: Option>, + host: Option>, empty: Option