diff --git a/interfaces/ImsdMessages-Gesture.cppm b/interfaces/ImsdMessages-Gesture.cppm index 6415865..5a70b41 100644 --- a/interfaces/ImsdMessages-Gesture.cppm +++ b/interfaces/ImsdMessages-Gesture.cppm @@ -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; diff --git a/interfaces/ImsdMessages.Ui-App.cppm b/interfaces/ImsdMessages.Ui-App.cppm index 467c3bd..e19050f 100644 --- a/interfaces/ImsdMessages.Ui-App.cppm +++ b/interfaces/ImsdMessages.Ui-App.cppm @@ -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(want.caret.x), static_cast(want.caret.y), static_cast(want.caret.w), static_cast(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(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(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(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 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 spentKey; // the key whose press changed the screen (Key) guint blinkTimer = 0; diff --git a/project.cpp b/project.cpp index b4f59c1..e123546 100644 --- a/project.cpp +++ b/project.cpp @@ -85,7 +85,7 @@ extern "C" Configuration CrafterBuildProject(std::span 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 + diff --git a/tests/Gesture/main.cpp b/tests/Gesture/main.cpp index 2f13ee4..6fc2e8e 100644 --- a/tests/Gesture/main.cpp +++ b/tests/Gesture/main.cpp @@ -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.