From 24f34d150d3f4e6ae2ef63c63e4c5c117da5722f Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Tue, 17 Nov 2026 01:09:49 +0100 Subject: [PATCH 1/3] Rectangle and Triangle tools: the shape and path canvas menu (#13341, #13367) A right-click on the canvas with either tool opens the shape menu the Line tool has: in Shape mode over a pixel or shape layer, and in Path mode over a pixel layer with the Work Path shown, as both issues measured. The Line keeps opening nothing in Path mode. A Rectangle right-click on a shape or on a corner radius widget, which #13341 left unmeasured, opens nothing as before. --- crates/ui-egui/src/canvas/canvas_tool_menu.rs | 54 +++- .../canvas_tool_menu/line_menu_tests.rs | 6 +- .../canvas_tool_menu/rectangle_menu_tests.rs | 294 ++++++++++++++++++ .../canvas_tool_menu/triangle_menu_tests.rs | 45 +++ 4 files changed, 384 insertions(+), 15 deletions(-) create mode 100644 crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs create mode 100644 crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu.rs b/crates/ui-egui/src/canvas/canvas_tool_menu.rs index 3ce0e4885ed..3f446ca3d33 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu.rs @@ -1,4 +1,4 @@ -//! Canvas context menus for the selection tools, the Pen, the Line tool in Shape mode, the Zoom +//! Canvas context menus for the selection tools, the Pen, the shape tools, the Zoom //! and Hand tools, an active Free Transform box, text being edited with a Type tool, committed type //! under a Type tool, the pasteboard (outside the image), a guide and a pending crop. @@ -683,19 +683,43 @@ fn shape_layer_row(command: &str) -> bool { ) } -/// Shape tools whose Shape-mode right-click opens [`SHAPE_MENU`] (#13249). The Ellipse (#13232) -/// and the other shape tools join here once measured. +/// Shape tools whose right-click opens [`SHAPE_MENU`]: the Line (#13249), the Rectangle (#13341) +/// and the Triangle (#13367). The other shape tools join here once measured. pub fn shape_tool_menu(tool: Tool) -> bool { - matches!(tool, Tool::Line) + matches!(tool, Tool::Line | Tool::Rectangle | Tool::Triangle) } -/// Does a right-click with a shape tool open its menu? Only where measured: Shape mode, over -/// a pixel or shape layer. Path and Pixels mode, and other layer kinds, keep opening nothing. -fn shape_menu_measured(app: &PhotocraftApp) -> bool { - app.ui.tool_options.shape_mode == "shape" - && app.session.active().is_some_and(|st| { - st.active_layer.and_then(|id| st.doc.layer(id)).is_some_and(|l| matches!(l.content, LayerContent::Raster(_) | LayerContent::Shape(_))) - }) +/// Shape tools whose menu was measured in Path mode too, over a Work Path with a pixel layer +/// active: the Rectangle (#13341) and the Triangle (#13367). +fn shape_menu_in_path_mode(tool: Tool) -> bool { + matches!(tool, Tool::Rectangle | Tool::Triangle) +} + +/// Does a right-click with shape tool `tool` at document point `doc` open its menu? Only where +/// measured: in Shape mode over a pixel or shape layer, and for [`shape_menu_in_path_mode`]'s tools +/// in Path mode over a pixel layer with the Work Path shown. Pixels mode, Path mode elsewhere and +/// other layer kinds keep opening nothing; so does the Rectangle's right-click on a shape or on a +/// corner radius widget, which #13341 left unmeasured. +fn shape_menu_measured(app: &PhotocraftApp, tool: Tool, doc: Option<[f64; 2]>) -> bool { + let Some(st) = app.session.active() else { return false }; + let Some(layer) = st.active_layer.and_then(|id| st.doc.layer(id)) else { return false }; + let pixel = matches!(layer.content, LayerContent::Raster(_)); + let opens = match app.ui.tool_options.shape_mode.as_str() { + "shape" => pixel || matches!(layer.content, LayerContent::Shape(_)), + "path" => shape_menu_in_path_mode(tool) && pixel && crate::vector_ui::active_path_name(app).as_deref() == Some("work"), + _ => false, + }; + opens && !(tool == Tool::Rectangle && doc.is_some_and(|d| rectangle_keeps_right_click(app, d))) +} + +/// Is document point `doc` on a shape (as Path Selection picks one) or on a corner radius widget? +/// The Rectangle's right-click there wasn't measured (#13341), so it opens nothing, as before. +fn rectangle_keeps_right_click(app: &PhotocraftApp, doc: [f64; 2]) -> bool { + if !doc.iter().all(|v| v.is_finite()) { + return false; + } + let tol = crate::path_components::press_tolerance(app); + crate::corner_radius::hovers(app, doc) || app.session.active().is_some_and(|st| crate::vector_ui::pick_shape_at(&st.doc, doc, tol).is_some()) } /// The label a row shows, untranslated. The Pen's transform row names what it acts on, as in @@ -1396,7 +1420,7 @@ pub fn open_at(app: &mut PhotocraftApp, ctx: &Context, tool: Tool, pos: [f32; 2] if tool == Tool::RedEye && !red_eye_measured(app, doc) { return false; } - if shape_tool_menu(tool) && !text && !type_layer && !shape_menu_measured(app) { + if shape_tool_menu(tool) && !text && !type_layer && !shape_menu_measured(app, tool, doc) { return false; } app.ui.brush_picker = None; @@ -1939,5 +1963,11 @@ mod convert_point_menu_tests; #[cfg(test)] mod object_selection_menu_tests; +#[cfg(test)] +mod rectangle_menu_tests; + +#[cfg(test)] +mod triangle_menu_tests; + #[cfg(test)] mod line_menu_tests; diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/line_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/line_menu_tests.rs index b366e84bb8a..6c933b31f30 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/line_menu_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/line_menu_tests.rs @@ -45,17 +45,17 @@ fn right_click_entries(h: &mut Harness<'static, PhotocraftApp>) -> Vec { } /// The entries' labels, a separator as `None`. -fn shown(entries: &[Value]) -> Vec> { +pub(super) fn shown(entries: &[Value]) -> Vec> { entries.iter().map(|e| if e["separator"] == json!(true) { None } else { e["label"].as_str().map(String::from) }).collect() } /// The labels of the greyed entries. -fn greyed(entries: &[Value]) -> Vec { +pub(super) fn greyed(entries: &[Value]) -> Vec { entries.iter().filter(|e| e["enabled"] == json!(false)).filter_map(|e| e["label"].as_str().map(String::from)).collect() } /// The issue's notation: rows joined by " · ", separators written " ― ". -fn menu(spec: &str) -> Vec> { +pub(super) fn menu(spec: &str) -> Vec> { spec.split(" ― ") .enumerate() .flat_map(|(i, group)| (i > 0).then_some(None).into_iter().chain(group.split(" · ").map(|label| Some(label.to_owned())))) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs new file mode 100644 index 00000000000..199c8ea1ca7 --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs @@ -0,0 +1,294 @@ +//! #13341: a right-click on the canvas with the Rectangle tool opens the shape and path menu, as +//! measured in the reference application: in Shape mode over a pixel layer and on empty canvas +//! over a rectangle shape layer, and in Path mode over a pixel layer with a Work Path. A right-click +//! on a shape, on a corner radius widget or in Pixels mode opens nothing, as before. +//! +//! The checks shared with the Triangle tool (#13367), which measured the same three menus, live +//! here and take the tool; `triangle_menu_tests` runs them for the Triangle. + +use egui::{Context, Modifiers}; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::{Value, json}; + +use super::line_menu_tests::{greyed, menu, shown}; +use super::tests::{app, harness, right_click, screen_of}; +use super::{available, choose, open, open_at}; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// The measured menu over a pixel layer with no path, in the issue's notation. +const PIXEL_MENU: &str = "Define Custom Shape… ― Blending Options… ― Make Selection… · New Guides From Shape · Fill Path… · Stroke Path… ― \ + Rasterize Layer · Rasterize Layer Style · Rasterize Vector Mask ― Free Transform ― Unite Shapes · Subtract Front Shape · \ + Unite Shapes at Overlap · Subtract Shapes at Overlap ― Copy Fill · Copy Complete Stroke ― Paste Fill · Paste Complete Stroke ― \ + Isolate Layers"; + +/// The measured menu over a shape layer, and in Path mode over a Work Path: the transform row +/// reads Free Transform Path. +const PATH_MENU: &str = "Define Custom Shape… ― Blending Options… ― Make Selection… · New Guides From Shape · Fill Path… · Stroke Path… ― \ + Rasterize Layer · Rasterize Layer Style · Rasterize Vector Mask ― Free Transform Path ― Unite Shapes · Subtract Front Shape · \ + Unite Shapes at Overlap · Subtract Shapes at Overlap ― Copy Fill · Copy Complete Stroke ― Paste Fill · Paste Complete Stroke ― \ + Isolate Layers"; + +/// The four Unite rows, greyed in every measured menu. +const UNITE: [&str; 4] = ["Unite Shapes", "Subtract Front Shape", "Unite Shapes at Overlap", "Subtract Shapes at Overlap"]; + +/// A point of the 32 × 32 document off every shape the fixtures draw. +const EMPTY: [f32; 2] = [2.0, 2.0]; + +/// `app` with `tool` picked in `mode` ("shape", "path" or "pixels"). +pub(super) fn with_tool(mut app: PhotocraftApp, tool: Tool, mode: &str) -> PhotocraftApp { + app.ui.tool = tool; + app.ui.tool_options.shape_mode = mode.into(); + app +} + +/// A pixel layer filled red above the Background, active, with `tool` in Shape mode and no path. +pub(super) fn pixel_layer(tool: Tool) -> PhotocraftApp { + let mut app = app(); + app.run("layer.new.layer", json!({})).unwrap(); + app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); + with_tool(app, tool, "shape") +} + +/// `tool` drew a filled shape with a 2 px stroke from (8, 8) to (24, 24) in Shape mode: its +/// shape layer is active. +pub(super) fn shape_layer(tool: Tool) -> PhotocraftApp { + let mut app = with_tool(app(), tool, "shape"); + app.ui.tool_options.stroke_width = 2.0; + crate::vector_ui::finish_shape(&mut app, tool, [8.0, 8.0], [24.0, 24.0], Modifiers::NONE); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name(), "Shape", "{tool:?} made a shape layer"); + app +} + +/// [`pixel_layer`], then `tool` drew the same shape in Path mode: the Work Path holds it, and the +/// pixel layer stays active. +pub(super) fn work_path(tool: Tool) -> PhotocraftApp { + let mut app = with_tool(pixel_layer(tool), tool, "path"); + crate::vector_ui::finish_shape(&mut app, tool, [8.0, 8.0], [24.0, 24.0], Modifiers::NONE); + let st = app.session.active().unwrap(); + assert!(st.doc.work_path.is_some(), "{tool:?} drew the Work Path"); + assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name(), "Pixel", "the pixel layer stays active"); + assert_eq!(crate::vector_ui::active_path_name(&app).as_deref(), Some("work")); + app +} + +/// Right-click document point `doc`: what `ui.inspect` reports. +fn inspect_at(h: &mut Harness<'static, PhotocraftApp>, doc: [f32; 2]) -> Value { + let p = screen_of(h.state(), doc); + right_click(h, p, Modifiers::NONE); + crate::control::inspect(h.state(), &h.ctx) +} + +/// Right-click document point `doc` with `tool`: the open menu's entries. +pub(super) fn entries_at(h: &mut Harness<'static, PhotocraftApp>, tool: Tool, doc: [f32; 2]) -> Vec { + let inspected = inspect_at(h, doc); + assert_eq!(inspected["canvasToolMenu"]["tool"], json!(format!("{tool:?}")), "a menu opened at {doc:?}"); + inspected["canvasToolMenu"]["entries"].as_array().cloned().unwrap_or_default() +} + +/// A right-click at document point `doc` opens no menu, neither the tool's nor the layer menu. +pub(super) fn opens_nothing(h: &mut Harness<'static, PhotocraftApp>, doc: [f32; 2], case: &str) { + let inspected = inspect_at(h, doc); + assert_eq!(inspected["canvasToolMenu"], Value::Null, "{case}"); + assert_eq!(inspected["layerMenu"], Value::Null, "{case}"); +} + +/// Click the open menu's row `label`, which closes the menu. +fn pick(h: &mut Harness<'static, PhotocraftApp>, label: &str) { + h.get_by_label(label).click(); + h.run_steps(2); + assert!(h.state().ui.canvas_tool_menu.is_none(), "{label} closes the menu"); +} + +/// Copy Fill and Paste Fill over a pixel layer follow the rule every shape tool's menu shares, +/// read from the Line tool's menu: greyed on `main`, live once #13705 lands for all of them. The +/// other rows are pinned to the measurement as they are. +fn pixel_fill_rows_greyed() -> bool { + let mut app = pixel_layer(Tool::Line); + assert!(open(&mut app, &Context::default(), Tool::Line, [10.0, 10.0])); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + let copy = available(&app, &menu, "path.style.copyFill"); + assert_eq!(copy, available(&app, &menu, "path.style.pasteFill"), "the two fill rows share one rule"); + !copy +} + +/// The labels of `spec` that are in `greyed`, in menu order, plus Copy Fill and Paste Fill when +/// `pixel` and [`pixel_fill_rows_greyed`]. +fn greyed_rows(spec: &str, greyed: &[&str], pixel: bool) -> Vec { + let fill = pixel && pixel_fill_rows_greyed(); + menu(spec) + .into_iter() + .flatten() + .filter(|label| greyed.contains(&label.as_str()) || (fill && matches!(label.as_str(), "Copy Fill" | "Paste Fill"))) + .collect() +} + +/// Shape mode, a pixel layer active, no path: the measured rows, and Free Transform runs the menu +/// bar's on the layer. +pub(super) fn check_pixel_layer_menu(tool: Tool) { + let mut h = harness(pixel_layer(tool)); + let entries = entries_at(&mut h, tool, [16.0, 16.0]); + assert_eq!(shown(&entries), menu(PIXEL_MENU), "{tool:?}"); + let measured = [ + &["Define Custom Shape…", "Make Selection…", "Fill Path…", "Stroke Path…"][..], + &["Rasterize Layer", "Rasterize Layer Style", "Rasterize Vector Mask"], + &UNITE, + &["Copy Complete Stroke", "Paste Complete Stroke"], + ] + .concat(); + assert_eq!(greyed(&entries), greyed_rows(PIXEL_MENU, &measured, true), "{tool:?}"); + pick(&mut h, "Free Transform"); + assert!(h.state().ui.transform.is_some(), "{tool:?}: Free Transform opens the box on the layer"); +} + +/// Shape mode, the tool's shape layer active, right-click at `at`: the measured rows. Paste Fill +/// is live once a fill is copied, as in the measured session; Copy Fill copies the layer's. +pub(super) fn check_shape_layer_menu(tool: Tool, at: [f32; 2]) { + let mut h = harness(shape_layer(tool)); + let entries = entries_at(&mut h, tool, at); + assert_eq!(shown(&entries), menu(PATH_MENU), "{tool:?}"); + let before = h.state().session.journal.len(); + pick(&mut h, "Copy Fill"); + let ran: Vec<_> = h.state().session.journal[before..].iter().map(|(id, _)| id.clone()).collect(); + assert_eq!(ran, ["path.style.copyFill"], "{tool:?}"); + let st = h.state().session.active().unwrap(); + let fill = match &st.doc.layer(st.active_layer.unwrap()).unwrap().content { + photocraft_doc::LayerContent::Shape(sh) => sh.fill.clone(), + _ => None, + }; + assert!(fill.is_some() && h.state().session.path_fill_clipboard == fill, "{tool:?}: the layer's fill is copied"); + let entries = entries_at(&mut h, tool, at); + let measured = [&["Fill Path…", "Stroke Path…", "Rasterize Layer Style", "Rasterize Vector Mask"][..], &UNITE, &["Paste Complete Stroke"]].concat(); + assert_eq!(greyed(&entries), greyed_rows(PATH_MENU, &measured, false), "{tool:?}"); +} + +/// Path mode, a Work Path holding the tool's shape, the pixel layer active, right-click at `at`: +/// the measured rows, with Free Transform Path. +pub(super) fn check_work_path_menu(tool: Tool, at: [f32; 2]) { + let mut h = harness(work_path(tool)); + let entries = entries_at(&mut h, tool, at); + assert_eq!(shown(&entries), menu(PATH_MENU), "{tool:?}"); + let measured = + [&["Rasterize Layer", "Rasterize Layer Style", "Rasterize Vector Mask"][..], &UNITE, &["Copy Complete Stroke", "Paste Complete Stroke"]].concat(); + assert_eq!(greyed(&entries), greyed_rows(PATH_MENU, &measured, true), "{tool:?}"); +} + +/// What choosing a row did: the dialog it opened, the commands it recorded, whether a fill was +/// copied, and how many layers are isolated. +type Outcome = (Option>, Vec, bool, usize); + +/// A document builder for a tool, and the menu rows to choose on it. +type Case = (fn(Tool) -> PhotocraftApp, &'static [&'static str]); + +/// Choose `command` from `menu_tool`'s menu on the document `make(tool)` builds. +fn outcome(make: fn(Tool) -> PhotocraftApp, menu_tool: Tool, tool: Tool, command: &str) -> Outcome { + let mut app = make(tool); + app.ui.tool = menu_tool; + assert!(open(&mut app, &Context::default(), menu_tool, [10.0, 10.0]), "{menu_tool:?}"); + let menu = app.ui.canvas_tool_menu.clone().unwrap(); + assert!(available(&app, &menu, command), "{menu_tool:?}: {command} is enabled"); + let before = app.session.journal.len(); + choose(&mut app, &Context::default(), command); + assert!(!app.ui.status_error, "{menu_tool:?}: {command}: {}", app.ui.status); + let ran = app.session.journal[before..].iter().map(|(id, _)| id.clone()).collect(); + let isolated = app.session.active().map_or(0, |st| st.isolated_layers.len()); + (app.ui.dialogs.last().map(|d| d.fields.clone()), ran, app.session.path_fill_clipboard.is_some(), isolated) +} + +/// Each enabled path row opens the dialog or runs the command the Pen's menu does on the same +/// document, over the shape layer and over the Work Path; the layer rows run the menu bar's. +pub(super) fn check_rows_run_as_elsewhere(tool: Tool) { + let cases: [Case; 2] = [ + (shape_layer, &["edit.defineCustomShape", "path.toSelection", "path.transform", "path.style.copyFill", "select.isolateLayers"]), + (work_path, &["edit.defineCustomShape", "path.toSelection", "path.fill", "path.stroke", "path.transform", "select.isolateLayers"]), + ]; + for (make, commands) in cases { + for command in commands { + let pen = outcome(make, Tool::Pen, tool, command); + assert!(pen.0.is_some() || !pen.1.is_empty() || pen.2 || pen.3 > 0, "{command} does something from the Pen's menu"); + assert_eq!(outcome(make, tool, tool, command), pen, "{tool:?}: {command}"); + } + } + let mut app = shape_layer(tool); + assert!(open(&mut app, &Context::default(), tool, [10.0, 10.0])); + choose(&mut app, &Context::default(), "layer.rasterize.layer"); + assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("layer.rasterize.layer"), "{tool:?}"); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name(), "Pixel", "{tool:?}: rasterized"); +} + +/// Pixels mode, Path mode with no Work Path or with a shape layer active, and a type layer: no +/// menu, as before. +pub(super) fn check_unmeasured_cases_open_nothing(tool: Tool) { + for make in [pixel_layer, shape_layer, work_path] { + let mut h = harness(with_tool(make(tool), tool, "pixels")); + opens_nothing(&mut h, EMPTY, &format!("{tool:?}: Pixels mode")); + } + let mut h = harness(with_tool(pixel_layer(tool), tool, "path")); + opens_nothing(&mut h, EMPTY, &format!("{tool:?}: Path mode, no path")); + let mut h = harness(with_tool(shape_layer(tool), tool, "path")); + opens_nothing(&mut h, EMPTY, &format!("{tool:?}: Path mode, a shape layer active")); + let mut app = app(); + app.run("type.create", json!({"x": 4, "y": 20, "text": "A", "size": 12})).unwrap(); + let mut app = with_tool(app, tool, "shape"); + assert!(!open_at(&mut app, &Context::default(), tool, [10.0, 10.0], Some([2.0, 2.0])), "{tool:?}: a type layer"); + assert!(app.ui.canvas_tool_menu.is_none()); +} + +#[test] +fn on_a_pixel_layer_the_menu_lists_the_reference_rows() { + check_pixel_layer_menu(Tool::Rectangle); +} + +#[test] +fn on_empty_canvas_over_a_rectangle_layer_the_menu_lists_the_reference_rows() { + check_shape_layer_menu(Tool::Rectangle, EMPTY); +} + +#[test] +fn in_path_mode_over_a_work_path_the_menu_lists_the_reference_rows() { + // Inside the rectangle too: in Path mode it is part of the Work Path, not a shape layer. + check_work_path_menu(Tool::Rectangle, EMPTY); + check_work_path_menu(Tool::Rectangle, [16.0, 16.0]); +} + +#[test] +fn the_rows_run_the_pen_menu_and_menu_bar_commands() { + check_rows_run_as_elsewhere(Tool::Rectangle); +} + +#[test] +fn pixels_mode_and_unmeasured_cases_open_nothing() { + check_unmeasured_cases_open_nothing(Tool::Rectangle); +} + +#[test] +fn inside_the_rectangle_opens_nothing() { + let mut h = harness(shape_layer(Tool::Rectangle)); + opens_nothing(&mut h, [16.0, 16.0], "inside the rectangle"); + // On empty canvas the same document opens the menu. + entries_at(&mut h, Tool::Rectangle, EMPTY); +} + +#[test] +fn on_a_corner_radius_widget_opens_nothing() { + // No fill, so the widget inside the top-left corner is off the shape as Path Selection picks + // it: only the widget keeps the right-click. + let mut app = with_tool(app(), Tool::Rectangle, "shape"); + app.ui.tool_options.shape_fill = false; + app.ui.tool_options.stroke_width = 2.0; + crate::vector_ui::finish_shape(&mut app, Tool::Rectangle, [8.0, 8.0], [24.0, 24.0], Modifiers::NONE); + let mut h = harness(app); + let inset = 10.0 / f64::from(h.state().point_zoom()); + let widget = [8.0 + inset, 8.0 + inset]; + assert!(crate::corner_radius::hovers(h.state(), widget), "the top-left widget is at {widget:?}"); + let st = h.state().session.active().unwrap(); + assert!(crate::vector_ui::pick_shape_at(&st.doc, widget, crate::path_components::press_tolerance(h.state())).is_none()); + opens_nothing(&mut h, [widget[0] as f32, widget[1] as f32], "on the widget"); + // Also through the control channel's path, which calls `open_at` directly. + let ctx = h.ctx.clone(); + assert!(!open_at(h.state_mut(), &ctx, Tool::Rectangle, [10.0, 10.0], Some(widget))); + entries_at(&mut h, Tool::Rectangle, EMPTY); +} diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs new file mode 100644 index 00000000000..063a20fb16b --- /dev/null +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs @@ -0,0 +1,45 @@ +//! #13367: a right-click on the canvas with the Triangle tool opens the shape and path menu, as +//! measured in the reference application: in Shape mode over a pixel layer and over a triangle +//! shape layer (the same menu on empty canvas and inside the triangle), and in Path mode over a +//! pixel layer with a Work Path. Pixels mode opens nothing, as before. The checks are the +//! Rectangle's (#13341), which measured the same three menus. + +use super::rectangle_menu_tests::{ + check_pixel_layer_menu, check_rows_run_as_elsewhere, check_shape_layer_menu, check_unmeasured_cases_open_nothing, check_work_path_menu, shape_layer, +}; +use crate::state::Tool; + +/// A point inside the triangle the fixtures draw from (8, 8) to (24, 24), away from its corners. +const INSIDE: [f32; 2] = [16.0, 18.0]; + +#[test] +fn on_a_pixel_layer_the_menu_lists_the_reference_rows() { + check_pixel_layer_menu(Tool::Triangle); +} + +#[test] +fn over_a_triangle_layer_the_menu_is_the_same_off_and_inside_the_triangle() { + let app = shape_layer(Tool::Triangle); + let st = app.session.active().unwrap(); + let tol = crate::path_components::press_tolerance(&app); + let inside = [f64::from(INSIDE[0]), f64::from(INSIDE[1])]; + assert_eq!(crate::vector_ui::pick_shape_at(&st.doc, inside, tol), st.active_layer, "{INSIDE:?} is on the triangle"); + check_shape_layer_menu(Tool::Triangle, [2.0, 2.0]); + check_shape_layer_menu(Tool::Triangle, INSIDE); +} + +#[test] +fn in_path_mode_over_a_work_path_the_menu_lists_the_reference_rows() { + check_work_path_menu(Tool::Triangle, [2.0, 2.0]); + check_work_path_menu(Tool::Triangle, INSIDE); +} + +#[test] +fn the_rows_run_the_pen_menu_and_menu_bar_commands() { + check_rows_run_as_elsewhere(Tool::Triangle); +} + +#[test] +fn pixels_mode_and_unmeasured_cases_open_nothing() { + check_unmeasured_cases_open_nothing(Tool::Triangle); +} From 085343bc2992a76771d456c19521c9094719efc9 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 08:56:41 +0100 Subject: [PATCH 2/3] Rectangle and Triangle menu tests: zoom in so the right-clicks miss the box handles --- .../src/canvas/canvas_tool_menu/rectangle_menu_tests.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs index 4f4170c6ee8..5f7fdd377be 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs @@ -39,10 +39,13 @@ const UNITE: [&str; 4] = ["Unite Shapes", "Subtract Front Shape", "Unite Shapes /// A point of the 32 × 32 document off every shape the fixtures draw. const EMPTY: [f32; 2] = [2.0, 2.0]; -/// `app` with `tool` picked in `mode`. +/// `app` with `tool` picked in `mode`, shown at 800 % so a right-click a few pixels off the shape is +/// clear of the shape's bounding-box handles. pub(super) fn with_tool(mut app: Box, tool: Tool, mode: VectorMode) -> Box { app.ui.tool = tool; app.ui.tool_options.shape_mode = mode; + app.sync_views(); + app.ui.views[0].zoom = 8.0; app } @@ -197,7 +200,7 @@ type Case = (fn(Tool) -> Box, &'static [&'static str]); fn outcome(make: fn(Tool) -> Box, menu_tool: Tool, tool: Tool, command: &str) -> Outcome { let mut app = make(tool); app.ui.tool = menu_tool; - assert!(open_here(&mut app, menu_tool, [10.0, 10.0]), "{menu_tool:?}"); + assert!(open_here(&mut app, menu_tool, [2.0, 2.0]), "{menu_tool:?}"); let menu = app.ui.canvas_tool_menu.clone().unwrap(); assert!(available(&app, &menu, command), "{menu_tool:?}: {command} is enabled"); let before = app.session.journal.len(); @@ -223,7 +226,7 @@ pub(super) fn check_rows_run_as_elsewhere(tool: Tool) { } } let mut app = shape_layer(tool); - assert!(open_here(&mut app, tool, [10.0, 10.0])); + assert!(open_here(&mut app, tool, [2.0, 2.0])); pick(&mut app, "layer.rasterize.layer"); assert_eq!(app.session.journal.last().map(|(id, _)| id.as_str()), Some("layer.rasterize.layer"), "{tool:?}"); let st = app.session.active().unwrap(); From 1ad43aaf202fe44e278ee6d64816e68c800d1b60 Mon Sep 17 00:00:00 2001 From: ljkx <177671075+ljkx@users.noreply.github.com> Date: Mon, 23 Nov 2026 09:19:50 +0100 Subject: [PATCH 3/3] Rectangle and Triangle menu tests: build the fixtures through the shape commands on a larger document --- .../canvas_tool_menu/rectangle_menu_tests.rs | 75 +++++++++++-------- .../canvas_tool_menu/triangle_menu_tests.rs | 10 +-- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs index 5f7fdd377be..1b97c92f61b 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/rectangle_menu_tests.rs @@ -6,6 +6,7 @@ //! The checks shared with the Triangle tool (#13367), which measured the same three menus, live //! here and take the tool; `triangle_menu_tests` runs them for the Triangle. +use craft::draw::geometry::ShapeGeometry; use craft::draw::ui::VectorMode; use craft::keymap::Mods; use egui::Modifiers; @@ -17,7 +18,6 @@ use super::line_menu_tests::{greyed, menu, shown}; use super::tests::{any_dialog, app, harness, open_here, pick, right_click, screen_of}; use super::{available, open_at}; use crate::PhotocraftApp; -use crate::canvas::testing; use crate::state::Tool; /// The measured menu over a pixel layer with no path, in the issue's notation. @@ -36,51 +36,66 @@ const PATH_MENU: &str = "Define Custom Shape… ― Blending Options… ― Make /// The four Unite rows, greyed in every measured menu. const UNITE: [&str; 4] = ["Unite Shapes", "Subtract Front Shape", "Unite Shapes at Overlap", "Subtract Shapes at Overlap"]; -/// A point of the 32 × 32 document off every shape the fixtures draw. -const EMPTY: [f32; 2] = [2.0, 2.0]; +/// A point of the 200 × 200 document off every shape the fixtures draw, clear of the shape's +/// bounding-box handles. +pub(super) const EMPTY: [f32; 2] = [10.0, 10.0]; -/// `app` with `tool` picked in `mode`, shown at 800 % so a right-click a few pixels off the shape is -/// clear of the shape's bounding-box handles. -pub(super) fn with_tool(mut app: Box, tool: Tool, mode: VectorMode) -> Box { - app.ui.tool = tool; - app.ui.tool_options.shape_mode = mode; - app.sync_views(); - app.ui.views[0].zoom = 8.0; +/// A point inside every fixture's shape. +pub(super) const INSIDE: [f32; 2] = [80.0, 90.0]; + +/// A 200 × 200 document. +fn document() -> Box { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default()); + app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); app } -/// The selected tool drags from (8, 8) to (24, 24) on the canvas. -fn draw(app: &mut PhotocraftApp) { - testing::press(app, [8.0, 8.0], Modifiers::NONE); - testing::move_to(app, [16.0, 16.0], Modifiers::NONE); - testing::move_to(app, [24.0, 24.0], Modifiers::NONE); - testing::release(app, [24.0, 24.0], Modifiers::NONE); +/// `app` with `tool` picked in `mode`. +pub(super) fn with_tool(mut app: Box, tool: Tool, mode: VectorMode) -> Box { + app.ui.tool = tool; + app.ui.tool_options.shape_mode = mode; + app +} + +/// The geometry of the shape `tool` draws from (40, 40) to (120, 120). +fn geometry(tool: Tool) -> Value { + json!({"kind": if tool == Tool::Triangle { "triangle" } else { "rect" }, "rect": [40.0, 40.0, 80.0, 80.0]}) } /// A pixel layer filled red above the Background, active, with `tool` in Shape mode and no path. pub(super) fn pixel_layer(tool: Tool) -> Box { - let mut app = app(); + let mut app = document(); app.run("layer.new.layer", json!({})).unwrap(); app.run("edit.fill", json!({"color": "#ff0000"})).unwrap(); with_tool(app, tool, VectorMode::Shape) } -/// `tool` drew a filled shape with a 2 px stroke from (8, 8) to (24, 24) in Shape mode: its -/// shape layer is active. -pub(super) fn shape_layer(tool: Tool) -> Box { - let mut app = with_tool(app(), tool, VectorMode::Shape); +/// `tool`'s shape from (40, 40) to (120, 120) with a 2 px stroke, filled when `filled`, as a drag +/// in Shape mode makes it: its shape layer is active and selected. +fn shape_layer_filled(tool: Tool, filled: bool) -> Box { + let mut app = with_tool(document(), tool, VectorMode::Shape); + app.ui.tool_options.shape_fill = filled; app.ui.tool_options.stroke_width = 2.0; - draw(&mut app); + let rect = craft::behavior::kurbo::Rect::new(40.0, 40.0, 120.0, 120.0); + let shape = if tool == Tool::Triangle { ShapeGeometry::Triangle { rect } } else { ShapeGeometry::Rect { rect, radii: [0.0; 4] } }; + crate::vector_ui::create_geometry(&mut app, &shape, crate::vector_ui::kind_of(tool), None).unwrap(); let st = app.session.active().unwrap(); assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name(), "Shape", "{tool:?} made a shape layer"); app } +/// [`shape_layer_filled`], filled. +pub(super) fn shape_layer(tool: Tool) -> Box { + shape_layer_filled(tool, true) +} + /// [`pixel_layer`], then `tool` drew the same shape in Path mode: the Work Path holds it, and the /// pixel layer stays active. pub(super) fn work_path(tool: Tool) -> Box { let mut app = with_tool(pixel_layer(tool), tool, VectorMode::Path); - draw(&mut app); + let outline = photocraft_engine::vector_cmds::shape_path(&geometry(tool)).unwrap(); + app.run("path.set", json!({"name": "work", "path": photocraft_engine::vector_cmds::path_json(&outline)})).unwrap(); + app.ui.selected_path = Some("work".into()); let st = app.session.active().unwrap(); assert!(st.doc.work_path.is_some(), "{tool:?} drew the Work Path"); assert_eq!(st.doc.layer(st.active_layer.unwrap()).unwrap().content.kind_name(), "Pixel", "the pixel layer stays active"); @@ -143,7 +158,7 @@ fn greyed_rows(spec: &str, greyed: &[&str], pixel: bool) -> Vec { /// bar's on the layer. pub(super) fn check_pixel_layer_menu(tool: Tool) { let mut h = harness(pixel_layer(tool)); - let entries = entries_at(&mut h, tool, [16.0, 16.0]); + let entries = entries_at(&mut h, tool, INSIDE); assert_eq!(shown(&entries), menu(PIXEL_MENU), "{tool:?}"); let measured = [ &["Define Custom Shape…", "Make Selection…", "Fill Path…", "Stroke Path…"][..], @@ -265,7 +280,7 @@ fn on_empty_canvas_over_a_rectangle_layer_the_menu_lists_the_reference_rows() { fn in_path_mode_over_a_work_path_the_menu_lists_the_reference_rows() { // Inside the rectangle too: in Path mode it is part of the Work Path, not a shape layer. check_work_path_menu(Tool::Rectangle, EMPTY); - check_work_path_menu(Tool::Rectangle, [16.0, 16.0]); + check_work_path_menu(Tool::Rectangle, INSIDE); } #[test] @@ -281,7 +296,7 @@ fn pixels_mode_and_unmeasured_cases_open_nothing() { #[test] fn inside_the_rectangle_opens_nothing() { let mut h = harness(shape_layer(Tool::Rectangle)); - opens_nothing(&mut h, [16.0, 16.0], "inside the rectangle"); + opens_nothing(&mut h, INSIDE, "inside the rectangle"); // On empty canvas the same document opens the menu. entries_at(&mut h, Tool::Rectangle, EMPTY); } @@ -290,12 +305,8 @@ fn inside_the_rectangle_opens_nothing() { fn on_a_corner_radius_widget_opens_nothing() { // No fill, so a point inside the top-left corner, where its widget sits, is off the shape as // Path Selection picks it: only the widget keeps the right-click. - let mut app = with_tool(app(), Tool::Rectangle, VectorMode::Shape); - app.ui.tool_options.shape_fill = false; - app.ui.tool_options.stroke_width = 2.0; - draw(&mut app); - let mut h = harness(app); - let widget = [12.0, 12.0]; + let mut h = harness(shape_layer_filled(Tool::Rectangle, false)); + let widget = [50.0, 50.0]; assert!(!crate::shape_controls::corner_targets(h.state()).is_empty(), "the rectangle shows its corner widgets"); let st = h.state().session.active().unwrap(); assert!(crate::vector_ui::pick_shape_at(&st.doc, widget, crate::path_components::press_tolerance(h.state())).is_none()); diff --git a/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs b/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs index 063a20fb16b..6b70c1a4383 100644 --- a/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs +++ b/crates/ui-egui/src/canvas/canvas_tool_menu/triangle_menu_tests.rs @@ -5,13 +5,11 @@ //! Rectangle's (#13341), which measured the same three menus. use super::rectangle_menu_tests::{ - check_pixel_layer_menu, check_rows_run_as_elsewhere, check_shape_layer_menu, check_unmeasured_cases_open_nothing, check_work_path_menu, shape_layer, + EMPTY, INSIDE, check_pixel_layer_menu, check_rows_run_as_elsewhere, check_shape_layer_menu, check_unmeasured_cases_open_nothing, check_work_path_menu, + shape_layer, }; use crate::state::Tool; -/// A point inside the triangle the fixtures draw from (8, 8) to (24, 24), away from its corners. -const INSIDE: [f32; 2] = [16.0, 18.0]; - #[test] fn on_a_pixel_layer_the_menu_lists_the_reference_rows() { check_pixel_layer_menu(Tool::Triangle); @@ -24,13 +22,13 @@ fn over_a_triangle_layer_the_menu_is_the_same_off_and_inside_the_triangle() { let tol = crate::path_components::press_tolerance(&app); let inside = [f64::from(INSIDE[0]), f64::from(INSIDE[1])]; assert_eq!(crate::vector_ui::pick_shape_at(&st.doc, inside, tol), st.active_layer, "{INSIDE:?} is on the triangle"); - check_shape_layer_menu(Tool::Triangle, [2.0, 2.0]); + check_shape_layer_menu(Tool::Triangle, EMPTY); check_shape_layer_menu(Tool::Triangle, INSIDE); } #[test] fn in_path_mode_over_a_work_path_the_menu_lists_the_reference_rows() { - check_work_path_menu(Tool::Triangle, [2.0, 2.0]); + check_work_path_menu(Tool::Triangle, EMPTY); check_work_path_menu(Tool::Triangle, INSIDE); }