Nostro/interfaces/Nostro.Ui-Edit.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

86 lines
4 KiB
C++

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
export module Nostro.Ui:Edit;
import std;
import :Text;
// A text field's content and its editing, by code point: an edit that took a single byte would leave half a character
// behind. A field is one line. The window draws the state, places the cursor where a tap lands and turns its keys into
// EditKeys; what an input method is still composing is shown at the cursor and is not yet part of the value.
export namespace Nostro::Ui {
// What a field holds, which decides what can be typed or pasted into it.
enum class FieldKind : std::uint8_t {
Text, // a line of prose: a name, a description
Plain, // a line taken as typed: an IBAN, an e-mail address, a web address
Decimal, // an amount: digits and one of , or .
Digits, // a whole number
Secret // a key or a password: shown as dots, never copied, wiped from memory when it goes
};
enum class EditKey : std::uint8_t {
Backspace,
Delete,
Left,
Right,
Home,
End
};
// One field. It stays where it is made (a form's member), so what draws it and what types into it can point at it,
// and a secret has no copy elsewhere to forget.
struct EditState {
// The most a field holds, in bytes. A secret's buffer is that large from the start, so growing never leaves a
// copy behind.
static constexpr std::size_t limit = 2048;
static constexpr std::size_t secretLimit = 512;
std::string value;
std::size_t cursor = 0; // a byte offset on a code point boundary
std::size_t anchor = 0; // where the selection began; the cursor when nothing is selected
std::string preedit; // the input method's composing text
FieldKind kind;
explicit EditState(FieldKind kind = FieldKind::Text);
EditState(const EditState&) = delete;
EditState& operator=(const EditState&) = delete;
~EditState();
// Nothing typed, and nothing being composed.
bool Empty() const noexcept;
// The text as shown, the composing text at the cursor, and where the cursor is in it.
std::string Shown() const;
std::size_t ShownCursor() const noexcept;
bool HasSelection() const noexcept;
std::size_t SelectionBegin() const noexcept;
std::size_t SelectionEnd() const noexcept;
std::string_view Selected() const noexcept;
// Replaces everything with `text`, as far as the kind takes it; the cursor goes to its end.
void Set(std::string_view text);
// Empties the field. A secret's bytes are overwritten first.
void Clear();
// What a key typed or the input method committed: it takes the place of the selection and of the composing text.
void Commit(std::string_view utf8);
// What was pasted: the composing text is kept, as part of the value, and the pasted text follows it.
void Paste(std::string_view utf8);
// Applies one key. With `extend` (Shift held) a move stretches the selection instead of ending it. False when
// Backspace or Delete had nothing to delete; a move is true even where the cursor cannot go further.
bool Apply(EditKey key, bool extend = false);
// The input method's deletion: so many bytes before and after the cursor, rounded out to code points. The
// composing text stays; the input method replaces it when it changes.
void DeleteAround(std::size_t beforeBytes, std::size_t afterBytes);
// Takes the composing text into the value at the cursor, as the input method would commit it.
void CommitPreedit();
// Puts the cursor at a byte offset of the value (brought to a code point boundary), with nothing selected.
void MoveTo(std::size_t byte);
void SelectAll();
// Removes what is selected; false when nothing was.
bool DeleteSelection();
private:
void Insert(std::string_view utf8);
};
}