Gesture, Ui, project: Crafter.Graphics at 364d210, whose pointer and touch events carry their time

The pinned library dropped the compositor's time of every pointer and
touch event, so the app dated them by its own clock at dispatch. Events
queued while the main loop was busy — a List importing a backlog, each
Import an fdatasync — all carried the time the loop came back: a swipe's
backlog read as a few milliseconds' travel and flung the list to its far
end, and a press whose down and up both waited out a stall had them at
one time, a tap where the user held — on a Failed bubble a Retry, an SMS
the user did not ask for. Crafter.Graphics 364d210 keeps the time on the
window (lastInputTime, set before the event fires), one commit on the
master of 2026-11-02, which added the library's review configuration and
no code.

The gesture detector now gets each event's own time, unwrapped past the
wrap at 2^32 into one running clock (an event stamped 0, which the
compositor's time reads for one millisecond in 2^32, gets the host's
clock), and the long-press timer's expiry is the host's elapsed since
the press put on that clock, so a press and its release keep their
distance however late the loop dispatches them — and a release or a move
the detector sees past the long-press time with no timer fired is the
long press itself, at the press, where the press can be one, since the
hold is measured on the events' clock (tests/Gesture pins both). The pin
moves from 3f646ec; the contracts this app relies on are unchanged by
the two commits between.
This commit is contained in:
Jorijn van der Graaf 2026-11-02 16:09:33 +01:00
commit e6b343985d
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
4 changed files with 55 additions and 6 deletions

View file

