Nostro/implementations/Nostro.Ui-Gesture.cpp

207 lines
5.9 KiB
C++
Raw Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
module Nostro.Ui:Gesture_impl;
import std;
import :Gesture;
import :Geometry;
using namespace Nostro::Ui;
GestureEvent GestureDetector::LongPressed() {
longPressed = true;
GestureEvent event;
event.kind = GestureEvent::Kind::LongPress;
event.at = down;
return event;
}
// Keeps the last 100 ms of movement: a fling's speed is the finger's as it left, not as it travelled.
void GestureDetector::Forget(std::int64_t tMs) {
while(samples.size() > 1 && tMs - samples.front().tMs > 100) {
samples.erase(samples.begin());
}
}
GestureEvent GestureDetector::Feed(PointerPhase phase, Point p, std::int64_t tMs) {
GestureEvent event;
if(phase == PointerPhase::Down) {
pressed = true;
dragging = false;
longPressed = false;
down = p;
downAt = tMs;
last = p;
samples.clear();
samples.push_back({p, tMs});
return event;
}
if(phase == PointerPhase::Cancel) {
pressed = false;
dragging = false;
longPressed = false;
return event;
}
if(!pressed) {
return event;
}
// Held past the long-press time with no Timeout yet, the events of a stall having been dispatched before the timer.
const bool overdue = longPress && !dragging && !longPressed && tMs - downAt >= longPressMs;
if(phase == PointerPhase::Move) {
if(overdue) {
// The long press, before any movement is read.
return LongPressed();
}
if(!dragging && !longPressed && (std::abs(p.x - down.x) > slopPx || std::abs(p.y - down.y) > slopPx)) {
dragging = true;
event.kind = GestureEvent::Kind::DragStart;
event.at = down;
event.delta = {p.x - down.x, p.y - down.y};
} else if(dragging) {
event.kind = GestureEvent::Kind::Drag;
event.at = p;
event.delta = {p.x - last.x, p.y - last.y};
}
last = p;
samples.push_back({p, tMs});
Forget(tMs);
return event;
}
// The release.
pressed = false;
if(dragging) {
dragging = false;
// A finger held still before letting go has no speed, whatever it did before: no event comes while it is still.
Forget(tMs);
const std::int64_t dt = tMs - samples.front().tMs;
Point velocity;
if(dt > 0) {
velocity = {(p.x - samples.front().p.x) * 1000.0f / static_cast<float>(dt), (p.y - samples.front().p.y) * 1000.0f / static_cast<float>(dt)};
}
event.at = p;
if(std::hypot(velocity.x, velocity.y) >= flingMinPxPerS) {
event.kind = GestureEvent::Kind::Fling;
event.velocity = velocity;
} else {
event.kind = GestureEvent::Kind::Release;
}
return event;
}
if(overdue) {
// The long press, at the release.
return LongPressed();
}
if(longPressed) {
event.kind = GestureEvent::Kind::Release;
event.at = p;
return event;
}
event.kind = GestureEvent::Kind::Tap;
event.at = down;
return event;
}
GestureEvent GestureDetector::Timeout(std::int64_t tMs) {
if(!longPress || !pressed || dragging || longPressed || tMs - downAt < longPressMs) {
return GestureEvent();
}
return LongPressed();
}
bool GestureDetector::Pressed() const noexcept {
return pressed;
}
bool GestureDetector::Dragging() const noexcept {
return dragging;
}
std::optional<std::int64_t> GestureDetector::LongPressDue() const noexcept {
if(!longPress || !pressed || dragging || longPressed) {
return std::nullopt;
}
return downAt + longPressMs;
}
float ScrollState::Max() const noexcept {
return std::max(0.0f, content - viewport);
}
void ScrollState::Clamp() noexcept {
offset = std::clamp(offset, 0.0f, Max());
}
void ScrollState::ScrollBy(float dy) noexcept {
offset += dy;
velocity = 0;
Clamp();
}
void ScrollState::Fling(float pxPerS) noexcept {
velocity = pxPerS;
}
bool ScrollState::Tick(float dt) noexcept {
if(std::abs(velocity) < 20.0f) {
velocity = 0;
return false;
}
const float before = offset;
offset += velocity * dt;
Clamp();
// Stopped by an end of the content, or going nowhere.
if(offset == before || offset == 0.0f || offset == Max()) {
velocity = 0;
return false;
}
velocity *= std::exp(-4.0f * dt);
return true;
}
bool ScrollState::EnsureVisible(float top, float bottom, float margin) noexcept {
const float before = offset;
if(bottom + margin > offset + viewport) {
offset = bottom + margin - viewport;
}
// The top wins where the span is taller than the room: that is where reading starts.
if(top - margin < offset) {
offset = top - margin;
}
Clamp();
if(offset == before) {
return false;
}
velocity = 0;
return true;
}
std::int64_t EventClock::At(std::uint32_t stamp, std::int64_t hostNowMs) noexcept {
if(stamp == 0) {
// The one millisecond in 2^32 that reads 0, and an event the compositor gave no time.
return hostNowMs;
}
// The difference of two stamps as a signed number takes the wrap in its stride.
unwrapped = lastStamp == 0 ? stamp : unwrapped + static_cast<std::int32_t>(stamp - lastStamp);
lastStamp = stamp;
ahead = ahead ? std::min(*ahead, hostNowMs - unwrapped) : hostNowMs - unwrapped;
return unwrapped + *ahead;
}
bool FrameWait::Again(std::uint32_t waiting) noexcept {
if(waiting == 0) {
Reset();
return false;
}
if(last == 0 || waiting != last) {
gainless = 0;
} else {
gainless++;
}
last = waiting;
return gainless < patience;
}
void FrameWait::Reset() noexcept {
last = 0;
gainless = 0;
}