On a touchscreen a drag over the launcher's list scrolls it by pixels, a flick keeps it going, and a tap still launches (or stops a moving list). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
477 lines
14 KiB
MoonBit
477 lines
14 KiB
MoonBit
///| Touch. Fingers on an app's own content go to the app (wl_touch), which scrolls, pinches and
|
|
/// long-presses as it knows how. Everywhere else (the panel, the on-screen keyboard, the desktop,
|
|
/// title bars, dialogs, apps that don't listen to touches) one finger works like a mouse: a tap
|
|
/// clicks, a drag is a left-button drag, and holding still is a right click. Only the launcher's
|
|
/// list scrolls: a drag moves it with the finger, and a quick lift keeps it going.
|
|
|
|
///| Holding still this long is a right click (apps time their own).
|
|
let touch_long_ms : UInt64 = 500UL
|
|
|
|
///| A finger that moved less than this is still tapping or holding.
|
|
let touch_drag_px : Double = 18.0
|
|
|
|
///|
|
|
enum TouchMode {
|
|
/// Nothing, or fingers left over from a gesture that is over.
|
|
NoTouch
|
|
/// Finger `id` works the pointer.
|
|
AsPointer(TouchPointer)
|
|
/// Fingers on an app's surface.
|
|
InClient(TouchClient)
|
|
}
|
|
|
|
///|
|
|
struct TouchPointer {
|
|
id : Int
|
|
x0 : Double
|
|
y0 : Double
|
|
since : UInt64
|
|
mut phase : TouchPhase
|
|
/// The press is on the launcher's list: a drag scrolls it.
|
|
list : Bool
|
|
/// Where the list was last scrolled to, and the finger's last moves (time, y).
|
|
mut y : Double
|
|
moves : Array[(UInt, Double)]
|
|
}
|
|
|
|
///|
|
|
enum TouchPhase {
|
|
Pending
|
|
Dragging
|
|
/// Scrolling the launcher's list.
|
|
Scrolling
|
|
/// The right click was made: the rest of the touch does nothing.
|
|
Held
|
|
} derive(Eq)
|
|
|
|
///|
|
|
struct TouchClient {
|
|
/// The window or popup, and its subsurface (0: itself).
|
|
win : UInt64
|
|
sub : UInt
|
|
/// The fingers down, and where each is and was put down.
|
|
fingers : Map[Int, (Double, Double)]
|
|
x0 : Double
|
|
y0 : Double
|
|
first : Int
|
|
/// Still a tap: one finger that hasn't moved.
|
|
mut tap : Bool
|
|
}
|
|
|
|
///|
|
|
struct TouchState {
|
|
mut mode : TouchMode
|
|
/// The launcher's list still moving after a lift: pixels per ms, when last moved, and the part
|
|
/// of a pixel left over.
|
|
mut fling : Double
|
|
mut fling_at : UInt64
|
|
mut fling_rest : Double
|
|
}
|
|
|
|
///|
|
|
fn TouchState::new() -> TouchState {
|
|
{ mode: NoTouch, fling: 0.0, fling_at: 0UL, fling_rest: 0.0 }
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::touch_input(
|
|
self : ChandraCompositor,
|
|
ev : @nested.TouchEvent,
|
|
time_ms : UInt,
|
|
) -> Unit {
|
|
match ev {
|
|
Present => self.server.set_touch()
|
|
Down(id, x, y) => {
|
|
self.server.set_touch()
|
|
self.touch_down(id, x, y, time_ms)
|
|
}
|
|
Motion(id, x, y) => self.touch_motion(id, x, y, time_ms)
|
|
Up(id) => self.touch_up(id, time_ms)
|
|
Cancel => self.touch_cancel(time_ms)
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::touch_down(
|
|
self : ChandraCompositor,
|
|
id : Int,
|
|
x : Double,
|
|
y : Double,
|
|
time_ms : UInt,
|
|
) -> Unit {
|
|
match self.touch.mode {
|
|
AsPointer(_) => () // one finger works the pointer; others do nothing
|
|
InClient(t) => {
|
|
t.tap = false
|
|
t.fingers.set(id, (x, y))
|
|
self.note_activity(@sys.monotonic_ms())
|
|
let (lx, ly) = self.touch_local(t, x, y)
|
|
self.server.touch_down(t.win, time_ms, id, lx, ly, sub=t.sub)
|
|
}
|
|
NoTouch =>
|
|
match self.touch_target(x, y) {
|
|
Some((win, sub, lx, ly)) => {
|
|
self.note_activity(@sys.monotonic_ms())
|
|
self.input_time = time_ms
|
|
self.super_solo = false
|
|
self.leave_pointer()
|
|
self.press_fwd = false
|
|
self.shell.desktop_leave()
|
|
self.ptr_x = x
|
|
self.ptr_y = y
|
|
if self.windows.contains(win) {
|
|
self.activate_window(win)
|
|
}
|
|
self.touch.mode = InClient({
|
|
win,
|
|
sub,
|
|
fingers: Map::from_array([(id, (x, y))]),
|
|
x0: x,
|
|
y0: y,
|
|
first: id,
|
|
tap: true,
|
|
})
|
|
self.server.touch_down(win, time_ms, id, lx, ly, sub~)
|
|
}
|
|
None => {
|
|
let list = self.touch_list_at(x, y)
|
|
// A finger on a moving list holds it; that isn't a tap on what went by.
|
|
let held = list && self.touch.fling != 0.0
|
|
self.touch.fling = 0.0
|
|
self.touch.mode = AsPointer({
|
|
id,
|
|
x0: x,
|
|
y0: y,
|
|
since: @sys.monotonic_ms(),
|
|
phase: if held { Scrolling } else { Pending },
|
|
list,
|
|
y,
|
|
moves: [(time_ms, y)],
|
|
})
|
|
if held {
|
|
self.note_activity(@sys.monotonic_ms())
|
|
} else {
|
|
self.handle_input_inner(PointerMove(x, y), time_ms)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::touch_motion(
|
|
self : ChandraCompositor,
|
|
id : Int,
|
|
x : Double,
|
|
y : Double,
|
|
time_ms : UInt,
|
|
) -> Unit {
|
|
match self.touch.mode {
|
|
AsPointer(t) =>
|
|
if t.id == id {
|
|
t.moves.push((time_ms, y))
|
|
if t.moves.length() > 16 {
|
|
ignore(t.moves.remove(0))
|
|
}
|
|
if t.list && t.phase == Pending && moved(x - t.x0, y - t.y0) {
|
|
t.phase = Scrolling
|
|
}
|
|
if t.phase == Scrolling {
|
|
self.note_activity(@sys.monotonic_ms())
|
|
let px = (t.y - y).to_int()
|
|
if px != 0 {
|
|
t.y = t.y - px.to_double()
|
|
ignore(self.scroll_list(px))
|
|
}
|
|
return
|
|
}
|
|
if t.phase == Pending && moved(x - t.x0, y - t.y0) {
|
|
t.phase = Dragging
|
|
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
|
|
self.handle_input_inner(PointerButton(272U, true), time_ms)
|
|
}
|
|
if t.phase == Dragging {
|
|
self.handle_input_inner(PointerMove(x, y), time_ms)
|
|
}
|
|
}
|
|
InClient(t) =>
|
|
if t.fingers.contains(id) {
|
|
t.fingers.set(id, (x, y))
|
|
if moved(x - t.x0, y - t.y0) {
|
|
t.tap = false
|
|
}
|
|
if id == t.first {
|
|
self.ptr_x = x
|
|
self.ptr_y = y
|
|
}
|
|
self.note_activity(@sys.monotonic_ms())
|
|
let (lx, ly) = self.touch_local(t, x, y)
|
|
self.server.touch_motion(t.win, time_ms, id, lx, ly)
|
|
}
|
|
NoTouch => ()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::touch_up(self : ChandraCompositor, id : Int, time_ms : UInt) -> Unit {
|
|
match self.touch.mode {
|
|
AsPointer(t) =>
|
|
if t.id == id {
|
|
self.touch.mode = NoTouch
|
|
match t.phase {
|
|
Pending => {
|
|
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
|
|
self.handle_input_inner(PointerButton(272U, true), time_ms)
|
|
self.handle_input_inner(PointerButton(272U, false), time_ms)
|
|
}
|
|
Dragging => self.handle_input_inner(PointerButton(272U, false), time_ms)
|
|
Scrolling => self.fling_list(t.moves, time_ms)
|
|
Held => ()
|
|
}
|
|
}
|
|
InClient(t) =>
|
|
if t.fingers.contains(id) {
|
|
t.fingers.remove(id)
|
|
self.server.touch_up(t.win, time_ms, id)
|
|
if t.fingers.is_empty() {
|
|
self.touch.mode = NoTouch
|
|
// A tap in a text field brings a hidden on-screen keyboard back; scrolling doesn't.
|
|
if t.tap {
|
|
self.osk_on_window_click(t.win)
|
|
}
|
|
}
|
|
}
|
|
NoTouch => ()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::touch_cancel(self : ChandraCompositor, time_ms : UInt) -> Unit {
|
|
let mode = self.touch.mode
|
|
self.touch.mode = NoTouch
|
|
match mode {
|
|
AsPointer(t) =>
|
|
if t.phase == Dragging {
|
|
self.handle_input_inner(PointerButton(272U, false), time_ms)
|
|
}
|
|
InClient(t) => self.server.touch_cancel(t.win)
|
|
NoTouch => ()
|
|
}
|
|
}
|
|
|
|
///| Whether a finger at (x, y) is on the launcher's list, with nothing over it.
|
|
fn ChandraCompositor::touch_list_at(self : ChandraCompositor, x : Double, y : Double) -> Bool {
|
|
let xi = x.to_int()
|
|
let yi = y.to_int()
|
|
!self.touch_taken(xi, yi) && self.shell.launcher_list_at(xi, yi)
|
|
}
|
|
|
|
///|
|
|
fn ChandraCompositor::scroll_list(self : ChandraCompositor, px : Int) -> Bool {
|
|
let moved = self.shell.scroll_launcher(px)
|
|
if moved {
|
|
self.run_shell_actions()
|
|
self.damage.mark_full()
|
|
}
|
|
moved
|
|
}
|
|
|
|
///| The finger left the list: keep it going at the speed of the last 100 ms, if that is a flick.
|
|
fn ChandraCompositor::fling_list(
|
|
self : ChandraCompositor,
|
|
moves : Array[(UInt, Double)],
|
|
time_ms : UInt,
|
|
) -> Unit {
|
|
let recent = moves.filter(m => time_ms - m.0 <= 100U)
|
|
guard recent.length() >= 2 else { return }
|
|
let (t0, y0) = recent[0]
|
|
let (t1, y1) = recent[recent.length() - 1]
|
|
guard t1 > t0 else { return }
|
|
let v = (y0 - y1) / (t1 - t0).to_double()
|
|
if v.abs() >= fling_min {
|
|
self.touch.fling = if v > fling_max { fling_max } else if v < -fling_max { -fling_max } else { v }
|
|
self.touch.fling_at = @sys.monotonic_ms()
|
|
self.touch.fling_rest = 0.0
|
|
}
|
|
}
|
|
|
|
///| Move the list on and slow it down; it stops at an end, or when the launcher closes.
|
|
fn ChandraCompositor::fling_tick(self : ChandraCompositor) -> Unit {
|
|
if self.touch.fling == 0.0 {
|
|
return
|
|
}
|
|
let now = @sys.monotonic_ms()
|
|
let dt = (now - self.touch.fling_at).to_double()
|
|
if dt <= 0.0 {
|
|
return
|
|
}
|
|
self.touch.fling_at = now
|
|
let keep = @math.exp(-dt / fling_ms)
|
|
let d = self.touch.fling * fling_ms * (1.0 - keep) + self.touch.fling_rest
|
|
let px = d.to_int()
|
|
self.touch.fling_rest = d - px.to_double()
|
|
self.touch.fling = self.touch.fling * keep
|
|
if self.touch.fling.abs() < 0.02 || (px != 0 && !self.scroll_list(px)) {
|
|
self.touch.fling = 0.0
|
|
}
|
|
}
|
|
|
|
///| Flicks slower than this (pixels per ms) don't keep the list going; none goes faster than that.
|
|
let fling_min : Double = 0.15
|
|
|
|
///|
|
|
let fling_max : Double = 5.0
|
|
|
|
///| How long the list takes to slow to about a third.
|
|
let fling_ms : Double = 325.0
|
|
|
|
///| Long presses that are due, and the list's movement.
|
|
fn ChandraCompositor::touch_tick(self : ChandraCompositor) -> Unit {
|
|
self.fling_tick()
|
|
guard self.touch.mode is AsPointer(t) && t.phase == Pending else { return }
|
|
let now = @sys.monotonic_ms()
|
|
if now - t.since < touch_long_ms {
|
|
return
|
|
}
|
|
t.phase = Held
|
|
let time_ms = now.to_uint()
|
|
self.handle_input_inner(PointerMove(t.x0, t.y0), time_ms)
|
|
self.handle_input_inner(PointerButton(273U, true), time_ms)
|
|
self.handle_input_inner(PointerButton(273U, false), time_ms)
|
|
self.osk_sync()
|
|
}
|
|
|
|
///| Milliseconds until `touch_tick` has something to do (-1: nothing).
|
|
fn ChandraCompositor::touch_wake_ms(self : ChandraCompositor, now : UInt64) -> Int {
|
|
guard self.touch.mode is AsPointer(t) && t.phase == Pending else { return -1 }
|
|
let due = t.since + touch_long_ms
|
|
if due > now {
|
|
(due - now).to_int()
|
|
} else {
|
|
0
|
|
}
|
|
}
|
|
|
|
///| Something other than the apps and the shell has the input at (x, y).
|
|
fn ChandraCompositor::touch_taken(self : ChandraCompositor, x : Int, y : Int) -> Bool {
|
|
self.screen_off ||
|
|
self.manual_screen_off ||
|
|
self.locked ||
|
|
self.auth is Some(_) ||
|
|
self.auth_notice is Some(_) ||
|
|
self.shell.face.window is Some(_) ||
|
|
self.osk_hit_any(x, y) ||
|
|
self.dnd ||
|
|
self.emoji_ui is Some(_) ||
|
|
self.shot is Some(_) ||
|
|
self.shot_edit is Some(_) ||
|
|
(self.rec is Some(_) && self.rec_pill().contains(x, y)) ||
|
|
self.shell.inspecting() ||
|
|
self.wm.drag is Some(_) ||
|
|
self.split_drag is Some(_)
|
|
}
|
|
|
|
///| The client's surface a first finger at (x, y) belongs to: (window or popup, subsurface,
|
|
/// surface-local position). None: it works the pointer.
|
|
fn ChandraCompositor::touch_target(
|
|
self : ChandraCompositor,
|
|
x : Double,
|
|
y : Double,
|
|
) -> (UInt64, UInt, Double, Double)? {
|
|
let xi = x.to_int()
|
|
let yi = y.to_int()
|
|
if self.touch_taken(xi, yi) ||
|
|
self.ptr_grab ||
|
|
self.overview is Some(_) ||
|
|
self.shell.popup_open() {
|
|
return None
|
|
}
|
|
match self.popup_at(xi, yi) {
|
|
Some(p) => {
|
|
let r = self.popup_rect(p)
|
|
return if self.server.takes_touch(p.id) {
|
|
Some((p.id, 0U, x - r.x.to_double(), y - r.y.to_double()))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
None => ()
|
|
}
|
|
// A tap beside an open menu closes it.
|
|
if self.popups.length() > 0 || self.shell.hit(xi, yi) || self.handle_at(xi, yi) is Some(_) {
|
|
return None
|
|
}
|
|
guard self.window_at(xi, yi) is Some((wid, true)) else { return None }
|
|
// A floating window's edges resize it.
|
|
if self.resize_at(xi, yi) is Some(_) || !self.server.takes_touch(wid) {
|
|
return None
|
|
}
|
|
guard self.windows.get(wid) is Some(win) else { return None }
|
|
let (sub, lx, ly) = win.surface_at(x, y)
|
|
Some((wid, sub, lx, ly))
|
|
}
|
|
|
|
///| Screen point (x, y) in the coordinates of the surface the touch began on.
|
|
fn ChandraCompositor::touch_local(
|
|
self : ChandraCompositor,
|
|
t : TouchClient,
|
|
x : Double,
|
|
y : Double,
|
|
) -> (Double, Double) {
|
|
match self.windows.get(t.win) {
|
|
Some(win) => win.surface_local(t.sub, x, y)
|
|
None => {
|
|
for p in self.popups {
|
|
if p.id == t.win {
|
|
let r = self.popup_rect(p)
|
|
return (x - r.x.to_double(), y - r.y.to_double())
|
|
}
|
|
}
|
|
(x, y)
|
|
}
|
|
}
|
|
}
|
|
|
|
///| A client answered a press with a drag, a move or a resize (`act` starts it, when a button is
|
|
/// held on window `win`). If the press is a finger's, the gesture becomes a pointer drag: the
|
|
/// client's touch is cancelled and the finger drags from here on.
|
|
fn ChandraCompositor::touch_handover(self : ChandraCompositor, win : UInt64, act : () -> Unit) -> Unit {
|
|
match self.touch.mode {
|
|
InClient(t) if t.fingers.length() == 1 &&
|
|
(win == t.win || win == 0UL || self.server.same_client(win, t.win)) => {
|
|
self.server.touch_cancel(t.win)
|
|
let (lx, ly) = self.touch_local(t, self.ptr_x, self.ptr_y)
|
|
self.ptr_win = Some(t.win)
|
|
self.ptr_sub = t.sub
|
|
self.server.pointer_enter(t.win, lx, ly, sub=t.sub)
|
|
self.press_fwd = true
|
|
act()
|
|
if self.press_fwd {
|
|
// It wasn't started (a tiled window can't be resized): the touch is just over.
|
|
self.press_fwd = false
|
|
self.leave_pointer()
|
|
self.touch.mode = NoTouch
|
|
} else {
|
|
for id, _ in t.fingers {
|
|
self.touch.mode = AsPointer({
|
|
id,
|
|
x0: t.x0,
|
|
y0: t.y0,
|
|
since: @sys.monotonic_ms(),
|
|
phase: Dragging,
|
|
list: false,
|
|
y: t.y0,
|
|
moves: [],
|
|
})
|
|
}
|
|
}
|
|
}
|
|
_ => act()
|
|
}
|
|
}
|
|
|
|
///|
|
|
fn moved(dx : Double, dy : Double) -> Bool {
|
|
dx * dx + dy * dy > touch_drag_px * touch_drag_px
|
|
}
|