From 05d7ac607ab37ab6efccd33a261ed1ee9c0ea706 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 12:29:06 -0500 Subject: [PATCH] Select and Mask: E switches the tool mode between add and subtract (#11402) The reference application's default key "Cycle Tool Mode: E" had no binding in the Select and Mask workspace. E now flips the shared add / subtract mode when an editing tool is active, and the options bar's + and - buttons follow. Hand and Zoom, and E with any modifier held, do nothing. Co-Authored-By: Claude Fable 5.1 --- crates/ui-egui/src/dialogs/select_mask_ui.rs | 7 ++ .../select_mask_ui/cycle_tool_mode_tests.rs | 113 ++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 crates/ui-egui/src/dialogs/select_mask_ui/cycle_tool_mode_tests.rs diff --git a/crates/ui-egui/src/dialogs/select_mask_ui.rs b/crates/ui-egui/src/dialogs/select_mask_ui.rs index ebef97f740..a37d94d5a8 100644 --- a/crates/ui-egui/src/dialogs/select_mask_ui.rs +++ b/crates/ui-egui/src/dialogs/select_mask_ui.rs @@ -1408,6 +1408,11 @@ pub fn keys(app: &mut PhotocraftApp, ctx: &egui::Context) { w.pick(t); } } + // E cycles the tool mode (add, subtract); Hand and Zoom edit nothing, so it leaves them alone. + let bare_e = ctx.input(|i| i.events.iter().any(|e| matches!(e, egui::Event::Key { key: Key::E, pressed: true, modifiers, .. } if modifiers.is_none()))); + if bare_e && w.tool.edits() && pressed(Key::E, Modifiers::NONE) { + w.add = !w.add; + } if pressed(Key::OpenBracket, Modifiers::NONE) { let s = w.size(); w.set_size((s * 0.9).min(s - 1.0)); @@ -1973,5 +1978,7 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) { } } +#[cfg(test)] +mod cycle_tool_mode_tests; #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/dialogs/select_mask_ui/cycle_tool_mode_tests.rs b/crates/ui-egui/src/dialogs/select_mask_ui/cycle_tool_mode_tests.rs new file mode 100644 index 0000000000..94843aaffc --- /dev/null +++ b/crates/ui-egui/src/dialogs/select_mask_ui/cycle_tool_mode_tests.rs @@ -0,0 +1,113 @@ +use super::*; +use egui::{Key, Modifiers}; + +fn opened() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.session.execute("file.new", json!({"width": 160, "height": 100, "depth": 8})).unwrap(); + app.session.execute("select.rect", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); + app.session.prefs.edit(|p| { + p.dialogs.remove(REMEMBERED); + }); + app.sync_views(); + open(&mut app).unwrap(); + app +} + +fn ws(app: &PhotocraftApp) -> &SelectMaskWorkspace { + app.distort.select_mask.as_ref().unwrap() +} + +fn ui_selected(app: &mut PhotocraftApp) -> u64 { + control(app, &json!({"waitPreview": true})).unwrap()["selectedProxyPixels"].as_u64().unwrap() +} + +fn press(app: &mut PhotocraftApp, ctx: &egui::Context, key: Key, modifiers: Modifiers) { + let ev = egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers }; + let input = egui::RawInput { events: vec![ev], ..Default::default() }; + ctx.run_ui(input, |ui| keys(app, ui.ctx())).textures_delta.clear(); +} + +#[test] +fn e_switches_between_add_and_subtract_and_back() { + let mut app = opened(); + let ctx = egui::Context::default(); + assert!(ws(&app).add); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(!ws(&app).add); + assert_eq!(ws(&app).describe()["mode"].as_str(), Some("subtract")); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(ws(&app).add); + assert_eq!(ws(&app).describe()["mode"].as_str(), Some("add")); +} + +#[test] +fn e_works_with_every_editing_tool_and_keeps_the_tool() { + let ctx = egui::Context::default(); + for tool in [SmTool::Quick, SmTool::RefineEdge, SmTool::Brush, SmTool::Object, SmTool::Lasso, SmTool::PolygonalLasso] { + let mut app = opened(); + app.distort.select_mask.as_mut().unwrap().pick(tool); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(!ws(&app).add, "{tool:?}"); + assert_eq!(ws(&app).tool, tool); + } +} + +#[test] +fn e_does_nothing_with_hand_or_zoom() { + let ctx = egui::Context::default(); + for tool in [SmTool::Hand, SmTool::Zoom] { + let mut app = opened(); + app.distort.select_mask.as_mut().unwrap().pick(tool); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(ws(&app).add, "{tool:?}"); + } +} + +#[test] +fn e_with_command_alt_or_shift_does_nothing_new() { + let ctx = egui::Context::default(); + for m in [Modifiers::COMMAND, Modifiers::ALT, Modifiers::SHIFT, Modifiers::CTRL] { + let mut app = opened(); + press(&mut app, &ctx, Key::E, m); + assert!(ws(&app).add, "{m:?}"); + } +} + +#[test] +fn e_is_ignored_while_a_popup_has_the_keys() { + let mut app = opened(); + let ctx = egui::Context::default(); + ctx.run_ui(egui::RawInput::default(), |ui| { + egui::Popup::open_id(ui.ctx(), egui::Id::unique("sm-test-popup")); + }) + .textures_delta + .clear(); + assert!(egui::Popup::is_any_open(&ctx)); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(ws(&app).add); +} + +#[test] +fn e_chooses_the_mode_alt_swaps_while_held() { + let mut app = opened(); + let ctx = egui::Context::default(); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(!ws(&app).add); + // Alt held on top of subtract: the stroke adds, so the pixel under it becomes selected. + let before = ui_selected(&mut app); + let stroke = json!({"tool": "brush", "size": 10, "hardness": 50, "stroke": {"points": [[100.0, 10.0], [100.0, 90.0]], "alt": true}}); + control(&mut app, &stroke).unwrap(); + assert!(!ws(&app).add, "Alt does not change the chosen mode"); + assert!(ui_selected(&mut app) > before, "Alt over subtract adds"); +} + +#[test] +fn a_polygon_in_progress_keeps_its_starting_mode() { + let mut app = opened(); + let ctx = egui::Context::default(); + app.distort.select_mask.as_mut().unwrap().pick(SmTool::PolygonalLasso); + app.distort.select_mask.as_mut().unwrap().poly = Some((true, vec![[10.0, 10.0], [50.0, 10.0]])); + press(&mut app, &ctx, Key::E, Modifiers::NONE); + assert!(!ws(&app).add); + assert_eq!(ws(&app).poly.as_ref().map(|(a, _)| *a), Some(true)); +}