Skip to content

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

Address CodeRabbit review: the held Type press keeps the path pointer until it drags a box (#6230)

David Mann7d13c854a382
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index fd43ed6f7..e8fdbe95d 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -460,16 +460,29 @@ pub(crate) fn path_target(app: &PhotocraftApp, x: f64, y: f64) -> Option<PathCli
/// would make, unless the press edits the type layer under it instead (`pointer_down`: an
/// unshifted press on shown type), where Photoshop keeps the plain I-beam. The type hit is only
/// tested over a path, so a hover elsewhere lays out nothing. Nothing while a type edit is open
-/// (a click then commits rather than starting new type) or during a drag (dragging out a text
-/// box, whose release makes a box, not path type).
+/// (a click then commits rather than starting new type). While the button is held it is what the
+/// release makes (`pointer_up`): nothing once the drag is a text box, else the path click at the
+/// press.
pub(crate) fn hover_path_target(app: &mut PhotocraftApp, x: f64, y: f64, shift: bool) -> Option<PathClick> {
- if app.ui.text_edit.is_some() || app.drag.is_some() {
+ if app.ui.text_edit.is_some() {
return None;
}
+ if let Some(start) = app.drag.as_ref().map(|d| d.start) {
+ // The press already passed over editing type (`pointer_down`), so no type hit test.
+ return if makes_box(app, start, [x, y]) { None } else { path_target(app, start[0], start[1]) };
+ }
let target = path_target(app, x, y)?;
(shift || hit_layer(app, x, y).is_none()).then_some(target)
}
+/// Whether a press at `start` released at `end` drags out a paragraph box rather than clicking
+/// (at least 4 screen pixels both ways).
+fn makes_box(app: &PhotocraftApp, start: [f64; 2], end: [f64; 2]) -> bool {
+ let (w, h) = ((end[0] - start[0]).abs(), (end[1] - start[1]).abs());
+ let min = 4.0 / app.point_zoom().max(0.01) as f64;
+ w >= min && h >= min
+}
+
/// Pointer up. `rect` is the dragged box (document px) when no edit session consumed the press.
pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
if let Some(e) = app.ui.text_edit.as_mut() {
@@ -484,7 +497,7 @@ pub fn pointer_up(app: &mut PhotocraftApp, start: [f64; 2], end: [f64; 2]) {
let mask = app.ui.tool.is_type_mask();
// A click (not a drag) with the Type tool on a path: area type inside a closed path, type
// on the path on its outline. The Type Mask tools ignore paths.
- let target = if !mask && !(w >= min && h >= min) { path_target(app, start[0], start[1]) } else { None };
+ let target = if !mask && !makes_box(app, start, end) { path_target(app, start[0], start[1]) } else { None };
// Preferences ▸ Type: "Fill new type layers with placeholder text" (on by default). The Type
// Mask tools start empty, as Photoshop's do.
let text = if !mask && app.session.prefs().type_.fill_new_type_layers_with_placeholder { PLACEHOLDER } else { "" };
diff --git a/crates/ui-egui/src/type_tool_tests.rs b/crates/ui-egui/src/type_tool_tests.rs
index 65fc3d2e1..ddb3bc42a 100644
--- a/crates/ui-egui/src/type_tool_tests.rs
+++ b/crates/ui-egui/src/type_tool_tests.rs
@@ -2837,17 +2837,32 @@ fn clicking_on_a_path_outline_starts_type_on_the_path_where_clicked() {
);
}
-/// The path pointer shows what a click would do: none while a text box is dragged out over a
-/// path (the release makes a box) or while a type edit is open (a click commits).
+/// The path pointer shows what the release would do: kept while a press on a path is held or
+/// barely moves (the release still makes path type there), none once a text box is dragged out
+/// over a path (the release makes a box) or while a type edit is open (a click commits).
#[test]
fn the_path_pointer_is_off_while_dragging_a_text_box_or_editing() {
use crate::canvas::{ToolEvent, tool_event};
let mut app = new_app();
ellipse_layer(&mut app);
assert!(matches!(super::hover_path_target(&mut app, 400.0, 350.0, false), Some(PathClick::Area { .. })), "a hover inside the shape");
- tool_event(&mut app, ToolEvent::Down { x: 1200.0, y: 700.0, pressure: 1.0 }, Modifiers::NONE);
+ // Press and hold on the shape, then a flat drag: still a click on release.
+ tool_event(&mut app, ToolEvent::Down { x: 400.0, y: 350.0, pressure: 1.0 }, Modifiers::NONE);
+ assert!(app.drag.is_some(), "the press starts a drag");
+ assert!(matches!(super::hover_path_target(&mut app, 400.0, 350.0, false), Some(PathClick::Area { .. })), "press and hold");
+ tool_event(&mut app, ToolEvent::Move { x: 1200.0, y: 351.0, pressure: 1.0 }, Modifiers::NONE);
+ assert!(matches!(super::hover_path_target(&mut app, 1200.0, 351.0, false), Some(PathClick::Area { .. })), "a flat drag");
+ tool_event(&mut app, ToolEvent::Up { x: 1200.0, y: 351.0 }, Modifiers::NONE);
+ let id = LayerId(app.ui.text_edit.as_ref().expect("editing").layer);
+ assert!(
+ matches!(text_layer(&app.session.active().unwrap().doc, id).unwrap().shape, photocraft_doc::text::TextShape::Area { .. }),
+ "the release made area type"
+ );
+ let mut app = new_app();
+ ellipse_layer(&mut app);
+ tool_event(&mut app, ToolEvent::Down { x: 300.0, y: 300.0, pressure: 1.0 }, Modifiers::NONE);
tool_event(&mut app, ToolEvent::Move { x: 400.0, y: 350.0, pressure: 1.0 }, Modifiers::NONE);
- assert!(app.drag.is_some(), "dragging out a box");
+ assert!(app.drag.is_some(), "dragging out a box from inside the shape");
assert!(super::hover_path_target(&mut app, 400.0, 350.0, false).is_none(), "the drag makes a box, not area type");
tool_event(&mut app, ToolEvent::Up { x: 400.0, y: 350.0 }, Modifiers::NONE);
assert!(app.ui.text_edit.is_some(), "the box is being edited");