Skip to content

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

Merge PR #8128: Liquify: keep short strokes whole so undo removes only the last

aionggec1c38dfd121
diff --cc crates/ui-egui/src/liquify_ui.rs
index 91ef1cd87,30688fa95..cf1c93251
--- a/crates/ui-egui/src/liquify_ui.rs
+++ b/crates/ui-egui/src/liquify_ui.rs
@@@ -1720,88 -1589,78 +1723,160 @@@ mod tests
assert_eq!(app.distort.liquify.as_ref().unwrap().strokes.len(), 1, "Cmd+Shift+Z redoes the last stroke");
}
+ fn key(key: egui::Key) -> egui::Event {
+ egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::COMMAND }
+ }
+
+ /// A frame of the open dialog in a 1000 × 700 window with `events`; returns the preview's
+ /// centre (between the tool strip and the options panel, under the title strip).
+ fn frame(app: &mut PhotocraftApp, ctx: &egui::Context, events: Vec<egui::Event>) -> Pos2 {
+ let screen = ERect::from_min_size(Pos2::ZERO, vec2(1000.0, 700.0));
+ let raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() };
+ let mut out = ctx.run_ui(raw, |ui| show(app, ui.ctx()));
+ out.textures_delta.clear();
+ pos2((LEFT_W + screen.width() - RIGHT_W) / 2.0, (30.0 + screen.height()) / 2.0)
+ }
+
+ /// #7210: ⌘+ / ⌘− step Liquify's preview along the zoom ladder, ⌘0 fits it and ⌘1 is the
+ /// dialog's 100 %, through the canvas's own shortcuts; the canvas behind keeps its zoom.
+ #[test]
+ fn zoom_shortcuts_zoom_the_preview_not_the_canvas() {
+ let ctx = egui::Context::default();
+ PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
+ let mut app = app_with_layer();
+ open(&mut app, &ctx).unwrap();
+ frame(&mut app, &ctx, Vec::new());
+ let fitted = app.distort.liquify.as_ref().unwrap().zoom;
+ assert!(fitted > 0.0, "the preview fits on its first frame");
+ let canvas_zoom = app.ui.views.first().map(|v| v.zoom);
+ let mut press = |k: egui::Key| {
+ let raw = egui::RawInput { events: vec![key(k)], ..Default::default() };
+ let _ = ctx.run_ui(raw, |ui| crate::shortcuts::handle(&mut app, ui.ctx()));
+ app.distort.liquify.as_ref().unwrap().zoom
+ };
+ assert_eq!(press(egui::Key::Num1), 1.0);
+ assert_eq!(press(egui::Key::Equals), 2.0, "next step up the ladder");
+ assert_eq!(press(egui::Key::Plus), 3.0, "⌘+ typed as Plus too");
+ assert_eq!(press(egui::Key::Minus), 2.0);
+ assert_eq!(press(egui::Key::Num0), 0.0, "fit on the next frame");
+ frame(&mut app, &ctx, Vec::new());
+ assert_eq!(app.distort.liquify.as_ref().unwrap().zoom, fitted);
+ assert_eq!(app.ui.views.first().map(|v| v.zoom), canvas_zoom, "the canvas behind keeps its zoom");
+ }
+
+ /// #7210: a trackpad pinch over the preview zooms it about the pointer (macOS sends no wheel
+ /// for it), unless Preferences › Enhanced Controls › Zoom with Trackpad Pinch is off; a wheel
+ /// notch is one ×1.1 step.
+ #[test]
+ fn pinch_and_wheel_zoom_the_preview_about_the_pointer() {
+ let ctx = egui::Context::default();
+ PhotocraftApp::setup_context(&ctx, crate::theme::ThemeKind::ALL[0]);
+ let mut app = app_with_layer();
+ open(&mut app, &ctx).unwrap();
+ let center = frame(&mut app, &ctx, Vec::new());
+ let at = center + vec2(60.0, -40.0);
+ frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at)]);
+ let screen = ERect::from_min_size(Pos2::ZERO, vec2(1000.0, 700.0));
+ let area = ERect::from_min_max(pos2(LEFT_W, 30.0), pos2(screen.width() - RIGHT_W, screen.height()));
+ let (z0, under) = {
+ let d = app.distort.liquify.as_ref().unwrap();
+ (d.zoom, to_doc(d, area, at))
+ };
+ frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), egui::Event::Zoom(2.0)]);
+ let d = app.distort.liquify.as_ref().unwrap();
+ assert!((d.zoom - z0 * 2.0).abs() < 1e-4, "{} → {}", z0, d.zoom);
+ let still = to_doc(d, area, at);
+ assert!((still[0] - under[0]).abs() < 1e-3 && (still[1] - under[1]).abs() < 1e-3, "the point under the pointer stays: {under:?} → {still:?}");
+
+ let notch = ctx.options(|o| o.input_options.line_scroll_speed);
+ let wheel = egui::Event::MouseWheel {
+ unit: egui::MouseWheelUnit::Line,
+ delta: vec2(0.0, 1.0),
+ modifiers: egui::Modifiers::NONE,
+ phase: egui::TouchPhase::Move,
+ };
+ let z1 = app.distort.liquify.as_ref().unwrap().zoom;
+ frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), wheel]);
+ let z2 = app.distort.liquify.as_ref().unwrap().zoom;
+ assert!(notch > 0.0 && (z2 / z1 - crate::wheel_nav::NOTCH).abs() < 1e-3, "one notch is ×1.1: {z1} → {z2}");
+
+ app.session.prefs.edit(|p| p.enhanced_controls.zoom_with_trackpad_pinch = false);
+ frame(&mut app, &ctx, vec![egui::Event::PointerMoved(at), egui::Event::Zoom(2.0)]);
+ assert_eq!(app.distort.liquify.as_ref().unwrap().zoom, z2, "a pinch does nothing with the preference off");
+ }
+
+ /// The proxy rendered from the recorded strokes, i.e. what undo/replay rebuilds.
+ fn replayed_out(d: &LiquifyDialog) -> Vec<[u8; 4]> {
+ let field = LiquifyField::from_strokes(d.canvas, d.cell, &d.strokes);
+ let mut out = vec![[0u8; 4]; d.proxy.w * d.proxy.h];
+ d.proxy.render(&field, [0, 0, d.proxy.w, d.proxy.h], &mut out);
+ out
+ }
+
+ /// #8055: a second pointer-down while a stroke is open must not drop the dabs already applied.
+ #[test]
+ fn second_down_without_up_keeps_the_open_strokes_dabs_recorded() {
+ let ctx = egui::Context::default();
+ let mut app = app_with_layer();
+ open(&mut app, &ctx).unwrap();
+ control(&mut app, &json!({"tool": "forwardWarp", "size": 30})).unwrap();
+ for ev in [
+ ToolEvent::Down { x: 40.0, y: 40.0, pressure: 1.0 },
+ ToolEvent::Move { x: 46.0, y: 41.0, pressure: 1.0 },
+ ToolEvent::Down { x: 46.0, y: 41.0, pressure: 1.0 },
+ ToolEvent::Move { x: 55.0, y: 42.0, pressure: 1.0 },
+ ToolEvent::Up { x: 55.0, y: 42.0 },
+ ] {
+ pointer(&mut app, ev, egui::Modifiers::NONE);
+ }
+ let d = app.distort.liquify.as_ref().unwrap();
+ assert!(d.out == replayed_out(d), "the live field must match the field rebuilt from the recorded strokes");
+ }
+
+ /// #8055: short strokes drawn with the mouse (moves inside egui's drag threshold first) are
+ /// recorded whole, as one stroke each, so undo removes only the last one.
+ #[test]
+ fn short_mouse_strokes_are_recorded_whole_and_undo_removes_only_the_last() {
+ let ctx = egui::Context::default();
+ crate::theme::install_fonts(&ctx);
+ let mut app = app_with_layer();
+ open(&mut app, &ctx).unwrap();
+ control(&mut app, &json!({"tool": "forwardWarp", "size": 30})).unwrap();
+ let screen = ERect::from_min_size(pos2(0.0, 0.0), vec2(1280.0, 800.0));
+ let frame = |app: &mut PhotocraftApp, events: Vec<egui::Event>| {
+ let raw = egui::RawInput { screen_rect: Some(screen), events, ..Default::default() };
+ let mut out = ctx.run_ui(raw, |ui| show(app, ui.ctx()));
+ out.textures_delta.clear();
+ };
+ frame(&mut app, vec![]);
+ frame(&mut app, vec![]);
+ let mut before_last = Vec::new();
+ let strokes = 6;
+ for i in 0..strokes {
+ let start = pos2(560.0 + i as f32 * 25.0, 380.0 + (i % 2) as f32 * 20.0);
+ before_last = app.distort.liquify.as_ref().unwrap().out.clone();
+ frame(&mut app, vec![egui::Event::PointerMoved(start)]);
+ let press = egui::Event::PointerButton { pos: start, button: egui::PointerButton::Primary, pressed: true, modifiers: egui::Modifiers::NONE };
+ frame(&mut app, vec![press]);
+ let mut p = start;
+ for _ in 0..8 {
+ p += vec2(1.5, 1.0);
+ frame(&mut app, vec![egui::Event::PointerMoved(p)]);
+ }
+ let release = egui::Event::PointerButton { pos: p, button: egui::PointerButton::Primary, pressed: false, modifiers: egui::Modifiers::NONE };
+ frame(&mut app, vec![release]);
+ frame(&mut app, vec![]);
+ let d = app.distort.liquify.as_ref().unwrap();
+ assert_eq!(d.strokes.len(), i + 1, "one recorded stroke per mouse stroke");
+ assert!(d.out == replayed_out(d), "stroke {i}: the live field must match the recorded strokes");
+ }
+ control(&mut app, &json!({"undo": true})).unwrap();
+ let d = app.distort.liquify.as_ref().unwrap();
+ assert_eq!(d.strokes.len(), strokes - 1);
+ let differ = d.out.iter().zip(&before_last).filter(|(a, b)| a != b).count();
+ assert_eq!(differ, 0, "undo restores the pixels from before the last stroke");
+ }
+
#[test]
fn menu_opens_the_dialog_and_routes_pointer_events() {
let ctx = egui::Context::default();