diff --git a/crates/imaging/src/ui/fill/mod.rs b/crates/imaging/src/ui/fill/mod.rs index d04f4aafe..f88657eaf 100644 --- a/crates/imaging/src/ui/fill/mod.rs +++ b/crates/imaging/src/ui/fill/mod.rs @@ -71,7 +71,7 @@ use craft_ui::layout_components::Row; use craft_ui::patterns::form_dialogs::{FormDialogWords, ParamDialogWords}; use craft_ui::primitives::{Checker, IconSpec}; use craft_ui::rails::{LastUsed, RailTool, ToolRail, ToolSlot}; -use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; +use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase}; use craft_ui::runtime::env::{Env, Label}; use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft_ui::runtime::pending::{Job, Pending}; @@ -621,6 +621,10 @@ impl ContentAwareFillWorkspace { { while let Some((command, fired)) = cx.take_command::() { if let Some(tool) = command.tool() { + // A tool key's release picks nothing. + if fired.phase == FirePhase::Release { + continue; + } let slot = slots.iter().find(|slot| slot.tools.iter().any(|entry| entry.id == tool)); let tools: Vec<&RailTool> = slot.into_iter().flat_map(|slot| slot.tools.iter()).collect(); let picked = slot.and_then(|slot| { diff --git a/crates/imaging/src/ui/liquify/mod.rs b/crates/imaging/src/ui/liquify/mod.rs index a1cf5fd0f..1200701a5 100644 --- a/crates/imaging/src/ui/liquify/mod.rs +++ b/crates/imaging/src/ui/liquify/mod.rs @@ -80,7 +80,7 @@ use craft_ui::dialog::{ use craft_ui::layout_components::Row; use craft_ui::primitives::Checker; use craft_ui::rails::{LastUsed, ToolRail}; -use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; +use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase}; use craft_ui::runtime::env::Env; use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; use craft_ui::runtime::words::CommonWords; @@ -495,6 +495,10 @@ impl LiquifyWorkspace { { while let Some((command, fired)) = cx.take_command::() { if let Some(tool) = command.tool() { + // A tool key's release picks nothing. + if fired.phase == FirePhase::Release { + continue; + } let slot = slots.iter().find(|slot| slot.tools.iter().any(|entry| entry.id == tool)); let tools: Vec<_> = slot.into_iter().flat_map(|slot| slot.tools.iter()).collect(); let picked = slot.and_then(|slot| { diff --git a/crates/imaging/src/ui/select_mask/mod.rs b/crates/imaging/src/ui/select_mask/mod.rs index 0053ce320..ca1888db8 100644 --- a/crates/imaging/src/ui/select_mask/mod.rs +++ b/crates/imaging/src/ui/select_mask/mod.rs @@ -76,7 +76,7 @@ use craft_ui::dialog::{ use craft_ui::layout_components::{Row, Stack}; use craft_ui::primitives::Checker; use craft_ui::rails::{LastUsed, ToolRail}; -use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; +use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase}; use craft_ui::runtime::edit::next_gesture_id; use craft_ui::runtime::env::Env; use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value}; @@ -548,6 +548,10 @@ impl SelectMaskWorkspace { while let Some((command, fired)) = cx.take_command::() { if let Some(tool) = command.tool() { + // A tool key's release picks nothing. + if fired.phase == FirePhase::Release { + continue; + } let slot = slots.iter().find(|slot| slot.tools.iter().any(|entry| entry.id == tool)); let tools: Vec<_> = slot.into_iter().flat_map(|slot| slot.tools.iter()).collect(); let picked = slot.and_then(|slot| { diff --git a/crates/imaging/src/ui/vanishing_point/mod.rs b/crates/imaging/src/ui/vanishing_point/mod.rs index 60b41a577..b842de770 100644 --- a/crates/imaging/src/ui/vanishing_point/mod.rs +++ b/crates/imaging/src/ui/vanishing_point/mod.rs @@ -60,7 +60,7 @@ use craft_ui::dialog::{ use craft_ui::layout_components::Row; use craft_ui::primitives::Checker; use craft_ui::rails::{LastUsed, RailTool, ToolRail, ToolSlot}; -use craft_ui::runtime::commands::{CommandSet as _, Commands as _}; +use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase}; use craft_ui::runtime::env::Env; use craft_ui::runtime::windows::WindowSpec; use craft_ui::runtime::words::CommonWords; @@ -410,7 +410,8 @@ impl FullWindowWorkspace for VanishingPointWorkspace { work.new_plane.open = true; continue; } - let Some(pick) = command.pick() else { continue }; + // A tool key's release picks nothing. + let Some(pick) = command.pick().filter(|_| fired.phase != FirePhase::Release) else { continue }; let slot = slots.iter().find(|slot| slot.tools.iter().any(|entry| entry.id == pick)); let tools: Vec<_> = slot.into_iter().flat_map(|slot| slot.tools.iter()).collect(); let current = Pick::of(work.strip, work.editor.tool()); diff --git a/crates/imaging/src/ui/wide_angle/mod.rs b/crates/imaging/src/ui/wide_angle/mod.rs index be930c8e7..23fc5ed01 100644 --- a/crates/imaging/src/ui/wide_angle/mod.rs +++ b/crates/imaging/src/ui/wide_angle/mod.rs @@ -47,7 +47,7 @@ use craft_ui::layout_components::Row; use craft_ui::menu::{Menu, MenuItem}; use craft_ui::patterns::form_dialogs::{Param, ParamDialog, ParamKind}; use craft_ui::rails::{LastUsed, RailTool, ToolRail, ToolSlot}; -use craft_ui::runtime::commands::{CommandSet as _, Commands as _, Fired}; +use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase, Fired}; use craft_ui::runtime::env::{Env, Label}; use craft_ui::runtime::pending::Job; use craft_ui::runtime::words::CommonWords; @@ -553,8 +553,11 @@ fn edit_name<'a>(words: &'a WideAngleWords, edit: &WideAngleConstraintEdit) -> & } } -/// The tool a fired tool command picks, through its slot's pick rule. +/// The tool a fired tool command picks, through its slot's pick rule; a tool key's release picks nothing. fn pick(slots: &[ToolSlot], command: WideAngleCommands, fired: &Fired, body: &Body) -> Option { + if fired.phase == FirePhase::Release { + return None; + } let tool = TOOL_SLOT.iter().find(|(_, c)| *c == command)?.0; let slot = slots.iter().find(|slot| slot.tools.iter().any(|t| t.id == tool))?; let visible: Vec<&RailTool> = slot.tools.iter().collect(); diff --git a/crates/keymap/src/keymap/command.rs b/crates/keymap/src/keymap/command.rs index 57649a467..47019ebe6 100644 --- a/crates/keymap/src/keymap/command.rs +++ b/crates/keymap/src/keymap/command.rs @@ -11,7 +11,8 @@ pub enum CommandKind { /// Runs the command. Command, /// Selects a tool. Tools of one group share keys and cycle; tools without a group name form one - /// group together. + /// group together. The key's release fires the command again in the release phase, so an app can give back + /// the previous tool when the key was only held (a spring-loaded tool). Tool { /// The group the tool belongs to; `None` joins the group of tools without one. group: Option, diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index ee6338466..eee9d91a2 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -1202,7 +1202,9 @@ pub(crate) fn begin(ctx: &Context) { let target = if app { resolved.instance } else { instance }; let key = shortcut.input.physical.or(shortcut.input.typed); with(ctx, |board| { - if let Some(key) = key.filter(|_| board.def(&hit.command.id).is_some_and(|def| def.kind == CommandKind::Momentary)) { + if let Some(key) = key.filter(|_| { + board.def(&hit.command.id).is_some_and(|def| matches!(def.kind, CommandKind::Momentary | CommandKind::Tool { .. })) + }) { board.held.retain(|held| held.key != key); board.held.push(Held { key, command: hit.command.clone(), target }); } diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index c8edc8f5f..d621f15ee 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -157,7 +157,8 @@ pub enum FirePhase { Press, /// A repeat of a held key. Repeat, - /// The release; momentary tools act here. + /// The release of a momentary command's or a tool's key: a momentary command ends here, and a spring-loaded tool + /// gives back the previous one. Release, } diff --git a/crates/ui-gallery/src/mock/editor.rs b/crates/ui-gallery/src/mock/editor.rs index 2fc36b2e5..b01e39d3c 100644 --- a/crates/ui-gallery/src/mock/editor.rs +++ b/crates/ui-gallery/src/mock/editor.rs @@ -111,8 +111,11 @@ fn body(ui: &mut Ui, cx: &mut BodyCx<'_>, state: &mut MockState) { } } - while let Some((command, _)) = cx.take_command::() { - select(ui, state, command); + while let Some((command, fired)) = cx.take_command::() { + // A tool key's release picks nothing. + if fired.phase != commands::FirePhase::Release { + select(ui, state, command); + } } while let Some((key, _)) = cx.take_command::() { match key { diff --git a/crates/ui-shell/tests/it/commands/app_catalog.rs b/crates/ui-shell/tests/it/commands/app_catalog.rs index f5b07488e..0b6cc8f0c 100644 --- a/crates/ui-shell/tests/it/commands/app_catalog.rs +++ b/crates/ui-shell/tests/it/commands/app_catalog.rs @@ -20,6 +20,7 @@ use std::cell::{Cell, RefCell}; const EXPORT: &str = "trialCatalog.export"; const PEEK: &str = "trialCatalog.peek"; const QUICK: &str = "trialCatalog.quick"; +const BRUSH: &str = "trialCatalog.brush"; const RENAME: &str = "trialCatalog.rename"; /// An app-scope definition of `id` bound to `chord`. @@ -29,7 +30,12 @@ fn def(id: &str, label: &str, chord: &str) -> CommandDef { /// The app's catalog at `generation`: from generation 2 on it also holds Rename. fn catalog(generation: u64) -> CommandInput { - let mut defs = vec![def(EXPORT, "Export", "Cmd+Alt+U"), def(PEEK, "Peek", "F9").with_kind(CommandKind::Momentary), def(QUICK, "Quick Mask", "Q")]; + let mut defs = vec![ + def(EXPORT, "Export", "Cmd+Alt+U"), + def(PEEK, "Peek", "F9").with_kind(CommandKind::Momentary), + def(QUICK, "Quick Mask", "Q"), + def(BRUSH, "Brush", "J").with_kind(CommandKind::Tool { group: None }), + ]; if generation >= 2 { defs.push(def(RENAME, "Rename", "Cmd+Alt+P")); } @@ -45,8 +51,8 @@ fn hit(id: &str, phase: FirePhase) -> (String, Option, FirePhase) { (id.to_owned(), None, phase) } -/// A catalog command's chord fires it as an app command, with no target; a momentary catalog command fires again on -/// its key's release; a plain-letter chord fires with nothing focused, and waits for the text while a text field has +/// A catalog command's chord fires it as an app command, with no target; a momentary or tool catalog command fires +/// again on its key's release; a plain-letter chord fires with nothing focused, and waits for the text while a text field has /// the focus, which types the letter. #[trial] fn an_app_catalog_command_fires_by_its_chord_and_a_focused_text_field_keeps_a_typing_chord() { @@ -76,6 +82,9 @@ fn an_app_catalog_command_fires_by_its_chord_and_a_focused_text_field_keeps_a_ty frames.frame(vec![key_press(Key::Q, Modifiers::NONE)], &mut show); frames.frame(vec![key_release(Key::Q, Modifiers::NONE)], &mut show); assert_eq!(fired(&frames), [hit(QUICK, FirePhase::Press)], "with nothing focused the plain chord fires"); + frames.frame(vec![key_press(Key::J, Modifiers::NONE)], &mut show); + frames.frame(vec![key_release(Key::J, Modifiers::NONE)], &mut show); + assert_eq!(fired(&frames), [hit(BRUSH, FirePhase::Press), hit(BRUSH, FirePhase::Release)], "a tool's key fires on press and release"); let at = field.get().center(); frames.frame(vec![moved(at)], &mut show);