PhotoCraft: An open-source, photo editor written by scratch in pure Rust
fix(ui): guides draw over the Free Transform preview, under its box (#5086) (#5214)
Sabbir Ahmed4dec65f4ceb4
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 2e56d2528..3af62be74 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3642,6 +3642,9 @@ 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()) {+ // 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);if app.ui.extras.grid && app.ui.view.extras {crate::rulers::draw_grid(app, &painter, &xf, &doc);}@@ -3924,6 +3927,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}}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;@@ -7370,6 +7374,51 @@ mod tests {assert_ne!(dragged.center, center0, "a secondary window's Hand pans while transforming");}+ #[test]+ fn guides_draw_over_the_transform_preview_and_under_its_box() {+ // #5086: a guide crossing a layer under Free Transform was hidden behind the preview of+ // the moving pixels. The preview is image content, drawn before the grid and guides like+ // the document is; the box and handles still go over them, as in Photoshop.+ let ctx = egui::Context::default();+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());+ app.run("file.new", json!({"width": 400, "height": 300})).unwrap();+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("select.rect", json!({"x": 100, "y": 100, "width": 100, "height": 60})).unwrap();+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();+ app.run("select.deselect", json!({})).unwrap();+ app.run("view.newGuide", json!({"orientation": "vertical", "position": 150})).unwrap();+ app.ui.extras.guides = true;+ app.ui.extras.rulers = false;+ app.sync_views();+ crate::transform_tool::begin(&mut app, &ctx).expect("a transform starts");+ let mut view = app.ui.views[0].clone();+ view.zoom = 1.0;+ view.center = [200.0, 150.0];+ view.fit_pending = false;+ view.fill_pending = false;+ let rect = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));+ let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() };+ let mut out = ctx.run_ui(input, |ui| {+ canvas_view(&mut app, ui, 0, rect, view.clone(), true);+ });+ out.textures_delta.clear();+ // At 100 % centred on the document, screen and document coordinates coincide.+ let layer = Rect::from_min_max(pos2(100.0, 100.0), pos2(200.0, 160.0));+ let near = |r: Rect| (r.min - layer.min).length() < 1.0 && (r.max - layer.max).length() < 1.0;+ let position = |pred: &dyn Fn(&egui::Shape) -> bool| out.shapes.iter().position(|s| pred(&s.shape));+ // The moving pixels: the mesh that spans the layer's box.+ let preview = position(&|s| matches!(s, egui::Shape::Mesh(m) if near(m.calc_bounds())));+ // The guide: a vertical line at x = 150.+ let guide = position(&|s| matches!(s, egui::Shape::LineSegment { points, .. } if points.iter().all(|p| (p.x - 150.0).abs() < 0.01)));+ // The box: the closed four-point outline round the layer.+ let frame = position(&|s| matches!(s, egui::Shape::Path(p) if p.closed && p.points.len() == 4 && near(Rect::from_points(&p.points))));+ let (Some(preview), Some(guide), Some(frame)) = (preview, guide, frame) else {+ panic!("all painted: preview {preview:?}, guide {guide:?}, box {frame:?}");+ };+ assert!(preview < guide, "the guide ({guide}) draws over the transform preview ({preview})");+ assert!(guide < frame, "the box ({frame}) draws over the guide ({guide})");+ }+#[test]fn brush_drags_paint_no_stand_in_shape_over_the_canvas() {// #189: v0.1 drew the stroke being dragged as a foreground-coloured egui polyline as widediff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex 1cd81f3a3..000a943ad 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -2048,19 +2048,14 @@ fn front_vertices(h: &Homography, n: usize) -> Vec<bool> {(0..=n).flat_map(|j| (0..=n).map(move |i| (i as f64 / n as f64, j as f64 / n as f64))).map(|(u, v)| front * h.w(u, v) > 1e-9).collect()}-/// Warped preview of the moving pixels plus the box, handles and reference point.-pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {+/// The moving pixels as the box shows them (warped, skewed or styled), plus the layers above+/// them. This is image content: the canvas draws it right after the document, under the grid+/// and guides, so a guide stays visible across a layer being transformed (#5086), as in+/// Photoshop. The box, handles and reference point are [`draw_overlay`], drawn over the guides.+pub fn draw_content(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {let (Some(t), Some(pv)) = (&app.ui.transform, &app.transform_preview) else { return };- let scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);if let Some(w) = &t.warp {- let over_canvas = painter.ctx().input(|i| i.pointer.hover_pos()).filter(|p| xf.rect.contains(*p)).map(|p| xf.to_doc(p));- let at = pv.split_pointer.or(over_canvas);- let alt = painter.ctx().input(|i| i.modifiers.alt || i.modifiers.command);- let guide = at.filter(|_| !warps_type(app)).and_then(|p| {- let tool = pv.split_tool.or_else(|| (alt || pv.split_quick).then(|| quick_split_kind(&w.to_mesh(1, 1), w.bounds, p)));- tool.map(|tool| (tool, p))- });- draw_warp(painter, xf, t, w, pv, guide);+ draw_warp_content(painter, xf, t, w, pv);return;}let ctx = painter.ctx();@@ -2108,6 +2103,24 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXformif let Some(a) = &pv.above {a.draw(painter, xf);}+}++/// The box, handles, reference point and path outline over the preview that [`draw_content`]+/// drew; the warp grid and its split guides for a warp.+pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform) {+ let (Some(t), Some(pv)) = (&app.ui.transform, &app.transform_preview) else { return };+ let scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);+ if let Some(w) = &t.warp {+ let over_canvas = painter.ctx().input(|i| i.pointer.hover_pos()).filter(|p| xf.rect.contains(*p)).map(|p| xf.to_doc(p));+ let at = pv.split_pointer.or(over_canvas);+ let alt = painter.ctx().input(|i| i.modifiers.alt || i.modifiers.command);+ let guide = at.filter(|_| !warps_type(app)).and_then(|p| {+ let tool = pv.split_tool.or_else(|| (alt || pv.split_quick).then(|| quick_split_kind(&w.to_mesh(1, 1), w.bounds, p)));+ tool.map(|tool| (tool, p))+ });+ draw_warp_overlay(painter, xf, w, pv, guide);+ return;+ }if pv.committing.is_some() {return;}@@ -2338,8 +2351,8 @@ fn draw_warp_points(painter: &egui::Painter, m: &BezierMesh, scr: impl Fn([f64;}}-fn draw_warp(painter: &egui::Painter, xf: &ViewXform, t: &TransformSession, w: &Warp, pv: &TransformPreview, guide: Option<(SplitTool, [f64; 2])>) {- let scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);+/// The warped pixels and the layers above them ([`draw_content`] for a warp).+fn draw_warp_content(painter: &egui::Painter, xf: &ViewXform, t: &TransformSession, w: &Warp, pv: &TransformPreview) {let r = t.rect;let m = w.to_mesh(1, 1);let (us, vs) = crate::warp_preview::samples(&m, xf.zoom as f64);@@ -2359,6 +2372,12 @@ fn draw_warp(painter: &egui::Painter, xf: &ViewXform, t: &TransformSession, w: &if let Some(a) = &pv.above {a.draw(painter, xf);}+}++/// The warp grid, its control points and split guides ([`draw_overlay`] for a warp).+fn draw_warp_overlay(painter: &egui::Painter, xf: &ViewXform, w: &Warp, pv: &TransformPreview, guide: Option<(SplitTool, [f64; 2])>) {+ let scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);+ let m = w.to_mesh(1, 1);let accent = crate::theme::Tokens::get(painter.ctx()).accent;// Transform Path › Warp: the path drawn through the warp.if let Some(path) = &pv.path {@@ -4390,12 +4409,12 @@ mod tests {assert!(pv.texture.size()[0] as f64 >= t.rect[2] - t.rect[0]);}- /// The textured meshes `draw_overlay` paints for the moving pixels (not the box or handles).+ /// The textured meshes `draw_content` paints for the moving pixels (not the box or handles).fn preview_meshes(app: &PhotocraftApp, xf: &ViewXform) -> Vec<egui::Mesh> {let ctx = gpu_ctx();let mut out = ctx.run_ui(egui::RawInput { max_texture_side: Some(16384), ..Default::default() }, |ui| {let painter = ui.ctx().layer_painter(egui::LayerId::background());- draw_overlay(app, &painter, xf);+ draw_content(app, &painter, xf);});out.textures_delta.clear();out.shapes@@ -4523,6 +4542,7 @@ mod tests {rotation: 0.0,aspect: 1.0,};+ draw_content(&app, &painter, &xf);draw_overlay(&app, &painter, &xf);});t0.elapsed().as_secs_f64() * 1e3