PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Free Transform: right-click menu with the transform modes, rotate and flip (#2832) (#2840)
Valentin Kleind79fc5e8efc3
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 2df3e15a7..20a92d947 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -2867,7 +2867,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:}}// Right-click while transforming: switch the box's mode (Free Transform, Scale, Rotate,- // Skew, Distort, Perspective, Warp).+ // Skew, Distort, Perspective, Warp), or rotate or flip the box.let transforming = app.ui.transform.as_ref().is_some_and(|t| t.warp.is_none());if response.secondary_clicked()&& transformingdiff --git a/crates/ui-egui/src/canvas_tool_menu.rs b/crates/ui-egui/src/canvas_tool_menu.rsindex 70da79084..30611076b 100644--- a/crates/ui-egui/src/canvas_tool_menu.rs+++ b/crates/ui-egui/src/canvas_tool_menu.rs@@ -29,7 +29,9 @@ pub struct CanvasToolMenu {/// One menu row: label and command id. `None` is a separator.pub type Row = Option<(&'static str, &'static str)>;-/// Right-click while transforming: what the box's handles do.+/// Right-click while transforming, in Photoshop's order: what the box's handles do, then Rotate+/// and Flip, which turn the open box without committing it (`transform_tool::intercept`).+/// Content-Aware Scale and Puppet Warp are greyed while a box is open.pub const TRANSFORM_MENU: &[Row] = &[Some(("Free Transform", "edit.freeTransform")),Some(("Scale", "edit.transform.scale")),@@ -38,6 +40,16 @@ pub const TRANSFORM_MENU: &[Row] = &[Some(("Distort", "edit.transform.distort")),Some(("Perspective", "edit.transform.perspective")),Some(("Warp", "edit.transform.warp")),+ None,+ Some(("Content-Aware Scale", "edit.contentAwareScale")),+ Some(("Puppet Warp", "edit.puppetWarp")),+ None,+ Some(("Rotate 180°", "edit.transform.rotate180")),+ Some(("Rotate 90° Clockwise", "edit.transform.rotate90Cw")),+ Some(("Rotate 90° Counter Clockwise", "edit.transform.rotate90Ccw")),+ None,+ Some(("Flip Horizontal", "edit.transform.flipHorizontal")),+ Some(("Flip Vertical", "edit.transform.flipVertical")),];/// Photoshop's pasteboard menu (right-click outside the image): the pasteboard colour, the choice@@ -541,6 +553,86 @@ mod tests {assert!(h.state().ui.canvas_tool_menu.is_none() && h.state().ui.brush_picker.is_some());}+ /// #2832: right-click over an open Free Transform box: Photoshop's transform menu. Skew+ /// switches the box's mode; Rotate and Flip turn the box about its reference point without+ /// committing it; Content-Aware Scale and Puppet Warp are greyed.+ #[test]+ fn right_click_during_free_transform_switches_modes_rotates_and_flips_the_box() {+ let mut app = app();+ app.ui.tool = Tool::RectMarquee;+ app.run("layer.new.layer", json!({})).unwrap();+ app.run("select.rect", json!({"x": 4, "y": 6, "width": 10, "height": 8})).unwrap();+ app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();+ app.run("select.deselect", json!({})).unwrap();+ let mut h = harness(app);+ let ctx = h.ctx.clone();+ crate::menus::invoke(h.state_mut(), &ctx, "edit.freeTransform", json!({})).unwrap();+ h.run_steps(2);+ let center = h.state().last_canvas_rect.center();+ let (revision, journal) = (h.state().session.active().unwrap().revision, h.state().session.journal.len());+ right_click(&mut h, center, Modifiers::NONE);+ let menu = h.state().ui.canvas_tool_menu.clone().expect("the transform menu");+ assert!(menu.transform && rows(&menu) == TRANSFORM_MENU);+ for (id, on) in+ [("edit.contentAwareScale", false), ("edit.puppetWarp", false), ("edit.transform.rotate90Cw", true), ("edit.transform.flipVertical", true)]+ {+ assert_eq!(entry_enabled(h.state(), &menu, id), on, "{id}");+ }+ choose(h.state_mut(), &ctx, "edit.transform.skew");+ assert_eq!(h.state().ui.transform.as_ref().unwrap().mode, crate::state::TransformMode::Skew);+ let t = h.state().ui.transform.clone().unwrap();+ let (q0, c) = (t.quad, t.pivot);+ right_click(&mut h, center, Modifiers::NONE);+ choose(h.state_mut(), &ctx, "edit.transform.flipHorizontal");+ let q = h.state().ui.transform.as_ref().expect("the box stays open").quad;+ for (a, b) in q.iter().zip(q0) {+ assert!((a[0] - (2.0 * c[0] - b[0])).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9, "mirrored about the reference point: {q0:?} -> {q:?}");+ }+ right_click(&mut h, center, Modifiers::NONE);+ choose(h.state_mut(), &ctx, "edit.transform.rotate180");+ let q = h.state().ui.transform.as_ref().unwrap().quad;+ for (a, b) in q.iter().zip(q0) {+ assert!((a[1] - (2.0 * c[1] - b[1])).abs() < 1e-9 && (a[0] - b[0]).abs() < 1e-9, "flipped then turned 180°: {q0:?} -> {q:?}");+ }+ let st = h.state().session.active().unwrap();+ assert_eq!((st.revision, h.state().session.journal.len()), (revision, journal), "nothing is committed");+ }++ #[test]+ fn transform_menu_follows_photoshop_order() {+ assert_eq!(+ labels(TRANSFORM_MENU),+ vec![+ Some("Free Transform"),+ Some("Scale"),+ Some("Rotate"),+ Some("Skew"),+ Some("Distort"),+ Some("Perspective"),+ Some("Warp"),+ None,+ Some("Content-Aware Scale"),+ Some("Puppet Warp"),+ None,+ Some("Rotate 180°"),+ Some("Rotate 90° Clockwise"),+ Some("Rotate 90° Counter Clockwise"),+ None,+ Some("Flip Horizontal"),+ Some("Flip Vertical"),+ ]+ );+ // Over a box on editable type, Distort and Perspective are greyed (#2630).+ let mut app = app();+ app.run("type.create", json!({"x": 2, "y": 20, "text": "Hi", "size": 16})).unwrap();+ crate::transform_tool::begin(&mut app, &Context::default()).unwrap();+ assert!(open_transform(&mut app, [10.0, 10.0]));+ let menu = app.ui.canvas_tool_menu.clone().unwrap();+ for (id, on) in [("edit.transform.skew", true), ("edit.transform.distort", false), ("edit.transform.perspective", false)] {+ assert_eq!(entry_enabled(&app, &menu, id), on, "{id}");+ }+ }+fn labels(rows: &[Row]) -> Vec<Option<&str>> {rows.iter().map(|row| row.map(|(label, _)| label)).collect()}diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rsindex b31654d3b..e41e9c71d 100644--- a/crates/ui-egui/src/control.rs+++ b/crates/ui-egui/src/control.rs@@ -930,6 +930,13 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)}if secondary {let down = matches!(ev, ToolEvent::Down { .. });+ // Right-click while transforming: the box's menu, as on the canvas.+ if app.ui.transform.as_ref().is_some_and(|t| t.warp.is_none()) {+ if down {+ crate::canvas_tool_menu::open_transform(app, screen_point(app, x, y));+ }+ continue;+ }// Right-click with the Move tool, or ⌘/Ctrl+right-click: list the layers there.if crate::layer_pick_ui::is_gesture(app.ui.tool, mods) {if down {diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex afff5fbf9..be0e1d305 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -936,21 +936,58 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> Option<bool> {match id {"edit.undo" => Some(steps.is_some_and(|s| s.can_undo(&Step::of(t)))),"edit.redo" => Some(steps.is_some_and(|s| !s.redo.is_empty())),- "edit.toggleLastState" => Some(false),+ "edit.toggleLastState" | "edit.contentAwareScale" | "edit.puppetWarp" => Some(false),+ // Rotate and Flip turn the box (not while warping, where the mesh is the box).+ _ if box_preset(id).is_some() => Some(t.warp.is_none()),+ // Editable type can't be distorted (#2630, as #2701 rules for the menus outside a box).+ "edit.transform.distort" | "edit.transform.perspective" if moves_type(app, t) => Some(false),_ => None,}}+/// Does the box move an editable type layer (not its selection outline, a lone mask or a path)?+fn moves_type(app: &PhotocraftApp, t: &TransformSession) -> bool {+ !t.selection+ && t.target.is_none()+ && t.path.is_none()+ && app.session.active().and_then(|st| st.doc.layer(LayerId(t.layer))).is_some_and(|l| matches!(l.content, LayerContent::Text(_)))+}++/// Edit › Transform's Rotate and Flip presets while a box is open: what they do to the box, as+/// `(kx, ky, angle)`: a scale along the box's own axes, then a rotation (radians, clockwise on+/// screen), both about the reference point.+fn box_preset(id: &str) -> Option<(f64, f64, f64)> {+ use std::f64::consts::{FRAC_PI_2, PI};+ Some(match id {+ "edit.transform.rotate180" => (1.0, 1.0, PI),+ "edit.transform.rotate90Cw" => (1.0, 1.0, FRAC_PI_2),+ "edit.transform.rotate90Ccw" => (1.0, 1.0, -FRAC_PI_2),+ "edit.transform.flipHorizontal" => (-1.0, 1.0, 0.0),+ "edit.transform.flipVertical" => (1.0, -1.0, 0.0),+ _ => return None,+ })+}+/// While transforming, the box owns the history for every caller (menus, shortcuts, the control/// channel, MCP): Undo and Redo step through it, and Toggle Last State, which would change the-/// document under the box, is refused. `None` for other commands or when not transforming.+/// document under the box, is refused. Edit › Transform's Rotate and Flip turn the box itself, as+/// in Photoshop, instead of the layer under it; committing stays with ↩ and the options bar.+/// `None` for other commands or when not transforming.pub fn intercept(app: &mut PhotocraftApp, id: &str) -> Option<Result<serde_json::Value, String>> {- app.ui.transform.as_ref()?;+ let warping = app.ui.transform.as_ref()?.warp.is_some();match id {"edit.undo" => Some(Ok(step(app, false))),"edit.redo" => Some(Ok(step(app, true))),- "edit.toggleLastState" => Some(Err("Commit or cancel the transform first".into())),- _ => None,+ "edit.toggleLastState" | "edit.contentAwareScale" | "edit.puppetWarp" => Some(Err("Commit or cancel the transform first".into())),+ _ => {+ let (kx, ky, angle) = box_preset(id)?;+ if warping {+ return Some(Err("Commit or cancel the transform first".into()));+ }+ scale_about_pivot(app, kx, ky);+ rotate_about_pivot(app, angle);+ Some(Ok(json!({"transform": app.ui.transform})))+ }}}diff --git a/docs/scorecard.md b/docs/scorecard.mdindex d1ad771d3..1d051c4df 100644--- a/docs/scorecard.md+++ b/docs/scorecard.md@@ -8,7 +8,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js|---|---:|---:|---:|---|| [Performance](#performance) | 3 | 22 | 22 | 56 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 17 not run, 5 not measurable yet (baseline 2026-10-05) || [File compatibility](#file-compatibility) | 5 | 11 | 11 | io corpus: oracle ≥ 146/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 236/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 133/256, round trip ≥ 258/256 |-| [Tools](#tools) | 23 | 16 | 4 | |+| [Tools](#tools) | 24 | 16 | 3 | || [Workspace and UI](#workspace-and-ui) | 6 | 7 | 10 | settings that do nothing: 37 of 153 (target 0) || [Type](#type) | 0 | 3 | 6 | || [Automation](#automation) | 3 | 2 | 6 | 630 of 630 menu items live (100.0%) |@@ -149,7 +149,7 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t| TOOL-214-2 | Marquee: Shift square, Alt from centre, live W x H readout | done | [#214](https://github.com/storytold/photocraft/issues/214) | canvas.rs marquee_corners: Shift square or circle, Alt from the centre, live W x H readout beside the cursor (marquee_readout); marquee_tests.rs || TOOL-214-3 | Color Range test: Sampled Colors at Fuzziness 0 selects the exact match only | partial | [#214](https://github.com/storytold/photocraft/issues/214) | Fuzziness-0 tests cover midtones/shadows only (selection_cmds.rs:444-447) || TOOL-214-4 | Move tool: box-select layers, Auto-Select hover outline, double-click fit, drag to a tab | partial | [#214](https://github.com/storytold/photocraft/issues/214) | Auto-Select hover outline done (Preferences › Tools › Show Highlight on Rollover, #2834); box-select layers, double-click fit and drag to a tab open |-| TOOL-214-5 | Right-click menu during Free Transform (scale, rotate, skew, warp, flip, rotate 90/180) | missing | [#214](https://github.com/storytold/photocraft/issues/214) | No context menu in canvas.rs or transform_tool.rs |+| TOOL-214-5 | Right-click menu during Free Transform (scale, rotate, skew, warp, flip, rotate 90/180) | done | [#214](https://github.com/storytold/photocraft/issues/214) | canvas_tool_menu.rs TRANSFORM_MENU: modes, Content-Aware Scale / Puppet Warp greyed, Rotate 180 / 90 CW / 90 CCW and Flip turn the open box (transform_tool::intercept, #2832) || TOOL-214-6 | Puppet Warp mesh follows the object outline (constrained triangulation) | missing | [#214](https://github.com/storytold/photocraft/issues/214) | algo/puppet.rs:136 builds a block-grid mesh || TOOL-214-7 | Content-Aware Scale through the Free Transform box with a live proxy preview | partial | [#214](https://github.com/storytold/photocraft/issues/214) | edit.contentAwareScale (edit_menu_cmds.rs:899) behind a parameter dialog || TOOL-214-8 | A measured ruler angle prefills Image Rotation > Arbitrary (0.01 deg) | missing | [#214](https://github.com/storytold/photocraft/issues/214) | Arbitrary rotation is a generic dialog; ruler only feeds straighten |diff --git a/scorecard/tools.toml b/scorecard/tools.tomlindex af26a066b..b5445e2a4 100644--- a/scorecard/tools.toml+++ b/scorecard/tools.toml@@ -141,9 +141,9 @@ note = "Auto-Select hover outline done (Preferences › Tools › Show Highlight[[item]]id = "TOOL-214-5"target = "Right-click menu during Free Transform (scale, rotate, skew, warp, flip, rotate 90/180)"-status = "missing"+status = "done"issue = 214-note = "No context menu in canvas.rs or transform_tool.rs"+note = "canvas_tool_menu.rs TRANSFORM_MENU: modes, Content-Aware Scale / Puppet Warp greyed, Rotate 180 / 90 CW / 90 CCW and Flip turn the open box (transform_tool::intercept, #2832)"[[item]]id = "TOOL-214-6"