Skip to content

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

Liquify: keep short strokes whole so undo removes only the last

NetterSilver010c542be4f7
diff --git a/crates/ui-egui/src/liquify_ui.rs b/crates/ui-egui/src/liquify_ui.rs
index 9f46020b1..30688fa95 100644
--- a/crates/ui-egui/src/liquify_ui.rs
+++ b/crates/ui-egui/src/liquify_ui.rs
@@ -255,6 +255,8 @@ impl LiquifyDialog {
/// Starts a stroke; `alt` draws with the selected tool's opposite (see [`alt_tool`]).
fn begin(&mut self, p: [f64; 3], now: f64, alt: bool) {
+ // A stroke still open has already changed the field; record it so undo can replay it (#8055).
+ self.end();
self.redo.clear();
let tool = if alt { alt_tool(self.opts.tool) } else { self.opts.tool };
let mut s = self.template(tool);
@@ -1078,7 +1080,8 @@ pub fn show(app: &mut PhotocraftApp, ctx: &egui::Context) {
events.push(ToolEvent::Move { x: q[0], y: q[1], pressure: pr });
ctx.request_repaint();
}
- } else if resp.drag_started() || (resp.is_pointer_button_down_on() && d.cur.is_none()) {
+ } else if (resp.drag_started() || resp.is_pointer_button_down_on()) && d.cur.is_none() {
+ // The stroke already began at the press; egui's later drag start must not restart it (#8055).
events.push(ToolEvent::Down { x: q[0], y: q[1], pressure: pr });
} else if resp.dragged() || resp.is_pointer_button_down_on() {
let moved = d.cur.as_ref().is_some_and(|(_, l)| l[0] != q[0] || l[1] != q[1]);
@@ -1586,6 +1589,78 @@ mod tests {
assert_eq!(app.distort.liquify.as_ref().unwrap().strokes.len(), 1, "Cmd+Shift+Z redoes the last stroke");
}
+ /// 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();