Nostro/interfaces/Nostro.Ui.Shell-Window.cppm
Jorijn van der Graaf 5b4037955b 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

118 lines
5.9 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro.Ui.Shell:Window;
import std;
import Crafter.Graphics;
import Crafter.Event;
import Nostro.Ui;
import :Loop;
import :Canvas;
// The app's window: a toolkit Context shown on a Canvas and fed from it. What the compositor reports of the pointer,
// the fingers, the keys and the input method becomes the Context's events, in pixels and key names; what the Context
// wants of the window afterwards (a frame, a timer, the on-screen keyboard, the clipboard) is done here. Nothing in
// here knows what the screens are: they are built through onBuild.
//
// Three rules hold it together. An event acts on a frame that is true: before one is handed on, time is brought up to
// it and a frame that went stale is built again. A frame is drawn only when the compositor asks for one, after
// something changed. And what cannot be done inside a listener waits for the loop: the clipboard is read from there.
export namespace Nostro::Ui::Shell {
class Window {
public:
// Makes the window; throws what Canvas does. Nothing is drawn before Start.
Window(Loop& loop, Context& context, Fonts& fonts, const CanvasOptions& options);
~Window();
Window(const Window&) = delete;
Window& operator=(const Window&) = delete;
// Builds the screens into a frame. Called for every frame drawn, and whenever an event needs a true one.
std::function<void(Frame&)> onBuild;
// The user closed the window. Called from inside the compositor's events: whoever takes the window down for it
// does so from the loop, not from here.
std::function<void()> onClose;
// The window can draw no more, and why: a graphics error in a frame, or the compositor's connection gone.
// Nothing is drawn on it from here.
std::function<void(std::string_view why)> onFailed;
// A frame was drawn.
std::function<void()> onFrame;
// A line for whoever keeps a log.
std::function<void(std::string_view line)> onLog;
// Draws the first frame, which shows the window. Throws when it cannot be drawn.
void Start();
// Looks at what the Context wants since the last time and does it. The loop calls this after every source
// that ran (Loop::onSettle), which covers the window's own events and whatever else changed the screens.
void Settle();
// Draws a frame now and writes it to the file as a PNG. Throws when that fails.
void Screenshot(const std::filesystem::path& png);
// Brings the screens up to now, as before an event of the window's own: time has passed for them, and a frame
// that went stale is built again. For what acts on the Context from outside the window, a debugger's command:
// it then acts on what the window shows, and Settle does what it led to.
void Prepare();
Canvas& Surface() noexcept;
bool Failed() const noexcept;
private:
void Layout();
// Brings time up to an event and makes the frame true, before the event is handed to the Context.
void Before(std::int64_t atMs);
std::int64_t EventTime();
Point Pointer() const noexcept;
void Press(PointerPhase phase);
void OnKey(Crafter::KeyCode code);
void OnText(std::string_view text);
void Apply(const Context::Wants& wants, bool redraw);
void ApplyTextInput(const TextInputRequest& want);
void DrawFrame();
void AfterFrame();
void Paste();
void Fail(std::string_view why);
void Log(std::string_view line) const;
Loop& loop;
Context& context;
Canvas canvas;
WaylandSource source;
FrameDriver frames;
Timer wake; // the Context's own: a long press, a caret's blink, a notice's end
Timer repeat; // a held key's repeats
Defer paste;
bool failed = false;
bool overflowSaid = false;
bool down = false; // the pointer's button, or the first finger
// Where the mouse is, while it is in the window; a finger is nowhere once it lifts. What is under a pointer that
// rests changes when the screens do or a list moves, so it is looked up again with every frame built.
std::optional<Point> resting;
EventClock clock;
FrameWait atlasWait;
TextInputRequest applied; // what the compositor was last told
std::string pasteInto; // the field that asked for the clipboard
// The first finger drives the pointer. A touch the compositor takes over for a gesture of its own arrives as a
// release without the finger's lifting: that is a cancel, which taps nothing.
std::optional<std::int32_t> touch;
bool touchLifted = false;
// A key whose press changed the screen or moved the focus, and the text it types: that text is the old
// screen's, at the press and at every repeat while the key stays down.
struct Spent {
Crafter::KeyCode code = 0;
std::string text;
};
std::optional<Spent> spent;
// The listeners on the window library's events; they go before the canvas does.
std::deque<Crafter::EventListener<void>> plain;
std::optional<Crafter::EventListener<Crafter::TouchPoint>> touchDown;
std::optional<Crafter::EventListener<Crafter::TouchPoint>> touchUp;
std::optional<Crafter::EventListener<std::uint32_t>> scroll;
std::optional<Crafter::EventListener<Crafter::KeyCode>> keyDown;
std::optional<Crafter::EventListener<Crafter::KeyCode>> keyUp;
std::optional<Crafter::EventListener<const std::string_view>> text;
std::optional<Crafter::EventListener<const std::string_view>> preedit;
std::optional<Crafter::EventListener<Crafter::TextDelete>> textDelete;
};
}