124 lines
5.6 KiB
Text
124 lines
5.6 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.Ui:Gesture;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import :Geometry;
|
||
|
|
|
||
|
|
// What a finger or a mouse did, decided from raw pointer phases; the scroll physics of a list; and the frames a
|
||
|
|
// window that draws only on demand still owes. Pure arithmetic over points and times, so the tests feed it by hand.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui {
|
||
|
|
enum class PointerPhase : std::uint8_t {
|
||
|
|
Down,
|
||
|
|
Move,
|
||
|
|
Up,
|
||
|
|
Cancel // the compositor took the touch for a gesture of its own: nothing was tapped
|
||
|
|
};
|
||
|
|
|
||
|
|
struct GestureEvent {
|
||
|
|
enum class Kind : std::uint8_t {
|
||
|
|
None,
|
||
|
|
Tap,
|
||
|
|
LongPress,
|
||
|
|
DragStart,
|
||
|
|
Drag,
|
||
|
|
Fling,
|
||
|
|
Release
|
||
|
|
};
|
||
|
|
Kind kind = Kind::None;
|
||
|
|
Point at; // the press for Tap, LongPress and DragStart; the current position for Drag, Fling and Release
|
||
|
|
Point delta; // DragStart and Drag: the movement since the previous event
|
||
|
|
Point velocity; // Fling: pixels per second
|
||
|
|
};
|
||
|
|
|
||
|
|
// One pointer at a time. A press that ends within the slop and before the long-press time is a tap. A press held
|
||
|
|
// past that time without moving is a long press: the host runs the timer and calls Timeout, or the first move or the
|
||
|
|
// release past that time is, when it comes before the timer could (the host's loop dispatched the press and what
|
||
|
|
// followed it together, behind a stall, each with its own time). Movement past the slop starts a drag, whose release
|
||
|
|
// is a fling when it was fast enough. Where the host says that what the press landed on has no long press, a press
|
||
|
|
// held as long is still a tap at its release.
|
||
|
|
class GestureDetector {
|
||
|
|
public:
|
||
|
|
float slopPx = 8;
|
||
|
|
std::int64_t longPressMs = 500;
|
||
|
|
float flingMinPxPerS = 60;
|
||
|
|
// Whether the press that follows can be a long press: the host's word, before each Down.
|
||
|
|
bool longPress = true;
|
||
|
|
|
||
|
|
GestureEvent Feed(PointerPhase phase, Point p, std::int64_t tMs);
|
||
|
|
// The host's timer at Down + longPressMs.
|
||
|
|
GestureEvent Timeout(std::int64_t tMs);
|
||
|
|
bool Pressed() const noexcept;
|
||
|
|
bool Dragging() const noexcept;
|
||
|
|
// When the timer for the press that is down is due; nothing while no press waits for one.
|
||
|
|
std::optional<std::int64_t> LongPressDue() const noexcept;
|
||
|
|
|
||
|
|
private:
|
||
|
|
struct Sample {
|
||
|
|
Point p;
|
||
|
|
std::int64_t tMs;
|
||
|
|
};
|
||
|
|
|
||
|
|
GestureEvent LongPressed();
|
||
|
|
void Forget(std::int64_t tMs);
|
||
|
|
|
||
|
|
bool pressed = false;
|
||
|
|
bool dragging = false;
|
||
|
|
bool longPressed = false;
|
||
|
|
Point down;
|
||
|
|
Point last;
|
||
|
|
std::int64_t downAt = 0;
|
||
|
|
std::vector<Sample> samples;
|
||
|
|
};
|
||
|
|
|
||
|
|
// A scrollable extent: the offset is how far the content moved up.
|
||
|
|
struct ScrollState {
|
||
|
|
float offset = 0;
|
||
|
|
float velocity = 0; // pixels per second, positive scrolls further down
|
||
|
|
float content = 0;
|
||
|
|
float viewport = 0;
|
||
|
|
|
||
|
|
float Max() const noexcept;
|
||
|
|
void Clamp() noexcept;
|
||
|
|
void ScrollBy(float dy) noexcept;
|
||
|
|
void Fling(float pxPerS) noexcept;
|
||
|
|
// Advances the fling by dt seconds; true while still moving.
|
||
|
|
bool Tick(float dt) noexcept;
|
||
|
|
// Scrolls as little as it takes to bring the span from `top` to `bottom` of the content into view, `margin`
|
||
|
|
// clear of the viewport's edges where the content allows it: what keeps a field that got the focus on screen
|
||
|
|
// when the on-screen keyboard takes half the window. True when that moved anything.
|
||
|
|
bool EnsureVisible(float top, float bottom, float margin) noexcept;
|
||
|
|
};
|
||
|
|
|
||
|
|
// The time of a pointer or touch event on the host's own clock. The compositor stamps each event with the time it
|
||
|
|
// happened, in milliseconds from a base of its own, wrapping at 2^32; events queued while the loop was busy are
|
||
|
|
// dispatched together when it comes back, so by the host's clock at dispatch a swipe's backlog is a burst, read as a
|
||
|
|
// fling at many times the finger's speed, and a press and its release that both waited out a stall happen at once.
|
||
|
|
// The stamps, unwrapped, tick like the host's clock a constant apart, and no event is dispatched before it
|
||
|
|
// happened: the smallest difference seen is the nearest to that constant.
|
||
|
|
struct EventClock {
|
||
|
|
// `stamp` is the compositor's time of the event, 0 when it gave none; `hostNowMs` the host's clock now.
|
||
|
|
std::int64_t At(std::uint32_t stamp, std::int64_t hostNowMs) noexcept;
|
||
|
|
|
||
|
|
private:
|
||
|
|
std::uint32_t lastStamp = 0;
|
||
|
|
std::int64_t unwrapped = 0;
|
||
|
|
std::optional<std::int64_t> ahead; // the host's clock minus the unwrapped stamps
|
||
|
|
};
|
||
|
|
|
||
|
|
// The frames a screen owes the font atlas. A glyph that found no cell drew blank and gets one when a later frame
|
||
|
|
// begins, from the glyphs not drawn in the last `patience` frames (the frames in flight plus one), and a frame comes
|
||
|
|
// only when asked for: Again(waiting) after each frame asks for another while glyphs wait, until `patience` frames in
|
||
|
|
// a row gained no cell, when the screen needs more cells than the atlas holds. A change in the count (a gain, a
|
||
|
|
// screen change) starts the count over; Reset does on any other change of what is drawn.
|
||
|
|
struct FrameWait {
|
||
|
|
std::uint32_t patience = 4;
|
||
|
|
std::uint32_t last = 0; // the previous frame's waiting glyphs
|
||
|
|
std::uint32_t gainless = 0; // frames in a row that gained no cell
|
||
|
|
|
||
|
|
bool Again(std::uint32_t waiting) noexcept;
|
||
|
|
void Reset() noexcept;
|
||
|
|
};
|
||
|
|
}
|