Skip to content

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

Type: paragraph box handles are picked at the unsnapped press (#6819)

David Mannc354a25a6b14
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 57d357511..bf93f2359 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -5445,7 +5445,14 @@ pub fn tool_event(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers)
polygon_click(app, x, y, mods);
return;
}
- Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask if crate::type_tool::pointer_down(app, x, y, mods) => {
+ // The Type tools pick the box handle or glyph under the press where the user
+ // clicked: the snapped press can land off an edge handle's grab area (#6819).
+ Tool::Type | Tool::VerticalType | Tool::HorizontalTypeMask | Tool::VerticalTypeMask
+ if {
+ let [px, py] = if let ToolEvent::Down { x, y, .. } = raw { [x, y] } else { [x, y] };
+ crate::type_tool::pointer_down(app, px, py, mods)
+ } =>
+ {
return;
}
_ => {}
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index e0b122c37..892bd4ca5 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -235,6 +235,12 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
// doesn't snap to its own layer.
let exclude = if cut { app.session.active().map(|s| s.selected_layers()).unwrap_or_default() } else { Vec::new() };
selection_rect(app).map(|rect| (Gesture::SelectionMove { rect }, exclude))
+ } else if tool.is_type()
+ && let Some(layer) = app.ui.text_edit.as_ref().map(|e| LayerId(e.layer))
+ {
+ // Editing type: a paragraph-box handle drag snaps to guides and other layers, not to the
+ // text being resized; a press in the text places the caret where it is (#6819).
+ crate::type_tool::box_handle_at(app, layer, p[0], p[1]).map(|_| (Gesture::Point, vec![layer]))
} else if is_point_tool(tool) {
Some((Gesture::Point, Vec::new()))
} else {
diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs
index 7a23c9ccc..763a92a2d 100644
--- a/crates/ui-egui/src/type_tool_tests.rs
+++ b/crates/ui-egui/src/type_tool_tests.rs
@@ -1928,6 +1928,77 @@ fn multi_click_selects_word_line_paragraph_and_all_in_point_text() {
assert_eq!(selection(&h), (0, edited.chars().count()), "⌘A selects all text");
}
+/// #6819: all eight paragraph-box handles resize the box while editing, with View › Snap on (the
+/// default), at 25–200 % zoom, 1× and 2× HiDPI, for a plain, a scaled and a rotated layer. A press
+/// on a handle (anywhere in its grab area) never moves the caret, and the edited text's own glyph
+/// bounds don't pull the press off the handle.
+#[test]
+fn every_paragraph_box_handle_resizes_the_box_with_snapping_on() {
+ let scaled = Affine { m: [1.5, 0.0, 0.0, 0.75, 300.0, 300.0] };
+ let rotated = Affine { m: [1.2 * 0.94, 1.2 * 0.342, -1.2 * 0.342, 1.2 * 0.94, 300.0, 300.0] };
+ for ppp in [1.0, 2.0] {
+ for zoom in [0.25f32, 0.5, 1.0, 2.0] {
+ for (name, tf) in [("plain", None), ("scaled", Some(scaled)), ("rotated", Some(rotated))] {
+ let mut app = new_app();
+ assert!(app.ui.extras.snap, "snapping is on by default");
+ let size = 24.0 / zoom;
+ let p = json!({"text": "HOHOHO HOHO HOHOHO HOHO HOHOHO", "size": size, "x": 300, "y": 300, "box": [300.0, 300.0, size * 8.0, size * 4.0]});
+ let id = LayerId(app.run("type.create", p).unwrap()["layer"].as_u64().unwrap());
+ if let Some(a) = tf {
+ app.run("type.edit", json!({"layer": id.0, "transform": a.m})).unwrap();
+ }
+ let mut h = harness(ppp, app);
+ let (bx, by, w, bh) = super::box_shape(h.state(), id).unwrap();
+ let aff = layout(h.state_mut(), id).unwrap().1;
+ let m = aff.apply(Point::new(f64::from(bx + w / 2.0), f64::from(by + bh / 2.0)));
+ let v = &mut h.state_mut().ui.views[0];
+ (v.zoom, v.center, v.fit_pending) = (zoom, [m.x as f32, m.y as f32], false);
+ h.run_steps(2);
+ let ctx = format!("ppp {ppp} zoom {zoom} {name}");
+ let p = glyph(&mut h, id, 2, 0.2);
+ click(&mut h, p);
+ assert_eq!(selection(&h), (2, 2), "{ctx}: editing");
+ for i in 0..8u8 {
+ // Press at the handle and inside its grab area, towards the text.
+ for off in [0.0f32, 4.0] {
+ let (bx, by, w, bh) = super::box_shape(h.state(), id).unwrap();
+ let (l, r, t, b) = super::handle_sides(i);
+ let hx = if l {
+ bx
+ } else if r {
+ bx + w
+ } else {
+ bx + w / 2.0
+ };
+ let hy = if t {
+ by
+ } else if b {
+ by + bh
+ } else {
+ by + bh / 2.0
+ };
+ let dir = vec2(f32::from(u8::from(r)) - f32::from(u8::from(l)), f32::from(u8::from(b)) - f32::from(u8::from(t)));
+ let at = screen(&mut h, id, hx, hy);
+ let out = (screen(&mut h, id, hx + dir.x * 10.0, hy + dir.y * 10.0) - at).normalized();
+ let a = at - out * off;
+ drag(&mut h, a, a + out * 30.0);
+ let ctx = format!("{ctx} handle {i} offset {off}");
+ assert_eq!(selection(&h), (2, 2), "{ctx}: the caret stays put");
+ let (_, _, w2, h2) = super::box_shape(h.state(), id).unwrap();
+ // 30 points outward (a corner's diagonal: about 21 per side) is that over
+ // zoom × layer scale in text units; snapping may shift the release by up to
+ // its 8 point reach.
+ let min = 6.0 / (zoom / h.state().canvas_ppp()) / 1.5;
+ let grew = |was: f32, now: f32, moved: bool| if moved { now - was >= min } else { (now - was).abs() < 1e-3 };
+ assert!(grew(w, w2, l || r), "{ctx}: width {w} -> {w2}");
+ assert!(grew(bh, h2, t || b), "{ctx}: height {bh} -> {h2}");
+ }
+ }
+ }
+ }
+ }
+}
+
/// Paragraph text: three clicks select only the visual (soft-wrapped) line, four the whole
/// paragraph; vertical type selects the column the same way.
#[test]