imsd-messages/tests/Gesture/main.cpp

133 lines
8.6 KiB
C++
Raw Permalink Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Gestures from raw pointer phases: tap, long press by the host's timer,
// drag with its deltas, fling by velocity, cancel; the scroll physics; the
// topmost hit region.
import std;
import ImsdMessages;
using namespace ImsdMessages;
using Kind = GestureEvent::Kind;
namespace {
std::int32_t failures = 0;
void Check(bool ok, std::string_view what) {
std::println("{} {}", ok ? "PASS" : "FAIL", what);
if (!ok) ++failures;
}
}
int main() {
GestureDetector g;
Check(g.Feed(PointerPhase::Down, { 10, 10 }, 0).kind == Kind::None, "a press decides nothing yet");
Check(g.Feed(PointerPhase::Move, { 13, 12 }, 50).kind == Kind::None, "movement within the slop is not a drag");
const GestureEvent tap = g.Feed(PointerPhase::Up, { 13, 12 }, 120);
Check(tap.kind == Kind::Tap && tap.at == Point{ 10, 10 }, "a short press is a tap at the press position");
g.Feed(PointerPhase::Down, { 10, 10 }, 1000);
Check(g.Timeout(1400).kind == Kind::None, "the timer before the long-press time decides nothing");
const GestureEvent lp = g.Timeout(1500);
Check(lp.kind == Kind::LongPress && lp.at == Point{ 10, 10 }, "the timer at the long-press time is a long press");
Check(g.Feed(PointerPhase::Move, { 40, 40 }, 1600).kind == Kind::None, "movement after a long press is not a drag");
Check(g.Feed(PointerPhase::Up, { 40, 40 }, 1700).kind == Kind::Release, "the release after a long press is a plain release");
g.Feed(PointerPhase::Down, { 100, 100 }, 2000);
const GestureEvent start = g.Feed(PointerPhase::Move, { 100, 120 }, 2016);
Check(start.kind == Kind::DragStart && start.at == Point{ 100, 100 } && start.delta == Point{ 0, 20 }, "movement past the slop starts a drag with the whole movement as delta");
const GestureEvent drag = g.Feed(PointerPhase::Move, { 100, 150 }, 2032);
Check(drag.kind == Kind::Drag && drag.delta == Point{ 0, 30 } && drag.at == Point{ 100, 150 }, "a drag reports the delta since the previous event");
Check(g.Dragging(), "dragging");
const GestureEvent fling = g.Feed(PointerPhase::Up, { 100, 200 }, 2048);
Check(fling.kind == Kind::Fling && fling.velocity.y > 1000 && fling.velocity.x == 0, "a fast release is a fling with a velocity in px/s");
Check(g.Timeout(2500).kind == Kind::None, "the timer after a drag decides nothing");
g.Feed(PointerPhase::Down, { 100, 100 }, 3000);
g.Feed(PointerPhase::Move, { 100, 150 }, 3016);
g.Feed(PointerPhase::Move, { 100, 200 }, 3032);
const GestureEvent held = g.Feed(PointerPhase::Up, { 100, 200 }, 3500);
Check(held.kind == Kind::Release, "a release after the finger was held still is not a fling, however fast it moved before");
g.Feed(PointerPhase::Down, { 0, 0 }, 3000);
g.Feed(PointerPhase::Move, { 0, 30 }, 3100);
g.Feed(PointerPhase::Move, { 0, 31 }, 3900);
Check(g.Feed(PointerPhase::Up, { 0, 31 }, 4000).kind == Kind::Release, "a slow release after a drag is a release, not a fling");
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");
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.
2026-11-02 16:09:33 +01:00
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");
Ui, main: the new-conversation screen and the long-press sheets The + opens a screen with a number field (its placeholder "Number", a parameter of the compose field now) and Start; the number becomes a conversation key through Number's ConversationKey, as an sms: link's does (E.164 through the country code, a short code as digits), anything else is refused in place. A long press on a conversation row offers to delete the conversation, on a message to copy its text to the clipboard or delete it; only those two have one: App tells the recogniser before each press whether what it lands on has a long press (GestureDetector's longPress), and without one a press held as long — on a button, a field, a sheet's row — stays a tap at its release, so a slow finger presses the button all the same. A sheet draws on the overlay layer over a scrim and pushes its regions last, so they win the hit test, and the wheel scrolls nothing under it; Escape, the back arrow and a tap outside dismiss it. Deleting a conversation also closes its notification and drops its draft; the + keeps the draft of the conversation it leaves, as Back does, and so does a view that closes because its last message was deleted. The deletes go through Sync (Erase, EraseConversation: the commit before, which keeps a deleted text deleted). The number field's box is drawn by its screen, as the send bar's is by the bar: the shared field drawing paints no background of its own. Opening a conversation, from a notification or a link, leaves the new-conversation screen and any sheet, and the + leaves any sheet. A key whose press changed the screen acts once: held, its repeats are not the new screen's — Enter, which started the conversation, would type line breaks into its field, and Escape would go on back, from a sheet through the conversation and its draft. A sheet that asks to delete a conversation goes when a text arrives for it: a yes would delete that text unread. A press on the conversation's row opens no such sheet either while the row it aimed at had not shown the text — the regions are a frame's: the press that is down when the text arrives, and one that comes down before the next frame is drawn, have no long press; a release before the long-press time is still the row's tap, while the row stays under the finger. --new and --bench-sheet put either on screen for the bench (--bench-sheet conversation or message, each on the screen a long press opens it over: another value is refused, and one that finds nothing to press says so in the log), and are window flags as --screenshot is. Tested: the screens and the sheets by the bench (--new, --bench-sheet) and the phone step's screenshots; the deletes' logic with the commit before, in tests/Store and tests/Sync; the press without a long press in tests/Gesture (the timer gives no long press and a move within the slop nothing; the release is the tap, however late; a slide off the control is a drag); a deleted conversation's draft dropped, in tests/Edit. The press on a row a text arrived for has no bench: none holds a finger on a row.
2026-11-01 18:11:40 +01:00
// 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.
g.longPress = false;
g.Feed(PointerPhase::Down, { 5, 5 }, 8000);
Check(g.Timeout(8600).kind == Kind::None, "without a long press the timer past the time gives nothing");
Check(g.Feed(PointerPhase::Move, { 6, 5 }, 8700).kind == Kind::None, "nor does a move within the slop");
const GestureEvent slow = g.Feed(PointerPhase::Up, { 6, 5 }, 9500);
Check(slow.kind == Kind::Tap && slow.at == Point{ 5, 5 }, "and the release is the tap, however late");
g.Feed(PointerPhase::Down, { 5, 5 }, 10000);
Check(g.Feed(PointerPhase::Move, { 5, 40 }, 10800).kind == Kind::DragStart, "a finger that slides off past the time drags, as one that slides off before it");
Check(g.Feed(PointerPhase::Up, { 5, 40 }, 11500).kind == Kind::Release, "and its release taps nothing");
g.longPress = true;
g.Feed(PointerPhase::Down, { 5, 5 }, 12000);
Check(g.Timeout(12500).kind == Kind::LongPress, "a press the host gives a long press has it as before");
g.Feed(PointerPhase::Up, { 5, 5 }, 12600);
ScrollState s;
s.content = 1000;
s.viewport = 400;
s.ScrollBy(100);
Check(s.offset == 100 && s.Max() == 600, "scroll by and the maximum");
s.ScrollBy(5000);
Check(s.offset == 600, "the offset clamps at the end");
s.ScrollBy(-5000);
Check(s.offset == 0, "and at the start");
s.Fling(1000);
Check(s.Tick(0.1f) && s.offset > 0 && s.velocity < 1000, "a fling moves the content and slows down");
while (s.Tick(0.1f)) {}
Check(s.velocity == 0 && s.offset > 0 && s.offset <= 600, "a fling comes to rest within the extent");
s.offset = 0;
s.Fling(-500);
Check(!s.Tick(0.1f) && s.offset == 0, "a fling against the edge stops at once");
s.content = 300;
s.offset = 250;
s.Clamp();
Check(s.offset == 0, "content shorter than the viewport has no scroll");
const std::vector<HitRegion> regions = { { { 0, 0, 100, 100 }, 1 }, { { 50, 50, 100, 100 }, 2 } };
Check(HitTest(regions, { 75, 75 }) == std::optional<std::uint32_t>(2), "the last drawn region wins where they overlap");
Check(HitTest(regions, { 10, 10 }) == std::optional<std::uint32_t>(1), "the lower region where only it is");
Check(!HitTest(regions, { 500, 500 }), "nothing outside");
// What a point names, by identity: a bubble that another frame moved
// under the finger names another message, and nothing matches.
const std::vector<HitRegion> frame = { { { 0, 0, 100, 20 }, 7 }, { { 0, 20, 100, 20 }, 1000 }, { { 0, 40, 100, 20 }, 1001 }, { { 0, 60, 100, 20 }, 100000 }, { { 0, 80, 100, 20 }, 100001 } };
const std::vector<std::string> keys = { "+31611111111", "+31622222222" };
const std::vector<std::uint64_t> drawn = { 40, 41 };
const std::vector<std::uint64_t> snapped = { 41, 42 }; // a new text at the bottom: every bubble one slot up
Check(TargetAt(frame, { 5, 5 }, 1000, keys, 100000, drawn) == HitTarget{ 7 }, "a control names its id");
Check(TargetAt(frame, { 5, 45 }, 1000, keys, 100000, drawn) == HitTarget{ 1000, "+31622222222" }, "a row names its conversation");
Check(TargetAt(frame, { 5, 85 }, 1000, keys, 100000, drawn) == HitTarget{ 100000, "", 41 }, "a bubble names its message");
Check(!TargetAt(frame, { 5, 150 }, 1000, keys, 100000, drawn), "nothing under the point names nothing");
Check(TargetAt(frame, { 5, 85 }, 1000, keys, 100000, snapped) != TargetAt(frame, { 5, 85 }, 1000, keys, 100000, drawn), "the frame that moved another bubble under the point names it: the release matches no press");
Check(TargetAt(frame, { 5, 65 }, 1000, keys, 100000, snapped) == TargetAt(frame, { 5, 85 }, 1000, keys, 100000, drawn), "the same message, moved, still names itself");
Gesture, Ui, project: Crafter.Graphics at 3f646ec, whose key repeat fires once at the delay The pinned library's key repeat counted the compositor's delay as missed periods: lastFireTime was the press itself and the catch-up loop fired once per period since it, so the first repeat after the delay was a burst of floor(delay / period) events — with rate 25 and delay 600 a held Backspace deleted sixteen characters at the delay mark and a held letter put fifteen copies in the field, from a hardware keyboard and from an on-screen keyboard that sends Backspace as key events; a rate above 1000 made the period zero and the loop endless. Crafter.Graphics 3f646ec lands the first repeat at the delay, keeps the period at a millisecond or more and caps the catch-up after a stalled frame; the app's own repeat timer and the frame path's TickKeyRepeats need nothing more. The pin moves from 6f471ca to the library's master of 2026-11-01, eight commits on: the swapchain sized for its present mode and frames in flight keyed by frame, the font atlas's SDF decoded in atlas pixels and glyphs that find no room given a cell at the next frame, Win32 linking and titles and a Win32 test's stand-in dropped, the library's AGENTS template, the key repeat. One contract changes with a4be6f1: once the atlas evicts, a glyph item is good for the frame it was shaped for, so item buffers must be one set per frame in flight (the library's usage contract since; its HelloUI shows it). The Canvas's layers keep Window::numFrames sets of their quad, circle and glyph buffers and fill the one at UIBuildArgs::frameIdx, where one set had served the three frames paced by frame callbacks — about 3 MB of host-visible memory in all; the descriptor heap grows to hold their slots. The atlas's contract changed with the eviction: a glyph that finds no free cell draws blank and gets one when the next frame begins — a frame this app, drawing on demand, has to ask for: the layers count the glyphs drawn blank (FontAtlas::Lookup, not Resident) and Build asks for another frame while glyphs wait, until the frames in flight plus one have passed without a cell gained — a cell comes from glyphs not drawn in those frames, so a screen change needs that many before the cells it displaced come free (FrameWait in Gesture, beside the fling's frame rule, added with this, its use — restarted by every redraw request and by each frame of a fling, which changes what is drawn as a request does — and tested there). The other contracts this app relies on hold at the new commit: the disable ends the composition and clears the shown record, CaptureNextFrame writes its PNG inside the frame that records the copy, a touch's up precedes the synthesised release, the deletion queue still wants the app's drain when a window ends (DrainDeletions), libevdev stays vendored; the licence texts are unchanged.
2026-11-01 00:59:06 +01:00
FrameWait wait{ .patience = 4 };
Check(!wait.Again(0), "no glyph waits: no frame owed");
Check(wait.Again(5) && wait.Again(5) && wait.Again(5) && wait.Again(5), "glyphs waiting: a frame is owed by the first sighting and by three gainless ones after it (the cells of the screen before come free at the third)");
Check(!wait.Again(5), "the fourth gainless frame ends it: the screen needs more cells than the atlas holds");
wait.Reset();
Check(wait.Again(5) && wait.Again(3) && wait.Again(3) && wait.Again(1) && !wait.Again(0), "a gain starts the count over; none waiting ends it");
Check(wait.Again(2) && wait.Again(7) && wait.Again(7) && wait.Again(7) && wait.Again(7) && !wait.Again(7), "a changed count (a screen change) starts the count over");
return failures == 0 ? 0 : 1;
}