From a9c6c15ef9eaa3326797370cf64a05746ded8b98 Mon Sep 17 00:00:00 2001 From: FacuM Date: Sun, 25 Oct 2026 18:05:31 -0300 Subject: [PATCH] Add a pan-or-zoom contact drag and the barrel press while touching The new `pan_zoom` contact-drag action classifies the drag once it passes the slop: a mostly vertical drag (within about 27 degrees of vertical) zooms with whole wheel notches, pen up zooms in. Any other drag holds the pan combination (Space by default) and pans from the touch point. The mode stays until the lift. With Pan or PanZoom as the contact drag, pressing the barrel while the tip is already down ends the stroke and continues as if the tip had touched with the barrel held, instead of forwarding the barrel raw. The Android settings offer "Pan or zoom" in every locale, the pan shortcut editor is shown for it, and docs/input.md describes both behaviours. --- .../com/librestatic/ezdisplay/InputPrefs.kt | 2 +- .../ezdisplay/settings/SettingsStore.kt | 15 +- .../app/src/main/res/values-de/strings.xml | 1 + .../app/src/main/res/values-es/strings.xml | 1 + .../app/src/main/res/values-fr/strings.xml | 1 + .../app/src/main/res/values-it/strings.xml | 1 + .../app/src/main/res/values-pt/strings.xml | 1 + android/app/src/main/res/values/strings.xml | 1 + .../ezdisplay/settings/SettingsRulesTest.kt | 1 + .../ezdisplay/settings/SettingsStoreTest.kt | 16 + .../ezdisplay/settings/SettingsWideTest.kt | 2 +- crates/ezd-input/src/config.rs | 9 +- crates/ezd-input/src/gesture.rs | 139 +++++++- crates/ezd-input/src/tests.rs | 299 ++++++++++++++++++ docs/input.md | 20 ++ 15 files changed, 497 insertions(+), 12 deletions(-) diff --git a/android/app/src/main/kotlin/com/librestatic/ezdisplay/InputPrefs.kt b/android/app/src/main/kotlin/com/librestatic/ezdisplay/InputPrefs.kt index 75c70e3..d2e21ff 100644 --- a/android/app/src/main/kotlin/com/librestatic/ezdisplay/InputPrefs.kt +++ b/android/app/src/main/kotlin/com/librestatic/ezdisplay/InputPrefs.kt @@ -130,7 +130,7 @@ object InputPrefs { p.getString(CUSTOM_DOUBLE_HOLD, "none") == "keys" || p.getString(CUSTOM_HOLD_DRAG_CONTACT, "raw") == "step_zoom" || p.getString(CUSTOM_HOLD_DRAG_HOVER, "raw") == "step_zoom" || - p.getString(CUSTOM_HOLD_DRAG_CONTACT, "raw") == "pan" + p.getString(CUSTOM_HOLD_DRAG_CONTACT, "raw") in setOf("pan", "pan_zoom") else -> false } } diff --git a/android/app/src/main/kotlin/com/librestatic/ezdisplay/settings/SettingsStore.kt b/android/app/src/main/kotlin/com/librestatic/ezdisplay/settings/SettingsStore.kt index d493f91..fc26110 100644 --- a/android/app/src/main/kotlin/com/librestatic/ezdisplay/settings/SettingsStore.kt +++ b/android/app/src/main/kotlin/com/librestatic/ezdisplay/settings/SettingsStore.kt @@ -20,13 +20,13 @@ class Trigger(val key: String, val label: Int, val default: String, val actions: /** * One key-combo editor: where it is stored ([prefKey]), its [title], and the Custom [triggerKeys] that enable it - * when any of them is set to [action]. + * when any of them is set to one of [actions]. */ class KeyEditorSpec( val prefKey: String, val title: Int, val triggerKeys: List, - val action: String = "keys", + val actions: Set = setOf("keys"), ) { constructor(prefKey: String, title: Int, triggerKey: String) : this(prefKey, title, listOf(triggerKey)) } @@ -48,6 +48,7 @@ val ActionLabels = mapOf( "step_zoom" to R.string.action_step_zoom, "wheel_zoom" to R.string.action_wheel_zoom, "pan" to R.string.action_pan, + "pan_zoom" to R.string.action_pan_zoom, "none" to R.string.action_none, "keys" to R.string.action_keys, ) @@ -70,7 +71,7 @@ val Triggers = listOf( Trigger(InputPrefs.CUSTOM_DOUBLE_HOLD, R.string.trigger_double_hold, "none", listOf("eraser", "keys", "none")), Trigger(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, R.string.trigger_hold_drag_contact, "raw", - listOf("raw", "scroll", "pc_zoom", "step_zoom", "wheel_zoom", "pan", "none")), + listOf("raw", "scroll", "pc_zoom", "step_zoom", "wheel_zoom", "pan", "pan_zoom", "none")), Trigger(InputPrefs.CUSTOM_HOLD_DRAG_HOVER, R.string.trigger_hold_drag_hover, "raw", listOf("raw", "scroll", "pc_zoom", "step_zoom", "wheel_zoom", "none")), ) @@ -83,15 +84,15 @@ val KeyEditors = listOf( KeyEditorSpec(InputPrefs.KEYS_DOUBLE_HOLD, R.string.keys_title_double_hold, InputPrefs.CUSTOM_DOUBLE_HOLD), KeyEditorSpec( InputPrefs.KEYS_ZOOM_IN, R.string.keys_title_zoom_in, - listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, InputPrefs.CUSTOM_HOLD_DRAG_HOVER), "step_zoom", + listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, InputPrefs.CUSTOM_HOLD_DRAG_HOVER), setOf("step_zoom"), ), KeyEditorSpec( InputPrefs.KEYS_ZOOM_OUT, R.string.keys_title_zoom_out, - listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, InputPrefs.CUSTOM_HOLD_DRAG_HOVER), "step_zoom", + listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, InputPrefs.CUSTOM_HOLD_DRAG_HOVER), setOf("step_zoom"), ), KeyEditorSpec( InputPrefs.KEYS_PAN, R.string.keys_title_pan, - listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT), "pan", + listOf(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT), setOf("pan", "pan_zoom"), ), ) @@ -132,7 +133,7 @@ fun visibleKeyEditors(state: SettingsState): List = when (state.profile) "graphics_tablet" -> listOf(InputPrefs.KEYS_HOLD, InputPrefs.KEYS_DOUBLE_HOLD) "custom" -> KeyEditors.filter { e -> // The drag triggers are unused while hold sends keys. - e.triggerKeys.any { state.custom[it] == e.action && !(it in DragTriggerKeys && dragsDisabled(state)) } + e.triggerKeys.any { state.custom[it] in e.actions && !(it in DragTriggerKeys && dragsDisabled(state)) } }.map { it.prefKey } else -> emptyList() } diff --git a/android/app/src/main/res/values-de/strings.xml b/android/app/src/main/res/values-de/strings.xml index 5ba4aae..6578e47 100644 --- a/android/app/src/main/res/values-de/strings.xml +++ b/android/app/src/main/res/values-de/strings.xml @@ -38,6 +38,7 @@ Zoom in Stufen Zoom mit dem Mausrad, in Stufen Arbeitsfläche verschieben + Verschieben oder zoomen Keine Zoomstufe Die Air-Aktionen von Samsung sind aktiviert. Sie können auf Tastendrücke reagieren, die bei aufgesetzter Stiftspitze beginnen. Deaktiviere sie unter Einstellungen → Erweiterte Funktionen → S Pen → Air-Aktionen. diff --git a/android/app/src/main/res/values-es/strings.xml b/android/app/src/main/res/values-es/strings.xml index 68705dd..c19a54d 100644 --- a/android/app/src/main/res/values-es/strings.xml +++ b/android/app/src/main/res/values-es/strings.xml @@ -38,6 +38,7 @@ Zoom por pasos Zoom con la rueda, por pasos Mover el lienzo + Mover o hacer zoom Nada Nivel de zoom Las acciones aéreas de Samsung están activadas. Pueden reaccionar a pulsaciones del botón que empiezan con la punta apoyada. Desactivalas en Ajustes → Funciones avanzadas → S Pen → Acciones aéreas. diff --git a/android/app/src/main/res/values-fr/strings.xml b/android/app/src/main/res/values-fr/strings.xml index 47d1ee5..d82f806 100644 --- a/android/app/src/main/res/values-fr/strings.xml +++ b/android/app/src/main/res/values-fr/strings.xml @@ -38,6 +38,7 @@ Zoom par paliers Zoom par molette, par paliers Déplacer le canevas + Déplacer ou zoomer Aucune Niveau de zoom Les actions aériennes de Samsung sont activées. Elles peuvent réagir aux appuis sur le bouton qui commencent avec la pointe posée. Désactivez-les dans Paramètres → Fonctions avancées → S Pen → Actions aériennes. diff --git a/android/app/src/main/res/values-it/strings.xml b/android/app/src/main/res/values-it/strings.xml index 7a94830..7e2dc21 100644 --- a/android/app/src/main/res/values-it/strings.xml +++ b/android/app/src/main/res/values-it/strings.xml @@ -38,6 +38,7 @@ Zoom a scatti Zoom con la rotellina, a scatti Sposta la tela + Sposta o ingrandisci Nessuna Livello di zoom Le azioni aeree di Samsung sono attive. Possono reagire alle pressioni del pulsante che iniziano con la punta appoggiata. Disattivale in Impostazioni → Funzioni avanzate → S Pen → Azioni aeree. diff --git a/android/app/src/main/res/values-pt/strings.xml b/android/app/src/main/res/values-pt/strings.xml index 54c6916..dd63270 100644 --- a/android/app/src/main/res/values-pt/strings.xml +++ b/android/app/src/main/res/values-pt/strings.xml @@ -38,6 +38,7 @@ Zoom em etapas Zoom com a roda, em etapas Mover a tela + Mover ou dar zoom Nada Nível de zoom As ações aéreas da Samsung estão ativadas. Elas podem reagir a pressionamentos do botão que começam com a ponta apoiada. Desative-as em Configurações → Recursos avançados → S Pen → Ações aéreas. diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml index 941e5cb..b9a55e0 100644 --- a/android/app/src/main/res/values/strings.xml +++ b/android/app/src/main/res/values/strings.xml @@ -38,6 +38,7 @@ Zoom in steps Zoom with the wheel, in steps Pan the canvas + Pan or zoom Nothing Zoom level Samsung Air actions are on. They may react to button presses that start with the pen tip down. Turn them off in Settings → Advanced features → S Pen → Air actions. diff --git a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsRulesTest.kt b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsRulesTest.kt index 5e631e9..e4de4ba 100644 --- a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsRulesTest.kt +++ b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsRulesTest.kt @@ -75,6 +75,7 @@ class SettingsRulesTest { @Test fun panOnTheContactDragShowsThePanEditor() { assertEquals(listOf(InputPrefs.KEYS_PAN), visibleKeyEditors(custom(contact = "pan"))) + assertEquals(listOf(InputPrefs.KEYS_PAN), visibleKeyEditors(custom(contact = "pan_zoom"))) assertEquals(emptyList(), visibleKeyEditors(custom(hover = "pan"))) assertEquals(emptyList(), visibleKeyEditors(custom(contact = "wheel_zoom", hover = "wheel_zoom"))) assertEquals(listOf(InputPrefs.KEYS_HOLD), visibleKeyEditors(custom(hold = "keys", contact = "pan"))) diff --git a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsStoreTest.kt b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsStoreTest.kt index be784dc..ced37e6 100644 --- a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsStoreTest.kt +++ b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsStoreTest.kt @@ -138,6 +138,22 @@ class SettingsStoreTest { assertEquals("raw", s.custom[InputPrefs.CUSTOM_HOLD]) } + @Test fun panZoomIsOnlyAcceptedByTheContactDragAndUsesKeys() { + store.setProfile("custom") + store.setCustomAction(InputPrefs.CUSTOM_HOLD_DRAG_HOVER, "pan_zoom") + store.setCustomAction(InputPrefs.CUSTOM_HOLD, "pan_zoom") + assertEquals("raw", s.custom[InputPrefs.CUSTOM_HOLD_DRAG_HOVER]) + assertEquals("raw", s.custom[InputPrefs.CUSTOM_HOLD]) + assertFalse(InputPrefs.usesKeys(context)) + store.setCustomAction(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, "pan_zoom") + assertEquals("pan_zoom", prefs.getString(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, null)) + assertTrue(InputPrefs.usesKeys(context)) + assertEquals( + "pan_zoom", + JSONObject(InputPrefs.toJson(context)).getJSONObject("custom").getString("hold_drag_contact"), + ) + } + @Test fun panShortcutDefaultsToSpaceAndReachesTheJson() { assertEquals(KeyCombo(0, 0x2C), s.keys[InputPrefs.KEYS_PAN]) store.setKey(InputPrefs.KEYS_PAN, 0x04) diff --git a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsWideTest.kt b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsWideTest.kt index 7192abe..be9b4a6 100644 --- a/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsWideTest.kt +++ b/android/app/src/test/kotlin/com/librestatic/ezdisplay/settings/SettingsWideTest.kt @@ -83,7 +83,7 @@ class SettingsWideTest : SettingsUiTestBase() { @Test fun customPanShowsThePanShortcut() { launch(AirActions.On) { putString(InputPrefs.PROFILE, "custom") - putString(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, "pan") + putString(InputPrefs.CUSTOM_HOLD_DRAG_CONTACT, "pan_zoom") putString(InputPrefs.CUSTOM_HOLD_DRAG_HOVER, "wheel_zoom") } tag("shortcut_keys_zoom_in").assertDoesNotExist() diff --git a/crates/ezd-input/src/config.rs b/crates/ezd-input/src/config.rs index 1deb114..8667dd8 100644 --- a/crates/ezd-input/src/config.rs +++ b/crates/ezd-input/src/config.rs @@ -48,6 +48,13 @@ pub enum ButtonAction { /// ([`KeyBindings::pan`], Space by default) is kept pressed while the tip /// touches, so a drag with the pen pans in many drawing apps. Pan, + /// Pan or zoom the canvas of the host by dragging with the tip (contact + /// drag only), like a graphics-tablet pan/zoom button: a mostly vertical + /// drag zooms with the wheel (as [`ButtonAction::WheelZoom`]), any other + /// drag pans (as [`ButtonAction::Pan`]). The first drag past the slop + /// decides, until the lift. With an empty pan combination the pan branch + /// just forwards the contact. + PanZoom, /// Toggle the local zoom of the view. LocalZoom, /// Switch to the eraser while held (hold trigger only). @@ -246,7 +253,7 @@ impl ButtonMap { _ => None, }, hold_drag_contact: match self.hold_drag_contact { - Pan => Pan, + Pan | PanZoom => self.hold_drag_contact, a => drag(a), }, hold_drag_hover: drag(self.hold_drag_hover), diff --git a/crates/ezd-input/src/gesture.rs b/crates/ezd-input/src/gesture.rs index 7091bd1..ec7a40b 100644 --- a/crates/ezd-input/src/gesture.rs +++ b/crates/ezd-input/src/gesture.rs @@ -40,6 +40,17 @@ const STEP_ZOOM_GAP_US: u64 = 60_000; /// Zoom steps that can be queued by fast travel; the rest is dropped. const STEP_ZOOM_BACKLOG: f32 = 3.0; +/// Decision of the [`Held::PanZoom`] drag, taken once per touch. +#[derive(Debug, Clone, Copy, PartialEq)] +pub(crate) enum PanZoomMode { + /// The travel from the anchor has not passed the slop yet. + Classify, + /// Wheel zoom steps; `rem` holds the upward travel not yet turned into steps. + Zoom, + /// The pan combination is pressed and the contact is forwarded. + Pan, +} + /// A resolved gesture whose barrel button is still down. #[derive(Debug, Clone, Copy, PartialEq)] pub(crate) enum Held { @@ -60,6 +71,16 @@ pub(crate) enum Held { /// pressed (`PenState::keys_held`) while the tip touches and released at /// the lift. The pen reports contact and motion as usual. PanDrag, + /// Pan or zoom drag. The tip touch is reported as hover until the drag + /// picks a mode; `Pan` then forwards the contact (starting at `anchor`, the + /// first touch point) and `Zoom` keeps reporting hover. After a lift the + /// state waits for the next touch (barrel still held). + PanZoom { + anchor: (f32, f32), + touching: bool, + mode: PanZoomMode, + rem: f32, + }, /// The hold key combination is pressed (`PenState::keys_held`); the pen /// reports contact and motion as usual. Keys, @@ -208,7 +229,10 @@ impl InputEngine { !matches!(m.hold, ButtonAction::Keys | ButtonAction::None) && (drag_effect(m.hold_drag_hover) || drag_effect(m.hold_drag_contact) - || m.hold_drag_contact == ButtonAction::Pan) + || matches!( + m.hold_drag_contact, + ButtonAction::Pan | ButtonAction::PanZoom + )) } /// Hold action fired by the release of a barrel that was held still past the @@ -323,6 +347,35 @@ impl InputEngine { self.set_held(Held::PanDrag); } + /// Starts the pan or zoom drag at the tip touch: contact is reported as + /// hover until the drag picks a mode. + fn start_pan_zoom(&mut self) { + self.pen.suppress = true; + self.set_held(Held::PanZoom { + anchor: self.cur_pos(), + touching: true, + mode: PanZoomMode::Classify, + rem: 0.0, + }); + } + + /// The barrel went down with the tip already touching, and the contact drag + /// is a pan or pan/zoom: ends the stroke with a lift sample, then goes on as + /// if the tip had just touched with the barrel held. + fn barrel_while_touching(&mut self, t: u64, out: &mut Vec) { + self.pen.suppress = true; + let f = self.base_flags(); + self.emit_flush(t, f, out); + if self.p.map.hold_drag_contact == ButtonAction::Pan { + self.start_pan(t, out); + self.pen.suppress = false; + let f = self.base_flags(); + self.emit_flush(t, f, out); + } else { + self.start_pan_zoom(); + } + } + /// Starts the double-hold action: presses its combination and keeps it /// pressed until the barrel is released, or switches to the eraser. fn resolve_double_hold(&mut self, t: u64, out: &mut Vec) { @@ -441,6 +494,16 @@ impl InputEngine { let g = self.pen.gesture; if down { match g { + Gesture::Idle + if self.pen.contact + && self.p.map.hold != ButtonAction::Keys + && matches!( + self.p.map.hold_drag_contact, + ButtonAction::Pan | ButtonAction::PanZoom + ) => + { + self.barrel_while_touching(t, out); + } Gesture::Idle if self.pen.contact => { // Pressed with the tip already down: forward raw. self.pen.barrel_fwd = true; @@ -474,7 +537,17 @@ impl InputEngine { } else { // The held keys are released whatever the gesture state is, except // the pan keys of a touching tip: they stay until the lift. - if !(g == Gesture::Held(Held::PanDrag) && self.pen.contact) { + let pan_keys = matches!( + g, + Gesture::Held( + Held::PanDrag + | Held::PanZoom { + mode: PanZoomMode::Pan, + .. + } + ) + ); + if !(pan_keys && self.pen.contact) { self.release_keys(t, out); } self.pen.gesture = Gesture::Idle; @@ -598,6 +671,12 @@ impl InputEngine { { self.start_pan(t, out); } + Gesture::Pending { .. } + | Gesture::Held(Held::HoverDrag { .. } | Held::PanZoom { .. }) + if self.p.map.hold_drag_contact == ButtonAction::PanZoom => + { + self.start_pan_zoom(); + } Gesture::Pending { past_t: true, .. } if self.defers_hold() && !drag_effect(self.p.map.hold_drag_contact) => { @@ -685,6 +764,14 @@ impl InputEngine { self.instant(click, Trigger::Click, t, out); } } + if let Gesture::Held(Held::PanZoom { anchor, .. }) = self.pen.gesture { + self.set_held(Held::PanZoom { + anchor, + touching: false, + mode: PanZoomMode::Classify, + rem: 0.0, + }); + } if self.pen.eraser_switch_pending { self.eraser_end(t, out); } @@ -732,6 +819,45 @@ impl InputEngine { } let prev = self.pen.prev; let d = (cur.0 - prev.0, cur.1 - prev.1); + if let Gesture::Held(Held::PanZoom { + anchor, + touching: true, + mode: PanZoomMode::Classify, + .. + }) = self.pen.gesture + && dist(cur, anchor) > self.p.slop + { + let (dx, dy) = (cur.0 - anchor.0, cur.1 - anchor.1); + if dy.abs() >= 2.0 * dx.abs() { + // The travel up to the previous sample; this sample's delta is + // added below, so the slop distance counts for the first step. + self.set_held(Held::PanZoom { + anchor, + touching: true, + mode: PanZoomMode::Zoom, + rem: anchor.1 - prev.1, + }); + } else { + let c = self.p.keys.pan; + if self.pen.keys_held.is_none() { + Self::press_combo(c, t, out); + self.pen.keys_held = Some(c); + } + self.pen.suppress = false; + self.set_held(Held::PanZoom { + anchor, + touching: true, + mode: PanZoomMode::Pan, + rem: 0.0, + }); + // The app pans from where the tip first touched. + let saved = (self.pen.cur.x, self.pen.cur.y); + (self.pen.cur.x, self.pen.cur.y) = anchor; + let f = self.base_flags(); + self.emit_flush(t, f, out); + (self.pen.cur.x, self.pen.cur.y) = saved; + } + } let n = self.p.scroll_n; let slop = self.p.slop; let mut emit: Option<((f32, f32), Units)> = None; @@ -766,6 +892,15 @@ impl InputEngine { } } } + Gesture::Held(Held::PanZoom { + anchor, + touching: true, + mode: PanZoomMode::Zoom, + rem, + }) => { + zoom = step_zoom_dir(rem, d.1, zstep, &mut self.pen.step_last, t) + .map(|up| (*anchor, up, true)); + } Gesture::Held(Held::Pan) => { self.view = crate::view::ViewTransform { tx: self.view.tx + d.0, diff --git a/crates/ezd-input/src/tests.rs b/crates/ezd-input/src/tests.rs index 7e9228f..af72121 100644 --- a/crates/ezd-input/src/tests.rs +++ b/crates/ezd-input/src/tests.rs @@ -2439,6 +2439,305 @@ fn pan_config_json_and_sanitizing() { assert_eq!(c.button_map().hold_drag_contact, ButtonAction::None); } +// ---- pan / zoom drag ---------------------------------------------------------------- + +/// A Custom rig with `contact` on the contact drag, the barrel pressed in the air at (500,500). +fn pz_rig(contact: ButtonAction) -> Rig { + let mut r = custom_keys( + ButtonMap { + click: ButtonAction::None, + double_click: ButtonAction::None, + hold: ButtonAction::None, + double_hold: ButtonAction::None, + hold_drag_contact: contact, + hold_drag_hover: ButtonAction::None, + }, + KeyBindings::default(), + ); + r.feed(hover(0, 500.0, 500.0, 0)); + r.feed(press(100, 500.0, 500.0)); + r +} + +fn pz_touching() -> Rig { + let mut r = pz_rig(ButtonAction::PanZoom); + assert_eq!( + r.feed(pen_down(110, 500.0, 500.0, PRIMARY)), + ["pen I 500,500"] + ); + r +} + +#[test] +fn pan_zoom_vertical_drag_zooms_without_contact_or_keys() { + let mut r = pz_touching(); + // Under the slop: nothing but hover. + assert_eq!( + r.feed(pen_move(120, 500.0, 490.0, PRIMARY)), + ["pen I 500,490"] + ); + // Past the slop (24 px) and 80 px of travel counted from the anchor. + assert_eq!( + r.feed(pen_move(130, 510.0, 420.0, PRIMARY)), + ["scroll 0,120 m0 @500,500", "pen I 510,420"] + ); + // Locked: a sideways move keeps zooming (down = zoom out), still no contact. + let out = r.feed(pen_move(200, 900.0, 500.0, PRIMARY)); + assert_eq!(out, ["scroll 0,-120 m0 @500,500", "pen I 900,500"]); + let out = r.feed(pen_move(300, 100.0, 500.0, PRIMARY)); + assert_eq!(out, ["pen I 100,500"]); + assert_eq!( + r.feed(pen_up(310, 100.0, 500.0, PRIMARY)), + ["pen I 100,500"] + ); +} + +#[test] +fn pan_zoom_first_step_counts_the_slop_distance() { + let mut r = pz_touching(); + // 40 px up in one sample: past the slop but under one step: nothing yet. + assert_eq!( + r.feed(pen_move(120, 500.0, 460.0, PRIMARY)), + ["pen I 500,460"] + ); + // 40 px more: the step is due. + assert_eq!( + r.feed(pen_move(130, 500.0, 420.0, PRIMARY))[0], + "scroll 0,120 m0 @500,500" + ); +} + +#[test] +fn pan_zoom_other_drags_pan_from_the_anchor_and_stay_locked() { + for (x, y) in [(560.0, 500.0), (560.0, 530.0)] { + let mut r = pz_touching(); + assert_eq!( + r.feed(pen_move(120, x, y, PRIMARY)), + [ + SPACE_DOWN, + "pen IC 500,500 p=32768", + &format!("pen IC {x},{y} p=32768"), + ] + ); + // Locked: a vertical move now keeps panning. + assert_eq!( + r.feed(pen_move(200, x, y - 300.0, PRIMARY)), + [format!("pen IC {x},{} p=32768", y - 300.0)] + ); + assert_eq!( + r.feed(pen_up(210, x, y - 300.0, PRIMARY)), + [format!("pen I {x},{}", y - 300.0), SPACE_UP.to_string()] + ); + } +} + +#[test] +fn pan_zoom_tap_under_the_slop_does_nothing_and_the_next_touch_reclassifies() { + let mut r = pz_touching(); + assert_eq!( + r.feed(pen_move(120, 510.0, 505.0, PRIMARY)), + ["pen I 510,505"] + ); + assert_eq!( + r.feed(pen_up(130, 510.0, 505.0, PRIMARY)), + ["pen I 510,505"] + ); + r.feed(pen_down(200, 510.0, 505.0, PRIMARY)); + let out = r.feed(pen_move(210, 600.0, 505.0, PRIMARY)); + assert_eq!(out[0], SPACE_DOWN, "{out:?}"); + assert_eq!(out[1], "pen IC 510,505 p=32768"); +} + +#[test] +fn pan_zoom_barrel_released_mid_touch_keeps_the_mode_until_the_lift() { + // Pan: the keys stay until the lift and the contact keeps flowing. + let mut r = pz_touching(); + r.feed(pen_move(120, 600.0, 500.0, PRIMARY)); + let out = r.feed(release(130, 610.0, 500.0)); + assert!(keys_of(&out).is_empty(), "{out:?}"); + assert_eq!( + r.feed(pen_move(140, 620.0, 500.0, 0)), + ["pen IC 620,500 p=32768"] + ); + assert_eq!( + r.feed(pen_up(150, 620.0, 500.0, 0)), + ["pen I 620,500", SPACE_UP] + ); + // Before the mode was picked: no contact until the lift, no keys. + let mut r = pz_touching(); + r.feed(release(120, 500.0, 500.0)); + let out = r.feed(pen_move(130, 700.0, 500.0, 0)); + assert_eq!(out, ["pen I 700,500"]); + assert_eq!(r.feed(pen_up(140, 700.0, 500.0, 0)), ["pen I 700,500"]); +} + +#[test] +fn pan_zoom_releases_the_keys_on_cancel_pen_out_and_release_all() { + let panning = || { + let mut r = pz_touching(); + r.feed(pen_move(120, 600.0, 500.0, PRIMARY)); + r + }; + let mut r = panning(); + let out = r.feed(pen_at(ACTION_CANCEL, 130, 600.0, 500.0, PRIMARY)); + assert_eq!(keys_of(&out), [SPACE_UP]); + let mut r = panning(); + let mut all = r.feed(exit(130, 600.0, 500.0)); + all.extend(r.tick(200)); + assert_eq!(keys_of(&all), [SPACE_UP]); + let mut r = panning(); + let mut out = Vec::new(); + r.e.release_all(ms(130), &mut out); + let out: Vec = out.iter().map(show).collect(); + assert_eq!(keys_of(&out), [SPACE_UP]); +} + +#[test] +fn pan_zoom_with_an_empty_pan_combo_still_zooms_and_pans_without_keys() { + let mut r = custom_keys( + ButtonMap { + hold_drag_contact: ButtonAction::PanZoom, + ..ButtonMap::for_profile(Profile::Raw) + }, + KeyBindings { + pan: KeyCombo::default(), + ..KeyBindings::default() + }, + ); + r.feed(hover(0, 500.0, 500.0, 0)); + r.feed(press(100, 500.0, 500.0)); + r.feed(pen_down(110, 500.0, 500.0, PRIMARY)); + let out = r.feed(pen_move(120, 600.0, 500.0, PRIMARY)); + assert_eq!(out, ["pen IC 500,500 p=32768", "pen IC 600,500 p=32768"]); +} + +#[test] +fn pan_zoom_yields_to_a_keys_hold() { + let mut r = custom_keys( + ButtonMap { + hold: ButtonAction::Keys, + hold_drag_contact: ButtonAction::PanZoom, + ..ButtonMap::for_profile(Profile::Raw) + }, + KeyBindings::default(), + ); + r.feed(hover(0, 500.0, 500.0, 0)); + r.feed(press(100, 500.0, 500.0)); + let out = r.feed(pen_down(110, 500.0, 500.0, PRIMARY)); + assert_eq!(keys_of(&out), CTRL_SPACE_DOWN); +} + +/// A rig with the tip already down (drawing) at (500,500) and the barrel not pressed. +fn touching_first(contact: ButtonAction) -> Rig { + let mut r = custom_keys( + ButtonMap { + hold_drag_contact: contact, + ..ButtonMap::for_profile(Profile::Raw) + }, + KeyBindings::default(), + ); + r.feed(hover(0, 500.0, 500.0, 0)); + assert_eq!( + r.feed(pen_down(50, 500.0, 500.0, 0)), + ["pen IC 500,500 p=32768"] + ); + r +} + +#[test] +fn barrel_while_touching_starts_pan_zoom_with_a_lift_sample() { + let mut r = touching_first(ButtonAction::PanZoom); + // The stroke ends (lift sample), then the drag classifies as usual. + assert_eq!(r.feed(press(100, 500.0, 500.0)), ["pen I 500,500"]); + assert_eq!( + r.feed(pen_move(110, 540.0, 420.0, PRIMARY)), + ["scroll 0,120 m0 @500,500", "pen I 540,420"] + ); + // The real lift sends no key or release, just hover. + assert_eq!( + r.feed(pen_up(120, 540.0, 420.0, PRIMARY)), + ["pen I 540,420"] + ); + + let mut r = touching_first(ButtonAction::PanZoom); + r.feed(press(100, 500.0, 500.0)); + assert_eq!( + r.feed(pen_move(110, 600.0, 500.0, PRIMARY)), + [ + SPACE_DOWN, + "pen IC 500,500 p=32768", + "pen IC 600,500 p=32768" + ] + ); + assert_eq!( + r.feed(pen_up(120, 600.0, 500.0, PRIMARY)), + ["pen I 600,500", SPACE_UP] + ); +} + +#[test] +fn barrel_while_touching_starts_pan_with_the_keys_between_lift_and_contact() { + let mut r = touching_first(ButtonAction::Pan); + assert_eq!( + r.feed(press(100, 520.0, 500.0)), + ["pen I 520,500", SPACE_DOWN, "pen IC 520,500"] + ); + assert_eq!( + r.feed(pen_move(110, 560.0, 500.0, PRIMARY)), + ["pen IC 560,500 p=32768"] + ); + // Barrel released before the lift: the keys stay until the lift. + let out = r.feed(release(120, 560.0, 500.0)); + assert!(keys_of(&out).is_empty(), "{out:?}"); + assert_eq!( + r.feed(pen_up(130, 560.0, 500.0, 0)), + ["pen I 560,500", SPACE_UP] + ); +} + +#[test] +fn barrel_while_touching_pan_releases_the_keys_on_cancel() { + let mut r = touching_first(ButtonAction::Pan); + r.feed(press(100, 520.0, 500.0)); + let out = r.feed(pen_at(ACTION_CANCEL, 110, 520.0, 500.0, PRIMARY)); + assert_eq!(keys_of(&out), [SPACE_UP]); +} + +#[test] +fn barrel_while_touching_stays_raw_for_other_configs() { + for contact in [ + ButtonAction::Raw, + ButtonAction::Scroll, + ButtonAction::WheelZoom, + ] { + let mut r = touching_first(contact); + let out = r.feed(press(100, 500.0, 500.0)); + assert_eq!(out, ["pen ICB 500,500"], "{contact:?}"); + } +} + +#[test] +fn pan_zoom_config_json_and_sanitizing() { + let c = InputConfig::from_json( + r#"{"profile":"custom","custom":{"hold_drag_contact":"pan_zoom","hold_drag_hover":"pan_zoom"}}"#, + ) + .unwrap(); + let m = c.button_map(); + assert_eq!(m.hold_drag_contact, ButtonAction::PanZoom); + assert_eq!(m.hold_drag_hover, ButtonAction::None); + let c = InputConfig::from_json( + r#"{"profile":"custom","custom":{"click":"pan_zoom","hold":"pan_zoom","double_hold":"pan_zoom"}}"#, + ) + .unwrap(); + let m = c.button_map(); + assert_eq!([m.click, m.hold, m.double_hold], [ButtonAction::None; 3]); + let c = InputConfig::from_json( + r#"{"profile":"custom","custom":{"hold_drag_contact":"pan_zoom"},"keys":{"pan":{"mods":0,"key":0}}}"#, + ) + .unwrap(); + assert_eq!(c.button_map().hold_drag_contact, ButtonAction::PanZoom); +} + // ---- 9. drags win over the hold ---------------------------------------------- fn custom_map(hold: ButtonAction, contact: ButtonAction, hover: ButtonAction) -> Rig { diff --git a/docs/input.md b/docs/input.md index 99674fc..9b6a015 100644 --- a/docs/input.md +++ b/docs/input.md @@ -65,6 +65,26 @@ the button is released first, the shortcut stays down until the lift. It is also range, on cancel and when the session ends. The pan starts at the touch, without waiting for the drag distance. If hold is set to a shortcut, the drags are ignored as usual. +### Pan or zoom with one drag + +"Pan or zoom" on "Hold + drag touching" (Custom profile, not available in the air) works like the pan/zoom +button of a graphics-tablet driver: hold the button, touch and drag. The touch is first reported as hover. Once +the pen has moved farther than the movement tolerance from the touch point, the direction decides, once, until +the lift: a mostly vertical drag (within about 27 degrees of vertical) zooms like "Zoom with the wheel, in +steps" (the travel from the touch point counts, so the first step is not delayed), and any other drag pans like +"Pan the canvas": the pan shortcut is pressed, the contact starts at the point where the tip first touched, +and the shortcut is released after the lift. A touch that never leaves the tolerance does nothing. With an +empty pan shortcut the pan branch just forwards the contact. If the button is released mid-touch the mode +stays until the lift (the pan shortcut stays down, a zoom just stops, and an undecided touch stays hover). + +### Button pressed while touching + +For "Pan the canvas" and "Pan or zoom" the button may also be pressed while the tip already touches (for +example while drawing). The stroke ends with a lift sample, then the drag goes on as if the tip had just +touched with the button held: Pan presses its shortcut and continues the contact from the current point, Pan or +zoom starts deciding from the current point. The real lift later sends nothing extra. For every other +configuration the button pressed while touching is still forwarded as is. A shortcut hold still wins. + Keys are physical positions (USB HID usages of a US keyboard); the PC applies its own layout, as with a real USB keyboard. The host announces the `KEYS` capability; an older host does not, and the tablet then drops the keys and says the host needs an update.