diff --git a/crates/engine/src/shell_commands.rs b/crates/engine/src/shell_commands.rs index 6e8eeac1d7..3c76e151b7 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..94b790efb8 100644 --- a/crates/ui-egui/src/input/shortcuts.rs +++ b/crates/ui-egui/src/input/shortcuts.rs @@ -905,7 +905,12 @@ 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 (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) { 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 6a18440c6e..4a855e1138 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -798,6 +798,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)] mod toolbar_group_order_tests; diff --git a/crates/ui-egui/src/panels/toolbar.rs b/crates/ui-egui/src/panels/toolbar.rs index 933b67c04b..839e461177 100644 --- a/crates/ui-egui/src/panels/toolbar.rs +++ b/crates/ui-egui/src/panels/toolbar.rs @@ -51,6 +51,12 @@ pub(crate) 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 5adedaf3c8..8dda7ce231 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..83c3ca33cc --- /dev/null +++ b/crates/ui-egui/src/tool_shift_cycle_order_tests.rs @@ -0,0 +1,164 @@ +//! 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:?}"); + } +} + +#[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]); +} + +#[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"); +} 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);