diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index b3659ec000..e52c88d6d2 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -39,6 +39,9 @@ pub struct Lazy { key: u64, estimate: f32, reveal: bool, + /// Out of view in a scroll area it takes its remembered extent in place of running; a memoized view shown out of + /// view ([`MemoView::shown_out_of_view`](super::MemoView::shown_out_of_view)) does not. + pub(super) culls: bool, } impl Lazy { @@ -48,7 +51,7 @@ impl Lazy { use std::hash::Hasher; let mut hasher = std::collections::hash_map::DefaultHasher::new(); key.hash(&mut hasher); - Lazy { key: hasher.finish(), estimate, reveal: false } + Lazy { key: hasher.finish(), estimate, reveal: false, culls: true } } /// When `reveal` holds, the scroll area glides until the child starts at the view's start (as far as the @@ -115,7 +118,7 @@ impl Lazy { let owner = view.map(|view| view.owner); // A run around this one that is replayed later keeps the child's entry. replay::journal(&ctx, || (owner, id), |ctx, &(owner, id)| lazy::keep(ctx, owner, id)); - if let Some(view) = view { + if let Some(view) = view.filter(|_| self.culls) { // A run around this child decides it by the scroll view: it replays only while the view is the same. replay::note_input(&ctx, crate::runtime::inputs::Input::Scroll); let at = |size: Vec2| Rect::from_min_size(at_start(available, rtl, size), size); diff --git a/crates/ui-contract/src/contract/memo_view.rs b/crates/ui-contract/src/contract/memo_view.rs index 5838a2cddf..886bb5c688 100644 --- a/crates/ui-contract/src/contract/memo_view.rs +++ b/crates/ui-contract/src/contract/memo_view.rs @@ -123,6 +123,14 @@ impl MemoVie MemoView { lazy: Lazy::new(id, 0.0), props, show } } + /// Shows the view in a scroll area as outside one: out of view it still runs or replays, its focus stops and + /// accessibility nodes stay registered, and `show` returns a value. A toolbar that scrolls shows each button's view + /// this way. + pub fn shown_out_of_view(mut self) -> Self { + self.lazy.culls = false; + self + } + /// Runs the view in a child `Ui` placed in flow in `ui`, or adds its last run again. Returns what the run returned, /// the kept value when it replayed; `None` only when it is out of view in a scroll area. A measuring run that /// replays returns the kept real run's value, laid out in that run's room and flow: code that measures sizes from diff --git a/crates/ui-panels/src/rails/tool.rs b/crates/ui-panels/src/rails/tool.rs index 134873e659..97798cbb1c 100644 --- a/crates/ui-panels/src/rails/tool.rs +++ b/crates/ui-panels/src/rails/tool.rs @@ -95,7 +95,8 @@ pub struct ToolRailOutput { /// The Edit Toolbar button's response while the rail shows it: a click on it opens the drawer, and the drawer /// takes it as its opener ([`ToolbarDrawer::opened_by`](crate::rails::ToolbarDrawer::opened_by)). pub edit_toolbar: Option, - /// The slot whose flyout is open. + /// The slot whose flyout is open, in a pass where that slot's button ran: a slot whose view replays reports + /// nothing. pub flyout: Option, /// A tool dropped on the rail while the toolbar is edited, and where it goes. pub dropped: Option, @@ -430,10 +431,6 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut let tips = tips.map(|tips| tips.tips); let visible = visible_slots(slots, hidden_in(hidden)); let cells = visible.len() + usize::from(props.edit_label.is_some()); - // Each visible tool's command, resolved once for the pass: the buttons show these and read none again. - let resolve = resolver_in(ui); - let commands: Vec>>> = - visible.iter().map(|(_, tools)| tools.iter().map(|tool| resolve(tool.command.clone())).collect()).collect(); let at: Vec = visible.iter().map(|(index, _)| *index).collect(); // The first caption among a group's slots heads the group. let caption_of = |group: &[usize]| { @@ -458,63 +455,34 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut match visible.get(k) { Some((index, tools)) => { let slot = &slots[*index]; - let states = commands.get(k).map_or(&[][..], Vec::as_slice); - let on = |at: usize| states.get(at).and_then(Option::as_ref).is_some_and(|resolved| resolved.enabled); - let enabled = |tool: &RailTool| tools.iter().position(|own| std::ptr::eq(*own, tool)).is_some_and(on); - // The slot's own tools, unless the host hides some of them. - let owned: Cow<'_, [RailTool]> = if tools.len() == slot.tools.len() { - Cow::Borrowed(slot.tools.as_slice()) - } else { - Cow::Owned(tools.iter().map(|tool| (*tool).clone()).collect()) + let row_of = |tool: Option<&T>| tool.and_then(|tool| tools.iter().position(|own| own.id == *tool)); + let tips = tips.map(|tips| HostTips::new(tips, std::slice::from_ref(slot))); + let mut events = Vec::new(); + let props = SlotProps { + slots, + index: *index, + current, + last_used, + hidden, + editing, + opens, + columns, + tear_off, + tips: tips.as_ref(), + marked: row_of(mark), + reveal: row_of(reveal), + sent: Outbox::new(&mut events), }; - let Some(shown) = shown_tool(current, slot.id, tools, last_used, enabled) else { continue }; - let selected = tools.iter().any(|tool| tool.id == *current); - let mut button = FlyoutButton::new(&owned, &shown.id) - .id_salt(("slot", slot.id)) - .selected(selected) - .enabled((0..tools.len()).any(on)) - .resolved(states) - .opens(opens); - if editing { - button = button.edit(SlotEdit::new(slot.id, *index, slot.tools.len(), columns == 1)); + // Each slot is a view of its own: a press held on one button, or its open + // flyout, runs that slot's view while the others replay. A button scrolled out + // of the rail's view stays a focus stop and an accessibility node. + let view = MemoView::new(("slot", slot.id), props, slot_button::).shown_out_of_view(); + let shown = view.show(ui).flatten(); + if let Some(rect) = shown { + shown_at.push((*index, rect)); } - if tear_off { - button = button.tear_off(); - } - if let Some(tips) = tips { - button = button.tips(tips); - } - let row_of = |tool: Option<&T>| tool.and_then(|tool| owned.iter().position(|own| own.id == *tool)); - let marked = row_of(mark); - if let Some(at) = marked { - button = button.marked_row(at); - } - if let Some(at) = row_of(reveal) { - button = button.reveal_row(at); - } - let out = match tips.filter(|tips| tips.has(&shown.id)) { - Some(tips) => button.show_with_tip(ui, |ui| tips.show(&shown.id, ui)), - None => button.show(ui), - }; - shown_at.push((*index, out.response.rect)); - // The slot wears the mark; while its flyout is open the tool's row does. - if marked.is_some() && !out.opened { - TargetMark::new(out.response.rect).id_salt(("mark", slot.id)).show_mark(ui); - } - if let Some(pointer) = out.torn_off { - sent.send(RailEvent::TornOff(TornOff::new(vec![slot.id], slot.id, pointer))); - } - if let Some(tool) = out.select { - sent.send(RailEvent::Select(tool)); - } - if out.double_clicked { - sent.send(RailEvent::DoubleClicked(shown.id.clone())); - } - if out.opened { - sent.send(RailEvent::Flyout(slot.id)); - } - if let Some(dropped) = out.dropped { - sent.send(RailEvent::Dropped(dropped)); + for event in events { + sent.send(event); } } None => { @@ -535,6 +503,94 @@ fn grid(props: &mut GridProps<'_, T>, ui: &mut (edit_toolbar, shown_at) } +/// What one slot's memoized view reads: the slot (by its index among the host's slots), the tool state its button +/// shows, the rows of its tools that wear the mark or are revealed, and an outbox for what the button reports. +#[derive(MemoProps)] +struct SlotProps<'a, T: PlainData> { + slots: &'a Tracked>>, + index: usize, + current: &'a T, + last_used: &'a Tracked>, + hidden: Option<&'a Tracked>>, + editing: bool, + opens: Side, + columns: usize, + tear_off: bool, + tips: Option<&'a HostTips<'a, T>>, + marked: Option, + reveal: Option, + sent: Outbox<'a, RailEvent>, +} + +/// One slot's button, with the mark beside it while the slot wears it. The result is the button's rect, `None` for a +/// slot whose tools are all hidden. +fn slot_button(props: &mut SlotProps<'_, T>, ui: &mut Ui) -> Option { + let SlotProps { slots, index, current, last_used, hidden, editing, opens, columns, tear_off, tips, marked, reveal, .. } = *props; + let tips = tips.map(|tips| tips.tips); + let slot = slots.get(index)?; + let hidden = hidden_in(hidden); + let tools: Vec<&RailTool> = slot.tools.iter().filter(|tool| !hidden.contains(&tool.id)).collect(); + // The slot's tool commands, resolved once for the run: the button shows these and reads none again. + let resolve = resolver_in(ui); + let states: Vec>> = tools.iter().map(|tool| resolve(tool.command.clone())).collect(); + let on = |at: usize| states.get(at).and_then(Option::as_ref).is_some_and(|resolved| resolved.enabled); + let enabled = |tool: &RailTool| tools.iter().position(|own| std::ptr::eq(*own, tool)).is_some_and(on); + // The slot's own tools, unless the host hides some of them. + let owned: Cow<'_, [RailTool]> = if tools.len() == slot.tools.len() { + Cow::Borrowed(slot.tools.as_slice()) + } else { + Cow::Owned(tools.iter().map(|tool| (*tool).clone()).collect()) + }; + let shown = shown_tool(current, slot.id, &tools, last_used, enabled)?; + let selected = tools.iter().any(|tool| tool.id == *current); + let mut button = FlyoutButton::new(&owned, &shown.id) + .id_salt(("slot", slot.id)) + .selected(selected) + .enabled((0..tools.len()).any(on)) + .resolved(&states) + .opens(opens); + if editing { + button = button.edit(SlotEdit::new(slot.id, index, slot.tools.len(), columns == 1)); + } + if tear_off { + button = button.tear_off(); + } + if let Some(tips) = tips { + button = button.tips(tips); + } + if let Some(at) = marked { + button = button.marked_row(at); + } + if let Some(at) = reveal { + button = button.reveal_row(at); + } + let out = match tips.filter(|tips| tips.has(&shown.id)) { + Some(tips) => button.show_with_tip(ui, |ui| tips.show(&shown.id, ui)), + None => button.show(ui), + }; + // The slot wears the mark; while its flyout is open the tool's row does. + if marked.is_some() && !out.opened { + TargetMark::new(out.response.rect).id_salt(("mark", slot.id)).show_mark(ui); + } + let sent = &mut props.sent; + if let Some(pointer) = out.torn_off { + sent.send(RailEvent::TornOff(TornOff::new(vec![slot.id], slot.id, pointer))); + } + if let Some(tool) = out.select { + sent.send(RailEvent::Select(tool)); + } + if out.double_clicked { + sent.send(RailEvent::DoubleClicked(shown.id.clone())); + } + if out.opened { + sent.send(RailEvent::Flyout(slot.id)); + } + if let Some(dropped) = out.dropped { + sent.send(RailEvent::Dropped(dropped)); + } + Some(out.response.rect) +} + impl<'a, T: Value + MemoProps + PlainData + 'static> ToolRail<'a, T> { /// Shows the rail in the width `ui` has left. `footer` draws under the last slot (colour chips, Quick Mask, /// Screen Mode); it stays in view when the room is short, unless it scrolls with the slots diff --git a/crates/ui-panels/tests/it/rails/work.rs b/crates/ui-panels/tests/it/rails/work.rs index 8ca2ed9106..6db4d87cb5 100644 --- a/crates/ui-panels/tests/it/rails/work.rs +++ b/crates/ui-panels/tests/it/rails/work.rs @@ -1,13 +1,13 @@ -//! The work a rail does in a pass: its grid replays while the pointer is away, tips included, and a grid that runs reads -//! each tool's command once. +//! The work a rail does in a pass: its grid replays while the pointer is away, tips included, a grid that runs reads +//! each tool's command once, and a press held on one slot runs only that slot's button. -use super::{CUT, REDO, UNDO, tools}; +use super::{CUT, HELD, REDO, UNDO, tools}; use craft_ui::contract::tooltip::DELAY; use craft_ui::contract::{Role, Show as _, Tracked}; use craft_ui::display::Text; use craft_ui::rails::{LastUsed, ToolRail, ToolSlot, ToolTips}; -use craft_ui::testing::pointer::moved; -use craft_ui::testing::{Fixture, MemoRun, audit_replays, last_pass_command_reads, last_pass_memo_runs, trial}; +use craft_ui::testing::pointer::{moved, press}; +use craft_ui::testing::{Fixture, MemoRun, audit_replays, last_pass_command_reads, last_pass_memo_runs, last_pass_runs, trial}; use egui::Ui; use std::cell::Cell; use std::rc::Rc; @@ -90,6 +90,44 @@ fn a_grid_that_runs_reads_each_tool_command_once() { assert!(reads <= READS, "a pass of the grid reads {reads} command states, at most {READS}"); } +/// The flyout buttons that ran in each of 3 passes while a press holds the flyout of the first slot open, in a rail of +/// that slot (Undo and Redo, with the tear-off bar) and `others` more slots of Cut and Redo. The audit checks each +/// replay of the passes against a run. +fn held_flyout_button_runs(others: u32) -> Vec { + let mut all = vec![ToolSlot::new(0, tools(&[UNDO, REDO]))]; + all.extend((1..=others).map(|id| ToolSlot::new(id, tools(&[CUT, REDO])))); + let (slots, last) = (Tracked::new(all), Tracked::new(LastUsed::default())); + let mut fixture = Fixture::new(move |ui| { + ToolRail::new("Tools", &slots, &UNDO, &last).tear_off().show_rail(ui, |_, _| {}); + }); + fixture.room(egui::Vec2::new(300.0, 600.0)).settle(); + let undo = fixture.by_role(Role::BUTTON).into_iter().next().expect("the Undo slot's button shows").center(); + fixture.frame(vec![moved(undo), press(undo)]); + fixture.advance(HELD); + fixture.frame(vec![]); + assert!(fixture.by_label("Redo").is_some(), "the held press opened the Undo slot's flyout"); + audit_replays(fixture.ctx(), true); + let runs = (0..3) + .map(|_| { + fixture.frame(vec![]); + let runs = last_pass_runs(fixture.ctx()); + runs.iter().filter(|(name, _)| name.contains("FlyoutButton")).map(|(_, runs)| *runs).sum() + }) + .collect(); + audit_replays(fixture.ctx(), false); + assert!(fixture.by_label("Redo").is_some(), "the flyout stays open while the press is held"); + runs +} + +/// While a press holds one slot's flyout open the rail runs that slot's button in every pass; the other slots' buttons +/// replay, so the work of a pass does not grow with the slots. +#[trial] +fn a_held_flyout_runs_only_its_own_slots_button() { + let alone = held_flyout_button_runs(0); + assert!(alone.iter().all(|runs| *runs > 0), "the held button runs in every pass: {alone:?}"); + assert_eq!(held_flyout_button_runs(6), alone, "the 6 other slots' buttons replay while the press is held"); +} + /// The command reads of a pass in which the grid runs: each of the 3 tools once, and those of the two buttons' faces. /// A grid whose buttons resolve each tool again reads 18. const READS: u32 = 9; diff --git a/crates/ui-widgets/tests/it/body/scroll.rs b/crates/ui-widgets/tests/it/body/scroll.rs index a21245e1d4..d773e32d98 100644 --- a/crates/ui-widgets/tests/it/body/scroll.rs +++ b/crates/ui-widgets/tests/it/body/scroll.rs @@ -391,6 +391,41 @@ fn an_overflowing_bodys_settled_memo_children_run_no_measuring_run_when_it_measu assert_eq!(ran("::Line"), 0, "no settled line ran, in a measuring run or the real run: {runs:?}"); } +/// A memoized view holding one button, `Line `. +#[derive(MemoProps)] +struct Labeled { + at: usize, +} + +fn labeled(props: &mut Labeled, ui: &mut Ui) -> usize { + Button::new(format!("Line {}", props.at)).show(ui); + props.at +} + +/// The buttons of 30 memoized views in a scrolling body the first few fill, each view shown out of view when `out` +/// holds: whether the first and the last button have accessibility nodes after the body settles. +fn labeled_nodes(out: bool) -> (bool, bool) { + let mut fixture = Fixture::new(move |ui| { + Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("labeled").show(ui, |ui, _| { + for at in 0..30 { + let view = MemoView::new(("labeled", at), Labeled { at }, labeled); + let view = if out { view.shown_out_of_view() } else { view }; + view.show(ui); + } + }); + }); + fixture.room(VIEW).settle(); + (fixture.by_label("Line 0").is_some(), fixture.by_label("Line 29").is_some()) +} + +/// A memoized view in a scrolling body takes its extent out of view without running; one shown out of view runs or +/// replays there, so its button keeps its node while the body has scrolled away from it. +#[trial] +fn a_memo_view_shown_out_of_view_keeps_its_nodes_out_of_view() { + assert_eq!(labeled_nodes(false), (true, false), "out of view, a view's button has no node"); + assert_eq!(labeled_nodes(true), (true, true), "a view shown out of view keeps its button's node"); +} + /// A vertical scroll body of `Lazy` children, child `at` as tall as `height(at)` and estimated at `estimate(at)` /// before it ran. It counts each child's runs and keeps the rects of the children the last real pass ran. struct LazyColumn {