JFLib

ui/touch.rs#

Source: lib/ui/touch.rs

touch -- the finger: one gesture recogniser for every host

  touch_takes(ev) -> a raw finger event (pointer data PTR_TOUCH, process_events)
  touch_input(ev)                 the raw finger: the recogniser's state, events for the widgets
  touch_tick()                    the clock: the long press, the hover of a resting finger
  touch_apply()                   run_passes: what the finger and the fling moved, into the views
  pan_view(...) / pan_take(id)    a view with its own position rolls by finger (vlist, md_view,
                                  text views, the console)
  touch_mode() -> the pointer is a finger (no scroll bars)   touch_drag()
  touch_take(TF_*) a widget takes the finger or declines it   touch_fingers() -> what is down
  touch_two() -> two fingers on the last widget (TF_TWO): their midpoint and scale (pinch, pan)
  touch_hold_part(id) -> how far the press and hold on a title bar is (TF_HOLD_LONG)

A host only forwards raw fingers: plat::finger(win, phase, id, x, y, age_ms), or jfgui_finger where
the windows live in one surface (the Android host: GalleryActivity.java, bridge.c; the browser's
pointer events: web/loader.js); scripts: swipe, flick, tap, finger, `pointer touch` (auto.rs).  Only
this file sees them raw.  It knows no platform: all of them behave the same and scripts test it on
the virtual clock.

Every finger is in a table (touch_fingers).  The first one down drives the gesture.  A second one
takes nothing from it: while nothing is decided it is no tap any more (no click, no long press);
when the first one lifts, a pan goes on with the other one, a press ends, the rest waits for the
last finger to lift.  A widget with TF_TWO gets the second finger for itself: no press (one younger
than 200 ms is taken back: touch_two says so), and a pass whenever a finger moves, for its own pan and
zoom.

A pen comes first: while it is down, hovers or left less than 500 ms ago a finger is a palm and does
not exist (no finger mode either); a pen that comes down ends the fingers already on the screen.

A finger is wider than what it means: on the background (a title bar, an empty bar, no hit box at
all) the nearest small widget wins whose rectangle, grown to 44 DIP each way around its middle, holds
the point (touch_aim).  An exact hit stays what it is; a mouse sees none of this and nothing is drawn
differently.  A tap is a click wherever the finger lifts within the slop.

The first decision waits until the finger left the slop (ViewConfiguration: 8 DIP) or lifted:
  - lifted: a tap, press and release at once (no flash when a scroll starts instead);
  - the widget under the down point takes this direction (grabs: a slider its axis, a colour
    wheel, the handle of a row, a window's title bar every direction -- those get the press at
    the touch itself, no slop, unless TF_SLOP or TF_HOLD): press and moves go to the widget like
    a mouse's;
  - a scroll view around the down point can move along the dominant axis: the pan.  The chain
    of views is fixed there (from the hit boxes of the last pass), innermost first: each moves
    as far as it can, the rest goes to the next.  The axis is locked (a view that scrolls both
    ways stays free unless the finger starts along an axis: rails);
  - nothing to roll: press and moves go to the widget after all (TF_DECLINE, TF_HOLD: nothing).
The pan moves the positions with no smoothing, by the finger's pixels; the slop's travel is
caught up on the next 16 DIP (half a pixel per finger pixel) or when it lifts: no jump, nothing
lost.  Lifted
with speed (least squares over the last 100 ms, as Android's VelocityTracker; 50..8000 DIP/s)
the fling runs along OverScroller's curve; it hands over along the chain, a new touch stops it
and is no click.  A finger that rests: after 100 ms it hovers (the widget shows it is meant, a
tooltip follows), after 400 ms the right button (a context menu; plat::touch_long_press lets the
host give feedback; TF_HOLD: the left button, the drag starts, TF_HOLD_LONG after a second; TF_REST:
the left button, no context menu: a canvas); if it moves after that, the same decision.  After the
finger left, the hover goes.

With a finger (and at the start on Android, and in a phone's browser) there are no scroll bars:
no pixels and no hit box in sbar_core, the room they would have stays, so nothing jumps; a mouse
brings them back.  ui.touch_drag() marks a rectangle whose hit box is someone else's (the handle
inside a list row); widgets that need the finger say so themselves (HB_GRAB_*, the shape of their
cursor, touch_grab).
  • struct Finger a finger that is down (ui.touch_fingers): px of the window, logical in a mirrored one

    • id: u8 -- the host's number for it, while it is down
    • x: i32
    • y: i32
    • down_x: i32 -- where it came down
    • down_y: i32
    • time_ms: u32 -- its last sample (ms, as Ui::now)
  • struct TwoFingers two fingers on a widget that said TF_TWO (ui.touch_two): px of the window, logical in a mirrored one

    • x: i32 -- the point between them
    • y: i32
    • start_x: i32 -- where it was when this pair came down
    • start_y: i32
    • scale: f64 -- their distance against the one at the start (1.0 = the same)
    • began: bool -- the first read of this pair: take the anchor now
    • taken_back: bool -- the first finger's press went out less than 200 ms ago and was taken back for this pair: its release comes through as usual, undo what the press began

impl Ui

  • fn touch_mode(&self) -> bool ui_touch_mode: the pointer is a finger (the last pointer event came from one; on Android and in a phone's browser from the start): the library shows no scroll bars. A mouse ends it
  • fn touch_drag(&mut self) ui_touch_drag: a finger that starts on the last widget's rectangle drags in every direction instead of rolling what is around it: for a part of a widget that has no hit box of its own (the handle of a list's row). Sliders, colour wheels, splitters and the like take the finger by themselves. Call it after the widget, every frame
  • fn touch_take(&mut self, flags: u32) ui_touch_take(flags): what the last widget (all hit boxes in its rectangle) wants of a finger, TF_*, instead of what its hit boxes say by themselves. Call it after the widget, every frame
  • fn touch_fingers(&self) -> Vec<Finger> ui_touch_fingers: the fingers down on the live window, the first one first (a widget with TF_TWO gets a pass whenever one moves)
  • fn touch_two(&mut self) -> Option<TwoFingers> ui_touch_two: two fingers on the last widget (it said TF_TWO; call it after the widget, every frame): where they are and how far apart. None while the first finger drags the widget: a stroke older than 200 ms goes on, the second finger waits
  • fn touch_hold_part(&self, id: u32) -> Option<u32> ui_touch_hold_part(id) -> how far a finger's press and hold on widget id of this window is, in 1/1000 (TF_HOLD_LONG): None without one, 1000 once the drag runs. Show it: it is long without a sign

Source: docs/API.md in the JFLib repository.