From 8d33de79b0d8abc6bd361526a6a0efcd8b26ca58 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 12:53:30 -0500 Subject: [PATCH 1/3] Shift+letter cycles a tool group in toolbar order and the anchor point tools no longer take the letter P (#11376) The group Shift+letter steps through is now sorted by position in TOOL_SECTIONS, so it follows the flyout instead of Tool::ALL. Add Anchor Point, Delete Anchor Point and Convert Point have no default letter, so P cycles only Pen, Freeform Pen and Curvature Pen. The engine's shortcut table lists the three anchor point tools without a key to match, and two tests that expected the letter P for them are updated. Co-Authored-By: Claude Fable 5.1 --- crates/engine/src/shell_commands.rs | 6 +- crates/ui-egui/src/input/shortcuts.rs | 4 +- crates/ui-egui/src/lib.rs | 2 + crates/ui-egui/src/panels.rs | 2 +- crates/ui-egui/src/panels/toolbar.rs | 8 +- crates/ui-egui/src/state.rs | 4 +- crates/ui-egui/src/state/tool.rs | 4 +- .../src/tool_shift_cycle_order_tests.rs | 128 ++++++++++++++++++ .../ui-egui/src/tools/anchor_tools/tests.rs | 8 +- 9 files changed, 151 insertions(+), 15 deletions(-) create mode 100644 crates/ui-egui/src/tool_shift_cycle_order_tests.rs diff --git a/crates/engine/src/shell_commands.rs b/crates/engine/src/shell_commands.rs index 3b52c03a03..ddd1a9fcba 100644 --- a/crates/engine/src/shell_commands.rs +++ b/crates/engine/src/shell_commands.rs @@ -157,9 +157,9 @@ pub const TOOLS: &[(&str, &str, Option<&str>)] = &[ ("tools.select.pen", "Pen Tool", Some("P")), ("tools.select.freeformPen", "Freeform Pen Tool", Some("P")), ("tools.select.curvaturePen", "Curvature Pen Tool", Some("P")), - ("tools.select.addAnchorPoint", "Add Anchor Point Tool", Some("P")), - ("tools.select.deleteAnchorPoint", "Delete Anchor Point Tool", Some("P")), - ("tools.select.convertPoint", "Convert Point Tool", Some("P")), + ("tools.select.addAnchorPoint", "Add Anchor Point Tool", None), + ("tools.select.deleteAnchorPoint", "Delete Anchor Point Tool", None), + ("tools.select.convertPoint", "Convert Point Tool", None), ("tools.select.pathSelection", "Path Selection Tool", Some("A")), ("tools.select.directSelection", "Direct Selection Tool", Some("A")), ("tools.select.rectangle", "Rectangle Tool", Some("U")), diff --git a/crates/ui-egui/src/input/shortcuts.rs b/crates/ui-egui/src/input/shortcuts.rs index 26dfa5e96f..52b2a1cc41 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -905,7 +905,9 @@ fn select_tool_by_key(app: &mut PhotocraftApp, ctx: &egui::Context, pressed: &im let cycle_shift = shift_switch && ctx.input_mut(|i| i.consume_key(Modifiers::SHIFT, k)); if cycle_shift || pressed(k) { let before = app.ui.tool; - let group: Vec = Tool::ALL.iter().copied().filter(|x| x.key() == t.key() && crate::tool_shortcuts::uses_default(app, *x)).collect(); + let mut group: Vec = Tool::ALL.iter().copied().filter(|x| x.key() == t.key() && crate::tool_shortcuts::uses_default(app, *x)).collect(); + // Shift+key steps in the toolbar flyout's order, not the order of `Tool::ALL`. + group.sort_by_key(|x| crate::panels::toolbar::toolbar_position(*x)); app.ui.tool = match group.iter().position(|x| *x == app.ui.tool) { Some(i) if shift_switch && !cycle_shift => group[i], Some(i) => group[(i + 1) % group.len()], diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 54edda6264..198fb42711 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -791,6 +791,8 @@ mod pencil_tests; #[cfg(test)] mod tool_options_theme_tests; #[cfg(test)] +mod tool_shift_cycle_order_tests; +#[cfg(test)] mod tool_shortcut_tests; #[cfg(test)] diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index ab5475ff9b..c872d7f69d 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -60,7 +60,7 @@ pub mod swatches_ui; pub(super) mod timeline_ui; pub(crate) mod title_bar; pub(super) mod titlebar; -mod toolbar; +pub(crate) mod toolbar; pub mod type_panels_ui; pub(super) mod variables_ui; diff --git a/crates/ui-egui/src/panels/toolbar.rs b/crates/ui-egui/src/panels/toolbar.rs index c8f62a602e..fbb8a84a8f 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -6,7 +6,7 @@ use super::{Align2, Color32, PhotocraftApp, Pos2, Rect, Sense, Stroke, StrokeKin /// Tool groups separated by hairlines, Photoshop order. /// Photoshop toolbar: divider-separated sections of slots; each slot is a flyout group whose button /// shows the group's last-used tool. -pub(super) const TOOL_SECTIONS: &[&[&[Tool]]] = &[ +pub(crate) const TOOL_SECTIONS: &[&[&[Tool]]] = &[ &[&[Tool::Move, Tool::Artboard]], &[ &[Tool::RectMarquee, Tool::EllipseMarquee, Tool::SingleRowMarquee, Tool::SingleColumnMarquee], @@ -51,6 +51,12 @@ pub(super) fn next_slot_tool(slot: &[Tool], from: Tool) -> Tool { i.checked_add(1).and_then(|n| n.checked_rem(slot.len())).and_then(|n| slot.get(n)).copied().unwrap_or(from) } +/// Position of `tool` in [`TOOL_SECTIONS`] read top to bottom, slot by slot. Tools outside the +/// toolbar sort last. Shift+letter orders a key's group by this so it steps like the flyout. +pub(crate) fn toolbar_position(tool: Tool) -> usize { + TOOL_SECTIONS.iter().flat_map(|section| section.iter()).flat_map(|slot| slot.iter()).position(|&t| t == tool).unwrap_or(usize::MAX) +} + /// [`TOOL_SECTIONS`] without the tools hidden in Edit › Toolbar (`hidden` holds tool names: the /// dialog writes `Tool` debug names, `edit.toolbar` takes any name `Tool::from_name` reads). A /// slot keeps only its visible tools, so its flyout lists just those; a slot with none left is diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index ca0259a434..47ff0baa2a 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -739,10 +739,10 @@ mod tests { assert_eq!(Tool::from_name("Vertical Type Mask Tool"), Some(Tool::VerticalTypeMask)); assert_eq!(Tool::from_name("addAnchorPoint"), Some(Tool::AddAnchorPoint)); assert_eq!(Tool::from_name("Delete Anchor Point Tool"), Some(Tool::DeleteAnchorPoint)); - assert_eq!(Tool::DeleteAnchorPoint.key(), 'P'); + assert_eq!(Tool::DeleteAnchorPoint.key(), ''); assert_eq!(Tool::from_name("convertPoint"), Some(Tool::ConvertPoint)); assert_eq!(Tool::from_name("Convert Point Tool"), Some(Tool::ConvertPoint)); - assert_eq!(Tool::ConvertPoint.key(), 'P', "it shares the Pen's key"); + assert_eq!(Tool::ConvertPoint.key(), '', "P never reaches it"); assert!(!Tool::ConvertPoint.is_brushlike()); assert_eq!(Tool::from_name("Remove Tool"), Some(Tool::Remove)); assert_eq!(Tool::Remove.key(), 'J'); diff --git a/crates/ui-egui/src/state/tool.rs b/crates/ui-egui/src/state/tool.rs index cd96f525a1..b9829aa847 100644 --- a/crates/ui-egui/src/state/tool.rs +++ b/crates/ui-egui/src/state/tool.rs @@ -284,7 +284,9 @@ impl Tool { Tool::SingleRowMarquee | Tool::SingleColumnMarquee => '\0', Tool::Dodge | Tool::Burn | Tool::Sponge => 'O', Tool::QuickSelection | Tool::ObjectSelection => 'W', - Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => 'P', + Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen => 'P', + // In the Pen flyout, but P never reaches them. + Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint => '', Tool::PathSelection | Tool::DirectSelection => 'A', Tool::Rectangle | Tool::EllipseShape | Tool::Triangle | Tool::Polygon | Tool::Line | Tool::CustomShape => 'U', } diff --git a/crates/ui-egui/src/tool_shift_cycle_order_tests.rs b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs new file mode 100644 index 0000000000..1f1d4885b8 --- /dev/null +++ b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs @@ -0,0 +1,128 @@ +//! Shift+letter steps through a key's tool group in the toolbar flyout's order, wrapping, and the +//! anchor point tools have no letter so P never reaches them. + +use egui::{Key, Modifiers}; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::panels::toolbar::{TOOL_SECTIONS, toolbar_position}; +use crate::state::Tool; + +fn app() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 20, "height": 10})).unwrap(); + app.run("layer.new.layer", json!({})).unwrap(); + app +} + +fn shift_press(app: &mut PhotocraftApp, key: Key) { + let modifiers = Modifiers::SHIFT; + let ctx = egui::Context::default(); + let raw = egui::RawInput { + events: vec![egui::Event::ModifiersChanged(modifiers), egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(app, &ctx); + ctx.end_pass().textures_delta.clear(); +} + +/// Every lettered tool group in toolbar order, with its letter. +fn groups() -> Vec<(char, Vec)> { + let mut keys: Vec = Tool::ALL.iter().map(|t| t.key()).filter(|k| *k != '\0').collect(); + keys.sort_unstable(); + keys.dedup(); + keys.into_iter() + .map(|k| { + let mut group: Vec = Tool::ALL.iter().copied().filter(|t| t.key() == k && toolbar_position(*t) != usize::MAX).collect(); + group.sort_by_key(|t| toolbar_position(*t)); + (k, group) + }) + .collect() +} + +fn key_for(letter: char) -> Key { + crate::spring_tools::key_of(letter).unwrap() +} + +fn sequence(letter: char, steps: usize) -> Vec { + let mut app = app(); + app.ui.tool = Tool::Hand; + (0..steps) + .map(|_| { + shift_press(&mut app, key_for(letter)); + app.ui.tool + }) + .collect() +} + +#[test] +fn from_every_tool_shift_letter_lands_on_the_next_tool_in_toolbar_order() { + for (letter, group) in groups() { + for (i, from) in group.iter().enumerate() { + let mut app = app(); + app.ui.tool = *from; + shift_press(&mut app, key_for(letter)); + let want = group[(i + 1) % group.len()]; + assert_eq!(app.ui.tool, want, "Shift+{letter} from {from:?}"); + } + } +} + +#[test] +fn shift_letter_wraps_from_the_last_tool_to_the_first() { + for (letter, group) in groups() { + let mut app = app(); + app.ui.tool = *group.last().unwrap(); + shift_press(&mut app, key_for(letter)); + assert_eq!(app.ui.tool, group[0], "Shift+{letter} wraps"); + } +} + +#[test] +fn the_first_shift_letter_from_another_tool_lands_on_the_groups_first_tool() { + for (letter, group) in groups() { + let mut app = app(); + app.ui.tool = Tool::Hand; + if group.contains(&Tool::Hand) { + continue; + } + shift_press(&mut app, key_for(letter)); + assert_eq!(app.ui.tool, group[0], "Shift+{letter} from the Hand tool"); + } +} + +#[test] +fn w_cycles_object_selection_quick_selection_magic_wand() { + assert_eq!(sequence('W', 4), vec![Tool::ObjectSelection, Tool::QuickSelection, Tool::MagicWand, Tool::ObjectSelection]); +} + +#[test] +fn t_cycles_horizontal_vertical_vertical_mask_horizontal_mask() { + assert_eq!(sequence('T', 5), vec![Tool::Type, Tool::VerticalType, Tool::VerticalTypeMask, Tool::HorizontalTypeMask, Tool::Type]); +} + +#[test] +fn p_cycles_the_three_pens_and_never_reaches_an_anchor_point_tool() { + let seen = sequence('P', 7); + assert_eq!(&seen[..4], &[Tool::Pen, Tool::FreeformPen, Tool::CurvaturePen, Tool::Pen]); + for t in seen { + assert!(!matches!(t, Tool::AddAnchorPoint | Tool::DeleteAnchorPoint | Tool::ConvertPoint), "{t:?}"); + } +} + +#[test] +fn the_anchor_point_tools_have_no_letter_but_stay_in_the_pen_flyout() { + for t in [Tool::AddAnchorPoint, Tool::DeleteAnchorPoint, Tool::ConvertPoint] { + assert_eq!(t.key(), '\0', "{t:?}"); + assert!(TOOL_SECTIONS.iter().flat_map(|s| s.iter()).any(|slot| slot.contains(&t)), "{t:?} stays in the toolbar"); + } +} + +#[test] +fn the_cycle_order_is_the_flattened_toolbar_order() { + let flat: Vec = TOOL_SECTIONS.iter().flat_map(|s| s.iter()).flat_map(|slot| slot.iter().copied()).collect(); + for (i, t) in flat.iter().enumerate() { + assert_eq!(toolbar_position(*t), i, "{t:?}"); + } +} diff --git a/crates/ui-egui/src/tools/anchor_tools/tests.rs b/crates/ui-egui/src/tools/anchor_tools/tests.rs index 02d0f45dc8..d7cd7256a4 100644 --- a/crates/ui-egui/src/tools/anchor_tools/tests.rs +++ b/crates/ui-egui/src/tools/anchor_tools/tests.rs @@ -44,13 +44,9 @@ fn click(app: &mut PhotocraftApp, at: [f64; 2], mods: Modifiers) { } #[test] -fn both_tools_join_the_pen_group() { +fn the_anchor_tools_have_no_letter() { let group: Vec = Tool::ALL.iter().copied().filter(|t| t.key() == 'P').collect(); - assert_eq!( - group, - vec![Tool::Pen, Tool::FreeformPen, Tool::CurvaturePen, Tool::AddAnchorPoint, Tool::DeleteAnchorPoint, Tool::ConvertPoint], - "P cycles Pen → Freeform → Curvature → Add → Delete → Convert Point" - ); + assert_eq!(group, vec![Tool::Pen, Tool::FreeformPen, Tool::CurvaturePen], "P cycles Pen → Freeform → Curvature and never reaches the anchor point tools"); let mut app = app(Tool::Brush); app.ui.tool = Tool::from_name("addAnchorPoint").unwrap(); assert_eq!(app.ui.tool, Tool::AddAnchorPoint); From 9afc9295a710f5bb0d951bc1243f613d6fc49fe2 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 14:19:44 -0500 Subject: [PATCH 2/3] Toolbar order applies to Shift-switch cycling only; plain letters keep their order (#11376) With "Use Shift Key for Tool Switch" off, plain letters cycle the group, and that case was not measured. The toolbar-order sort now runs only when the preference is on; with it off the group keeps its Tool::ALL order. A test pins plain W from Magic Wand going Quick Selection, Object Selection, Magic Wand. Co-Authored-By: Claude Fable 5.1 --- crates/ui-egui/src/input/shortcuts.rs | 7 +++++-- .../src/tool_shift_cycle_order_tests.rs | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/input/shortcuts.rs b/crates/ui-egui/src/input/shortcuts.rs index 52b2a1cc41..b4c00690cc 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -906,8 +906,11 @@ fn select_tool_by_key(app: &mut PhotocraftApp, ctx: &egui::Context, pressed: &im if cycle_shift || pressed(k) { let before = app.ui.tool; let mut group: Vec = Tool::ALL.iter().copied().filter(|x| x.key() == t.key() && crate::tool_shortcuts::uses_default(app, *x)).collect(); - // Shift+key steps in the toolbar flyout's order, not the order of `Tool::ALL`. - group.sort_by_key(|x| crate::panels::toolbar::toolbar_position(*x)); + // With the Shift-switch preference, Shift+key steps in the toolbar flyout's order; with it off + // plain keys keep the `Tool::ALL` order. + if shift_switch { + group.sort_by_key(|x| crate::panels::toolbar::toolbar_position(*x)); + } app.ui.tool = match group.iter().position(|x| *x == app.ui.tool) { Some(i) if shift_switch && !cycle_shift => group[i], Some(i) => group[(i + 1) % group.len()], diff --git a/crates/ui-egui/src/tool_shift_cycle_order_tests.rs b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs index 1f1d4885b8..017f3777fe 100644 --- a/crates/ui-egui/src/tool_shift_cycle_order_tests.rs +++ b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs @@ -126,3 +126,24 @@ fn the_cycle_order_is_the_flattened_toolbar_order() { assert_eq!(toolbar_position(*t), i, "{t:?}"); } } + +#[test] +fn with_the_shift_switch_preference_off_plain_w_keeps_the_old_order() { + let mut app = app(); + app.session.edit_prefs(|p| p.tools.use_shift_key_for_tool_switch = false); + app.ui.tool = Tool::MagicWand; + let mut seen = Vec::new(); + for _ in 0..3 { + let ctx = egui::Context::default(); + let modifiers = Modifiers::NONE; + let raw = egui::RawInput { + events: vec![egui::Event::Key { key: Key::W, physical_key: None, pressed: true, repeat: false, modifiers }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(&mut app, &ctx); + ctx.end_pass().textures_delta.clear(); + seen.push(app.ui.tool); + } + assert_eq!(seen, vec![Tool::QuickSelection, Tool::ObjectSelection, Tool::MagicWand]); +} From 366a4a0df999c6a0e8fb10d1c648536c9dd5c5b8 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 15:01:58 -0500 Subject: [PATCH 3/3] Toolbar order applies to Shift presses only; a plain letter from outside the group picks what it did before (#11376) The sort now runs only when the Shift key cycles, so the Shift cycle and the Shift fallback to the group's first tool use the toolbar order while plain presses keep the Tool::ALL order. A test pins plain W from the Move tool selecting the Magic Wand. Co-Authored-By: Claude Fable 5.1 --- crates/ui-egui/src/input/shortcuts.rs | 6 +++--- .../ui-egui/src/tool_shift_cycle_order_tests.rs | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/input/shortcuts.rs b/crates/ui-egui/src/input/shortcuts.rs index b4c00690cc..94b790efb8 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -906,9 +906,9 @@ fn select_tool_by_key(app: &mut PhotocraftApp, ctx: &egui::Context, pressed: &im if cycle_shift || pressed(k) { let before = app.ui.tool; let mut group: Vec = Tool::ALL.iter().copied().filter(|x| x.key() == t.key() && crate::tool_shortcuts::uses_default(app, *x)).collect(); - // With the Shift-switch preference, Shift+key steps in the toolbar flyout's order; with it off - // plain keys keep the `Tool::ALL` order. - if shift_switch { + // Shift+key (the cycle and the fallback to the group's first tool) uses the toolbar + // flyout's order; plain keys keep the `Tool::ALL` order. + if cycle_shift { group.sort_by_key(|x| crate::panels::toolbar::toolbar_position(*x)); } app.ui.tool = match group.iter().position(|x| *x == app.ui.tool) { diff --git a/crates/ui-egui/src/tool_shift_cycle_order_tests.rs b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs index 017f3777fe..83c3ca33cc 100644 --- a/crates/ui-egui/src/tool_shift_cycle_order_tests.rs +++ b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs @@ -147,3 +147,18 @@ fn with_the_shift_switch_preference_off_plain_w_keeps_the_old_order() { } assert_eq!(seen, vec![Tool::QuickSelection, Tool::ObjectSelection, Tool::MagicWand]); } + +#[test] +fn a_plain_w_from_outside_the_group_keeps_selecting_what_it_selected_before() { + let mut app = app(); + app.ui.tool = Tool::Move; + let ctx = egui::Context::default(); + let raw = egui::RawInput { + events: vec![egui::Event::Key { key: Key::W, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }], + ..Default::default() + }; + ctx.begin_pass(raw); + crate::shortcuts::handle(&mut app, &ctx); + ctx.end_pass().textures_delta.clear(); + assert_eq!(app.ui.tool, Tool::MagicWand, "the first W tool in Tool::ALL, not the first in the flyout"); +}