diff --git a/crates/ui-contract/src/contract/collection/events.rs b/crates/ui-contract/src/contract/collection/events.rs index 486201314..b9d884319 100644 --- a/crates/ui-contract/src/contract/collection/events.rs +++ b/crates/ui-contract/src/contract/collection/events.rs @@ -214,6 +214,15 @@ pub enum CollectionEvent { /// The modifiers of the pick; `Mods::NONE` for a key. mods: craft_keymap::Mods, }, + /// A click on an item that left the selection as it was: the click selects the item alone and it already was (a + /// click without Shift or Cmd/Ctrl on the sole selected item). A host that applies what a click picks (a preset, a + /// swatch) applies it again. + Picked { + /// The item. + key: K, + /// The modifiers of the click. + mods: craft_keymap::Mods, + }, /// The lead moved, by keys or by the pointer. LeadMoved { /// The item that took the lead. diff --git a/crates/ui-contract/src/contract/items/access.rs b/crates/ui-contract/src/contract/items/access.rs index 74ae7085e..25e9bd238 100644 --- a/crates/ui-contract/src/contract/items/access.rs +++ b/crates/ui-contract/src/contract/items/access.rs @@ -108,7 +108,7 @@ impl ItemsHooks<'_, '_, C> { if self.source.section_stop(at) { self.events.push(CollectionEvent::Activate(key)); } else { - self.pick(comp, at, false, false); + self.click_pick(comp, at, false, false, craft_keymap::Mods::NONE); if comp.items().activate == ActivateOn::Click { self.activation_clicks = 1; self.events.push(CollectionEvent::Activate(key)); diff --git a/crates/ui-contract/src/contract/items/lead.rs b/crates/ui-contract/src/contract/items/lead.rs index 461034c13..3c0ffadc7 100644 --- a/crates/ui-contract/src/contract/items/lead.rs +++ b/crates/ui-contract/src/contract/items/lead.rs @@ -105,6 +105,16 @@ impl<'s, 'r, C: Collection> ItemsHooks<'s, 'r, C> { self.pick_by(comp, at, extend, toggle, PickBy::Pointer(Mods::NONE)); } + /// A primary click with `mods` held that selects row `at` as `pick_by` does. A plain pick that leaves the selection + /// as it was reports `CollectionEvent::Picked`. + pub(super) fn click_pick(&mut self, comp: &C, at: usize, extend: bool, toggle: bool, mods: Mods) { + let before = self.selection.clone(); + self.pick_by(comp, at, extend, toggle, PickBy::Pointer(mods)); + if !(extend || toggle) && self.selection == before && self.holds(at) { + self.events.push(CollectionEvent::Picked { key: self.source.key(at), mods }); + } + } + /// A press or key `by` that selects row `at` the way the modifiers say. pub(super) fn pick_by(&mut self, comp: &C, at: usize, extend: bool, toggle: bool, by: PickBy) { if self.source.section_stop(at) { diff --git a/crates/ui-contract/src/contract/items/press.rs b/crates/ui-contract/src/contract/items/press.rs index b2b12dc6b..918b6903d 100644 --- a/crates/ui-contract/src/contract/items/press.rs +++ b/crates/ui-contract/src/contract/items/press.rs @@ -135,6 +135,7 @@ impl ItemsHooks<'_, '_, C> { None if again => {} None if hold => self.drags.collapse = Some(key.clone()), None if keep => {} + None if primary => self.click_pick(comp, at, mods.shift, mods.cmd, mods), None => self.pick_by(comp, at, mods.shift, mods.cmd, PickBy::Pointer(mods)), } self.move_lead(at, LeadSource::Pointer); @@ -210,7 +211,7 @@ impl ItemsHooks<'_, '_, C> { let mods = crate::runtime::pointer::frame_raw(&ctx).mods; if !(mods.shift || mods.cmd || mods.alt) { if !self.keeps_pick(at, start.clicks) { - self.pick_by(comp, at, false, false, PickBy::Pointer(mods)); + self.click_pick(comp, at, false, false, mods); } } else if props.command_click && mods.cmd { self.events.push(CollectionEvent::CommandClick { key: self.source.key(at), mods }); @@ -260,7 +261,7 @@ impl ItemsHooks<'_, '_, C> { if Self::select_mode(comp) == SelectMode::Single { self.change_by(SelectionChange::Clear, PickBy::Pointer(mods_now)); } else { - self.pick_by(comp, at, false, false, PickBy::Pointer(mods_now)); + self.click_pick(comp, at, false, false, mods_now); } } } diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index 3bf3386f7..d8619dd57 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -641,6 +641,8 @@ impl EditKind { pub enum CollectionKind { /// The selection changed. Selection, + /// A click picked the sole selected item again. + Picked, /// The lead moved. LeadMoved, /// The pointer rested on an item. @@ -686,6 +688,7 @@ impl CollectionKind { pub fn of(event: &CollectionEvent) -> Option { Some(match event { CollectionEvent::Selection { .. } => CollectionKind::Selection, + CollectionEvent::Picked { .. } => CollectionKind::Picked, CollectionEvent::LeadMoved { .. } => CollectionKind::LeadMoved, CollectionEvent::Activate(_) => CollectionKind::Activate, CollectionEvent::Rested(_) => CollectionKind::Rested, diff --git a/crates/ui-patterns/src/patterns/presets/browser.rs b/crates/ui-patterns/src/patterns/presets/browser.rs index 9b7c501de..e1a763014 100644 --- a/crates/ui-patterns/src/patterns/presets/browser.rs +++ b/crates/ui-patterns/src/patterns/presets/browser.rs @@ -654,6 +654,8 @@ fn seen(events: Vec>) -> Vec> { out.push(Seen::Selection(change)); out.extend(picked.map(Seen::Select)); } + // A click on the sole selected preset picks it again, as `Select` says. + CollectionEvent::Picked { key, .. } => out.push(Seen::Select(key)), CollectionEvent::Activate(key) => out.push(Seen::Activate(key)), CollectionEvent::Open(change) => out.push(Seen::Open(change)), CollectionEvent::ContextMenu(request) => out.push(Seen::Menu(request.key)), diff --git a/crates/ui-patterns/tests/it/presets/browser.rs b/crates/ui-patterns/tests/it/presets/browser.rs index 67b8cf8cb..10017eb9e 100644 --- a/crates/ui-patterns/tests/it/presets/browser.rs +++ b/crates/ui-patterns/tests/it/presets/browser.rs @@ -156,7 +156,7 @@ fn secondary_click_preset(fixture: &mut Fixture, key: u32) { } #[trial] -fn multi_selection_ranges_toggles_and_reports_select_only_for_a_plain_pick() { +fn multi_selection_ranges_toggles_and_reports_select_for_each_plain_pick() { for view in [PresetView::List, PresetView::Grid] { let (mut fixture, selection, events) = multi_rig(view); let items = || { @@ -168,6 +168,14 @@ fn multi_selection_ranges_toggles_and_reports_select_only_for_a_plain_pick() { click_preset(&mut fixture, 1, Modifiers::NONE); assert_eq!((items(), selects()), (vec![1], 1), "a plain click selects and reports Select in {view:?}"); + fixture.advance(1.0); + click_preset(&mut fixture, 1, Modifiers::ALT); + assert_eq!((items(), selects()), (vec![1], 2), "a click on the sole selected preset picks it again in {view:?}"); + assert!( + events.borrow().iter().any(|event| matches!(event, PresetEvent::Select { key: 1, mods, .. } if mods.alt)), + "the pick carries its modifiers in {view:?}: {:?}", + events.borrow() + ); events.borrow_mut().clear(); click_preset(&mut fixture, 3, Modifiers::SHIFT);