@ -40,7 +40,10 @@ export namespace ImsdMessages {
// 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);
// 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 lands on has
// no long press, a press held as long is still a tap at its release.
@ -66,6 +69,15 @@ export namespace ImsdMessages {
break;
case PointerPhase::Move: {
if (!pressed) break;
if (longPress && !dragging && !longPressed && tMs - downAt >= longPressMs) {
// Held past the long-press time with no Timeout yet (the
// events of a stall dispatched before the timer): the
// long press, before any movement is read.
longPressed = true;
e.kind = GestureEvent::Kind::LongPress;
e.at = down;
break;
}
if (!dragging && !longPressed && (std::abs(p.x - down.x) > slopPx || std::abs(p.y - down.y) > slopPx)) {
dragging = true;
e.kind = GestureEvent::Kind::DragStart;
@ -106,6 +118,12 @@ export namespace ImsdMessages {
} else if (!longPressed && (!longPress || tMs - downAt < longPressMs)) {
e.kind = GestureEvent::Kind::Tap;
e.at = down;
} else if (!longPressed) {
// Held past the long-press time with no Timeout yet: the
// long press, at the release.
longPressed = true;
e.kind = GestureEvent::Kind::LongPress;
e.at = down;
} else {
e.kind = GestureEvent::Kind::Release;
e.at = p;

View file

@ -596,7 +596,7 @@ export namespace ImsdMessages::Ui {
// forwarded link's or a notification's Open by the other thumb, Back
// from a key), and its long press does not fire.
void CancelGesture() {
if (gestures.Pressed()) gestures.Feed(PointerPhase::Cancel, Point{}, NowMs());
if (gestures.Pressed()) gestures.Feed(PointerPhase::Cancel, Point{}, eventClock);
if (longPressTimer) {
g_source_remove(longPressTimer);
longPressTimer = 0;
@ -691,6 +691,24 @@ export namespace ImsdMessages::Ui {
canvas->Window().SetTextInput(want.active, static_cast<std::int32_t>(want.caret.x), static_cast<std::int32_t>(want.caret.y), static_cast<std::int32_t>(want.caret.w), static_cast<std::int32_t>(want.caret.h), want.purpose);
}
// The event's own time: the compositor's stamp of the pointer or touch
// event the listener is being invoked for (Window::lastInputTime, ms
// from an undefined base, wrapping at 2^32), unwrapped into one
// running clock. Events queued while the loop was busy keep the
// times they happened at, so a backlog is not read as a burst: a
// swipe's as a fling at many times the finger's speed, a held press
// as a tap. The host's clock where the stamp is 0: the one
// millisecond in 2^32 that the compositor's time reads 0, and before
// the first event that has a time.
std::int64_t EventTimeMs() {
const std::uint32_t raw = canvas->Window().lastInputTime;
if (raw == 0) return NowMs();
if (lastRawEventMs != 0) eventClock += static_cast<std::int32_t>(raw - lastRawEventMs);
else eventClock = raw;
lastRawEventMs = raw;
return eventClock;
}
void Pointer(PointerPhase phase) {
const Point p{ canvas->Window().currentMousePos.x, canvas->Window().currentMousePos.y };
// Buffer pixels: the slop and the fling threshold follow the
@ -708,8 +726,11 @@ export namespace ImsdMessages::Ui {
// held: a slow finger presses the button all the same.
gestures.longPress = pressedOn && pressedOn->id >= Hit::rowBase;
}
Handle(gestures.Feed(phase, p, NowMs()));
const std::int64_t at = EventTimeMs();
Handle(gestures.Feed(phase, p, at));
if (phase == PointerPhase::Down) {
downEventMs = at;
downHostMs = NowMs();
if (longPressTimer) g_source_remove(longPressTimer);
longPressTimer = gestures.longPress ? g_timeout_add(static_cast<guint>(gestures.longPressMs), OnLongPress, this) : 0;
} else if (phase != PointerPhase::Move && longPressTimer) {
@ -721,7 +742,7 @@ export namespace ImsdMessages::Ui {
static gboolean OnLongPress(gpointer data) {
App& self = *static_cast<App*>(data);
self.longPressTimer = 0;
self.Handle(self.gestures.Timeout(NowMs()));
self.Handle(self.gestures.Timeout(self.downEventMs + (NowMs() - self.downHostMs))); // the host's elapsed since the press, on the events' clock
return G_SOURCE_REMOVE;
}
@ -992,6 +1013,10 @@ export namespace ImsdMessages::Ui {
GestureDetector gestures;
std::vector<HitRegion> hits;
guint longPressTimer = 0;
std::int64_t eventClock = 0; // the events' own clock (EventTimeMs), unwrapped
std::uint32_t lastRawEventMs = 0;
std::int64_t downEventMs = 0; // the press on both clocks: the long-press timer's expiry is mapped from the host's
std::int64_t downHostMs = 0;
guint repeatTimer = 0;
std::optional<Crafter::KeyCode> spentKey; // the key whose press changed the screen (Key)
guint blinkTimer = 0;

View file

@ -85,7 +85,7 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> a
// The window library.
Configuration* graphics = local
? LocalProject({ .projectFile = "../Crafter/Crafter.Graphics/project.cpp", .args = depArgs })
: GitProject({ .source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Graphics.git", .commit = "3f646ec42bee39056549098b77aceed7a00a26a4" }, .args = depArgs });
: GitProject({ .source = { .url = "https://forgejo.catcrafts.net/Catcrafts/Crafter.Graphics.git", .commit = "364d2101c973905e4ace263ac2b2085e75de725e" }, .args = depArgs });
// imsd-messages-core — numbers, time, the model, word wrap, gestures,
// the message field's editing, the store and the sync logic: std +

View file

@ -58,7 +58,13 @@ int main() {
g.Feed(PointerPhase::Down, { 0, 0 }, 5000);
g.Feed(PointerPhase::Cancel, { 0, 0 }, 5010);
Check(!g.Pressed() && g.Feed(PointerPhase::Up, { 0, 0 }, 5020).kind == Kind::None, "a cancel forgets the press");
Check(g.Feed(PointerPhase::Down, { 5, 5 }, 6000).kind == Kind::None && g.Feed(PointerPhase::Up, { 5, 5 }, 6600).kind == Kind::Release, "a long press released without the timer is a release");
g.Feed(PointerPhase::Down, { 5, 5 }, 6000);
const GestureEvent late = g.Feed(PointerPhase::Up, { 5, 5 }, 6600);
Check(late.kind == Kind::LongPress && late.at == Point{ 5, 5 }, "a release past the long-press time with no timer fired — the press and its release dispatched together behind a stall — is the long press, at the press");
g.Feed(PointerPhase::Down, { 5, 5 }, 7000);
const GestureEvent stalled = g.Feed(PointerPhase::Move, { 40, 40 }, 7600);
Check(stalled.kind == Kind::LongPress && stalled.at == Point{ 5, 5 }, "a move past the long-press time with no timer fired is the long press first, at the press: the finger was held that long before it moved");
Check(g.Feed(PointerPhase::Up, { 40, 40 }, 7700).kind == Kind::Release, "and its release a plain release, no drag");
// A press on what has no long press, the host's word before the Down:
// held as long, it is still a tap at its release.