Skip to content

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

Type: ⌥ on a paragraph-box handle resizes the block symmetrically

VladimirPozdnyakov064d8429d501
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 391ad464f..5fb8aae0e 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -5034,7 +5034,7 @@ pub(crate) fn composite_color(app: &mut PhotocraftApp, x: f64, y: f64) -> Option
fn tool_move(app: &mut PhotocraftApp, x: f64, y: f64, pressure: f32, mods: egui::Modifiers) {
let tool = app.active_tool();
if tool.is_type() && app.drag.is_none() {
- crate::type_tool::pointer_move(app, x, y);
+ crate::type_tool::pointer_move(app, x, y, mods);
}
if tool == Tool::Pen && mods.alt {
crate::vector_ui::pen_break_handle(app);
diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs
index 5cbda434e..01da47735 100644
--- a/crates/ui-egui/src/state.rs
+++ b/crates/ui-egui/src/state.rs
@@ -967,6 +967,17 @@ pub enum MadeLayer {
Place,
}
+/// A paragraph-box handle drag: the handle, and the box (text-space) and layer transform as
+/// they were at the press. Every move recomputes from this snapshot, so ⌥ pressed or released
+/// mid-drag mirrors about the centre the box started with — as if it had been held from the
+/// start.
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub struct BoxResize {
+ pub handle: u8,
+ pub shape: [f32; 4],
+ pub aff: photocraft_geom::Affine,
+}
+
/// In-progress inline type editing (Type tool). Offsets are character indices.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TextEdit {
@@ -984,7 +995,7 @@ pub struct TextEdit {
pub dragging: bool,
/// Paragraph-box handle being dragged (0-3 corners from top-left clockwise, 4-7 top/right/bottom/left edges).
#[serde(skip)]
- pub resize: Option<u8>,
+ pub resize: Option<BoxResize>,
/// IME composition in progress: (start, length) in characters. The preedit text lives in the
/// layer so it lays out like typed text; each IME update replaces it.
#[serde(skip)]
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index d73bb2e2c..83cd8f693 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -15,7 +15,7 @@ use serde_json::json;
use crate::PhotocraftApp;
use crate::canvas::ViewXform;
-use crate::state::TextEdit;
+use crate::state::{BoxResize, TextEdit};
pub const PLACEHOLDER: &str = "Lorem Ipsum";
@@ -212,18 +212,23 @@ pub(crate) fn box_handle_at(app: &mut PhotocraftApp, id: LayerId, x: f64, y: f64
.map(|i| i as u8)
}
-/// Drag handle `i` to the document point: the dragged edges follow it, the others stay put.
-fn resize_box(app: &mut PhotocraftApp, ed: &TextEdit, i: u8, x: f64, y: f64) {
- let id = LayerId(ed.layer);
- let Some((bx, by, w, h)) = box_shape(app, id) else { return };
- let Some((_, aff, _)) = layout(app, id) else { return };
- let (px, py) = to_text(&aff, x, y);
- let (l, r, t, b) = handle_sides(i);
+/// Drag the handle of `r` (the box and transform snapshotted at the press) to the document
+/// point: the dragged edges follow it, the others stay put. With ⌥ the opposite edges mirror
+/// the drag about the centre the box started with, so the block grows symmetrically instead of
+/// from one side — pressing or releasing ⌥ mid-drag switches that on and off as if it had been
+/// held from the start.
+fn resize_box(app: &mut PhotocraftApp, ed: &TextEdit, r: BoxResize, x: f64, y: f64, alt: bool) {
+ let (px, py) = to_text(&r.aff, x, y);
+ let (l, rw, t, b) = handle_sides(r.handle);
+ let (bx, by, w, h) = (r.shape[0], r.shape[1], r.shape[2], r.shape[3]);
let (mut x0, mut y0, mut x1, mut y1) = (bx, by, bx + w, by + h);
+ // The centre the ⌥ mirror turns about: the box as it stood at the press (every move
+ // recomputes from that snapshot, so the centre holds for the whole drag).
+ let (cx, cy) = (bx + w / 2.0, by + h / 2.0);
if l {
x0 = px.min(x1 - MIN_BOX);
}
- if r {
+ if rw {
x1 = px.max(x0 + MIN_BOX);
}
if t {
@@ -232,8 +237,28 @@ fn resize_box(app: &mut PhotocraftApp, ed: &TextEdit, i: u8, x: f64, y: f64) {
if b {
y1 = py.max(y0 + MIN_BOX);
}
+ if alt {
+ // The mirror keeps the centre fixed: the dragged edge clamps at MIN_BOX/2 from it, and
+ // the opposite edge follows, so the block never inverts below the minimum.
+ if l {
+ x0 = px.min(cx - MIN_BOX / 2.0);
+ x1 = 2.0 * cx - x0;
+ }
+ if rw {
+ x1 = px.max(cx + MIN_BOX / 2.0);
+ x0 = 2.0 * cx - x1;
+ }
+ if t {
+ y0 = py.min(cy - MIN_BOX / 2.0);
+ y1 = 2.0 * cy - y0;
+ }
+ if b {
+ y1 = py.max(cy + MIN_BOX / 2.0);
+ y0 = 2.0 * cy - y1;
+ }
+ }
// type.edit places the box's top-left at the given document point.
- let o = aff.apply(Point::new(f64::from(x0), f64::from(y0)));
+ let o = r.aff.apply(Point::new(f64::from(x0), f64::from(y0)));
let _ = app.run("type.edit", json!({"layer": ed.layer, "box": [o.x, o.y, x1 - x0, y1 - y0], "coalesce": ed.session}));
}
@@ -242,8 +267,11 @@ pub fn pointer_down(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifie
if let Some(ed) = app.ui.text_edit.clone() {
let id = LayerId(ed.layer);
if let Some(i) = box_handle_at(app, id, x, y) {
- if let Some(e) = app.ui.text_edit.as_mut() {
- e.resize = Some(i);
+ // Snapshot the box and transform now: every move recomputes from them, so ⌥ held
+ // or released mid-drag mirrors about the centre the drag started with.
+ let snap = box_shape(app, id).zip(layout(app, id)).map(|((bx, by, w, h), (_, aff, _))| BoxResize { handle: i, shape: [bx, by, w, h], aff });
+ if let (Some(snap), Some(e)) = (snap, app.ui.text_edit.as_mut()) {
+ e.resize = Some(snap);
}
return true;
}
@@ -282,10 +310,10 @@ pub fn pointer_down(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifie
false
}
-pub fn pointer_move(app: &mut PhotocraftApp, x: f64, y: f64) {
+pub fn pointer_move(app: &mut PhotocraftApp, x: f64, y: f64, mods: egui::Modifiers) {
let Some(ed) = app.ui.text_edit.clone() else { return };
- if let Some(i) = ed.resize {
- resize_box(app, &ed, i, x, y);
+ if let Some(r) = ed.resize {
+ resize_box(app, &ed, r, x, y, mods.alt);
return;
}
if ed.dragging {
@@ -2636,9 +2664,9 @@ mod tests {
let steps = app.session.active().unwrap().history.entries().len();
// Top-left corner: the box keeps its bottom-right corner.
assert!(pointer_down(&mut app, 10.0, 10.0, egui::Modifiers::NONE));
- assert_eq!(app.ui.text_edit.as_ref().unwrap().resize, Some(0));
- pointer_move(&mut app, 20.0, 25.0);
- pointer_move(&mut app, 30.0, 30.0);
+ assert_eq!(app.ui.text_edit.as_ref().unwrap().resize.map(|r| r.handle), Some(0));
+ pointer_move(&mut app, 20.0, 25.0, egui::Modifiers::NONE);
+ pointer_move(&mut app, 30.0, 30.0, egui::Modifiers::NONE);
pointer_up(&mut app, [10.0, 10.0], [30.0, 30.0]);
assert_eq!(box_shape(&app, id), Some((0.0, 0.0, 80.0, 30.0)));
let aff = layout(&mut app, id).unwrap().1;
@@ -2648,9 +2676,17 @@ mod tests {
assert!(app.ui.text_edit.is_some());
// Right edge: only the width changes, and never below the minimum.
assert!(pointer_down(&mut app, 110.0, 45.0, egui::Modifiers::NONE));
- pointer_move(&mut app, 0.0, 99.0);
+ pointer_move(&mut app, 0.0, 99.0, egui::Modifiers::NONE);
pointer_up(&mut app, [110.0, 45.0], [0.0, 99.0]);
assert_eq!(box_shape(&app, id), Some((0.0, 0.0, MIN_BOX, 30.0)));
+ // ⌥ mirrors the drag about the centre: the left edge follows the right one out.
+ assert!(pointer_down(&mut app, 34.0, 45.0, egui::Modifiers::ALT));
+ pointer_move(&mut app, 70.0, 45.0, egui::Modifiers::ALT);
+ pointer_up(&mut app, [34.0, 45.0], [70.0, 45.0]);
+ assert_eq!(box_shape(&app, id), Some((0.0, 0.0, MIN_BOX + 72.0, 30.0)));
+ let aff = layout(&mut app, id).unwrap().1;
+ // Text space −36..40 (centre 2) through the translation: the doc centre stays at 32.
+ assert_eq!((aff.m[4], aff.m[5]), (-6.0, 30.0));
}
#[test]
diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs
index f56829c6d..b2684de42 100644
--- a/crates/ui-egui/src/type_tool_tests.rs
+++ b/crates/ui-egui/src/type_tool_tests.rs
@@ -2062,3 +2062,85 @@ fn type_mask_shift_adds_and_alt_subtracts_without_a_layer_row() {
assert!(at(&app, stem.0, stem.1) < 0.01, "⌥ subtracts the glyphs");
assert!(at(&app, 1050, 150) > 0.99, "and keeps the rest");
}
+
+/// The paragraph box's document rect: its text-space origin through the layer transform, then
+/// width and height. Only what the symmetric-resize tests need (identity linear part).
+fn box_doc_rect(app: &PhotocraftApp, id: LayerId) -> [f64; 4] {
+ let t = text(app, id);
+ let (x, y, w, h) = match t.shape {
+ photocraft_doc::text::TextShape::Box { x, y, width, height } => (f64::from(x), f64::from(y), f64::from(width), f64::from(height)),
+ ref s => panic!("not a box: {s:?}"),
+ };
+ let p = t.transform.apply(Point::new(x, y));
+ [p.x, p.y, w, h]
+}
+
+/// ⌥ on an edge drag of a paragraph box mirrors the opposite edge about the box's centre, so
+/// the block grows symmetrically; without ⌥ the opposite edge stays put.
+#[test]
+fn alt_dragging_a_box_edge_mirrors_the_opposite_edge() {
+ use crate::canvas::{ToolEvent, tool_event};
+ let (mut app, id) = transform_app(true, false, Affine::translate(300.0, 300.0));
+ app.ui.extras.snap = false;
+ // The box is at (300, 300), 240×180: its bottom-edge handle is the midpoint (420, 480).
+ tool_event(&mut app, ToolEvent::Down { x: 420.0, y: 480.0, pressure: 1.0 }, Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 530.0, pressure: 1.0 }, Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Up { x: 420.0, y: 530.0 }, Modifiers::NONE);
+ assert_eq!(box_doc_rect(&app, id), [300.0, 300.0, 240.0, 230.0], "plain drag: the top edge stays");
+ tool_event(&mut app, ToolEvent::Down { x: 420.0, y: 530.0, pressure: 1.0 }, Modifiers::ALT);
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 580.0, pressure: 1.0 }, Modifiers::ALT);
+ tool_event(&mut app, ToolEvent::Up { x: 420.0, y: 580.0 }, Modifiers::ALT);
+ // The centre (300 + 115 = 415) held: the bottom followed the pointer (580), the top
+ // mirrored up to 250.
+ assert_eq!(box_doc_rect(&app, id), [300.0, 250.0, 240.0, 330.0]);
+}
+
+/// ⌥ pressed or released mid-drag applies as if it had been held from the start: the mirror
+/// turns about the centre the box had at the press, not about wherever the box drifted.
+#[test]
+fn alt_pressed_mid_drag_mirrors_about_the_press_centre() {
+ use crate::canvas::{ToolEvent, tool_event};
+ let (mut app, id) = transform_app(true, false, Affine::translate(300.0, 300.0));
+ app.ui.extras.snap = false;
+ // Bottom edge (420, 480) dragged down 80 px without ⌥: only the bottom moves.
+ tool_event(&mut app, ToolEvent::Down { x: 420.0, y: 480.0, pressure: 1.0 }, Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 520.0, pressure: 1.0 }, Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 560.0, pressure: 1.0 }, Modifiers::NONE);
+ assert_eq!(box_doc_rect(&app, id), [300.0, 300.0, 240.0, 260.0]);
+ // ⌥ pressed mid-drag: the bottom follows the pointer, the top jumps to the mirror about
+ // the press centre (390): 2*390 − 600 = 180 — not about the drifted one (430).
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 600.0, pressure: 1.0 }, Modifiers::ALT);
+ assert_eq!(box_doc_rect(&app, id), [300.0, 180.0, 240.0, 420.0]);
+ // ⌥ released again mid-drag: the top snaps back to where a plain drag leaves it.
+ tool_event(&mut app, ToolEvent::Move { x: 420.0, y: 600.0, pressure: 1.0 }, Modifiers::NONE);
+ assert_eq!(box_doc_rect(&app, id), [300.0, 300.0, 240.0, 300.0]);
+ tool_event(&mut app, ToolEvent::Up { x: 420.0, y: 600.0 }, Modifiers::NONE);
+}
+
+/// ⌥ mirrors a side-edge drag the same way, and a corner drag grows both axes about the
+/// centre; clamped, the ⌥ box stops at MIN_BOX about its centre instead of collapsing.
+#[test]
+fn alt_mirrors_side_and_corner_drags_and_clamps_at_the_minimum() {
+ use crate::canvas::{ToolEvent, tool_event};
+ let drag = |app: &mut PhotocraftApp, from: [f64; 2], to: [f64; 2], mods: Modifiers| {
+ tool_event(app, ToolEvent::Down { x: from[0], y: from[1], pressure: 1.0 }, mods);
+ tool_event(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, mods);
+ tool_event(app, ToolEvent::Up { x: to[0], y: to[1] }, mods);
+ };
+ // Right-edge handle: (540, 390) for the box at (300, 300), 240×180.
+ let (mut app, id) = transform_app(true, false, Affine::translate(300.0, 300.0));
+ app.ui.extras.snap = false;
+ drag(&mut app, [540.0, 390.0], [590.0, 390.0], Modifiers::ALT);
+ // cx = 420 held: the right edge followed to 590, the left mirrored from 300 to 250.
+ assert_eq!(box_doc_rect(&app, id), [250.0, 300.0, 340.0, 180.0]);
+ // Clamped: dragging the right edge past the centre leaves the minimum box about it.
+ drag(&mut app, [590.0, 390.0], [400.0, 390.0], Modifiers::ALT);
+ let r = box_doc_rect(&app, id);
+ assert_eq!(r[2], 4.0, "MIN_BOX wide");
+ assert!(((r[0] + r[2] / 2.0) - 420.0).abs() < 1e-9, "still centred on 420");
+ // Corner handle (bottom-right of the fresh box): ⌥ grows both axes about the centre.
+ let (mut app, id) = transform_app(true, false, Affine::translate(300.0, 300.0));
+ app.ui.extras.snap = false;
+ drag(&mut app, [540.0, 480.0], [590.0, 530.0], Modifiers::ALT);
+ assert_eq!(box_doc_rect(&app, id), [250.0, 250.0, 340.0, 280.0]);
+}