From fce6d91d1bf88e54f15c94166e7cf9f1219cc4e1 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Thu, 5 Nov 2026 13:45:00 -0500 Subject: [PATCH] The Artboard tool's Add New Artboard button switches on a placing mode where a click puts a bar-sized board at the click (#11386) The button now toggles a mode flag in ArtboardToolUi instead of adding a board beside the last one. With the mode on, a click on empty canvas runs one layer.new.artboard with rect = [click x, click y, bar width, bar height], rounded to whole pixels. Switching to another tool ends the mode, and layer.new.artboard without a rect is unchanged. Co-Authored-By: Claude Fable 5.1 --- crates/ui-egui/src/artboard_add_mode_tests.rs | 133 ++++++++++++++++++ crates/ui-egui/src/lib.rs | 3 + crates/ui-egui/src/tools/artboard_tool.rs | 46 ++++-- .../src/tools/artboard_tool/add_mode.rs | 46 ++++++ 4 files changed, 217 insertions(+), 11 deletions(-) create mode 100644 crates/ui-egui/src/artboard_add_mode_tests.rs create mode 100644 crates/ui-egui/src/tools/artboard_tool/add_mode.rs diff --git a/crates/ui-egui/src/artboard_add_mode_tests.rs b/crates/ui-egui/src/artboard_add_mode_tests.rs new file mode 100644 index 0000000000..6e4ff13720 --- /dev/null +++ b/crates/ui-egui/src/artboard_add_mode_tests.rs @@ -0,0 +1,133 @@ +//! The Artboard tool's Add New Artboard placing mode (#11386): with the mode on, a click on empty +//! canvas puts a board of the bar's size with its top-left corner at the click. + +use egui::Modifiers; +use photocraft_geom::Rect; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::canvas::{ToolEvent, tool_event}; +use crate::state::Tool; + +/// The measured document: two boards, the 750 x 1334 one selected, the other one apart from it. +fn app_with_selected_board() -> PhotocraftApp { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); + app.run("layer.new.artboard", json!({"rect": [2000, 0, 300, 300]})).unwrap(); + app.run("layer.new.artboard", json!({"rect": [900, 0, 750, 1334]})).unwrap(); + app.sync_views(); + app.ui.extras.snap = false; + app.ui.view.show.smart_guides = false; + app.ui.tool = Tool::Artboard; + app +} + +fn click(app: &mut PhotocraftApp, x: f64, y: f64) { + let m = Modifiers::NONE; + tool_event(app, ToolEvent::Down { x, y, pressure: 1.0 }, m); + tool_event(app, ToolEvent::Up { x, y }, m); +} + +fn boards(app: &PhotocraftApp) -> Vec { + app.session.active().unwrap().doc.artboards().iter().map(|b| b.2.rect).collect() +} + +fn last_step(app: &PhotocraftApp) -> Option { + app.session.active().unwrap().history.entries().last().cloned() +} + +#[test] +fn a_click_with_the_mode_on_puts_a_bar_sized_board_at_the_click() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + let before = boards(&app); + click(&mut app, 222.0, 600.0); + assert!(!app.ui.status_error, "{}", app.ui.status); + let after = boards(&app); + assert_eq!(after.len(), before.len() + 1); + // 750 x 1334, top-left at the click, over the selected board (it did not move aside). + assert_eq!(after.last().copied(), Some(Rect::new(222, 600, 972, 1934))); + assert_eq!(after.get(..before.len()), Some(before.as_slice())); + let st = app.session.active().unwrap(); + assert_eq!(st.doc.layers.last().map(|l| l.name.as_str()), Some("Artboard 3")); + assert_eq!(st.active_layer, Some(st.doc.artboards().last().unwrap().0)); + assert_eq!(last_step(&app).as_deref(), Some("New Artboard")); + // One undo step. + app.run("edit.undo", json!({})).unwrap(); + assert_eq!(boards(&app), before); +} + +#[test] +fn the_click_is_rounded_to_whole_pixels() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + click(&mut app, 222.4, 600.6); + assert_eq!(boards(&app).last().copied(), Some(Rect::new(222, 601, 972, 1935))); +} + +#[test] +fn the_mode_stays_on_after_a_board_is_placed() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + click(&mut app, 222.0, 600.0); + assert!(app.ui.artboard_tool.adding); + // The next click is another placement, at the new selected board's size. + click(&mut app, 3000.0, 100.0); + assert_eq!(boards(&app).last().copied(), Some(Rect::new(3000, 100, 3750, 1434))); +} + +#[test] +fn a_click_with_the_mode_off_still_places_nothing() { + let mut app = app_with_selected_board(); + let before = boards(&app); + click(&mut app, 222.0, 600.0); + assert_eq!(boards(&app), before); +} + +#[test] +fn with_no_board_the_click_uses_the_canvas_size() { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 800, "height": 600, "background": "white"})).unwrap(); + app.sync_views(); + app.ui.tool = Tool::Artboard; + app.ui.artboard_tool.adding = true; + click(&mut app, 40.0, 50.0); + assert_eq!(boards(&app), vec![Rect::new(40, 50, 840, 650)]); +} + +#[test] +fn a_drag_with_the_mode_on_still_draws_the_dragged_board() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + let m = Modifiers::NONE; + tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 30.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Move { x: 80.0, y: 70.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Move { x: 120.0, y: 110.0, pressure: 1.0 }, m); + tool_event(&mut app, ToolEvent::Up { x: 120.0, y: 110.0 }, m); + assert_eq!(boards(&app).last().copied(), Some(Rect::new(20, 30, 120, 110))); +} + +#[test] +fn a_click_on_an_existing_board_selects_it_and_places_nothing() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + let before = boards(&app); + click(&mut app, 2100.0, 100.0); + assert_eq!(boards(&app), before); + let st = app.session.active().unwrap(); + assert_eq!(st.active_layer, Some(st.doc.artboards()[0].0)); +} + +#[test] +fn another_tool_turns_the_mode_off() { + let mut app = app_with_selected_board(); + app.ui.artboard_tool.adding = true; + assert_eq!(serde_json::to_value(&app.ui.artboard_tool).unwrap()["adding"], json!(true)); + app.ui.tool = Tool::Move; + click(&mut app, 3000.0, 100.0); + assert!(!app.ui.artboard_tool.adding); + app.ui.tool = Tool::Artboard; + let before = boards(&app); + click(&mut app, 3000.0, 100.0); + assert_eq!(boards(&app), before); +} diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 54edda6264..d9939cfa0c 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -751,6 +751,9 @@ impl SyntheticTarget { } } +#[cfg(test)] +mod artboard_add_mode_tests; + #[cfg(test)] mod lifecycle_tests; diff --git a/crates/ui-egui/src/tools/artboard_tool.rs b/crates/ui-egui/src/tools/artboard_tool.rs index 81dd67bd02..9ff1a30a9a 100644 --- a/crates/ui-egui/src/tools/artboard_tool.rs +++ b/crates/ui-egui/src/tools/artboard_tool.rs @@ -3,9 +3,10 @@ //! selected board's edge and corner handles to resize it (⇧ on a corner keeps its proportions). //! The "+" buttons beside the selected board's sides add a board of the same size there (⌥: a copy //! with its contents). The options bar sets the selected board's size (preset, W/H, -//! portrait/landscape) and adds a board of the same size beside the last one. Every change is one -//! engine command (`layer.new.artboard`, `layer.artboard.set`, `layer.artboard.addBeside`), so each -//! gesture is one undo step; this module only turns pointer gestures into them. +//! portrait/landscape) and switches on Add New Artboard's placing mode (a click puts a board of the +//! bar's size at the click). Every change is one engine command (`layer.new.artboard`, +//! `layer.artboard.set`, `layer.artboard.addBeside`), so each gesture is one undo step; this module +//! only turns pointer gestures into them. use egui::{CursorIcon, Pos2, Rect, Stroke, vec2}; use photocraft_doc::{Document, Layer, LayerId}; @@ -18,6 +19,8 @@ use crate::crop_ui::{Hit, hit}; use crate::state::Tool; use crate::theme::Tokens; +mod add_mode; + /// How far from an edge (screen points) a press still grabs its handle. const HANDLE_PX: f64 = 6.0; /// A press that moves less than this (screen points) is a click, not a drawn board. @@ -34,6 +37,8 @@ const SIDES: [(&str, [f64; 2]); 4] = [("top", [0.0, -1.0]), ("right", [1.0, 0.0] #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(default)] pub struct ArtboardToolUi { + /// Add New Artboard's placing mode: a click on empty canvas puts a board there (#11386). + pub adding: bool, #[serde(skip)] pub drag: Option, } @@ -184,7 +189,11 @@ fn follow(d: &mut ArtboardDrag, p: [f64; 2], mods: egui::Modifiers) { pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool { // ⌘ is the Move tool (`hold_keys::cmd_moves`): a press with it held is not this tool's, // so it drags a guide or the layer (#6288). A drag already under way keeps its events. - if app.ui.tool != Tool::Artboard || (app.ui.artboard_tool.drag.is_none() && crate::canvas::event_tool(app, mods) != Tool::Artboard) { + if app.ui.tool != Tool::Artboard { + add_mode::leave(app); + return false; + } + if app.ui.artboard_tool.drag.is_none() && crate::canvas::event_tool(app, mods) != Tool::Artboard { return false; } let Some(doc) = app.session.active().map(|d| d.doc.clone()) else { return true }; @@ -222,7 +231,14 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> let result = match d { ArtboardDrag::New { start, cur } => { let screen = (cur[0] - start[0]).hypot(cur[1] - start[1]) * f64::from(app.point_zoom()); - if screen < CLICK_PX || r[2] <= r[0] || r[3] <= r[1] { + if screen < CLICK_PX { + // A click: with Add New Artboard's mode on, it places a board. + if let Some(placed) = add_mode::click(app, start) { + report(app, placed); + } + return true; + } + if r[2] <= r[0] || r[3] <= r[1] { return true; } app.run("layer.new.artboard", json!({"rect": xywh(r)})) @@ -292,6 +308,7 @@ pub fn draw_overlay(app: &PhotocraftApp, painter: &egui::Painter, xf: &ViewXform /// Landscape, and Add New Artboard. pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bool { if tool != Tool::Artboard { + add_mode::leave(app); return false; } let t = Tokens::get(ui.ctx()); @@ -328,12 +345,19 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo } crate::widgets::vline(ui, 22.0); let has_doc = app.session.active().is_some(); - if ui.add_enabled_ui(has_doc, |ui| crate::widgets::secondary_button(ui, tl!("Add New Artboard"), 0.0)).inner.clicked() { - // A board the size of the selected one, right of the rightmost board (the canvas size - // without a selected board). - let p = board.map_or(json!({}), |(_, r)| json!({"width": r[2] - r[0], "height": r[3] - r[1]})); - let r = app.run("layer.new.artboard", p); - report(app, r); + // Drawn as the primary button while the placing mode is on. + let adding = add_mode::active(app); + let button = ui + .add_enabled_ui(has_doc, |ui| { + if adding { + crate::widgets::primary_button(ui, tl!("Add New Artboard"), 0.0) + } else { + crate::widgets::secondary_button(ui, tl!("Add New Artboard"), 0.0) + } + }) + .inner; + if button.clicked() { + add_mode::toggle(app); } true } diff --git a/crates/ui-egui/src/tools/artboard_tool/add_mode.rs b/crates/ui-egui/src/tools/artboard_tool/add_mode.rs new file mode 100644 index 0000000000..c46d6d737d --- /dev/null +++ b/crates/ui-egui/src/tools/artboard_tool/add_mode.rs @@ -0,0 +1,46 @@ +//! The options bar's Add New Artboard placing mode (#11386): the button switches the mode on, and +//! while it is on a click on empty canvas puts a board of the bar's size with its top-left corner +//! at the click. The engine's `layer.new.artboard` does the work; this only turns the click into +//! its `rect`. + +use serde_json::{Value, json}; + +use super::active_board; +use crate::PhotocraftApp; +use crate::state::Tool; + +/// True while the Artboard tool is current and its placing mode is on. +pub(super) fn active(app: &PhotocraftApp) -> bool { + app.ui.tool == Tool::Artboard && app.ui.artboard_tool.adding +} + +/// The Add New Artboard button: turns the mode on, or off when it is already on. +pub(super) fn toggle(app: &mut PhotocraftApp) { + app.ui.artboard_tool.adding = !app.ui.artboard_tool.adding; +} + +/// Another tool is current: the mode ends. +pub(super) fn leave(app: &mut PhotocraftApp) { + app.ui.artboard_tool.adding = false; +} + +/// The size the options bar shows: the selected board's, else the canvas size (the engine's +/// fallback for a board with no size). +fn bar_size(app: &PhotocraftApp) -> Option<[i64; 2]> { + if let Some((_, r)) = active_board(app) { + return Some([r[2].saturating_sub(r[0]), r[3].saturating_sub(r[1])]); + } + let size = app.session.active()?.doc.size; + Some([i64::from(size.width), i64::from(size.height)]) +} + +/// A click at document point `p` on empty canvas: one `layer.new.artboard` with the bar's size and +/// its top-left corner at the click, rounded to whole pixels. None when the mode is off. +pub(super) fn click(app: &mut PhotocraftApp, p: [f64; 2]) -> Option> { + if !active(app) { + return None; + } + let [w, h] = bar_size(app)?; + let rect = [p[0].round() as i64, p[1].round() as i64, w, h]; + Some(app.run("layer.new.artboard", json!({"rect": rect}))) +}