Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Move typed text with a drag from just outside it (#5956)

Hector Villarreal00697fb77799
diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs
index 20e59a01d..09aaff253 100644
--- a/crates/ui-egui/src/type_tool_tests.rs
+++ b/crates/ui-egui/src/type_tool_tests.rs
@@ -777,6 +777,37 @@ fn assert_affine(a: Affine, b: Affine) {
}
}
+/// #5956: while typing, a plain drag from just outside the text moves it (Photoshop's Move cursor
+/// there), and typing carries on; a press further out commits the text, as before.
+#[test]
+fn a_drag_just_outside_the_typed_text_moves_it() {
+ let (app, id) = transform_app(false, false, Affine::translate(500.0, 350.0));
+ let mut h = harness(1.0, app);
+ let f = crate::type_transform::frame(h.state_mut()).unwrap();
+ let zoom = f64::from(h.state().point_zoom());
+ let mid = (f.quad[1][1] + f.quad[2][1]) / 2.0;
+ let near = frame_point(h.state(), [f.quad[1][0] + 20.0 / zoom, mid]);
+ let (ctx, view) = (h.ctx.clone(), xf(h.state()));
+ let cursor = crate::type_transform::cursor(h.state_mut(), &ctx, &view, near, Modifiers::NONE);
+ assert_eq!(cursor, Some(egui::CursorIcon::Move), "the Move cursor just outside the text");
+ let before = text(h.state(), id);
+ let count = h.state().session.active().unwrap().history.entries().len();
+ transform_drag(&mut h, near, near + egui::vec2(60.0, 30.0), Modifiers::NONE, false);
+ let after = text(h.state(), id);
+ assert_affine(after.transform, Affine::translate(60.0 / zoom, 30.0 / zoom).mul(&before.transform));
+ assert_eq!(after.text, before.text);
+ assert_eq!(h.state().ui.text_edit.as_ref().map(|e| e.layer), Some(id.0), "still typing");
+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), count + 1);
+ // Further out: the press commits the text.
+ let f = crate::type_transform::frame(h.state_mut()).unwrap();
+ let far = frame_point(h.state(), [f.quad[1][0] + 100.0 / zoom, (f.quad[1][1] + f.quad[2][1]) / 2.0]);
+ let view = xf(h.state());
+ assert_eq!(crate::type_transform::cursor(h.state_mut(), &ctx, &view, far, Modifiers::NONE), None);
+ click(&mut h, far);
+ assert!(h.state().ui.text_edit.is_none(), "committed");
+ assert_affine(text(h.state(), id).transform, after.transform);
+}
+
/// #1091: modifier changes alone draw/hide the oriented frame without touching editing/history.
#[test]
fn temporary_type_transform_visibility_and_cursors() {
diff --git a/crates/ui-egui/src/type_transform.rs b/crates/ui-egui/src/type_transform.rs
index 0c1b4837e..e139edcf6 100644
--- a/crates/ui-egui/src/type_transform.rs
+++ b/crates/ui-egui/src/type_transform.rs
@@ -17,6 +17,10 @@ use crate::transform_tool::{Gesture, Hit};
const PREVIEW_BASE: u64 = 1 << 37;
const ROTATE_PX: f64 = 28.0;
+/// How far outside the text being typed a plain press moves it, as the Move tool (#5956), in
+/// screen points. Photoshop 2025, measured at 100 %: a drag from 35 pt off the glyphs moved the
+/// text without ending typing, one from 48 pt committed it instead.
+const MOVE_REACH_PT: f64 = 40.0;
/// Cached rendered preview and bounded damage history. Unchanged pointer positions render nothing.
pub(crate) struct Preview {
@@ -140,6 +144,28 @@ fn near_border(t: &TransformSession, p: [f64; 2], tol: f64) -> bool {
})
}
+/// The text's frame when `p` is just outside it, where a plain press moves the text (#5956).
+fn move_zone(app: &mut PhotocraftApp, p: [f64; 2]) -> Option<TransformSession> {
+ let t = frame(app)?;
+ let reach = MOVE_REACH_PT / f64::from(app.point_zoom().max(0.01));
+ (crate::transform_tool::hit(&t, p, 0.0) == Hit::Outside && near_border(&t, p, reach)).then_some(t)
+}
+
+/// Start a temporary drag of `frame` from `p` on `h`. Returns false when it can't start.
+fn start(app: &mut PhotocraftApp, mut frame: TransformSession, h: Hit, p: [f64; 2]) -> bool {
+ let Some((_, original, _)) = crate::type_tool::layout(app, LayerId(frame.layer)) else { return false };
+ let Some(st) = app.session.active() else { return false };
+ let (document, revision) = (st.doc.id.0, st.revision);
+ frame.session = app.ui.alloc_id();
+ let gesture = Gesture { hit: h, start: p, quad0: frame.quad, pivot0: frame.pivot };
+ app.ui.type_transform = Some(TypeTransform { document, revision, original, frame, gesture });
+ if let Some(ed) = app.ui.text_edit.as_mut() {
+ ed.dragging = false;
+ ed.resize = None;
+ }
+ true
+}
+
fn hit(app: &PhotocraftApp, t: &TransformSession, p: [f64; 2]) -> Option<Hit> {
let h = crate::transform_tool::hit(t, p, crate::transform_tool::handle_tolerance(app));
(h != Hit::Outside || near_border(t, p, ROTATE_PX / f64::from(app.point_zoom().max(0.01)))).then_some(h)
@@ -242,20 +268,22 @@ pub(crate) fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: Modifiers) -
if !p.iter().all(|v| v.is_finite()) {
return true;
}
- let Some(mut frame) = frame(app) else { return true };
+ let Some(frame) = frame(app) else { return true };
let Some(h) = hit(app, &frame, p) else { return true };
- let Some((_, original, _)) = crate::type_tool::layout(app, LayerId(frame.layer)) else { return true };
- let Some(st) = app.session.active() else { return true };
- let (document, revision) = (st.doc.id.0, st.revision);
- frame.session = app.ui.alloc_id();
- let gesture = Gesture { hit: h, start: p, quad0: frame.quad, pivot0: frame.pivot };
- app.ui.type_transform = Some(TypeTransform { document, revision, original, frame, gesture });
- if let Some(ed) = app.ui.text_edit.as_mut() {
- ed.dragging = false;
- ed.resize = None;
- }
+ start(app, frame, h, p);
true
}
+ // Just outside the text, a plain press moves it as the Move tool would, without ending
+ // typing, as in Photoshop (#5956). Further out the press commits (`type_tool`), and a
+ // paragraph box's handles still resize it.
+ ToolEvent::Down { x, y, .. } if x.is_finite() && y.is_finite() => {
+ let id = app.ui.text_edit.as_ref().map(|e| LayerId(e.layer));
+ if id.is_some_and(|id| crate::type_tool::box_handle_at(app, id, x, y).is_some()) {
+ return false;
+ }
+ let Some(frame) = move_zone(app, [x, y]) else { return false };
+ start(app, frame, Hit::Inside, [x, y])
+ }
ToolEvent::Move { x, y, .. } | ToolEvent::Up { x, y } if active(app) => {
update(app, [x, y], mods);
if matches!(ev, ToolEvent::Up { .. }) {
@@ -380,7 +408,10 @@ pub(crate) fn cursor(app: &mut PhotocraftApp, ctx: &egui::Context, xf: &ViewXfor
if !visible(app, mods) {
let id = LayerId(app.ui.text_edit.as_ref()?.layer);
let q = xf.to_doc(p);
- let handle = crate::type_tool::box_handle_at(app, id, q[0], q[1])?;
+ let Some(handle) = crate::type_tool::box_handle_at(app, id, q[0], q[1]) else {
+ // Just outside the text a press moves it (#5956): the Move cursor, as in Photoshop.
+ return move_zone(app, q).map(|_| CursorIcon::Move);
+ };
let t = frame(app)?;
let direction = if handle < 4 {
let a = *t.quad.get(usize::from(handle))?;