mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
This commit is contained in:
commit
94e2e77b33
5 files changed
+176
-7
No files matched your search
@@ -1235,7 +1235,13 @@ fn handle_canvas_tool_clicks(
|
||||
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() {
|
||||
// A double-click commits an open Free Transform only on the box (handles included); outside
|
||||
// it does nothing (#11100).
|
||||
if let Some(t) = app.ui.transform.as_ref()
|
||||
&& response.double_clicked()
|
||||
&& let Some(p) = response.interact_pointer_pos()
|
||||
&& crate::transform_tool::hit(t, xf.to_doc(p), crate::transform_tool::handle_tolerance(app)) != crate::transform_tool::Hit::Outside
|
||||
{
|
||||
crate::transform_tool::commit(app);
|
||||
}
|
||||
if crate::cam_transform::active(app) && response.double_clicked() {
|
||||
|
||||
@@ -788,6 +788,9 @@ mod pixel_aspect_tests;
|
||||
#[cfg(test)]
|
||||
mod transform_fx_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod transform_commit_click_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod layer_props_ui_tests;
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -61,6 +61,11 @@ pub(super) fn accepts_outside_click(g: Gesture, end: [f64; 2], tol: f64) -> bool
|
||||
g.hit == Hit::Outside && (end[0] - g.start[0]).hypot(end[1] - g.start[1]) <= 2.0 * tol / HANDLE_PX
|
||||
}
|
||||
|
||||
/// Whether a document point is outside the document, on the pasteboard around the canvas.
|
||||
fn on_pasteboard(app: &PhotocraftApp, p: [f64; 2]) -> bool {
|
||||
app.session.active().is_some_and(|st| p[0] < 0.0 || p[1] < 0.0 || p[0] >= f64::from(st.doc.size.width) || p[1] >= f64::from(st.doc.size.height))
|
||||
}
|
||||
|
||||
/// Pointer input while transforming. Returns false when no transform is active.
|
||||
pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) -> bool {
|
||||
let Some(t) = app.ui.transform.clone() else { return false };
|
||||
@@ -95,11 +100,17 @@ pub fn pointer(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui::Modifiers) ->
|
||||
if matches!(ev, ToolEvent::Up { .. }) {
|
||||
pv.gesture = None;
|
||||
}
|
||||
// Clicking an empty spot commits instead of silently arming rotation (#2851).
|
||||
// Dragging the rotation zone and Option-clicking to move the pivot
|
||||
// keep their existing behavior.
|
||||
// Clicking the pasteboard (outside the document) commits; a click outside the box on the
|
||||
// image does nothing and the box stays open (#11100). Dragging the rotation zone and
|
||||
// Option-clicking to move the pivot keep their existing behavior.
|
||||
if matches!(ev, ToolEvent::Up { .. }) && g.is_some_and(|g| accepts_outside_click(g, [x, y], tol)) {
|
||||
commit(app);
|
||||
if on_pasteboard(app, [x, y]) {
|
||||
commit(app);
|
||||
} else if let (Some(g), Some(s)) = (g, app.ui.transform.as_mut()) {
|
||||
// Jitter before the release may have turned the box a little: put it back.
|
||||
s.quad = g.quad0;
|
||||
s.pivot = g.pivot0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
let type_layer = moves_type(app, &t);
|
||||
|
||||
@@ -28,12 +28,17 @@ fn a_plain_click_far_outside_accepts_transform_but_a_rotation_drag_does_not() {
|
||||
let before = app.session.active().unwrap().history.past_len();
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = t.quad.map(|[x, y]| [x + 6.0, y]);
|
||||
let outside = [44.0, 44.0];
|
||||
// A click outside the box on the image keeps it open; only the pasteboard commits (#11100).
|
||||
let none = egui::Modifiers::NONE;
|
||||
let on_image = [44.0, 44.0];
|
||||
assert!(pointer(&mut app, ToolEvent::Down { x: on_image[0], y: on_image[1], pressure: 1.0 }, none));
|
||||
assert!(pointer(&mut app, ToolEvent::Up { x: on_image[0], y: on_image[1] }, none));
|
||||
assert!(app.ui.transform.is_some(), "a click on the image does not commit");
|
||||
let outside = [-12.0, 44.0];
|
||||
assert!(pointer(&mut app, ToolEvent::Down { x: outside[0], y: outside[1], pressure: 1.0 }, none));
|
||||
assert!(app.ui.transform.is_some(), "press alone does not accept");
|
||||
assert!(pointer(&mut app, ToolEvent::Up { x: outside[0], y: outside[1] }, none));
|
||||
assert!(app.ui.transform.is_none(), "a release without a drag commits");
|
||||
assert!(app.ui.transform.is_none(), "a release without a drag on the pasteboard commits");
|
||||
assert_eq!(app.session.active().unwrap().history.past_len(), before + 1, "one undo step for the transform");
|
||||
let st = app.session.active().unwrap();
|
||||
let layer = st.doc.layer(st.active_layer.unwrap()).unwrap();
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
//! Free Transform commit rules for clicks (#11100): a click outside the box on the image keeps the
|
||||
//! box open, a click on the pasteboard commits, a double-click inside the box commits and one
|
||||
//! outside does nothing.
|
||||
|
||||
use egui::{Modifiers, PointerButton, Pos2};
|
||||
use egui_kittest::Harness;
|
||||
use photocraft_doc::LayerId;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::{ToolEvent, ViewXform, tool_event};
|
||||
|
||||
/// A 200 × 200 document with a red block (x 20..60, y 20..60) on layer "Paint", Free Transform
|
||||
/// open on it, and the box moved 10 px to the right as in the measurement.
|
||||
fn app() -> PhotocraftApp {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 200, "background": "white"})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.extras.rulers = false;
|
||||
app.ui.extras.snap = false;
|
||||
let paint = LayerId(app.run("layer.new.layer", json!({"name": "Paint"})).unwrap()["layer"].as_u64().unwrap());
|
||||
app.run("layer.select", json!({"layer": paint.0})).unwrap();
|
||||
app.run("select.rect", json!({"x": 20, "y": 20, "width": 40, "height": 40})).unwrap();
|
||||
app.run("edit.fill", json!({"contents": "color", "color": "#ff0000"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
crate::menus::invoke(&mut app, &egui::Context::default(), "edit.freeTransform", json!({})).unwrap();
|
||||
let t = app.ui.transform.as_mut().unwrap();
|
||||
t.quad = t.quad.map(|[x, y]| [x + 10.0, y]);
|
||||
t.pivot[0] += 10.0;
|
||||
app
|
||||
}
|
||||
|
||||
fn steps(app: &PhotocraftApp) -> usize {
|
||||
app.session.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
fn click(app: &mut PhotocraftApp, p: [f64; 2]) {
|
||||
tool_event(app, ToolEvent::Down { x: p[0], y: p[1], pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(app, ToolEvent::Up { x: p[0], y: p[1] }, Modifiers::NONE);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_outside_the_box_on_the_image_keeps_it_open() {
|
||||
let mut app = app();
|
||||
let (before, quad) = (steps(&app), app.ui.transform.as_ref().unwrap().quad);
|
||||
click(&mut app, [150.0, 150.0]);
|
||||
assert!(app.ui.transform.is_some(), "the box stays open");
|
||||
assert_eq!(app.ui.transform.as_ref().unwrap().quad, quad, "and unchanged");
|
||||
assert_eq!(steps(&app), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_jittery_click_outside_the_box_leaves_it_unchanged() {
|
||||
let mut app = app();
|
||||
let (quad, pivot) = (app.ui.transform.as_ref().unwrap().quad, app.ui.transform.as_ref().unwrap().pivot);
|
||||
tool_event(&mut app, ToolEvent::Down { x: 150.0, y: 150.0, pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(&mut app, ToolEvent::Move { x: 150.0, y: 151.0, pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 150.0, y: 151.0 }, Modifiers::NONE);
|
||||
let t = app.ui.transform.as_ref().unwrap();
|
||||
assert_eq!((t.quad, t.pivot), (quad, pivot));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_click_on_the_pasteboard_commits_one_free_transform_step() {
|
||||
for p in [[-30.0, 100.0], [100.0, -30.0], [230.0, 100.0], [100.0, 230.0]] {
|
||||
let mut app = app();
|
||||
let before = steps(&app);
|
||||
click(&mut app, p);
|
||||
assert!(app.ui.transform.is_none(), "{p:?}");
|
||||
assert_eq!(steps(&app), before + 1, "{p:?}");
|
||||
assert_eq!(app.session.active().unwrap().history.entries().last().map(ToString::to_string).as_deref(), Some("Free Transform"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_outside_the_box_still_rotates() {
|
||||
let mut app = app();
|
||||
let quad = app.ui.transform.as_ref().unwrap().quad;
|
||||
tool_event(&mut app, ToolEvent::Down { x: 150.0, y: 100.0, pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(&mut app, ToolEvent::Move { x: 150.0, y: 140.0, pressure: 1.0 }, Modifiers::NONE);
|
||||
tool_event(&mut app, ToolEvent::Up { x: 150.0, y: 140.0 }, Modifiers::NONE);
|
||||
assert!(app.ui.transform.is_some());
|
||||
assert_ne!(app.ui.transform.as_ref().unwrap().quad, quad);
|
||||
}
|
||||
|
||||
/// 60 frames a second, so the second click of a double-click lands inside egui's delay.
|
||||
fn harness(app: PhotocraftApp) -> Harness<'static, PhotocraftApp> {
|
||||
let mut h = Harness::builder().with_step_dt(1.0 / 60.0).with_size(egui::vec2(1200.0, 800.0)).build_ui_state(
|
||||
|ui, app: &mut PhotocraftApp| {
|
||||
let ctx = ui.ctx().clone();
|
||||
if !ctx.fonts(|f| f.families().contains(&egui::FontFamily::Name("medium".into()))) {
|
||||
return;
|
||||
}
|
||||
egui::CentralPanel::default().show(ui, |ui| crate::canvas::document_area(app, ui));
|
||||
},
|
||||
app,
|
||||
);
|
||||
PhotocraftApp::setup_context(&h.ctx, crate::theme::ThemeKind::ALL[0]);
|
||||
h.run_steps(4);
|
||||
h
|
||||
}
|
||||
|
||||
fn screen(app: &PhotocraftApp, p: [f64; 2]) -> Pos2 {
|
||||
let v = &app.ui.views[0];
|
||||
let xf = ViewXform {
|
||||
rect: crate::rulers::content_rect(app, app.last_canvas_rect),
|
||||
zoom: v.zoom / app.canvas_ppp(),
|
||||
center: v.center,
|
||||
flip: app.ui.view.flip_horizontal,
|
||||
rotation: v.rotation,
|
||||
aspect: app.ui.view.display_aspect(),
|
||||
};
|
||||
xf.to_screen(p[0] as f32, p[1] as f32)
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_double_click_inside_the_box_commits() {
|
||||
let mut h = harness(app());
|
||||
let before = steps(h.state());
|
||||
// The box is x 30..70, y 20..60 now; its middle is clear of the handles.
|
||||
let at = screen(h.state(), [50.0, 40.0]);
|
||||
double_click(&mut h, at);
|
||||
assert!(h.state().ui.transform.is_none(), "committed");
|
||||
assert_eq!(steps(h.state()), before + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_double_click_outside_the_box_does_nothing() {
|
||||
let mut h = harness(app());
|
||||
let before = steps(h.state());
|
||||
let at = screen(h.state(), [150.0, 150.0]);
|
||||
double_click(&mut h, at);
|
||||
assert!(h.state().ui.transform.is_some(), "the box stays open");
|
||||
assert_eq!(steps(h.state()), before);
|
||||
}
|
||||
Reference in new issue
Block a user