PhotoCraft: An open-source, photo editor written by scratch in pure Rust
test(liquify): zoom keys step the preview, not the canvas; pinch and wheel zoom about the pointer (#7210)
Korg1chcc3ca41c6d3f
diff --git a/crates/ui-egui/src/liquify_ui.rs b/crates/ui-egui/src/liquify_ui.rsindex f6894b5f1..5e42761ff 100644--- a/crates/ui-egui/src/liquify_ui.rs+++ b/crates/ui-egui/src/liquify_ui.rs@@ -1448,6 +1448,88 @@ 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");+ }+#[test]fn menu_opens_the_dialog_and_routes_pointer_events() {let ctx = egui::Context::default();