Skip to content

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

Guides: ⌘-drag over a guide moves it with the selection tools too (#2690) (#2699)

Valentin Klein9f82e6522fb9
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index aed97a4f7..223826e5a 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2882,9 +2882,12 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
crate::gradient_ui::draw_overlay(app, &painter, &xf);
crate::slice_ui::draw_overlay(app, &painter, &xf);
// Tool cursors (Photoshop-style).
- let guide_hover = response.hover_pos().filter(|_| tool == Tool::Move).and_then(|p| {
+ let guide_hover = response.hover_pos().and_then(|p| {
let d = xf.to_doc(p);
- crate::rulers::guide_at(app, d[0], d[1])
+ if tool == Tool::Move {
+ return crate::rulers::guide_at(app, d[0], d[1]);
+ }
+ command_guide_at(app, tool, d, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))
});
if let Some((vertical, _)) = guide_hover {
ui.ctx().set_cursor_icon(if vertical { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::ResizeVertical });
@@ -3805,6 +3808,14 @@ 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.
+ if let ToolEvent::Down { x, y, .. } = raw
+ && let Some((vertical, i)) = command_guide_at(app, app.active_tool(), [x, y], mods)
+ {
+ app.guide_drag = Some(crate::rulers::GuideDrag { vertical, index: Some(i), pos: if vertical { x } else { y } });
+ crate::snap_ui::begin_guide(app, [x, y]);
+ return;
+ }
if crate::lasso_ui::pointer(app, ev, mods) {
return;
}
@@ -4133,6 +4144,15 @@ 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.
+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()
+}
+
/// Photoshop's cursor over a selection: what a press (or the drag under way) would do.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SelCursor {
diff --git a/crates/ui-egui/src/lasso_ui.rs b/crates/ui-egui/src/lasso_ui.rs
index 7b5a3a687..67d5ace98 100644
--- a/crates/ui-egui/src/lasso_ui.rs
+++ b/crates/ui-egui/src/lasso_ui.rs
@@ -53,6 +53,12 @@ fn moving_selection(app: &PhotocraftApp) -> bool {
app.drag.as_ref().is_some_and(|d| d.tool == Tool::Lasso && d.sel_move.is_some())
}
+/// A drag the canvas handles rather than this module, which only feeds it the events: moving the
+/// selection, or a guide that ⌘ took (`canvas::command_guide_at`, #2690).
+fn handed_over(app: &PhotocraftApp) -> bool {
+ moving_selection(app) || app.guide_drag.is_some_and(|d| d.index.is_some())
+}
+
pub fn cancel_stale(app: &mut PhotocraftApp) {
if app
.drag
@@ -100,7 +106,7 @@ pub fn commit(app: &mut PhotocraftApp) {
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -> bool {
cancel_stale(app);
- if app.ui.tool != Tool::Lasso || moving_selection(app) {
+ if app.ui.tool != Tool::Lasso || handed_over(app) {
return false;
}
let p = match ev {
@@ -192,13 +198,13 @@ pub fn canvas_input(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXform
let p = xf.to_doc(pos);
if pressed && response.contains_pointer() && response.rect.contains(pos) {
Some(ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 })
- } else if !pressed && (active(app) || moving_selection(app)) {
+ } else if !pressed && (active(app) || handed_over(app)) {
Some(ToolEvent::Up { x: p[0], y: p[1] })
} else {
None
}
}
- Event::PointerMoved(pos) if active(app) || moving_selection(app) => {
+ Event::PointerMoved(pos) if active(app) || handed_over(app) => {
let p = xf.to_doc(pos);
Some(ToolEvent::Move { x: p[0], y: p[1], pressure: 1.0 })
}
diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rs
index 0bd6132d9..432b407d7 100644
--- a/crates/ui-egui/src/rulers.rs
+++ b/crates/ui-egui/src/rulers.rs
@@ -429,6 +429,40 @@ mod tests {
assert_eq!(guide_at(&app, 0.0, 0.0), None);
}
+ /// ⌘ is the Move tool with any tool, so a ⌘-drag over a guide moves the guide, never the layer
+ /// (#2690); with Lock Guides a ⌘-drag with a marquee moves the layer as before.
+ #[test]
+ 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)] {
+ 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();
+ app.ui.tool = tool;
+ app.ui.extras.lock_guides = locked;
+ let layer = app.session.active().unwrap().active_layer.unwrap();
+ app.session
+ .edit("paint", |doc, _| {
+ doc.layer_mut(layer).unwrap().surface_mut().unwrap().fill_rect(photocraft_geom::Rect::new(10, 10, 30, 30), &[1.0, 0.0, 0.0, 1.0]);
+ Ok(())
+ })
+ .unwrap();
+ app.run("view.newGuide", json!({"orientation": "vertical", "position": 50})).unwrap();
+ let cmd = egui::Modifiers::COMMAND;
+ tool_event(&mut app, ToolEvent::Down { x: 50.0, y: 40.0, pressure: 1.0 }, cmd);
+ tool_event(&mut app, ToolEvent::Move { x: 65.0, y: 40.0, pressure: 1.0 }, cmd);
+ tool_event(&mut app, ToolEvent::Up { x: 65.0, y: 40.0 }, cmd);
+ let doc = &app.session.active().unwrap().doc;
+ let moved = doc.layer(layer).unwrap().surface().unwrap().rgba(12, 12)[3] == 0.0;
+ if locked {
+ assert_eq!((doc.guides.vertical.clone(), moved), (vec![50.0], true), "{tool:?}, guides locked: the layer moves");
+ } else {
+ assert_eq!((doc.guides.vertical.clone(), moved), (vec![65.0], false), "{tool:?}: the guide moves, the layer stays");
+ }
+ }
+ }
+
#[test]
fn ruler_range_is_normalised_when_flipped() {
// #1814: with View > Flip Horizontal, `to_doc` negates x, so the raw
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index 4e37a9a5b..12d59f277 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -226,6 +226,14 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
app.prefs_rt.snap = Some(ActiveSnap { gesture, start: p, targets, smart, disabled: false });
}
+/// Snap a guide drag that ⌘ with a selection tool starts at `p` (`canvas::command_guide_at`,
+/// #2690) as the Move tool's guide drag: never to guides, so not back onto itself.
+pub fn begin_guide(app: &mut PhotocraftApp, p: [f64; 2]) {
+ app.prefs_rt.snap_lines.clear();
+ let targets = if snap_on(app) { build(app, &[], false).filtered(|k| k != SnapKind::Guide) } else { SnapTargets::default() };
+ app.prefs_rt.snap = Some(ActiveSnap { gesture: Gesture::Guide, start: p, targets, smart: SnapTargets::default(), disabled: false });
+}
+
/// Round to whole pixels when Preferences › Tools asks vector tools and transforms to snap to
/// the pixel grid.
fn pixel_round(app: &PhotocraftApp, p: [f64; 2]) -> [f64; 2] {