Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Guides: Ctrl/Cmd drags a guide with every tool that moves guides, and the cursor shows it (#6288) (#6294)

David Mannc136d2dd3db2
diff --git a/crates/ui-egui/src/artboard_tool.rs b/crates/ui-egui/src/artboard_tool.rs
index 0b12c3612..7f8ee838c 100644
--- a/crates/ui-egui/src/artboard_tool.rs
+++ b/crates/ui-egui/src/artboard_tool.rs
@@ -182,7 +182,9 @@ fn follow(d: &mut ArtboardDrag, p: [f64; 2], mods: egui::Modifiers) {
/// Pointer input for the Artboard tool. Returns true when the event was its own.
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
- if app.ui.tool != Tool::Artboard {
+ // ⌘ is the Move tool (`hold_keys::cmd_moves`): a press with it held is not this tool's,
+ // so it drags a guide or the layer (#6288). A drag already under way keeps its events.
+ if app.ui.tool != Tool::Artboard || (app.ui.artboard_tool.drag.is_none() && crate::canvas::event_tool(app, mods) != Tool::Artboard) {
return false;
}
let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return true };
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index b20f82798..3ebecc75e 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -5363,7 +5363,7 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
if crate::gradient_ui::pointer(app, ev, mods) {
return;
}
- // ⌘ with a selection tool over a guide takes the guide (#2690); the guide drag below moves it.
+ // ⌘ over a guide takes the guide (#2690, #6288); the guide drag below moves it.
if let ToolEvent::Down { x, y, .. } = raw
&& let Some((vertical, i)) = command_guide_at(app, app.active_tool(), [x, y], mods)
{
@@ -5758,13 +5758,16 @@ pub(crate) fn command_moves_layer(app: &PhotocraftApp, tool: Tool, p: [f64; 2],
selection_tool && mods.command && !mods.shift && !floating && app.ui.polygon.is_empty() && !inside_selection(app, p)
}
-/// The guide a ⌘ press with selection tool `tool` at `p` drags (vertical?, index), as the Move
-/// tool's press would (#2690): ⌘ is the Move tool, and a guide under the pointer comes before the
-/// layer or the selected pixels. Not while a polygon or lasso outline is being drawn.
+/// The guide a ⌘ press with `tool` at `p` drags (vertical?, index), as the Move tool's press
+/// would (#2690): ⌘ is the Move tool, and a guide under the pointer comes before the layer or the
+/// selected pixels. The selection tools, and every tool on which ⌘ is the Move tool
+/// (`hold_keys::cmd_moves`, #6288); the canvas shows the guide cursor where this finds a guide.
+/// Not while a polygon or lasso outline, a stroke, Free Transform or type editing is under way.
pub(crate) fn command_guide_at(app: &PhotocraftApp, tool: Tool, p: [f64; 2], mods: egui::Modifiers) -> Option<(bool, usize)> {
let selection_tool = matches!(tool, Tool::RectMarquee | Tool::EllipseMarquee | Tool::Lasso | Tool::PolygonLasso | Tool::MagicWand);
- let idle = app.ui.polygon.is_empty() && !crate::lasso_ui::active(app) && app.ui.transform.is_none();
- (selection_tool && mods.command && idle).then(|| crate::rulers::guide_at(app, p[0], p[1])).flatten()
+ let moves = selection_tool || crate::hold_keys::cmd_moves(tool);
+ let idle = app.ui.polygon.is_empty() && !crate::lasso_ui::active(app) && app.ui.transform.is_none() && app.ui.text_edit.is_none() && app.drag.is_none();
+ (moves && mods.command && idle).then(|| crate::rulers::guide_at(app, p[0], p[1])).flatten()
}
/// Photoshop's cursor over a selection: what a press (or the drag under way) would do.
diff --git a/crates/ui-egui/src/canvas_tool_menu.rs b/crates/ui-egui/src/canvas_tool_menu.rs
index f7f9892d2..43418d5ff 100644
--- a/crates/ui-egui/src/canvas_tool_menu.rs
+++ b/crates/ui-egui/src/canvas_tool_menu.rs
@@ -566,7 +566,8 @@ pub fn open_pasteboard(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos:
}
/// Right-click at document point `doc` on a guide that `tool` could drag (the Move tool, or ⌘ with
-/// a selection tool): select the guide and open the guide menu at screen point `pos`.
+/// a tool on which ⌘ moves guides, `canvas::command_guide_at`): select the guide and open the guide
+/// menu at screen point `pos`.
pub fn open_guide(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, mods: egui::Modifiers, pos: [f32; 2], doc: [f64; 2]) -> bool {
if !pos.iter().all(|v| v.is_finite()) || !doc.iter().all(|v| v.is_finite()) {
return false;
diff --git a/crates/ui-egui/src/frame_tool.rs b/crates/ui-egui/src/frame_tool.rs
index 4a1210b87..b2102b1cf 100644
--- a/crates/ui-egui/src/frame_tool.rs
+++ b/crates/ui-egui/src/frame_tool.rs
@@ -64,7 +64,9 @@ fn content_layer(app: &PhotocraftApp, r: [i64; 4]) -> Option<u64> {
/// Pointer input for the Frame tool. Returns true when the event was its own.
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
- if app.ui.tool != Tool::Frame {
+ // ⌘ is the Move tool (`hold_keys::cmd_moves`): a press with it held is not this tool's,
+ // so it drags a guide or the layer (#6288). A drag already under way keeps its events.
+ if app.ui.tool != Tool::Frame || (app.ui.frame_tool.drag.is_none() && crate::canvas::event_tool(app, mods) != Tool::Frame) {
return false;
}
let (square, centred) = (mods.shift, mods.alt);
diff --git a/crates/ui-egui/src/gradient_ui.rs b/crates/ui-egui/src/gradient_ui.rs
index be35f6602..dd91c1a74 100644
--- a/crates/ui-egui/src/gradient_ui.rs
+++ b/crates/ui-egui/src/gradient_ui.rs
@@ -219,6 +219,11 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->
app.gradient.drag = None;
return false;
}
+ // ⌘ is the Move tool (`hold_keys::cmd_moves`): a press with it held drags a guide or the layer
+ // (#6288). A drag already under way keeps its events.
+ if app.gradient.drag.is_none() && crate::canvas::event_tool(app, mods) != Tool::Gradient {
+ return false;
+ }
let zoom = app.point_zoom().max(0.01);
match ev {
ToolEvent::Down { x, y, .. } => {
diff --git a/crates/ui-egui/src/guide_cursor_tests.rs b/crates/ui-egui/src/guide_cursor_tests.rs
new file mode 100644
index 000000000..fd0e5c059
--- /dev/null
+++ b/crates/ui-egui/src/guide_cursor_tests.rs
@@ -0,0 +1,204 @@
+//! #6288: ⌘ (Ctrl off the Mac) over a guide is the Move tool's guide drag with every tool on
+//! which ⌘ is the Move tool, and the pointer shows it (the guide's double arrow), as in
+//! Photoshop. Driven through real egui input, so the held key goes through `hold_keys`.
+
+use egui::{Modifiers, PointerButton, Pos2, vec2};
+use egui_kittest::Harness;
+use serde_json::json;
+
+use crate::PhotocraftApp;
+use crate::canvas::ViewXform;
+use crate::state::Tool;
+
+fn harness(tool: Tool) -> Harness<'static, PhotocraftApp> {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
+ app.sync_views();
+ app.ui.extras.rulers = false;
+ app.ui.tool = tool;
+ app.run("view.newGuide", json!({"orientation": "vertical", "position": 200})).unwrap();
+ let mut h = Harness::builder().with_size(vec2(1000.0, 700.0)).build_ui_state(
+ |ui, app: &mut PhotocraftApp| {
+ let ctx = ui.ctx().clone();
+ if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
+ return;
+ }
+ crate::shortcuts::handle(app, &ctx);
+ egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
+ },
+ app,
+ );
+ PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
+ h.run_steps(4);
+ let v = &mut h.state_mut().ui.views[0];
+ v.zoom = 1.0;
+ v.center = [200.0, 150.0];
+ v.fit_pending = false;
+ h.run_steps(2);
+ h
+}
+
+fn screen(h: &Harness<'static, PhotocraftApp>, x: f32, y: f32) -> Pos2 {
+ let app = h.state();
+ let v = &app.ui.views[0];
+ ViewXform {
+ rect: crate::rulers::content_rect(app, app.last_canvas_rect),
+ zoom: v.zoom,
+ center: v.center,
+ flip: app.ui.view.flip_horizontal,
+ rotation: v.rotation,
+ aspect: app.ui.view.display_aspect(),
+ }
+ .to_screen(x, y)
+}
+
+fn hover(h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32, m: Modifiers) -> egui::CursorIcon {
+ h.event(egui::Event::ModifiersChanged(m));
+ let p = screen(h, x, y);
+ h.event(egui::Event::PointerMoved(p));
+ h.run_steps(2);
+ h.output().platform_output.cursor_icon
+}
+
+fn drag(h: &mut Harness<'static, PhotocraftApp>, from: f32, to: f32, m: Modifiers) {
+ let p = screen(h, from, 150.0);
+ h.event(egui::Event::PointerMoved(p));
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: m });
+ h.run_steps(1);
+ for k in 1..=4 {
+ let q = screen(h, from + (to - from) * k as f32 / 4.0, 150.0);
+ h.event(egui::Event::PointerMoved(q));
+ h.run_steps(1);
+ }
+ let q = screen(h, to, 150.0);
+ h.event(egui::Event::PointerButton { pos: q, button: PointerButton::Primary, pressed: false, modifiers: m });
+ h.run_steps(2);
+}
+
+fn diagonal(h: &mut Harness<'static, PhotocraftApp>, a: [f32; 2], b: [f32; 2], m: Modifiers) {
+ h.event(egui::Event::ModifiersChanged(m));
+ let p = screen(h, a[0], a[1]);
+ h.event(egui::Event::PointerMoved(p));
+ h.run_steps(1);
+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: m });
+ h.run_steps(1);
+ for k in 1..=4 {
+ let t = k as f32 / 4.0;
+ let q = screen(h, a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t);
+ h.event(egui::Event::PointerMoved(q));
+ h.run_steps(1);
+ }
+ let q = screen(h, b[0], b[1]);
+ h.event(egui::Event::PointerButton { pos: q, button: PointerButton::Primary, pressed: false, modifiers: m });
+ h.run_steps(2);
+}
+
+/// The cursor over the guide with ⌘ held, and where a ⌘-drag from 200 to 240 leaves it.
+fn command_drag(tool: Tool, locked: bool) -> (egui::CursorIcon, Vec<f64>) {
+ let mut h = harness(tool);
+ h.state_mut().ui.extras.lock_guides = locked;
+ let c = hover(&mut h, 200.0, 150.0, Modifiers::COMMAND);
+ drag(&mut h, 200.0, 240.0, Modifiers::COMMAND);
+ (c, h.state().session.active().unwrap().doc.guides.vertical.clone())
+}
+
+const GUIDE: egui::CursorIcon = egui::CursorIcon::ResizeHorizontal;
+
+#[test]
+fn command_over_a_guide_shows_the_guide_cursor_and_drags_it_with_painting_tools() {
+ for tool in [
+ Tool::Brush,
+ Tool::Eraser,
+ Tool::Gradient,
+ Tool::PaintBucket,
+ Tool::CloneStamp,
+ Tool::Eyedropper,
+ Tool::Dodge,
+ Tool::SpotHealing,
+ Tool::Smudge,
+ Tool::QuickSelection,
+ Tool::RectMarquee,
+ Tool::Lasso,
+ ] {
+ assert_eq!(command_drag(tool, false), (GUIDE, vec![240.0]), "{tool:?}");
+ }
+}
+
+/// The Frame and Artboard tools took a ⌘ press as their own (a new frame or board) although ⌘
+/// is the Move tool on them, so the guide cursor promised a drag that never moved the guide.
+#[test]
+fn command_drags_a_guide_with_the_frame_and_artboard_tools() {
+ for tool in [Tool::Frame, Tool::Artboard] {
+ assert_eq!(command_drag(tool, false), (GUIDE, vec![240.0]), "{tool:?}");
+ }
+ // Off a guide, ⌘ is the Move tool too: no frame is drawn.
+ let mut h = harness(Tool::Frame);
+ let layers = h.state().session.active().unwrap().doc.layers.len();
+ diagonal(&mut h, [100.0, 100.0], [160.0, 140.0], Modifiers::COMMAND);
+ assert_eq!(h.state().session.active().unwrap().doc.layers.len(), layers, "⌘-drag with the Frame tool draws no frame");
+ assert!(h.state().ui.frame_tool.drag.is_none());
+ // Without ⌘ the Frame tool still draws its frame.
+ diagonal(&mut h, [20.0, 20.0], [120.0, 90.0], Modifiers::NONE);
+ assert_eq!(h.state().session.active().unwrap().doc.layers.len(), layers + 1, "a plain drag draws a frame");
+}
+
+#[test]
+fn locked_guides_show_no_guide_cursor_and_do_not_move() {
+ for tool in [Tool::Brush, Tool::Eraser, Tool::Frame] {
+ let (cursor, guides) = command_drag(tool, true);
+ assert_ne!(cursor, GUIDE, "{tool:?}");
+ assert_eq!(guides, vec![200.0], "{tool:?}");
+ }
+}
+
+/// Tools that keep ⌘ for themselves (Photoshop: the Pen's Direct Selection, the Hand, Zoom, Crop,
+/// type, shapes, slices) neither show the guide cursor nor drag the guide.
+#[test]
+fn tools_with_their_own_command_keep_it() {
+ for tool in [Tool::Pen, Tool::Hand, Tool::Zoom, Tool::Crop, Tool::Type, Tool::Rectangle, Tool::Slice] {
+ let (cursor, guides) = command_drag(tool, false);
+ assert_ne!(cursor, GUIDE, "{tool:?}");
+ assert_eq!(guides, vec![200.0], "{tool:?}");
+ }
+}
+
+/// Window › Modifier Keys: a latched ⌘ made the press take the guide but left the brush cursor.
+#[test]
+fn sticky_command_shows_the_guide_cursor() {
+ for tool in [Tool::Brush, Tool::Eraser, Tool::RectMarquee] {
+ let mut h = harness(tool);
+ h.state_mut().ui.shell.sticky_command = true;
+ assert_eq!(hover(&mut h, 200.0, 150.0, Modifiers::NONE), GUIDE, "{tool:?}");
+ drag(&mut h, 200.0, 240.0, Modifiers::NONE);
+ assert_eq!(h.state().session.active().unwrap().doc.guides.vertical, vec![240.0], "{tool:?}");
+ }
+}
+
+/// The one decision for "a ⌘ press here takes the guide" outside the Move tool: not without ⌘,
+/// not with a locked guide, not during a stroke, not for a tool that keeps ⌘.
+#[test]
+fn command_guide_at_decides_for_every_tool_that_moves_guides() {
+ use crate::canvas::command_guide_at;
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
+ app.sync_views();
+ app.run("view.newGuide", json!({"orientation": "vertical", "position": 200})).unwrap();
+ let cmd = Modifiers::COMMAND;
+ for tool in [Tool::Brush, Tool::Eraser, Tool::Gradient, Tool::PaintBucket, Tool::Eyedropper, Tool::Frame, Tool::Artboard, Tool::MagicWand] {
+ assert_eq!(command_guide_at(&app, tool, [200.0, 100.0], cmd), Some((true, 0)), "{tool:?}");
+ assert_eq!(command_guide_at(&app, tool, [200.0, 100.0], Modifiers::NONE), None, "{tool:?} without ⌘");
+ assert_eq!(command_guide_at(&app, tool, [120.0, 100.0], cmd), None, "{tool:?} off the guide");
+ }
+ for tool in [Tool::Move, Tool::Pen, Tool::Hand, Tool::Zoom, Tool::Crop, Tool::Type, Tool::Rectangle, Tool::Slice, Tool::PathSelection] {
+ assert_eq!(command_guide_at(&app, tool, [200.0, 100.0], cmd), None, "{tool:?} keeps ⌘");
+ }
+ app.ui.extras.lock_guides = true;
+ assert_eq!(command_guide_at(&app, Tool::Brush, [200.0, 100.0], cmd), None, "locked guides");
+ app.ui.extras.lock_guides = false;
+ // A stroke under way keeps the pointer: ⌘ pressed mid-stroke does not take the guide.
+ app.ui.tool = Tool::Brush;
+ crate::canvas::tool_event(&mut app, crate::canvas::ToolEvent::Down { x: 100.0, y: 100.0, pressure: 1.0 }, Modifiers::NONE);
+ assert!(app.drag.is_some());
+ assert_eq!(command_guide_at(&app, Tool::Brush, [200.0, 100.0], cmd), None, "during a stroke");
+}
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 5948bd4ed..fb2c42eaa 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -2835,6 +2835,9 @@ mod dropdown_press_tests;
#[cfg(test)]
mod marquee_tests;
+#[cfg(test)]
+mod guide_cursor_tests;
+
#[cfg(test)]
mod canvas_menu_tests;
diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs
index 6caa6f00a..a5f4bb0fd 100644
--- a/crates/ui-egui/src/rulers.rs
+++ b/crates/ui-egui/src/rulers.rs
@@ -880,7 +880,17 @@ mod tests {
fn command_drag_over_a_guide_moves_the_guide_with_any_tool() {
use crate::canvas::{ToolEvent, tool_event};
use crate::state::Tool;
- for (tool, locked) in [(Tool::Brush, false), (Tool::RectMarquee, false), (Tool::Lasso, false), (Tool::MagicWand, false), (Tool::RectMarquee, true)] {
+ for (tool, locked) in [
+ (Tool::Brush, false),
+ (Tool::Eraser, false),
+ (Tool::Gradient, false),
+ (Tool::Frame, false),
+ (Tool::Artboard, false),
+ (Tool::RectMarquee, false),
+ (Tool::Lasso, false),
+ (Tool::MagicWand, false),
+ (Tool::RectMarquee, true),
+ ] {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 80, "height": 60, "background": "transparent"})).unwrap();
app.sync_views();