Skip to content

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

Fix #5414: Keep created paths visible and editable (#5424)

Ahmed0_0Mamdouh1f1e8d734fb6
diff --git a/crates/ui-egui/src/path_selection_tests.rs b/crates/ui-egui/src/path_selection_tests.rs
index 8f82e2ad6..b4aaa9e50 100644
--- a/crates/ui-egui/src/path_selection_tests.rs
+++ b/crates/ui-egui/src/path_selection_tests.rs
@@ -247,3 +247,87 @@ fn clicking_shape_with_path_selection_selects_its_layer() {
crate::vector_ui::path_selection_finish(h.state_mut(), [150.0, 90.0], [150.0, 90.0]);
assert_eq!(h.state().session.active().unwrap().active_layer.unwrap().0, rect_layer);
}
+
+/// Work paths remain visible (outline) regardless of the active tool. Before the fix,
+/// `draw_overlay` was gated on `vector_tool` so paths disappeared when switching to Brush, Move,
+/// etc. This test renders the canvas with a Brush tool active and a work path present, then
+/// confirms the path's outline pixels are painted in the accent colour, proving the path is drawn.
+#[test]
+fn path_overlay_drawn_for_non_vector_tools() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
+ app.sync_views();
+ // Place a work path whose top edge runs exactly along y=10 in document space.
+ app.run("path.set", json!({"name": "work", "path": rect_path(50.0, 10.0, 100.0, 80.0)})).unwrap();
+ // Switch to a non-vector tool — before the fix this hid the path outline.
+ app.ui.tool = Tool::Brush;
+
+ let mut h = Harness::builder().with_size(vec2(900.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;
+ }
+ 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(6);
+ // Fit the view so document coordinates map predictably to screen pixels.
+ {
+ let v = &mut h.state_mut().ui.views[0];
+ v.zoom = 2.0; // 2 px/doc-px → the 200×200 doc fills 400×400 screen pixels
+ v.center = [100.0, 100.0];
+ v.fit_pending = false;
+ }
+ h.run_steps(3);
+
+ // Get the accent colour so we know what to look for.
+ let accent = crate::theme::Tokens::get(&h.ctx).accent;
+
+ let xf = crate::canvas::ViewXform::active(h.state()).expect("active document view");
+ let ppp = h.ctx.pixels_per_point();
+ let img = h.render().unwrap();
+
+ // Sample the rendered outline at points derived from the same transform the canvas used. The
+ // top edge is doc-y=10; x=70..130 stays away from the rectangle's anchor squares. A 1 px
+ // egui stroke is anti-aliased over the white canvas, so accept visible blends of the accent.
+ let accent_r = accent.r();
+ let accent_g = accent.g();
+ let accent_b = accent.b();
+ let is_accent_stroke = |pixel: [u8; 4]| {
+ let denom = f32::from(255u8.saturating_sub(accent_r)).max(1.0);
+ let coverage = f32::from(255u8.saturating_sub(pixel[0])) / denom;
+ if !(0.2..=1.05).contains(&coverage) {
+ return false;
+ }
+ let blended = |channel: u8| 255.0 + coverage * (f32::from(channel) - 255.0);
+ (f32::from(pixel[1]) - blended(accent_g)).abs() <= 12.0 && (f32::from(pixel[2]) - blended(accent_b)).abs() <= 12.0
+ };
+
+ let has_accent_near = |at: egui::Pos2| {
+ let cx = (at.x * ppp).round() as i32;
+ let cy = (at.y * ppp).round() as i32;
+ let radius = (2.0 * ppp).ceil() as i32;
+ for y in (cy - radius)..=(cy + radius) {
+ for x in (cx - radius)..=(cx + radius) {
+ if x >= 0 && y >= 0 && (x as u32) < img.width() && (y as u32) < img.height() {
+ let pixel = img.get_pixel(x as u32, y as u32);
+ if is_accent_stroke([pixel[0], pixel[1], pixel[2], pixel[3]]) {
+ return true;
+ }
+ }
+ }
+ }
+ false
+ };
+ let hits = (70..=130).step_by(5).filter(|x| has_accent_near(xf.to_screen(*x as f32, 10.0))).count();
+ assert!(
+ hits >= 10,
+ "expected the work path outline to be visible with Brush tool active, \
+ but found accent-coloured pixels at only {hits} sample point(s) along the transformed top edge \
+ (accent = #{accent_r:02x}{accent_g:02x}{accent_b:02x}); \
+ the path must be drawn regardless of the active tool",
+ );
+}
diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs
index fe6db7407..f68a4a6a4 100644
--- a/crates/ui-egui/src/vector_ui.rs
+++ b/crates/ui-egui/src/vector_ui.rs
@@ -955,7 +955,6 @@ pub(crate) fn draw_handle(painter: &egui::Painter, anchor: Pos2, handle: Pos2, a
/// Work path / active shape path outlines, anchors, and the pen path in progress.
pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform, doc: &Document) {
let tool = app.ui.tool;
- let vector_tool = matches!(tool, Tool::Pen | Tool::FreeformPen | Tool::CurvaturePen | Tool::PathSelection) || is_shape_tool(tool);
let accent = Tokens::get(painter.ctx()).accent;
let to_scr = |q: [f64; 2]| xf.to_screen(q[0] as f32, q[1] as f32);
let draw_path = |p: &Path, anchors: bool| {
@@ -976,16 +975,16 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform
crate::anchor_tools::draw_hover(app, painter, &to_scr, accent, painter.ctx().input(|i| i.modifiers));
}
} else if paths {
- if vector_tool {
- if let Some(wp) = &doc.work_path {
- draw_path(wp, tool == Tool::PathSelection);
- }
- let st = app.session.active();
- if let Some(l) = st.and_then(|s| s.active_layer.and_then(|id| s.doc.layer(id)))
- && let LayerContent::Shape(sh) = &l.content
- {
- draw_path(&sh.path, tool == Tool::PathSelection);
- }
+ // Paths remain visible as outlines with any tool. Path Selection adds anchor squares; Direct
+ // Selection has its own overlay above.
+ if let Some(wp) = &doc.work_path {
+ draw_path(wp, tool == Tool::PathSelection);
+ }
+ let st = app.session.active();
+ if let Some(l) = st.and_then(|s| s.active_layer.and_then(|id| s.doc.layer(id)))
+ && let LayerContent::Shape(sh) = &l.content
+ {
+ draw_path(&sh.path, tool == Tool::PathSelection);
}
// A targeted vector mask shows its path with any tool (#196).
if let Some((_, p)) = targeted_vector_mask(app) {