PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Preferences: Enable Gestures off ignores touch-screen pinch and twist (#204) (#7478)
Nguyễn Minh Long72a3423a7e7c
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rsindex 2c2507226..3e87d43fc 100644--- a/crates/engine/src/prefs.rs+++ b/crates/engine/src/prefs.rs@@ -1040,7 +1040,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &["type.textEngine","type.recentFonts","enhancedControls.scrubbySliderAcceleration",- "enhancedControls.touchGestures","rawDefaults.colorSpace","rawDefaults.bitDepth","rawDefaults.resolution",diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 2c0453496..60e9fbaed 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -3327,6 +3327,9 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:let over_bars = ctx.data(|d| d.get_temp::<bool>(bars_id)).unwrap_or(false);let nav_hovered = response.hovered() || free_hover || over_bars;// Interface › Enable Animations off: the wheel moves the canvas without a glide (#5721).+ // Enhanced Controls › Enable Gestures off: two fingers on a touch screen neither zoom nor+ // turn the view (#204); the mouse and trackpad are unaffected.+ let touch_gestures = app.session.prefs().enhanced_controls.touch_gestures;let wheel = crate::wheel_nav::read_with(&ctx,app.session.prefs().general.zoom_with_scroll_wheel,@@ -3334,6 +3337,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:response.id.with("wheel"),nav_hovered,app.session.prefs().interface.enable_animations,+ touch_gestures,);if nav_hovered {let pointer = ui.input(|i| i.pointer.hover_pos());@@ -3366,7 +3370,7 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:_ => {}}let rotate_trackpad = app.session.prefs().enhanced_controls.rotate_view_with_trackpad;- if let Some(delta) = crate::rotate_view::trackpad_delta(&ctx, rotate_trackpad) {+ if let Some(delta) = crate::rotate_view::trackpad_delta(&ctx, rotate_trackpad, touch_gestures) {let next = view.rotation + crate::rotate_view::screen_delta(delta, xf.flip);if next.is_finite() {view.rotation = crate::rotate_view::wrap_deg(next);diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex f2961fd7b..9a1b07f2b 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -1980,8 +1980,10 @@ fn navigator(app: &mut PhotocraftApp, ui: &mut egui::Ui) {// Use the canvas wheel classifier to honour one ×1.1 step per notch without// applying egui's smoothed wheel tail a second time.let pinch_zooms = app.session.prefs().enhanced_controls.zoom_with_trackpad_pinch;+ let touch_gestures = app.session.prefs().enhanced_controls.touch_gestures;if resp.hovered()- && let Some(crate::wheel_nav::Wheel::Zoom(factor)) = crate::wheel_nav::read(&ctx, true, pinch_zooms, egui::Id::new("pc-navigator-wheel"), true)+ && let Some(crate::wheel_nav::Wheel::Zoom(factor)) =+ crate::wheel_nav::read_with(&ctx, true, pinch_zooms, egui::Id::new("pc-navigator-wheel"), true, true, touch_gestures){let view = &mut app.ui.views[idx];let next = crate::zoom_levels::clamp(view.zoom * factor, view.doc_size);diff --git a/crates/ui-egui/src/prefs_ui.rs b/crates/ui-egui/src/prefs_ui.rsindex 721340702..4156e380c 100644--- a/crates/ui-egui/src/prefs_ui.rs+++ b/crates/ui-egui/src/prefs_ui.rs@@ -3506,11 +3506,13 @@ mod tests {assert!(!prefs::is_hidden("type.useEscToCommit"));assert!(!prefs::is_hidden("type.smartQuotes"));assert!(!prefs::is_hidden("type.showFontNamesInEnglish"));- // Rotate View with Trackpad and the pinch zoom are live; the other Enhanced Controls rows- // stay hidden.+ // Rotate View with Trackpad, the pinch zoom and Enable Gestures are live; the other+ // Enhanced Controls rows stay hidden.assert!(has_visible_fields(&values, "enhancedControls"));assert!(!prefs::is_hidden("enhancedControls.rotateViewWithTrackpad"));assert!(!prefs::is_hidden("enhancedControls.zoomWithTrackpadPinch"));+ assert!(!prefs::is_hidden("enhancedControls.touchGestures"));+ assert!(prefs::is_hidden("enhancedControls.scrubbySliderAcceleration"));// Camera Raw Defaults shows only "Open in Camera Raw" so far.assert!(has_visible_fields(&values, "rawDefaults"));assert!(!prefs::is_hidden("rawDefaults.openInCameraRaw"));diff --git a/crates/ui-egui/src/rotate_view.rs b/crates/ui-egui/src/rotate_view.rsindex 5e173da5d..5137b9d31 100644--- a/crates/ui-egui/src/rotate_view.rs+++ b/crates/ui-egui/src/rotate_view.rs@@ -4,7 +4,7 @@//! decreases it in a flipped view). Shift snaps to 15°. Reset View,//! Escape, a double-click, or the on-canvas compass put the angle back to 0. History is not//! recorded. Trackpad rotate is gated by Preferences › Enhanced Controls › Rotate View with-//! Trackpad.+//! Trackpad; a two-finger twist on a touch screen also needs Enable Gestures (#204).use egui::{Align2, Pos2, Rect, Stroke, pos2, vec2};use serde_json::{Value, json};@@ -137,7 +137,9 @@ pub fn apply_trackpad(current: f32, enabled: bool, delta_rad: f32) -> Option<f32}/// This frame's trackpad rotation in degrees, if the preference is on and egui reported a delta.-pub fn trackpad_delta(ctx: &egui::Context, enabled: bool) -> Option<f32> {+/// The trackpad's own `Rotate` events always count; a touch screen's two-finger twist (egui's+/// multi-touch rotation) only while `touch_gestures` (Enhanced Controls › Enable Gestures) is on.+pub fn trackpad_delta(ctx: &egui::Context, enabled: bool, touch_gestures: bool) -> Option<f32> {if !enabled {return None;}@@ -150,7 +152,13 @@ pub fn trackpad_delta(ctx: &egui::Context, enabled: bool) -> Option<f32> {_ => None,}).sum();- if from_events != 0.0 { from_events } else { i.rotation_delta() }+ if from_events != 0.0 {+ from_events+ } else if touch_gestures {+ i.rotation_delta()+ } else {+ 0.0+ }});apply_trackpad(0.0, true, rad).filter(|_| rad != 0.0)}@@ -324,8 +332,8 @@ mod tests {let mut disabled = None;let raw = egui::RawInput { events: vec![Event::Rotate(std::f32::consts::FRAC_PI_6)], ..Default::default() };let mut output = ctx.run_ui(raw, |ui| {- enabled = trackpad_delta(ui.ctx(), true);- disabled = trackpad_delta(ui.ctx(), false);+ enabled = trackpad_delta(ui.ctx(), true, true);+ disabled = trackpad_delta(ui.ctx(), false, true);});output.textures_delta.clear();@@ -333,6 +341,36 @@ mod tests {assert_eq!(disabled, None);}+ /// Preferences › Enhanced Controls › Enable Gestures (#204): off, a two-finger twist on a+ /// touch screen doesn't turn the view; the trackpad's `Rotate` event still does.+ #[test]+ fn enable_gestures_switches_touch_screen_rotation() {+ use egui::TouchPhase::{Move, Start};+ let touch = |id: u64, phase, pos| Event::Touch { device_id: egui::TouchDeviceId(1), id: egui::TouchId(id), phase, pos, force: None };+ let twist = |touch_gestures: bool, extra: Vec<Event>| {+ let ctx = egui::Context::default();+ let frames = [+ // egui only starts a multi-touch gesture once it knows where the pointer is.+ vec![Event::PointerMoved(pos2(50.0, 50.0))],+ vec![touch(1, Start, pos2(40.0, 50.0)), touch(2, Start, pos2(60.0, 50.0))],+ // A quarter turn about the centre between the fingers.+ [vec![touch(1, Move, pos2(50.0, 40.0)), touch(2, Move, pos2(50.0, 60.0))], extra].concat(),+ ];+ let mut last = None;+ for (n, events) in (0u32..).zip(frames) {+ let raw = egui::RawInput { time: Some(f64::from(n) / 60.0), events, ..Default::default() };+ let mut out = ctx.run_ui(raw, |ui| last = trackpad_delta(ui.ctx(), true, touch_gestures));+ out.textures_delta.clear();+ }+ last+ };+ let on = twist(true, Vec::new());+ assert!(on.is_some_and(|d| (d.abs() - 90.0).abs() < 1e-3), "on: the fingers turn the view: {on:?}");+ assert_eq!(twist(false, Vec::new()), None, "off: the fingers don't");+ let trackpad = twist(false, vec![Event::Rotate(std::f32::consts::FRAC_PI_6)]);+ assert!(trackpad.is_some_and(|d| (d - 30.0).abs() < 1e-3), "off: a trackpad rotate still turns: {trackpad:?}");+ }+#[test]fn trackpad_pref_gates_rotation() {assert_eq!(apply_trackpad(10.0, false, 0.5), None);diff --git a/crates/ui-egui/src/wheel_nav.rs b/crates/ui-egui/src/wheel_nav.rsindex bde5e71ae..68a2321c4 100644--- a/crates/ui-egui/src/wheel_nav.rs+++ b/crates/ui-egui/src/wheel_nav.rs@@ -8,6 +8,9 @@//! - A trackpad pinch zooms around the pointer, unless Preferences › Enhanced Controls ›//! Zoom with Trackpad Pinch is off (then a pinch does nothing, and Windows' Ctrl + wheel//! fractions scroll sideways like the wheel, because the fold below is skipped).+//! - A two-finger pinch on a touch screen zooms the same way, unless Preferences › Enhanced+//! Controls › Enable Gestures is off (#204): then the fingers leave the view alone, and a+//! trackpad pinch still zooms ([`pinch_factor`]).//!//! [`configure`] makes egui fold ⌘/Ctrl + wheel into a sideways scroll as it does ⇧ + wheel, so//! only a real pinch (egui's `Event::Zoom`, or a touch screen) reaches `zoom_delta`. macOS and@@ -23,7 +26,7 @@//! keeps the smoothed delta. The Alt state is taken from the wheel events and kept until the next//! wheel event: releasing Alt while the tail is still settling must not turn it into a pan.-use egui::{Context, Event, Id, Modifiers, MouseWheelUnit, RawInput, Vec2};+use egui::{Context, Event, Id, InputState, Modifiers, MouseWheelUnit, RawInput, Vec2};/// Zoom factor of one wheel notch: every step is exactly ×1.1.pub const NOTCH: f32 = 1.1;@@ -198,13 +201,15 @@ const SNAP_IDLE_TIMEOUT: f64 = 0.5;/// accumulator per viewport. `hovered` gates accumulation: only the viewport receiving/// navigation may add raw input to the snap residual.pub fn read(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, owner: Id, hovered: bool) -> Option<Wheel> {- read_with(ctx, zoom_with_wheel, pinch_zooms, owner, hovered, true)+ read_with(ctx, zoom_with_wheel, pinch_zooms, owner, hovered, true, true)}/// [`read`], choosing how a plain wheel scroll pans: `smooth` glides it over a few frames (egui's/// smoothed delta); off, as with Interface › Enable Animations off, the view moves the whole notch/// in the frame the wheel turns and stops there (#5721), as scroll areas already do.-pub fn read_with(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, owner: Id, hovered: bool, smooth: bool) -> Option<Wheel> {+/// `touch_gestures` is Preferences › Enhanced Controls › Enable Gestures: off, a touch screen's+/// two-finger pinch doesn't zoom ([`pinch_factor`]).+pub fn read_with(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, owner: Id, hovered: bool, smooth: bool, touch_gestures: bool) -> Option<Wheel> {let notch = ctx.options(|o| o.input_options.line_scroll_speed);// --- Modifier tracking (always runs for all callers) ---@@ -225,7 +230,7 @@ pub fn read_with(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, owner:_ => None,}).collect();- (i.smooth_scroll_delta, i.zoom_delta(), latest_alt, latest_shift, wheel, i.content_rect().height())+ (i.smooth_scroll_delta, pinch_factor(i, touch_gestures), latest_alt, latest_shift, wheel, i.content_rect().height())});let alt = match latest_alt {Some(a) => {@@ -329,6 +334,24 @@ pub fn read_with(ctx: &Context, zoom_with_wheel: bool, pinch_zooms: bool, owner:}}+/// This frame's pinch zoom factor (1 = none). While two fingers are down on a touch screen, egui's+/// `zoom_delta` reports their pinch in place of the trackpad's `Event::Zoom`. With Preferences ›+/// Enhanced Controls › Enable Gestures off (`touch_gestures` false) the fingers are ignored and only+/// the trackpad's own pinch events count (Windows' Ctrl + wheel pinch included: [`fold_legacy_pinch`]+/// has already turned it into `Event::Zoom`).+pub fn pinch_factor(i: &InputState, touch_gestures: bool) -> f32 {+ if touch_gestures || i.multi_touch().is_none() {+ return i.zoom_delta();+ }+ i.events+ .iter()+ .filter_map(|e| match e {+ Event::Zoom(f) if f.is_finite() && *f > 0.0 => Some(*f),+ _ => None,+ })+ .product()+}+#[cfg(test)]mod tests {use egui::TouchPhase;@@ -393,7 +416,7 @@ mod tests {let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() };let mut pan = Vec2::ZERO;let mut out = ctx.run_ui(raw, |ui| {- if let Some(Wheel::Pan(d)) = read_with(ui.ctx(), false, true, owner, true, smooth) {+ if let Some(Wheel::Pan(d)) = read_with(ui.ctx(), false, true, owner, true, smooth, true) {pan = d;}});@@ -922,4 +945,62 @@ mod tests {let (_, _, snaps) = gesture(vec![vec![wheel(Vec2::new(0.0, -1.0), Modifiers::SHIFT)]], true, false);assert_eq!(snaps, -1, "Line-unit notch down produces −1 step");}++ /// Two fingers on a touch screen: down 20 points apart, spread to 40 (a ×2 pinch), lifted.+ fn touch_pinch_frames() -> Vec<Vec<Event>> {+ let touch = |id: u64, phase: TouchPhase, x: f32| Event::Touch {+ device_id: egui::TouchDeviceId(1),+ id: egui::TouchId(id),+ phase,+ pos: egui::pos2(x, 50.0),+ force: None,+ };+ vec![+ // egui only starts a multi-touch gesture once it knows where the pointer is.+ vec![Event::PointerMoved(egui::pos2(40.0, 50.0))],+ vec![touch(1, TouchPhase::Start, 40.0), touch(2, TouchPhase::Start, 60.0)],+ vec![touch(1, TouchPhase::Move, 30.0), touch(2, TouchPhase::Move, 70.0)],+ vec![touch(1, TouchPhase::End, 30.0), touch(2, TouchPhase::End, 70.0)],+ ]+ }++ /// The total zoom [`read_with`] reports over `frames`, Enable Gestures set to `touch_gestures`.+ fn touch_zoom(frames: Vec<Vec<Event>>, touch_gestures: bool) -> f32 {+ let owner = Id::new("test-touch");+ let ctx = Context::default();+ configure(&ctx);+ let mut zoom = 1.0;+ for (n, events) in (0u32..).zip(frames) {+ let raw = RawInput { time: Some(f64::from(n) / 60.0), predicted_dt: 1.0 / 60.0, events, ..Default::default() };+ let mut out = ctx.run_ui(raw, |ui| {+ if let Some(Wheel::Zoom(f)) = read_with(ui.ctx(), false, true, owner, true, true, touch_gestures) {+ zoom *= f;+ }+ });+ out.textures_delta.clear();+ }+ zoom+ }++ /// Preferences › Enhanced Controls › Enable Gestures (#204): on (the default) a two-finger+ /// pinch on a touch screen zooms; off, it leaves the view alone.+ #[test]+ fn enable_gestures_switches_touch_screen_pinch_zoom() {+ let on = touch_zoom(touch_pinch_frames(), true);+ assert!((on - 2.0).abs() < 1e-4, "on: the fingers zoom ×2: {on}");+ let off = touch_zoom(touch_pinch_frames(), false);+ assert_eq!(off, 1.0, "off: the fingers don't zoom");+ }++ /// Enable Gestures off only silences the touch screen: a trackpad pinch still zooms, also in a+ /// frame where two fingers rest on the screen (egui's `zoom_delta` would report theirs instead).+ #[test]+ fn enable_gestures_off_keeps_the_trackpad_pinch() {+ let zoom = touch_zoom(vec![vec![Event::Zoom(1.25)]], false);+ assert!((zoom - 1.25).abs() < 1e-4, "no fingers down: {zoom}");+ let mut frames = touch_pinch_frames();+ frames[2] = vec![Event::Zoom(1.25)];+ let zoom = touch_zoom(frames, false);+ assert!((zoom - 1.25).abs() < 1e-4, "fingers resting: {zoom}");+ }}diff --git a/docs/scorecard.md b/docs/scorecard.mdindex a02e8ed21..c8bfb03f3 100644--- a/docs/scorecard.md+++ b/docs/scorecard.md@@ -9,7 +9,7 @@ Generated by `cargo xtask scorecard` from `perf/budgets.toml`, `perf/baseline.js| [Performance](#performance) | 3 | 22 | 24 | 58 scenarios: 3 within budget, 22 over budget or failing, 9 tracked only, 19 not run, 5 not measurable yet (baseline 2026-10-05) || [File compatibility](#file-compatibility) | 7 | 14 | 6 | io corpus: oracle ≥ 148/170, round trip ≥ 169/170; psd-tools corpus: oracle ≥ 249/309, round trip ≥ 308/309; photoshop oracles: oracle ≥ 188/256, round trip ≥ 258/256 || [Tools](#tools) | 33 | 11 | 0 | |-| [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 26 of 168 (target 0) |+| [Workspace and UI](#workspace-and-ui) | 13 | 6 | 4 | settings that do nothing: 25 of 168 (target 0) || [Type](#type) | 4 | 3 | 2 | || [Automation](#automation) | 5 | 2 | 4 | 631 of 631 menu items live (100.0%) || [Reliability](#reliability) | 5 | 2 | 0 | never-crash lints on 30 of 30 crates; panic_hunt present |@@ -179,9 +179,9 @@ Corpus floors (`crates/io/tests/corpus.rs`): the enforced minimums; the corpus t## Workspace and UI-**Settings that do nothing: 26 of 168** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.+**Settings that do nothing: 25 of 168** (target 0, #204). Counted by the prefs audit (`xtask/src/scorecard.rs`): a `Preferences` field counts as read when code outside the prefs model (`prefs.rs` except its `impl Session` blocks; tests excluded; the schema-driven dialog never names a field) accesses `section.field`, or `.section` and `.field` in the same file, or calls a section method that reads `self.field`. It is a heuristic lower bound: a generic name can look read when it isn't.-Unread: `general.color_picker`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `file_handling.save_in_background`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.text_engine`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `enhanced_controls.touch_gestures`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.+Unread: `general.color_picker`, `general.animated_zoom`, `general.zoom_resizes_windows`, `workspace.auto_show_hidden_panels`, `workspace.enable_floating_document_window_docking`, `file_handling.save_in_background`, `performance.cache_levels`, `performance.legacy_compositing`, `scratch_disks.disks`, `units_and_rulers.column_width`, `units_and_rulers.gutter`, `plug_ins.show_extension_panels`, `plug_ins.allow_scripts_to_connect`, `plug_ins.generator_enabled`, `type_.text_engine`, `type_.recent_fonts`, `enhanced_controls.scrubby_slider_acceleration`, `raw_defaults.color_space`, `raw_defaults.bit_depth`, `raw_defaults.resolution`, `raw_defaults.sharpen_for`, `raw_defaults.open_as_smart_object`, `raw_defaults.apply_auto_tone`, `integrations.allow_agent_control`, `integrations.control_port`.| Id | Target | Status | Issue | Note ||---|---|---|---|---|