diff --git a/crates/ui-egui/src/vector_paint_tests.rs b/crates/ui-egui/src/vector_paint_tests.rs index c62277fb1e..321a106cb0 100644 --- a/crates/ui-egui/src/vector_paint_tests.rs +++ b/crates/ui-egui/src/vector_paint_tests.rs @@ -62,6 +62,101 @@ fn pointer(h: &mut Harness<'_, PhotocraftApp>, pos: Pos2, pressed: bool) { h.step(); } +fn stroke_width(h: &mut Harness<'_, PhotocraftApp>, width: &str) { + h.get_all_by_role(egui::accesskit::Role::SpinButton).next().expect("the stroke width").click(); + h.run(); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.event(Event::Text(width.into())); + h.key_press(Key::Enter); + h.run_steps(3); +} + +/// #6191: passing through zero hides the stroke without discarding its paint or style. +#[test] +fn zero_width_preserves_selected_shape_stroke_and_restores_it() { + for depth in [8, 16, 32] { + for (properties, tool) in [(true, Tool::Rectangle), (false, Tool::Rectangle), (false, Tool::PathSelection), (false, Tool::DirectSelection)] { + let mut app = app("rgb", depth); + app.ui.tool = tool; + app.run( + "shape.create", + json!({"rect": [20, 20, 80, 60], "fill": null, + "stroke": {"width": 4, "color": [0.123456, 0.345678, 0.567891, 0.618237], + "align": "inside", "cap": "round", "join": "bevel", "miterLimit": 7, + "dashes": [3, 2], "dashOffset": 1.25, "opacity": 63}}), + ) + .unwrap(); + let original = shape(&app).clone(); + let history = app.session.active().unwrap().history.past_len(); + let mut h = harness(app, properties); + stroke_width(&mut h, "0"); + let zero = shape(h.state()).clone(); + let mut expected = original.stroke.clone().unwrap(); + expected.width = 0.0; + assert_eq!(zero.stroke, Some(expected), "{tool:?}, Properties={properties}, depth={depth}: zero width retains the stroke"); + assert_eq!(zero.path, original.path); + assert!(zero.cache.as_ref().unwrap().content_bounds().is_empty(), "a zero-width stroke paints nothing"); + assert_eq!(h.state().session.active().unwrap().history.past_len(), history + 1); + + // A save at zero must retain the dormant stroke too. + let st = h.state().session.active().unwrap(); + let bytes = photocraft_format::save_to_bytes(&st.doc, &Default::default()).unwrap(); + let loaded = photocraft_format::load_from_bytes(&bytes).unwrap(); + let LayerContent::Shape(saved) = &loaded.layer(st.active_layer.unwrap()).unwrap().content else { panic!("saved shape") }; + assert_eq!(saved.stroke, zero.stroke); + + stroke_width(&mut h, "4"); + assert_eq!(shape(h.state()), &original, "restoring width restores the complete style and rendered pixels"); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(shape(h.state()), &zero); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(shape(h.state()), &original); + h.state_mut().run("edit.redo", json!({})).unwrap(); + assert_eq!(shape(h.state()), &zero); + h.state_mut().run("edit.redo", json!({})).unwrap(); + assert_eq!(shape(h.state()), &original); + } + } +} + +#[test] +fn zero_width_style_can_be_inspected_removed_or_reenabled_explicitly() { + for properties in [true, false] { + let mut app = app("rgb", 8); + app.run("shape.create", json!({"rect": [20, 20, 80, 60], "stroke": {"width": 4, "color": "#e73581", "dashes": [3, 2]}})).unwrap(); + let paint = shape(&app).stroke.as_ref().unwrap().paint.clone(); + let mut h = harness(app, properties); + stroke_width(&mut h, "0"); + let zero = shape(h.state()).clone(); + h.get_by_label("Stroke Options").click(); + h.run_steps(3); + assert_eq!(h.state().ui.stroke_editor.as_ref().unwrap().draft.dashes, [3.0, 2.0], "zero width still exposes the saved options"); + h.key_press(Key::Escape); + h.run_steps(2); + assert_eq!(shape(h.state()), &zero, "merely inspecting the style does not enable it"); + + h.get_by_label(if properties { "Set shape stroke type" } else { "Stroke color" }).click(); + h.run_steps(3); + h.get_by_label("No Color").click(); + h.run_steps(2); + assert!(shape(h.state()).stroke.is_none(), "No Color still removes the stroke"); + h.key_press(Key::Escape); + h.run_steps(2); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(shape(h.state()), &zero); + h.run_steps(2); + + h.get_by_label("Stroke Options").click(); + h.run_steps(3); + h.get_by_role_and_label(egui::accesskit::Role::RadioButton, "Dotted").click(); + h.run_steps(3); + let stroke = shape(h.state()).stroke.as_ref().unwrap(); + assert_eq!(stroke.width, 3.0, "a deliberate style choice enables the dormant stroke"); + assert_eq!(stroke.paint, paint); + assert_eq!(stroke.dashes, [0.0, 2.0]); + } +} + #[test] fn fill_and_stroke_swatches_open_with_pointer_and_keyboard_without_changing_paint() { for label in ["Fill color", "Stroke color"] { diff --git a/crates/ui-egui/src/vector_ui.rs b/crates/ui-egui/src/vector_ui.rs index 99363fbcd6..aca0825f67 100644 --- a/crates/ui-egui/src/vector_ui.rs +++ b/crates/ui-egui/src/vector_ui.rs @@ -1208,8 +1208,11 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo None => (o.shape_fill, o.shape_fill.then(|| solid_paint(fg)), (o.stroke_width > 0.0).then(|| o.shape_stroke.stroke(o.stroke_width, solid_paint(bg)))), }; let mut width = stroke.as_ref().map_or(0.0, |s| s.width); + let stroke_enabled = stroke.as_ref().is_some_and(|s| s.width > 0.0); let style = stroke.as_ref().map_or_else(|| o.shape_stroke.clone(), crate::shape_stroke_ui::StrokeOptions::from); - let key = |k: &str| target.as_ref().map(|s| format!("shape-options-{:?}-{}-{k}", s.doc, s.layer.0)); + let gesture_key = ui.id().with("shape-options-paint-gesture"); + let gesture = paint_gesture(ui, gesture_key); + let key = |k: &str| target.as_ref().map(|s| format!("shape-options-{:?}-{}-{k}:{}:{gesture}", s.doc, s.layer.0, gesture_key.value())); let mut edit: Option = None; lbl(ui, tl!("Fill:")); if crate::widgets::checkbox(ui, &mut has_fill, "").changed() { @@ -1225,15 +1228,17 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo if stroke.is_none() { p["color"] = json!(bg); } - edit = Some(if width <= 0.0 { json!({"stroke": null}) } else { json!({"stroke": p, "coalesce": key("stroke-w")}) }); + edit = Some(json!({"stroke": p, "coalesce": key("stroke-w")})); } let preview = target.as_ref().filter(|s| s.stroke.is_some()).map(|s| (s.doc, s.layer)); if let Some(change) = - crate::shape_stroke_ui::button(ui, "tool-stroke", &style, &mut app.ui.stroke_editor, true, tool != Tool::EllipseShape, preview, stroke.is_some()) + crate::shape_stroke_ui::button(ui, "tool-stroke", &style, &mut app.ui.stroke_editor, true, tool != Tool::EllipseShape, preview, stroke_enabled) { let mut p = change.options.params(); - if stroke.is_none() { + if !stroke_enabled { p["width"] = json!(3.0); + } + if stroke.is_none() { p["color"] = json!(bg); } edit = Some(json!({"stroke": p, "coalesce": change.coalesce})); @@ -1304,6 +1309,12 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo app.ui.status = e; app.ui.status_error = true; } + if !ui.input(|i| i.pointer.any_down()) { + ui.data_mut(|d| { + let gesture = d.get_temp_mut_or_default::<(u64, u64)>(gesture_key); + gesture.0 = gesture.0.wrapping_add(1); + }); + } } true } @@ -1544,8 +1555,7 @@ pub fn shape_properties(app: &mut PhotocraftApp, ui: &mut egui::Ui, id: photocra } let mut w = sh.stroke.as_ref().map_or(0.0, |s| s.width); if crate::widgets::value_field(ui, &mut w, 0.0..=288.0, "px", 60.0).changed() { - let stroke = if w <= 0.0 { Value::Null } else { json!({"width": w}) }; - edit = Some(json!({"stroke": stroke, "coalesce": coalesce("stroke-w")})); + edit = Some(json!({"stroke": {"width": w}, "coalesce": coalesce("stroke-w")})); } }); let style = sh.stroke.as_ref().map(crate::shape_stroke_ui::StrokeOptions::from).unwrap_or_default(); diff --git a/docs/shape-strokes.md b/docs/shape-strokes.md index 835eff76c2..61305c07b8 100644 --- a/docs/shape-strokes.md +++ b/docs/shape-strokes.md @@ -1,6 +1,6 @@ # Shape stroke options -Updated 2026-10-11: direct editing, compact layout, and clickable colour/style controls. +Updated 2026-10-15: retain stroke paint and options when width reaches zero (#6191). PhotoCraft's Shape tool options bar has a Stroke Options button beside the existing width and colour controls. It sets defaults for new Rectangle, Ellipse, Triangle, Polygon, Line @@ -15,6 +15,11 @@ colour or style deliberately enables a 3 px stroke when no stroke is present. Th remains available with no stroke, but opening or dismissing it alone does not enable one. Properties edits the selected shape; the options bar continues to set defaults for new shapes. +Setting an existing shape's stroke width to 0 px hides the stroke while retaining its paint, +opacity and geometry options. Raising the width restores the same style, including after a +native save and reopen. No Color still removes the stroke; deliberately choosing a colour or +style enables a zero-width stroke at 3 px. Width edits remain one undo step per numeric drag. + The editor offers Solid, Dashed (4 widths on, 2 off), Dotted (zero-length dashes with round caps, spaced every 2 widths), and custom repeating dash/gap sequences. Dash lengths and offsets are multiples of the stroke width; width itself is in document pixels. Odd-length