PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Move tool: double-click type on the canvas to edit it (#1132) (#3300)
LloydNicholson76c7d723f4b7
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 4f8ad0412..b7676386b 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3156,6 +3156,18 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:app.defer_live_stroke = false;crate::magnetic_lasso_ui::flush(app);}+ // Move tool: a double-click on type edits it, as in Photoshop (#1132). Only the selected+ // Move tool, not ⌘ held over another tool; an open transform box keeps its double-click+ // (commit, below).+ if tool == Tool::Move+ && app.ui.tool == Tool::Move+ && app.ui.transform.is_none()+ && response.double_clicked()+ && let Some(p) = response.interact_pointer_pos()+ {+ let d = xf.to_doc(p);+ crate::type_tool::edit_at(app, d[0], d[1]);+ }if app.ui.transform.is_some() && response.double_clicked() {crate::transform_tool::commit(app);}diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rsindex d80e234dd..3166caff4 100644--- a/crates/ui-egui/src/type_tool.rs+++ b/crates/ui-egui/src/type_tool.rs@@ -126,6 +126,29 @@ pub fn edit_active(app: &mut PhotocraftApp) -> Result<(), String> {Ok(())}+/// Double-click with the Move tool (Photoshop CC 2019+): edit the type layer under document+/// point (`x`, `y`) with the caret on the glyph there, switching to the Type tool. False when no+/// type is there.+pub fn edit_at(app: &mut PhotocraftApp, x: f64, y: f64) -> bool {+ let Some(id) = hit_layer(app, x, y) else { return false };+ commit(app);+ if app.session.select_layer(id).is_err() {+ return false;+ }+ let key = session_key(app);+ if begin_edit(app, id, &key).is_err() {+ return false;+ }+ let off = hit_offset(app, id, x, y);+ app.ui.text_edit =+ Some(TextEdit { layer: id.0, caret: off, anchor: off, session: key, created: false, mask: false, dragging: false, resize: None, preedit: None });+ let vertical = app.session.active().and_then(|st| text_layer(&st.doc, id)).is_some_and(|t| t.orientation == photocraft_doc::text::Orientation::Vertical);+ app.ui.tool = if vertical { crate::state::Tool::VerticalType } else { crate::state::Tool::Type };+ app.ui.mask_target = false;+ app.ui.vector_mask_target = false;+ true+}+fn hit_offset(app: &mut PhotocraftApp, id: LayerId, x: f64, y: f64) -> usize {let Some((l, aff, text)) = layout(app, id) else { return 0 };let (tx, ty) = to_text(&aff, x, y);diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rsindex 62c6a967d..d9bd9148f 100644--- a/crates/ui-egui/src/type_tool_tests.rs+++ b/crates/ui-egui/src/type_tool_tests.rs@@ -19,7 +19,17 @@ fn harness(ppp: f32, app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {}fn make_harness(ppp: f32, app: PhotocraftApp, render: bool) -> Harness<'static, PhotocraftApp> {- let builder = Harness::builder().with_size(vec2(1200.0, 800.0)).with_pixels_per_point(ppp);+ make_harness_with(Harness::builder(), ppp, app, render)+}++/// 60 frames a second: every queued event is a frame, and the default quarter-second step puts+/// the second click of a double-click past egui's double-click delay.+fn fast_harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {+ make_harness_with(Harness::builder().with_step_dt(1.0 / 60.0), 1.0, app, false)+}++fn make_harness_with(builder: egui_kittest::HarnessBuilder<PhotocraftApp>, ppp: f32, app: PhotocraftApp, render: bool) -> Harness<'static, PhotocraftApp> {+ let builder = builder.with_size(vec2(1200.0, 800.0)).with_pixels_per_point(ppp);let builder = if render { builder.wgpu() } else { builder };let mut h = builder.build_ui_state(|ui, app: &mut PhotocraftApp| {@@ -89,6 +99,16 @@ fn click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2) {h.run_steps(1);}+/// Two clicks within egui's double-click delay (use [`fast_harness`]).+fn double_click(h: &mut Harness<'static, PhotocraftApp>, p: Pos2) {+ h.hover_at(p);+ h.run_steps(1);+ for pressed in [true, false, true, false] {+ h.event(egui::Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE });+ }+ h.run_steps(2);+}+fn drag(h: &mut Harness<'static, PhotocraftApp>, a: Pos2, b: Pos2) {h.hover_at(a);h.run_steps(1);@@ -1522,6 +1542,36 @@ mod multi_layer_formatting {}}+/// #1132: with the Move tool, double-clicking type on the canvas edits it (Photoshop CC 2019+):+/// the Type tool takes over with the caret on the glyph under the pointer, on whichever type+/// layer was double-clicked, even when another layer is active. Elsewhere it does nothing.+#[test]+fn move_tool_double_click_on_type_edits_it() {+ let mut app = new_app();+ let id = LayerId(app.run("type.create", json!({"text": "HOHOHO", "size": 120, "x": 300, "y": 420})).unwrap()["layer"].as_u64().unwrap());+ app.run("layer.new.layer", json!({})).unwrap();+ app.ui.tool = crate::state::Tool::Move;+ let mut h = fast_harness(app);+ let p = glyph(&mut h, id, 2, 0.2);+ let steps = h.state().session.active().unwrap().history.entries().len();+ double_click(&mut h, p);+ assert_eq!(h.state().ui.tool, crate::state::Tool::Type, "the Type tool takes over");+ assert_eq!(h.state().ui.text_edit.as_ref().map(|e| e.layer), Some(id.0));+ assert_eq!(selection(&h), (2, 2), "the caret sits on the double-clicked glyph");+ assert_eq!(h.state().session.active().unwrap().active_layer, Some(id));+ assert_eq!(h.state().session.active().unwrap().history.entries().len(), steps, "nothing moved");+ super::insert(h.state_mut(), "x");+ assert_eq!(text(h.state(), id).text, "HOxHOHO");+ // Off the text, a double-click stays with the Move tool.+ super::commit(h.state_mut());+ h.state_mut().ui.tool = crate::state::Tool::Move;+ h.run_steps(2);+ let empty = screen(&mut h, id, -400.0, -300.0);+ double_click(&mut h, empty);+ assert_eq!(h.state().ui.tool, crate::state::Tool::Move);+ assert!(h.state().ui.text_edit.is_none());+}+fn selection_bounds(app: &PhotocraftApp) -> Option<photocraft_geom::Rect> {app.session.active().unwrap().doc.selection.as_ref().map(|m| m.content_bounds())}diff --git a/docs/ui-parity.md b/docs/ui-parity.mdindex 270214e1f..71f789743 100644--- a/docs/ui-parity.md+++ b/docs/ui-parity.md@@ -96,6 +96,7 @@ Triangle and Curvature Pen (minus legacy Rounded Rectangle and Targeted Adjustme| Auto-Select bounding box and snapping follow the picked layer | partial | follow the previous layer (#2666) || ⌘-drag temporary Move shows the box and snaps | partial | #2681 || ⌘-right-click layer list under the pointer | done | `layer_pick_ui.rs` |+| Double-click type to edit it (caret at the pointer, Type tool) | done | #1132, `type_tool::edit_at` || Moving a selection on a mask moves the mask, not the pixels | partial | #2468 |## Selection modifiers