Skip to content

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

Canvas overlays stay visible while panning (#6592)

Khawar Zamman Wani9b5cb2939551
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 57d357511..440d35521 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -3794,7 +3794,11 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
}
let xf = if crop_camera { ViewXform { center: view.center, rotation: view.rotation, ..xf } } else { xf };
let (img_rect, beyond, layer_edges) = painted.unwrap_or_else(|| paint_canvas(app, ui, &view, xf));
- if primary && !(hand_pan && response.dragged()) {
+ // A Hand drag (the tool, Space or the middle button) keeps every overlay on screen, as in
+ // Photoshop: the Pen path being drawn, paths, guides, grid, transform box (#6592). Only the
+ // tool cursors below give way to the fist.
+ let panning = hand_pan && response.dragged();
+ if primary {
// The transform preview is image content: it goes under the grid and guides, which
// stay visible across a layer being transformed (#5086); the box is drawn later.
crate::transform_tool::draw_content(app, &painter, &xf);
@@ -3857,7 +3861,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
}
command_guide_at(app, tool, d, crate::workspace_ui::sticky_mods(app, ui.input(|i| i.modifiers)))
});
- if let Some((vertical, _)) = guide_hover {
+ if panning {
+ // The fist set with the pan above stays (#2196).
+ } else if let Some((vertical, _)) = guide_hover {
ui.ctx().set_cursor_icon(crate::rulers::guide_cursor(&xf, vertical));
} else if let Some(p) = response.hover_pos()
&& let Some(c) = crate::transform_tool::cursor(app, &xf, xf.to_doc(p), ui.input(|i| i.modifiers.alt || i.modifiers.command))
@@ -4088,10 +4094,6 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
crate::stroke_constraint::draw_line_preview(app, &painter, &xf, p, tool, held.shift);
}
}
- if primary && hand_pan && response.dragged() {
- crate::transform_tool::draw_content(app, &painter, &xf);
- crate::transform_tool::draw_overlay(app, &painter, &xf);
- }
app.tool_override = None;
if let Some(b) = layer_edges_rect(app, idx) {
painter.set(layer_edges, egui::Shape::rect_stroke(xf.doc_rect(b), 0, Stroke::new(1.0, LAYER_EDGES), egui::StrokeKind::Outside));
diff --git a/crates/ui-egui/src/hold_keys/tests.rs b/crates/ui-egui/src/hold_keys/tests.rs
index cfe89d464..f4e006441 100644
--- a/crates/ui-egui/src/hold_keys/tests.rs
+++ b/crates/ui-egui/src/hold_keys/tests.rs
@@ -180,6 +180,50 @@ fn space_is_a_temporary_hand_and_gives_the_tool_back() {
assert!(!st.session.active().unwrap().history.can_undo(), "nothing was painted");
}
+/// The open paths the last frame painted, as their screen points.
+fn painted_paths(h: &Harness<'static, PhotocraftApp>) -> Vec<Vec<Pos2>> {
+ fn walk(s: &egui::Shape, out: &mut Vec<Vec<Pos2>>) {
+ match s {
+ egui::Shape::Path(p) if !p.closed => out.push(p.points.clone()),
+ egui::Shape::Vec(v) => v.iter().for_each(|s| walk(s, out)),
+ _ => {}
+ }
+ }
+ let mut out = Vec::new();
+ for c in &h.output().shapes {
+ walk(&c.shape, &mut out);
+ }
+ out
+}
+
+#[test]
+fn the_pen_path_being_drawn_stays_visible_while_space_pans() {
+ // #6592: the path vanished for as long as the Hand dragged.
+ let mut h = harness(Tool::Pen);
+ click(&mut h, 100.0, 100.0, Modifiers::NONE);
+ click(&mut h, 200.0, 100.0, Modifiers::NONE);
+ assert_eq!(h.state().ui.pen.as_ref().map(|p| p.knots.len()), Some(2));
+ key(&mut h, Key::Space, true, Modifiers::NONE);
+ let before = h.state().ui.views[0].center;
+ press_at(&mut h, 150.0, 200.0);
+ move_to(&mut h, 190.0, 230.0);
+ assert_ne!(h.state().ui.views[0].center, before, "the view is panning");
+ // Mid-drag: the segment is painted where its anchors now are on screen.
+ let (a, b) = (screen(&h, 100.0, 100.0), screen(&h, 200.0, 100.0));
+ let near = |p: &Pos2, q: Pos2| p.distance(q) < 1.0;
+ assert!(
+ painted_paths(&h).iter().any(|pts| pts.first().is_some_and(|p| near(p, a)) && pts.last().is_some_and(|p| near(p, b))),
+ "the Pen path from {a:?} to {b:?} is drawn while panning"
+ );
+ assert_eq!(h.output().platform_output.cursor_icon, crate::tool_cursor::hand(&h.ctx, true), "the fist while panning (#2196)");
+ release(&mut h, 190.0, 230.0, Modifiers::NONE);
+ key(&mut h, Key::Space, false, Modifiers::NONE);
+ h.run_steps(2);
+ let st = h.state();
+ assert_eq!(st.ui.pen.as_ref().map(|p| p.knots.len()), Some(2), "the path is still being drawn");
+ assert_eq!(st.ui.tool, Tool::Pen);
+}
+
#[test]
fn space_while_editing_type_is_not_the_hand_tool() {
let mut h = harness(Tool::Type);