ui-patterns: a preset browser with host closures replays under the host's version

A browser given a tip painter, row menu rows, drop rules or drag payloads
keyed its presets view as changing in every pass, because no key can follow
what a closure reads. The Swatches panel passes all of these, so its list
ran in every frame.

PresetBrowser::host_version and SwatchesPanel::host_version take a version
for everything those closures read. While it holds and nothing reaches the
presets, the view replays its last run; a new version runs it once. Without
a version the view still runs in every pass.
This commit is contained in:
dotCooCoo committed 2026-11-29 11:23:36 -08:00
1 parent 491736657c
commit 16f8962801
3 files changed
+100 -8

No files matched your search

+14
View File
@@ -101,6 +101,7 @@ pub struct SwatchesPanel<'a> {
report_invalid_renames: bool,
payloads: Vec<PayloadAdder<'a>>,
row_items: Option<RowItemsFn<'a>>,
host_version: Option<u64>,
document: Option<IdSalt>,
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);
}
@@ -361,6 +361,8 @@ pub struct PresetBrowser<'a, K: Clone + Eq + Hash + Send + Sync + MemoProps + 's
drops: Vec<PresetDrop<'a, K>>,
/// The host's payloads a preset drag carries beside its `ListDrag<K>`.
payloads: Vec<DragPayload>,
/// The version of what the host's closures read ([`host_version`](Self::host_version)).
host_version: Option<u64>,
/// 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<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> PresetBrowser<'_,
tip: tip_painter,
drops,
payloads,
host_version,
search_first,
footer,
max_rows,
@@ -857,11 +870,12 @@ impl<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static> 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<HostFns<'p, 'a, K>>,
host: Option<Host<'p, 'a, K>>,
empty: Option<Label>,
sent: Outbox<'p, ListRun<K>>,
}
@@ -1007,7 +1041,7 @@ fn list_view<K: Clone + Eq + Hash + Send + Sync + MemoProps + 'static>(props: &m
let label = label.clone();
let actions = props.actions;
let (tip_painter, row_items, drops, payloads) =
props.host.as_ref().map_or((None, None, &[][..], &[][..]), |host| (host.tip, host.row_items, host.drops, host.payloads));
props.host.as_ref().map(Host::fns).map_or((None, None, &[][..], &[][..]), |host| (host.tip, host.row_items, host.drops, host.payloads));
let reveal = props.reveal.clone();
let mut held = props.selection.clone();
let mut menu_target = props.menu_target.clone();
+45 -1
View File
@@ -7,7 +7,7 @@ use craft_ui::runtime::drag;
use craft_ui::testing::focus::AccessActionKind;
use craft_ui::testing::{Fixture, trial};
use egui::{Event, Id, Key, Modifiers, PointerButton, Pos2, Rect, Sense, UiBuilder, pos2, vec2};
use std::cell::RefCell;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
const GROUP: u32 = 100;
@@ -490,6 +490,50 @@ fn a_pointer_move_beside_the_browser_replays_it() {
}
}
/// A browser with the host's closures (row menu rows, a drop rule and a drag payload) keys its presets view by the
/// version the host gives for them: at rest the view replays in every pass, a new version runs it once, and the frame
/// after replays again. Without a version the view runs in every pass.
#[trial]
fn a_browser_with_host_closures_replays_while_their_version_holds() {
use craft_ui::contract::Entry;
use craft_ui::testing::{MemoRun, last_frame_memo_runs};
for versioned in [true, false] {
let rows = PresetSource::new(
(),
PresetFns::new(|_, _| vec![PresetRow::group(GROUP, "Group", 0), PresetRow::item(ALPHA, "Alpha", 1), PresetRow::item(2, "Beta", 1)]),
);
let mut state = PresetState::<u32>::new();
let version = Rc::new(Cell::new(1_u64));
let shown = Rc::clone(&version);
let mut fixture = Fixture::new(move |ui| {
ui.scope_builder(UiBuilder::new().max_rect(BROWSER), |ui| {
let browser = PresetBrowser::new("Patterns", Bind::bound(&mut state), &rows)
.view_lock(PresetView::List)
.row_items(|_| vec![Entry::new(String::from("options"), "Options…")])
.takes_drop(PresetDrop::new::<u8>(DropPlace::Onto, |_, _| true))
.drag_payload(|_: &ListDrag<u32>| Some(0_u8));
let browser = if versioned { browser.host_version(shown.get()) } else { browser };
let _ = browser.show(ui);
});
});
fixture.room(vec2(480.0, 320.0)).settle().idle(2);
let ran = |fixture: &Fixture| {
last_frame_memo_runs(fixture.ctx()).iter().filter(|(name, _, run)| name.contains("ListProps") && *run == MemoRun::Ran).count()
};
fixture.frame(Vec::new());
if !versioned {
assert!(ran(&fixture) > 0, "without a version the presets view runs in an idle pass");
continue;
}
assert_eq!(ran(&fixture), 0, "the presets view replays while the host's version holds");
version.set(2);
fixture.frame(Vec::new());
assert!(ran(&fixture) > 0, "a new version runs the presets view");
fixture.idle(2).frame(Vec::new());
assert_eq!(ran(&fixture), 0, "and the view replays again under the new version");
}
}
/// A preset dropped in the left half of another tile of the grid lands before that tile, in a grid with group headers
/// as in one without: the reorder's place counts the rows as the host's rows are.
#[trial]