PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Edit › Transform › Rotate and Skew no longer scale (#3941) (#3986)
Vikram Bhaskarana7919b24e1a4
diff --git a/crates/ui-egui/src/canvas_tool_menu.rs b/crates/ui-egui/src/canvas_tool_menu.rsindex 7fe4fce9e..37ee58b0a 100644--- a/crates/ui-egui/src/canvas_tool_menu.rs+++ b/crates/ui-egui/src/canvas_tool_menu.rs@@ -301,8 +301,8 @@ pub fn available(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> b}/// Is `command` the transform box's current mode (checked in the transform menu)?-/// Scale and Rotate are Free Transform's mode, so only Free Transform shows it checked. Warp is-/// not a box mode (it is its own session), and `for_command` would read it as Free.+/// Scale stays Free Transform's mode (only Free Transform is checked). Rotate is its own mode.+/// Warp is not a box mode (it is its own session), and `for_command` would read it as Free.pub(crate) fn mode_checked(app: &PhotocraftApp, menu: &CanvasToolMenu, command: &str) -> bool {if menu.pasteboard {return pasteboard_choice(command) == Some(app.session.prefs().interface.canvas_color);@@ -311,7 +311,7 @@ pub(crate) fn mode_checked(app: &PhotocraftApp, menu: &CanvasToolMenu, command:return type_checked(app, command);}menu.transform- && matches!(command, "edit.freeTransform" | "edit.transform.skew" | "edit.transform.distort" | "edit.transform.perspective")+ && matches!(command, "edit.freeTransform" | "edit.transform.rotate" | "edit.transform.skew" | "edit.transform.distort" | "edit.transform.perspective")&& app.ui.transform.as_ref().is_some_and(|t| t.mode == crate::state::TransformMode::for_command(command))}diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rsindex 0e3424f25..a76640314 100644--- a/crates/ui-egui/src/state.rs+++ b/crates/ui-egui/src/state.rs@@ -856,10 +856,12 @@ impl Default for ToolOptions {#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]pub enum TransformMode {- /// Free Transform, Scale and Rotate.+ /// Free Transform and Scale: handles scale, drag outside rotates.#[default]Free,- /// Edge handles skew.+ /// Edit › Transform › Rotate: handle drags rotate about the reference point (#3941).+ Rotate,+ /// Edge and corner handles skew: each moves along one axis.Skew,/// Corner handles move freely, one at a time; nothing else moves them (no rotating, no/// edges) and nothing snaps.@@ -872,6 +874,7 @@ impl TransformMode {/// The mode an `edit.transform.*` / `edit.freeTransform` menu id starts.pub fn for_command(id: &str) -> Self {match id {+ "edit.transform.rotate" => Self::Rotate,"edit.transform.skew" => Self::Skew,"edit.transform.distort" => Self::Distort,"edit.transform.perspective" => Self::Perspective,@@ -909,7 +912,7 @@ pub struct TransformSession {/// Cancel takes it back and OK folds it into one history step with the transform.#[serde(default)]pub made: Option<MadeLayer>,- /// Edit › Transform › Skew / Distort / Perspective (`Free` for Free Transform).+ /// Edit › Transform › Rotate / Skew / Distort / Perspective (`Free` for Free Transform and Scale).#[serde(default)]pub mode: TransformMode,/// Edit › Content-Aware Scale (#3096): the box only scales and moves, and OK runsdiff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex dafe4dfe3..b16831332 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -1501,6 +1501,13 @@ pub(crate) fn apply_mode_drag(s: &mut TransformSession, g: Gesture, p: [f64; 2],s.quad = keep_convex(q0, q);s.pivot = carry_pivot(q0, s.quad, g.pivot0);}+ // Rotate (#3941): a corner or edge turns about the reference point, the same drag as the+ // zone just outside the box. Free Transform still scales from those handles.+ (TransformMode::Rotate, Hit::Corner(_) | Hit::Edge(_)) => {+ let mut g = g;+ g.hit = Hit::Outside;+ apply_drag(s, g, p, mods);+ }// Skew: an edge slides only along itself (⌘⇧ in Free Transform).(TransformMode::Skew, Hit::Edge(_)) => {mods.command = true;@@ -1935,7 +1942,10 @@ pub fn split(app: &mut PhotocraftApp, id: &str, at: Option<[f64; 2]>) -> Result<fn near_rotate_corner(t: &TransformSession, p: [f64; 2], tol: f64) -> bool {t.warp.is_none()&& t.mode != TransformMode::Distort- && hit(t, p, tol) == Hit::Outside+ && {+ let h = hit(t, p, tol);+ h == Hit::Outside || (t.mode == TransformMode::Rotate && matches!(h, Hit::Corner(_) | Hit::Edge(_)))+ }&& t.quad.iter().any(|q| (p[0] - q[0]).hypot(p[1] - q[1]) <= tol * 3.0)}@@ -3854,6 +3864,25 @@ mod tests {assert_eq!(app.ui.transform.as_ref().unwrap().mode, TransformMode::Free);}+ /// #3941: Edit › Transform › Rotate must rotate from a corner handle, not scale.+ #[test]+ fn rotate_mode_turns_the_box_from_a_corner_handle() {+ let mut app = app_with_square(64, photocraft_geom::Rect::new(8, 8, 24, 24));+ app.ui.extras.snap = false;+ let ctx = egui::Context::default();+ crate::menus::invoke(&mut app, &ctx, "edit.transform.rotate", json!({})).unwrap();+ assert_eq!(app.ui.transform.as_ref().unwrap().mode, TransformMode::Rotate);+ let q0 = app.ui.transform.as_ref().unwrap().quad;+ press_drag(&mut app, q0[1], [q0[1][0], q0[1][1] + 16.0], egui::Modifiers::NONE);+ let q = app.ui.transform.as_ref().unwrap().quad;+ let w0 = (q0[1][0] - q0[0][0]).hypot(q0[1][1] - q0[0][1]);+ let w = (q[1][0] - q[0][0]).hypot(q[1][1] - q[0][1]);+ assert!((w - w0).abs() < 1e-6, "side length stays {w0}, got {w}: {q:?}");+ assert!((q[0][1] - q[1][1]).abs() > 1.0, "the box rotated, not scaled: {q:?}");+ crate::menus::invoke(&mut app, &ctx, "edit.transform.scale", json!({})).unwrap();+ assert_eq!(app.ui.transform.as_ref().unwrap().mode, TransformMode::Free);+ }+fn near2(a: Option<[f64; 2]>, b: [f64; 2]) -> bool {a.is_some_and(|a| (a[0] - b[0]).abs() < 1e-9 && (a[1] - b[1]).abs() < 1e-9)}