diff --git a/crates/automation/src/ui/actions/body.rs b/crates/automation/src/ui/actions/body.rs index e824a6f20..e77cda82d 100644 --- a/crates/automation/src/ui/actions/body.rs +++ b/crates/automation/src/ui/actions/body.rs @@ -6,7 +6,7 @@ use craft_theme::variant::Tone; use craft_ui::buttons::Button; use craft_ui::contract::{ Bind, CollectionEvent, ColumnKey, ColumnKind, ColumnSpec, ListDrag, OpenChange, Outcome as _, Props as _, ReorderMove, ReorderRule, Scroll, - SelectMode, Show as _, ShowCollection as _, ShowContainer as _, ToggleColumn, ToggleEdit, request_rename, + SelectMode, Show as _, ShowCollection as _, ShowContainer as _, SpaceAction, ToggleColumn, ToggleEdit, request_rename, }; use craft_ui::display::Text; use craft_ui::layout_components::{Row, Stack}; @@ -165,6 +165,7 @@ fn show_tree(ui: &mut Ui, body: &Body<'_>, state: &mut ActionsPanelState, sessio .tree(true) .select(SelectMode::Single) .allow_empty(true) + .space(SpaceAction::ToggleCell) .deletable(true) .rename(true) .rename_column(NAME_COLUMN) diff --git a/crates/automation/tests/it/actions.rs b/crates/automation/tests/it/actions.rs index 9d5a37d0b..2fce46d90 100644 --- a/crates/automation/tests/it/actions.rs +++ b/crates/automation/tests/it/actions.rs @@ -5,9 +5,9 @@ use std::rc::Rc; use craft_automation::actions::ActionList; use craft_automation::ui::actions::mock::{FakeActionsHost, action, list_of}; -use craft_automation::ui::actions::{ActionMove, ActionPlace, ActionSelection, ActionsEvent, ActionsPanel, ActionsPanelState}; +use craft_automation::ui::actions::{ActionMove, ActionPlace, ActionSelection, ActionsEvent, ActionsPanel, ActionsPanelState, INCLUDE_COLUMN}; use craft_ui::testing::{Fixture, Target, pointer, trial}; -use egui::{Pos2, Ui, Vec2}; +use egui::{Key, Modifiers, Pos2, Ui, Vec2}; /// The app around the panel: what it holds and what the panel reported. struct App { @@ -109,3 +109,21 @@ fn actions_panel_drop_before_a_later_set_lands_at_the_end_of_the_previous_set() "before the first set the action is loose, after the last loose one" ); } + +/// Space on the focused row flips its Include check. +#[trial] +fn actions_panel_space_flips_the_include_check_of_the_focused_row() { + let (mut fixture, shared) = rig(app(two_sets())); + let row = node(&fixture, "Loose").center(); + fixture.frame(vec![pointer::moved(row)]); + fixture.frame(vec![pointer::press(row)]); + fixture.frame(vec![pointer::release(row)]); + fixture.idle(2); + take(&shared); + fixture.key(Key::Space, Modifiers::NONE).idle(2); + let flipped = take(&shared).into_iter().any(|event| { + matches!(event, ActionsEvent::Toggle(craft_ui::contract::SweepStep::Flip(edit)) + if edit.key == ActionSelection::Action(0) && edit.column == INCLUDE_COLUMN) + }); + assert!(flipped, "Space flips the row's include cell"); +} diff --git a/crates/behavior/src/intent.rs b/crates/behavior/src/intent.rs index 0361d77bf..209a9eef6 100644 --- a/crates/behavior/src/intent.rs +++ b/crates/behavior/src/intent.rs @@ -160,6 +160,8 @@ pub enum Intent { }, /// Space on a collection: the item step turns it into the collection's `SpaceAction`. Space, + /// Ctrl+Space (Control on macOS) on a multi-select collection: toggles the lead's pick. + TogglePick, /// One typed digit, for a role that takes digits (`KeyRole::digits`). Digit(u8), /// Rename the lead. diff --git a/crates/behavior/src/keys.rs b/crates/behavior/src/keys.rs index f84bee741..7a348ef97 100644 --- a/crates/behavior/src/keys.rs +++ b/crates/behavior/src/keys.rs @@ -15,6 +15,8 @@ pub struct KeyRole { pub resettable: bool, /// A collection: Space is its own intent (`Intent::Space`), which the item step turns into its `SpaceAction`. pub space: bool, + /// A collection that selects several items: Ctrl+Space (Control on macOS) toggles the lead's pick. + pub multi_select: bool, /// A reorderable collection: the stacking keys (`REORDER_KEYS`) move the selected items. pub reorderable: bool, /// A collection with `CollectionProps.rename`. @@ -50,6 +52,7 @@ impl KeyRole { KeyRole { resettable: false, space: false, + multi_select: false, reorderable: false, rename: false, pages: false, @@ -77,6 +80,12 @@ impl KeyRole { self } + /// Ctrl+Space toggles the lead's pick. + pub const fn multi_select(mut self, on: bool) -> Self { + self.multi_select = on; + self + } + /// The stacking keys move the selected items. pub const fn reorderable(mut self, on: bool) -> Self { self.reorderable = on; @@ -235,6 +244,7 @@ pub fn intent(os: Os, key: Key, mods: Mods, role: KeyRole) -> Option { match key { Key::Delete | Key::Backspace if plain && role.resettable => return Some(Intent::Reset), Key::Space if plain && role.space => return Some(Intent::Space), + Key::Space if role.multi_select && only(if mac { Mods::CTRL } else { Mods::CMD }) => return Some(Intent::TogglePick), Key::Menu if plain && !mac => return Some(Intent::ContextMenu), Key::F10 if only(Mods::SHIFT) => return Some(Intent::ContextMenu), Key::F2 if plain && role.rename => return Some(Intent::Rename), diff --git a/crates/behavior/tests/key_table.rs b/crates/behavior/tests/key_table.rs index 7406a666b..b7e1aca75 100644 --- a/crates/behavior/tests/key_table.rs +++ b/crates/behavior/tests/key_table.rs @@ -15,3 +15,16 @@ fn f2_renames_and_enter_activates_on_every_platform() { assert_ne!(intent(os, Key::Down, Mods::CMD, role), Some(Intent::Activate), "{os:?}: Cmd+Down does not activate"); } } + +/// Ctrl+Space (Control on macOS) toggles the lead's pick only where several items can be picked; elsewhere it is the +/// app's. +#[test] +fn ctrl_space_toggles_the_pick_only_in_a_multi_select_collection() { + let single = KeyRole::new().space(true).moves(true); + let multi = single.multi_select(true); + for (os, mods) in [(Os::Mac, Mods::CTRL), (Os::Windows, Mods::CMD), (Os::Linux, Mods::CMD)] { + assert_eq!(intent(os, Key::Space, mods, multi), Some(Intent::TogglePick), "{os:?}: multi-select"); + assert_eq!(intent(os, Key::Space, mods, single), None, "{os:?}: single-select"); + } + assert_eq!(intent(Os::Mac, Key::Space, Mods::CMD, multi), None, "Cmd+Space stays the system's on macOS"); +} diff --git a/crates/ui-contract/src/contract/collection/events.rs b/crates/ui-contract/src/contract/collection/events.rs index fcbac4b0a..dcaffc25f 100644 --- a/crates/ui-contract/src/contract/collection/events.rs +++ b/crates/ui-contract/src/contract/collection/events.rs @@ -169,6 +169,8 @@ pub enum CollectionEvent { }, /// Enter or a double click on an item. Activate(K), + /// Space on the lead of a collection whose Space previews (`SpaceAction::Preview`). + Preview(K), /// The user asked for an item's context menu. ContextMenu(ContextMenuRequest), /// Delete or Backspace at the lead of a `deletable` collection: the selected items when the lead is selected, else diff --git a/crates/ui-contract/src/contract/collection/props.rs b/crates/ui-contract/src/contract/collection/props.rs index ee6789156..98dd2380a 100644 --- a/crates/ui-contract/src/contract/collection/props.rs +++ b/crates/ui-contract/src/contract/collection/props.rs @@ -76,6 +76,8 @@ pub enum SpaceAction { Activate, /// Flips the lead row's first enabled toggle cell; with several rows selected, the cell of every selected row. ToggleCell, + /// Reports `CollectionEvent::Preview` for the lead row (Bridge's preview); Ctrl+Space still toggles the lead's pick. + Preview, /// Nothing: the key is the app's. None, } diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 4ac23b293..c0991aebd 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -57,6 +57,7 @@ impl ShapeHooks for ItemsHooks<'_, '_, C> { .type_ahead(props.type_ahead) .digits(props.digit_keys) .space(true) + .multi_select(matches!(props.select, crate::contract::collection::SelectMode::Multi | crate::contract::collection::SelectMode::Cells)) .moves(true) .deletes(props.deletable) .owner_arrows(props.item_keys == crate::contract::collection::ItemKeys::Owner) diff --git a/crates/ui-contract/src/contract/items/keys.rs b/crates/ui-contract/src/contract/items/keys.rs index 053fd66c9..7cc1f3ec6 100644 --- a/crates/ui-contract/src/contract/items/keys.rs +++ b/crates/ui-contract/src/contract/items/keys.rs @@ -127,7 +127,7 @@ impl ItemsHooks<'_, '_, C> { true } - /// Space and activation at the lead. + /// Space, Ctrl+Space and activation at the lead. pub(super) fn intent_actions(&mut self, comp: &C, cx: &mut Cx<'_, C>, props: &CollectionProps, lead_at: Option, header_held: bool) { if !header_held && cx.core.intents.take(Intent::Space) @@ -135,6 +135,12 @@ impl ItemsHooks<'_, '_, C> { { self.space_key(comp, cx, props, row); } + if !header_held + && cx.core.intents.take(Intent::TogglePick) + && let Some(row) = lead_at + { + self.pick(comp, row, false, true); + } // The pass's `update_inputs` turned an `Activate` intent into the keyed activation. let activated = cx.core.intents.take(Intent::Activate) || cx.core.activation.is_some_and(|activation| activation.by_key); if activated @@ -186,6 +192,7 @@ impl ItemsHooks<'_, '_, C> { mode => self.pick(comp, row, false, mode != SelectMode::Single), }, SpaceAction::ToggleCell => self.toggle_by_key(cx, row), + SpaceAction::Preview => self.events.push(CollectionEvent::Preview(self.source.key(row))), SpaceAction::None => {} } } diff --git a/crates/ui-contract/src/testing/story.rs b/crates/ui-contract/src/testing/story.rs index 020310fe7..f20c603a2 100644 --- a/crates/ui-contract/src/testing/story.rs +++ b/crates/ui-contract/src/testing/story.rs @@ -407,6 +407,8 @@ pub enum CollectionKind { LeadMoved, /// Enter or a double click on an item. Activate, + /// Space previewed the lead. + Preview, /// A context menu was asked for. ContextMenu, /// Delete was pressed on items. @@ -442,6 +444,7 @@ impl CollectionKind { CollectionEvent::Selection(_) => CollectionKind::Selection, CollectionEvent::LeadMoved { .. } => CollectionKind::LeadMoved, CollectionEvent::Activate(_) => CollectionKind::Activate, + CollectionEvent::Preview(_) => CollectionKind::Preview, CollectionEvent::ContextMenu(_) => CollectionKind::ContextMenu, CollectionEvent::Delete(_) => CollectionKind::Delete, CollectionEvent::Open(_) => CollectionKind::Open, diff --git a/crates/ui-contract/tests/it/collection/kit.rs b/crates/ui-contract/tests/it/collection/kit.rs index 68b06dd41..0abe3e10d 100644 --- a/crates/ui-contract/tests/it/collection/kit.rs +++ b/crates/ui-contract/tests/it/collection/kit.rs @@ -197,6 +197,12 @@ impl<'a> List<'a> { self } + /// What Space does. + pub fn space(mut self, action: craft_ui::contract::SpaceAction) -> Self { + self.items = self.items.space(action); + self + } + /// Typed letters move the lead. pub fn type_ahead(mut self) -> Self { self.items = self.items.type_ahead(true); diff --git a/crates/ui-contract/tests/it/collection/list.rs b/crates/ui-contract/tests/it/collection/list.rs index ed8cf4268..887bcf62d 100644 --- a/crates/ui-contract/tests/it/collection/list.rs +++ b/crates/ui-contract/tests/it/collection/list.rs @@ -190,6 +190,36 @@ fn enter_picks_the_lead_where_arrows_leave_the_selection() { assert!(activated, "Enter activates the lead"); } +/// Where Space previews, Space reports the lead's preview and leaves the selection; Ctrl+Space toggles the lead's pick. +#[trial] +fn space_previews_and_ctrl_space_toggles_the_lead() { + let words = Words::flat(&labels()); + let seen = Seen::default(); + let selection = RefCell::new(Selection::::single(0)); + let mut frames = Frames::new(); + let mut show = |ui: &mut Ui| { + let mut held = selection.borrow().clone(); + let list = List::new(SelectMode::Multi).space(craft_ui::contract::SpaceAction::Preview).selection(Bind::bound(&mut held)); + show_words(ui, list, &words, &seen); + *selection.borrow_mut() = held; + }; + frames.idle(2, &mut show); + focus_list(&mut frames, &seen, &mut show); + press(&mut frames, Key::ArrowDown, Modifiers::COMMAND, &mut show); + let lead = *seen.leads().last().expect("the lead moved"); + seen.events.borrow_mut().clear(); + press(&mut frames, Key::Space, Modifiers::NONE, &mut show); + let previewed = seen.events.borrow().iter().any(|event| matches!(event, CollectionEvent::Preview(key) if *key == lead)); + assert!(previewed, "Space previews the lead"); + assert_eq!(selection.borrow().items, vec![0], "Space leaves the selection"); + press(&mut frames, Key::Space, Modifiers::CTRL, &mut show); + let mut items = selection.borrow().items.clone(); + items.sort_unstable(); + assert_eq!(items, vec![0, lead], "Ctrl+Space adds the lead"); + press(&mut frames, Key::Space, Modifiers::CTRL, &mut show); + assert_eq!(selection.borrow().items, vec![0], "Ctrl+Space again takes it out"); +} + /// In a list that wraps, Down on the last row goes to the first and Up on the first to the last. #[trial] fn arrows_wrap_at_the_ends_of_a_wrapping_list() { diff --git a/crates/ui-widgets/src/tile_grid/grid.rs b/crates/ui-widgets/src/tile_grid/grid.rs index be5cf6db7..5b0409555 100644 --- a/crates/ui-widgets/src/tile_grid/grid.rs +++ b/crates/ui-widgets/src/tile_grid/grid.rs @@ -326,7 +326,12 @@ impl<'a, K: Clone + Eq + Hash + Send + Sync + 'static> TileGrid<'a, K> { } else { Vec::new() }; - self.items = self.items.columns(columns).header(!on).space(if on { SpaceAction::ToggleCell } else { SpaceAction::default() }); + self.items = self.items.columns(columns).header(!on); + if on { + self.items = self.items.space(SpaceAction::ToggleCell); + } else if self.items.space == SpaceAction::ToggleCell { + self.items = self.items.space(SpaceAction::default()); + } self }