diff --git a/crates/ui-egui/src/state/tool_options.rs b/crates/ui-egui/src/state/tool_options.rs index ea741e4b97..874c444d19 100644 --- a/crates/ui-egui/src/state/tool_options.rs +++ b/crates/ui-egui/src/state/tool_options.rs @@ -122,6 +122,9 @@ pub struct ToolOptions { pub shape_fill_gradient: Option, pub shape_stroke_gradient: Option, pub corner_radius: f32, + /// Shape tools' Align Edges: snap the drawn shape's box to whole pixels. On by default; the + /// Triangle tool shows it, enabled in Shape mode only (#13370); the other shape tools always snap. + pub align_edges: bool, pub polygon_sides: u32, pub line_weight: f32, /// Line arrowheads. @@ -519,6 +522,7 @@ impl Default for ToolOptions { shape_fill_gradient: None, shape_stroke_gradient: None, corner_radius: 0.0, + align_edges: true, polygon_sides: 5, line_weight: 1.0, arrow_start: false, diff --git a/crates/ui-egui/src/tools/shape_options.rs b/crates/ui-egui/src/tools/shape_options.rs index 6d7596492f..761cea5471 100644 --- a/crates/ui-egui/src/tools/shape_options.rs +++ b/crates/ui-egui/src/tools/shape_options.rs @@ -121,6 +121,11 @@ pub(crate) fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool lbl(ui, label); sides_alert = sides_field(ui, label, &mut o.polygon_sides); } + Tool::Triangle => { + crate::widgets::vline(ui, 22.0); + // Only a shape layer has pixel edges to align: greyed in Path mode (and in a pixels mode). + ui.add_enabled_ui(o.shape_mode == "shape", |ui| crate::widgets::checkbox(ui, &mut o.align_edges, tl!("Align Edges"))); + } Tool::Line => { crate::widgets::vline(ui, 22.0); lbl(ui, tl!("Weight:")); diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index cd92901b57..80e0554f8f 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -247,6 +247,9 @@ mod pen_mode_menu_tests; #[cfg(test)] mod ellipse_path_menus_tests; +#[cfg(test)] +mod triangle_align_edges_tests; + pub(crate) use pen::freeform_locked_shape; #[cfg(test)] use pen_resume::pen_finish_on_redo; diff --git a/crates/ui-egui/src/tools/vector_ui/shapes.rs b/crates/ui-egui/src/tools/vector_ui/shapes.rs index ffc3634a49..2143c579c0 100644 --- a/crates/ui-egui/src/tools/vector_ui/shapes.rs +++ b/crates/ui-egui/src/tools/vector_ui/shapes.rs @@ -123,7 +123,11 @@ pub(super) fn shape_geometry(o: &ToolOptions, tool: Tool, start: [f64; 2], end: } let (x0, y0) = if mods.alt { (start[0] - w, start[1] - h) } else { (start[0], start[1]) }; let (w, h) = if mods.alt { (w * 2.0, h * 2.0) } else { (w, h) }; - let rect = [x0.min(x0 + w).round(), y0.min(y0 + h).round(), w.abs().round(), h.abs().round()]; + let rect = [x0.min(x0 + w), y0.min(y0 + h), w.abs(), h.abs()]; + // Align Edges (#13370) is the Triangle tool's option, and only in Shape mode, where its + // checkbox is enabled. The other shape tools, and a greyed checkbox, still snap. + let keep_fraction = tool == Tool::Triangle && o.shape_mode == "shape" && !o.align_edges; + let rect = if keep_fraction { rect } else { rect.map(f64::round) }; if rect[2] < 1.0 || rect[3] < 1.0 { return None; } diff --git a/crates/ui-egui/src/tools/vector_ui/triangle_align_edges_tests.rs b/crates/ui-egui/src/tools/vector_ui/triangle_align_edges_tests.rs new file mode 100644 index 0000000000..903d0b46d7 --- /dev/null +++ b/crates/ui-egui/src/tools/vector_ui/triangle_align_edges_tests.rs @@ -0,0 +1,81 @@ +//! #13370: the Triangle tool's Align Edges checkbox. On (the default) the drawn box snaps to whole +//! pixels; off it keeps the drag's fractional coordinates. It is greyed outside Shape mode. + +use egui::accesskit::Toggled; +use egui_kittest::kittest::Queryable; + +use super::add_anchor_options_bar_tests::harness; +use super::shapes::shape_geometry; +use crate::state::{Tool, ToolOptions}; + +const START: [f64; 2] = [10.3, 10.6]; +const END: [f64; 2] = [40.7, 30.2]; + +fn rect(o: &ToolOptions, tool: Tool, start: [f64; 2], end: [f64; 2]) -> Option<[f64; 4]> { + let g = shape_geometry(o, tool, start, end, egui::Modifiers::NONE)?; + serde_json::from_value(g["rect"].clone()).ok() +} + +#[test] +fn align_edges_is_on_by_default_and_saved() { + assert!(ToolOptions::default().align_edges); + let off = ToolOptions { align_edges: false, ..Default::default() }; + let back: ToolOptions = serde_json::from_str(&serde_json::to_string(&off).unwrap()).unwrap(); + assert!(!back.align_edges, "a saved off survives a round trip"); + let empty: ToolOptions = serde_json::from_str("{}").unwrap(); + assert!(empty.align_edges, "an older file without the field gets the default"); +} + +#[test] +fn triangle_box_snaps_with_align_edges_and_stays_fractional_without() { + let mut o = ToolOptions::default(); + let r = rect(&o, Tool::Triangle, START, END).unwrap(); + assert!(r.iter().all(|v| v.fract() == 0.0), "whole pixels: {r:?}"); + o.align_edges = false; + let r = rect(&o, Tool::Triangle, START, END).unwrap(); + for (got, want) in r.iter().zip([10.3, 10.6, 30.4, 19.6]) { + assert!((got - want).abs() < 1e-9, "{r:?}"); + } +} + +#[test] +fn other_shape_tools_always_snap() { + let o = ToolOptions { align_edges: false, ..Default::default() }; + for tool in [Tool::Polygon, Tool::EllipseShape, Tool::Rectangle, Tool::Star] { + let r = rect(&o, tool, START, END).unwrap(); + assert!(r.iter().all(|v| v.fract() == 0.0), "{tool:?}: {r:?}"); + } +} + +#[test] +fn a_greyed_checkbox_keeps_snapping_in_path_mode() { + let o = ToolOptions { align_edges: false, shape_mode: "path".into(), ..Default::default() }; + let r = rect(&o, Tool::Triangle, START, END).unwrap(); + assert!(r.iter().all(|v| v.fract() == 0.0), "Path mode: {r:?}"); +} + +#[test] +fn a_sub_pixel_triangle_drag_still_draws_nothing() { + let o = ToolOptions { align_edges: false, ..Default::default() }; + assert!(rect(&o, Tool::Triangle, [10.0, 10.0], [10.4, 30.0]).is_none()); + assert!(rect(&o, Tool::Triangle, [10.0, 10.0], [30.0, 10.9]).is_none()); +} + +#[test] +fn options_bar_shows_align_edges_for_the_triangle_only() { + let mut h = harness(Tool::Triangle); + let node = h.get_by_label("Align Edges"); + assert!(!node.accesskit_node().is_disabled(), "enabled in Shape mode"); + assert_eq!(node.accesskit_node().toggled(), Some(Toggled::True)); + h.get_by_label("Align Edges").click(); + h.run_steps(3); + assert!(!h.state().ui.tool_options.align_edges, "clicking toggles the option"); + assert_eq!(h.get_by_label("Align Edges").accesskit_node().toggled(), Some(Toggled::False)); + + h.state_mut().ui.tool_options.shape_mode = "path".into(); + h.run_steps(4); + assert!(h.get_by_label("Align Edges").accesskit_node().is_disabled(), "greyed in Path mode"); + + let h = harness(Tool::Rectangle); + assert!(h.query_by_label("Align Edges").is_none()); +}