From 6acd584c30bbcd9e53366198cbfd2c134148905b Mon Sep 17 00:00:00 2001 From: bflatastic Date: Mon, 23 Nov 2026 05:00:09 +0700 Subject: [PATCH] ui-contract: a lead whose row leaves the source takes the host's current item without a report, so PageList reports no Go the user never made --- .../ui-contract/src/contract/items/hooks.rs | 2 +- crates/ui-contract/src/contract/items/lead.rs | 11 ++++-- crates/ui-contract/tests/it/collection/kit.rs | 6 ++++ .../ui-contract/tests/it/collection/rows.rs | 35 +++++++++++++++++++ 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 53687a19c..a8d020d4f 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -259,7 +259,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { let header_held = self.header_step(cx); self.offset_x = self.offset_x.clamp(0.0, f64::from((self.content.x - rect.width()).max(0.0))); self.open_lead(comp); - self.follow_lead_source(); + self.follow_lead_source(comp); // A lead set in place (from the selection, `open_lead`, a row that left) has not rested yet: a lead that comes // back to a row it rested on before reports the rest again. if self.lead.rested != self.lead.key { diff --git a/crates/ui-contract/src/contract/items/lead.rs b/crates/ui-contract/src/contract/items/lead.rs index 75c1e9292..461034c13 100644 --- a/crates/ui-contract/src/contract/items/lead.rs +++ b/crates/ui-contract/src/contract/items/lead.rs @@ -260,16 +260,21 @@ impl ItemsHooks<'_, '_, C> { .any(|(owner, part)| owner == item && part.is_some_and(|part| pointer::part_senses(ctx, owner, part, Sense::CLICK))) } - /// A lead whose key left the source moves to the key now in its place, else the one before it. The selection is + /// A lead whose key left the source moves to the host's current item when the source holds it, with no report: the + /// host is there already. Otherwise it moves to the key now in its place, else the one before it. The selection is /// the host's: a row that left (deleted, or hidden by a filter) is not replaced by its neighbour. The user did not /// move the lead, so the new lead does not rest at once. - pub(super) fn follow_lead_source(&mut self) { + pub(super) fn follow_lead_source(&mut self, comp: &C) { let len = self.source.len(); if let Some(key) = self.lead.key.clone() && self.index_of(&key).is_none() { self.lead.key = None; - if len > 0 { + let current = comp.items().current.as_deref().and_then(|key| key.downcast_ref::()); + if let Some((at, key)) = current.and_then(|key| self.index_of(key).map(|at| (at, key.clone()))) { + self.lead.key = Some(key); + self.lead.at = at; + } else if len > 0 { self.place_lead(self.lead.at.min(len - 1), LeadSource::Keys); } } else if let Some(at) = self.lead.key.as_ref().and_then(|key| self.index_of(key)) { diff --git a/crates/ui-contract/tests/it/collection/kit.rs b/crates/ui-contract/tests/it/collection/kit.rs index be0431990..1b84be7d8 100644 --- a/crates/ui-contract/tests/it/collection/kit.rs +++ b/crates/ui-contract/tests/it/collection/kit.rs @@ -253,6 +253,12 @@ impl<'a> List<'a> { self } + /// The host's current item. + pub fn current(mut self, key: usize) -> Self { + self.items = self.items.current(Some(key)); + self + } + pub fn tree(mut self) -> Self { self.tree = true; self diff --git a/crates/ui-contract/tests/it/collection/rows.rs b/crates/ui-contract/tests/it/collection/rows.rs index 39cf924b8..55bdf62c3 100644 --- a/crates/ui-contract/tests/it/collection/rows.rs +++ b/crates/ui-contract/tests/it/collection/rows.rs @@ -474,6 +474,41 @@ fn a_selected_lead_that_leaves_the_source_moves_the_lead_and_leaves_the_selectio assert_eq!(leads.last(), Some(&13), "the lead moves to the key now in its place"); } +/// A lead whose key leaves the source moves to the host's current item without a report: the host is there already +/// (an Undo that removes the current page, whose host has picked another). +#[trial] +fn a_lead_that_leaves_the_source_takes_the_hosts_current_item_unreported() { + let rows = RefCell::new(vec![10_usize, 11, 12, 13]); + let current = Cell::new(12); + let events = RefCell::new(Vec::new()); + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + let rows = rows.borrow(); + let source = keyed(&rows, |row| *row); + in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| { + let out = List::new(SelectMode::Single) + .arrows_move_only() + .current(current.get()) + .id_salt("list") + .show_items(ui, &source, &mut |_, _| super::kit::Row::new()); + events.borrow_mut().extend(out.events); + }); + }; + frames.idle(3, &mut show); + pointer::click(&mut frames, ORIGIN + Vec2::new(5.0, ROW_H * 2.5), &mut show); + let lead = |events: &RefCell>>| { + events.borrow().iter().filter_map(|event| if let CollectionEvent::LeadMoved { key, .. } = event { Some(*key) } else { None }).last() + }; + assert_eq!(lead(&events), Some(12), "the press made the current row the lead"); + events.borrow_mut().clear(); + rows.borrow_mut().retain(|row| *row != 12); + current.set(10); + frames.idle(2, &mut show); + assert_eq!(lead(&events), None, "the host's own move is not reported back"); + press(&mut frames, Key::ArrowDown, Modifiers::NONE, &mut show); + assert_eq!(lead(&events), Some(11), "the lead stood on the current item: Down steps to the row after it"); +} + /// 17.3, I1: the lead row above the view grows (an auto row it measures taller): the rows in view stay where they /// were, in the frame it grows, and the frame after runs no extra pass. Guards the collection's scroll anchor: its /// position follows the first row at or after the view's start.