mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
ui-contract: a click on the sole selected item reports CollectionEvent::Picked, and PresetBrowser reports it as Select
This commit is contained in:
@@ -214,6 +214,15 @@ pub enum CollectionEvent<K, E> {
|
||||
/// 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.
|
||||
|
||||
@@ -108,7 +108,7 @@ impl<C: Collection> 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));
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -135,6 +135,7 @@ impl<C: Collection> 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<C: Collection> 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<C: Collection> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<K, E>(event: &CollectionEvent<K, E>) -> Option<CollectionKind> {
|
||||
Some(match event {
|
||||
CollectionEvent::Selection { .. } => CollectionKind::Selection,
|
||||
CollectionEvent::Picked { .. } => CollectionKind::Picked,
|
||||
CollectionEvent::LeadMoved { .. } => CollectionKind::LeadMoved,
|
||||
CollectionEvent::Activate(_) => CollectionKind::Activate,
|
||||
CollectionEvent::Rested(_) => CollectionKind::Rested,
|
||||
|
||||
@@ -654,6 +654,8 @@ fn seen<K: Clone, E>(events: Vec<CollectionEvent<K, E>>) -> Vec<Seen<K>> {
|
||||
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)),
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user