PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Free Transform: ⌘-drag of a corner scales editable type instead of distorting it (#3010) (#3012)
Valentin Kleind9445c7d1da6
diff --git a/crates/ui-egui/src/transform_tool.rs b/crates/ui-egui/src/transform_tool.rsindex b64d04cb6..4933b9290 100644--- a/crates/ui-egui/src/transform_tool.rs+++ b/crates/ui-egui/src/transform_tool.rs@@ -1134,6 +1134,7 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->warp_pointer(app, ev, tol, mods);return true;}+ let no_distort = distort_refused(app, &t);let Some(pv) = app.transform_preview.as_mut() else { return false };match ev {ToolEvent::Down { x, y, .. } => {@@ -1167,13 +1168,35 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->if let (Some(g), Some(s)) = (g, app.ui.transform.as_mut())&& distort_allows(t.mode, g.hit){- apply_drag(s, g, [x, y], mode_mods(t.mode, g.hit, legacy_mods(legacy, g.hit, mods)));+ let (mode, mods) = if no_distort { scale_not_distort(t.mode, g.hit, mods) } else { (t.mode, mods) };+ apply_drag(s, g, [x, y], mode_mods(mode, g.hit, legacy_mods(legacy, g.hit, mods)));}}}true}+/// Editable type can be scaled, rotated and skewed but not distorted (#2630): the rule that greys+/// Distort and Perspective in `enable_rules::disabled`, for the box's own layer. A box on a path,+/// on the selection outline or on a lone mask or channel moves no type.+fn distort_refused(app: &PhotocraftApp, t: &TransformSession) -> bool {+ t.path.is_none()+ && !t.selection+ && t.target.is_none()+ && app.session.active().and_then(|st| st.doc.layer(LayerId(t.layer))).is_some_and(|l| matches!(l.content, LayerContent::Text(_)))+}++/// Where Distort and Perspective are refused ([`distort_refused`]), ⌘-drag and ⌘⌥⇧-drag of a+/// corner scale like a plain corner drag (with ⌥ and ⇧ keeping their meaning), so the box never+/// takes a shape the commit would refuse (#3010).+fn scale_not_distort(mode: TransformMode, hit: Hit, mut mods: egui::Modifiers) -> (TransformMode, egui::Modifiers) {+ if matches!(hit, Hit::Corner(_)) {+ mods.command = false;+ }+ let mode = if matches!(mode, TransformMode::Distort | TransformMode::Perspective) { TransformMode::Free } else { mode };+ (mode, mods)+}+/// Preferences › General › Use Legacy Free Transform, Photoshop's scaling before CC 2019: corner/// drags stretch freely and ⇧ keeps the proportions, edge drags stretch their one axis whatever/// the keys. The default is proportional for both, with ⇧ freeing them.diff --git a/crates/ui-egui/src/transform_type_tests.rs b/crates/ui-egui/src/transform_type_tests.rsindex a26e62234..ea3c128a1 100644--- a/crates/ui-egui/src/transform_type_tests.rs+++ b/crates/ui-egui/src/transform_type_tests.rs@@ -87,3 +87,30 @@ fn free_transform_with_a_selection_frames_and_moves_the_whole_type_layer() {let st = app.session.active().unwrap();assert!(matches!(st.doc.layer(id).unwrap().content, photocraft_doc::LayerContent::Text(_)), "still editable type");}++/// ⌘-drag (distort) and ⌘⌥⇧-drag (perspective) of a corner on editable type scale like a plain+/// corner drag (#3010): the box stays a parallelogram and the commit keeps the type live, instead+/// of a distorted box the engine then refuses.+#[test]+fn distort_and_perspective_corner_drags_scale_editable_type() {+ use crate::canvas::ToolEvent;+ let ctx = egui::Context::default();+ let cmd = egui::Modifiers::COMMAND;+ for mods in [cmd, cmd | egui::Modifiers::ALT | egui::Modifiers::SHIFT] {+ let mut app = app();+ crate::menus::invoke(&mut app, &ctx, "edit.freeTransform", json!({})).unwrap();+ let q0 = app.ui.transform.as_ref().unwrap().quad;+ let [x, y] = q0[1];+ crate::transform_tool::pointer(&mut app, ToolEvent::Down { x, y, pressure: 1.0 }, mods);+ crate::transform_tool::pointer(&mut app, ToolEvent::Move { x: x + 15.0, y: y - 10.0, pressure: 1.0 }, mods);+ crate::transform_tool::pointer(&mut app, ToolEvent::Up { x: x + 15.0, y: y - 10.0 }, mods);+ let q = app.ui.transform.as_ref().unwrap().quad;+ assert_ne!(q, q0, "the drag scaled the box ({mods:?})");+ let skew = (0..2).map(|k| (q[0][k] + q[2][k] - q[1][k] - q[3][k]).abs()).fold(0.0, f64::max);+ assert!(skew < 1e-6, "the box stays a parallelogram ({mods:?}): {q:?}");+ crate::transform_tool::commit(&mut app);+ assert_eq!(app.ui.status, "", "{mods:?}");+ let st = app.session.active().unwrap();+ assert!(matches!(st.doc.layer(st.active_layer.unwrap()).unwrap().content, photocraft_doc::LayerContent::Text(_)), "still editable type");+ }+}