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>
This commit is contained in:
parent
1ea50c50bc
commit
5b4037955b
106 changed files with 17804 additions and 289 deletions
|
|
@ -15,6 +15,10 @@ namespace Nostro {
|
|||
std::chrono::system_clock::time_point from = std::chrono::system_clock::time_point::min();
|
||||
std::chrono::system_clock::time_point to = std::chrono::system_clock::time_point::max();
|
||||
std::size_t limit = 100;
|
||||
// For nostrod, which asks the bank for what is new before it answers: true has it answer from what it has stored
|
||||
// alone, at once and without a call to the bank, as for a search in a list it has just given. FindTransactions
|
||||
// only ever reads what is stored, and does not look at this.
|
||||
bool stored = false;
|
||||
};
|
||||
|
||||
export struct Account {
|
||||
|
|
|
|||
|
|
@ -37,6 +37,10 @@ namespace Nostro {
|
|||
|
||||
// An error response from bunq, with its HTTP status (bunq also uses 466, 490 and 491) and error_description.
|
||||
export struct BunqError : std::runtime_error {
|
||||
// What ends the message of an error to a write that may have been carried out all the same: a 5xx, a lost
|
||||
// connection or an unreadable answer. It travels with nostrod's errors, so its clients can tell.
|
||||
static constexpr std::string_view maybeCarriedOut = "; the request may have been carried out, check before trying again";
|
||||
|
||||
std::uint16_t status;
|
||||
BunqError(std::uint16_t status, const std::string& message);
|
||||
};
|
||||
|
|
@ -94,6 +98,8 @@ namespace Nostro {
|
|||
|
||||
// Throws std::invalid_argument for a unit bunq does not know or `every` of 0, as Schedule would.
|
||||
void Check() const;
|
||||
// A unit as a person writes it, "monthly", as bunq names it: "MONTHLY". Check() decides whether bunq knows it.
|
||||
static std::string UnitFromWord(std::string_view word);
|
||||
};
|
||||
|
||||
// A payment bunq makes on a schedule (a bunq SchedulePayment).
|
||||
|
|
@ -126,13 +132,28 @@ namespace Nostro {
|
|||
std::uint64_t id;
|
||||
std::string kind; // bunq's object: CardDebit, CardCredit, ...
|
||||
std::string productType; // MASTERCARD_DEBIT, MAESTRO_DEBIT, ...
|
||||
std::string status; // ACTIVE, DEACTIVATED, LOST, STOLEN, CANCELLED, EXPIRED or PIN_TRIES_EXCEEDED
|
||||
std::string orderStatus;
|
||||
std::string status; // ACTIVE, DEACTIVATED, LOST, STOLEN, CANCELLED, EXPIRED or PIN_TRIES_EXCEEDED: of the plastic, see State()
|
||||
std::string orderStatus; // how far bunq is with making and delivering it: VIRTUAL_DELIVERY, ACCEPTED_FOR_PRODUCTION, DELIVERED_TO_CUSTOMER, ...
|
||||
std::string nameOnCard;
|
||||
std::string secondLine;
|
||||
std::string lastFour; // last four digits of the card number
|
||||
std::string expiry; // YYYY-MM-DD
|
||||
std::string iban; // the account it pays from
|
||||
|
||||
// Whether the card is a virtual one: bunq delivered it as a number and went no further, so there is no plastic
|
||||
// of it, or none yet.
|
||||
bool Virtual() const noexcept;
|
||||
// How the card stands, in one word: bunq's status, except that a virtual card that is not gone for good is
|
||||
// VIRTUAL. bunq has such a card as DEACTIVATED however well it pays, and leaves it so when it is asked to
|
||||
// freeze or to unfreeze it, without an error (a real card and the sandbox, 2026-11-19): DEACTIVATED is its
|
||||
// word for plastic that was not activated, and it gives a virtual card no status that says whether it is
|
||||
// frozen. Reporting one LOST or STOLEN does take.
|
||||
std::string_view State() const noexcept;
|
||||
// Throws std::runtime_error when bunq did not do what was asked: it kept another status than `wanted`, as it
|
||||
// does for a card that was never activated, or the card is a virtual one asked to freeze or unfreeze, which
|
||||
// bunq has as DEACTIVATED before and after. SetCardStatus answers with the card as bunq has it, which is not
|
||||
// always what was asked.
|
||||
void ExpectStatus(std::string_view wanted) const;
|
||||
};
|
||||
|
||||
export struct BunqAccount : public Account {
|
||||
|
|
@ -238,6 +259,10 @@ namespace Nostro {
|
|||
// OAuth: send the user to this URL, where they approve with the bunq app; bunq then redirects to
|
||||
// `redirectUri` with `code` and `state` in the query. `redirectUri` must be registered with the OAuth client.
|
||||
static std::string OAuthAuthorizationUrl(BunqEnvironment environment, std::string_view clientId, std::string_view redirectUri, std::string_view state);
|
||||
// The code in the address the browser was redirected to after that approval. Throws std::runtime_error when
|
||||
// bunq did not authorize (error= in it), when it has no code, or when its state is not `state`: a code from
|
||||
// anyone else's authorization is refused.
|
||||
static std::string OAuthCodeFromRedirect(std::string_view redirected, std::string_view state);
|
||||
// Exchanges the code from that redirect for an access token, which Create takes in place of an API key.
|
||||
static std::string OAuthExchangeCode(BunqEnvironment environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code);
|
||||
|
||||
|
|
@ -347,7 +372,7 @@ struct std::formatter<Nostro::BunqDraftPayment> : std::formatter<std::string> {
|
|||
template<>
|
||||
struct std::formatter<Nostro::BunqCard> : std::formatter<std::string> {
|
||||
auto format(const Nostro::BunqCard& obj, std::format_context& ctx) const {
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t**** {}\t{}\t{}\t{}\texpires {}", obj.id, obj.status, obj.productType, obj.lastFour, obj.nameOnCard, obj.secondLine, obj.iban, obj.expiry), ctx);
|
||||
return std::formatter<std::string>::format(std::format("{}\t{}\t{}\t**** {}\t{}\t{}\t{}\texpires {}", obj.id, obj.State(), obj.productType, obj.lastFour, obj.nameOnCard, obj.secondLine, obj.iban, obj.expiry), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
#ifdef NOSTRO_DBUS_ENABLED
|
||||
#include <systemd/sd-bus.h>
|
||||
#endif
|
||||
|
||||
export module Nostro:Client;
|
||||
#ifdef NOSTRO_DBUS_ENABLED
|
||||
|
||||
|
|
@ -19,49 +25,143 @@ namespace Nostro {
|
|||
DaemonError(std::string name, const std::string& message);
|
||||
};
|
||||
|
||||
// nostrod is not running, and the system bus cannot start it: not installed, or not set up.
|
||||
export struct DaemonMissing : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
// A call that changes something got no answer in time, or bunq answered it with an error that leaves open whether
|
||||
// it was carried out (a 5xx, BunqError::maybeCarriedOut): what was asked may have happened. Check before asking again.
|
||||
export struct OutcomeUnknown : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
// What an asynchronous call answers: its result, or the exception the blocking call would have thrown, and the
|
||||
// library's warnings that came with it. An error answer carries no notes.
|
||||
export template<typename T>
|
||||
struct Answer {
|
||||
std::variant<T, std::exception_ptr> result;
|
||||
std::vector<std::string> notes;
|
||||
|
||||
// The result, or the exception rethrown.
|
||||
T& Get() {
|
||||
if(const std::exception_ptr* error = std::get_if<std::exception_ptr>(&result)) {
|
||||
std::rethrow_exception(*error);
|
||||
}
|
||||
return std::get<T>(result);
|
||||
}
|
||||
bool Failed() const {
|
||||
return std::holds_alternative<std::exception_ptr>(result);
|
||||
}
|
||||
// The exception, or null when the call worked.
|
||||
std::exception_ptr Error() const {
|
||||
const std::exception_ptr* error = std::get_if<std::exception_ptr>(&result);
|
||||
return error ? *error : nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
// nostrod over the system bus: each method of its interface (Bus::methods), with nostro's own types. `account` is
|
||||
// an account's name or IBAN. A call may wait for a password dialog, which nostrod has polkit show, and for the
|
||||
// bank. The library's warnings during the calls come out of TakeNotes().
|
||||
//
|
||||
// Each method also has an asynchronous form, for a program with an event loop that must not wait: Attach() the
|
||||
// client to an sd-event loop, and the method returns at once with a Pending, and calls `then` from that loop once
|
||||
// nostrod answered, with an Answer. Notes are collected per user by nostrod's worker, so an answer's notes can be
|
||||
// another call's of the same user, the CLI's included.
|
||||
export class Client {
|
||||
public:
|
||||
// An asynchronous call whose answer is awaited. Destroying it, or Cancel(), drops the answer: `then` is not
|
||||
// called. It does not undo the call, which nostrod and the bank carry on with.
|
||||
class Pending {
|
||||
public:
|
||||
Pending() = default;
|
||||
Pending(Pending&& other) noexcept;
|
||||
Pending& operator=(Pending&& other) noexcept;
|
||||
Pending(const Pending&) = delete;
|
||||
Pending& operator=(const Pending&) = delete;
|
||||
~Pending();
|
||||
|
||||
void Cancel();
|
||||
// Whether `then` is still to be called.
|
||||
bool Active() const;
|
||||
|
||||
private:
|
||||
friend class Client;
|
||||
explicit Pending(sd_bus_slot* slot);
|
||||
sd_bus_slot* slot = nullptr;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
using Then = std::function<void(Answer<T>)>;
|
||||
|
||||
// Connects to the system bus; D-Bus starts nostrod with the first call. A call waits up to `timeout` for its
|
||||
// answer: long enough for the user to type a password, and for the bank's rate limits. Throws when there is
|
||||
// no system bus.
|
||||
static Client System(std::chrono::seconds timeout = std::chrono::minutes(10));
|
||||
|
||||
// Hands the connection to `event`, whose loop then sends the asynchronous calls and delivers their answers.
|
||||
// Throws when the client is attached already.
|
||||
void Attach(sd_event* event);
|
||||
// Whether the connection to the bus is still open; once it is not, make a new client.
|
||||
bool Connected() const;
|
||||
|
||||
std::vector<ConnectorSummary> Connectors();
|
||||
// `environment` is "production", "sandbox", or empty for the one the key's prefix says.
|
||||
[[nodiscard]] Pending Connectors(Then<std::vector<ConnectorSummary>> then);
|
||||
// The key says itself which environment it is of, by its prefix: leave `environment` empty. Naming one,
|
||||
// "production" or "sandbox", only has a key of the other refused (std::invalid_argument), before anything is sent.
|
||||
AddedConnector AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment);
|
||||
[[nodiscard]] Pending AddBunq(std::string_view apiKey, std::string_view allowedIps, std::string_view environment, Then<AddedConnector> then);
|
||||
AddedConnector AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code);
|
||||
[[nodiscard]] Pending AddBunqOAuth(std::string_view environment, std::string_view clientId, std::string_view clientSecret, std::string_view redirectUri, std::string_view code, Then<AddedConnector> then);
|
||||
// By name, or by its number in Connectors(); returns the name.
|
||||
std::string RemoveConnector(std::string_view connector);
|
||||
[[nodiscard]] Pending RemoveConnector(std::string_view connector, Then<std::string> then);
|
||||
std::vector<ConnectorAccounts> Accounts();
|
||||
// Fetches what is new first, then answers from the stored history.
|
||||
[[nodiscard]] Pending Accounts(Then<std::vector<ConnectorAccounts>> then);
|
||||
// Fetches what is new from the bank first, then answers from the stored history; with `query.stored` from the
|
||||
// stored history alone, at once and without the bank.
|
||||
std::vector<Transaction> Transactions(std::string_view account, const TransactionQuery& query);
|
||||
[[nodiscard]] Pending Transactions(std::string_view account, const TransactionQuery& query, Then<std::vector<Transaction>> then);
|
||||
|
||||
Created Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
[[nodiscard]] Pending Send(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, Then<Created> then);
|
||||
Created Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule);
|
||||
[[nodiscard]] Pending Schedule(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, const BunqSchedule& schedule, Then<Created> then);
|
||||
Created Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
[[nodiscard]] Pending Draft(std::string_view account, std::string_view to, std::string_view name, std::int64_t amount, std::string_view description, Then<Created> then);
|
||||
BunqPaymentRequest Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description);
|
||||
[[nodiscard]] Pending Request(std::string_view account, std::string_view from, std::string_view name, std::int64_t amount, std::string_view description, Then<BunqPaymentRequest> then);
|
||||
|
||||
std::vector<BunqPaymentRequest> Requests(std::string_view account);
|
||||
[[nodiscard]] Pending Requests(std::string_view account, Then<std::vector<BunqPaymentRequest>> then);
|
||||
void RevokeRequest(std::string_view account, std::uint64_t id);
|
||||
[[nodiscard]] Pending RevokeRequest(std::string_view account, std::uint64_t id, Then<std::monostate> then);
|
||||
std::vector<BunqIncomingRequest> IncomingRequests(std::string_view account);
|
||||
[[nodiscard]] Pending IncomingRequests(std::string_view account, Then<std::vector<BunqIncomingRequest>> then);
|
||||
void RespondToRequest(std::string_view account, std::uint64_t id, bool accept);
|
||||
[[nodiscard]] Pending RespondToRequest(std::string_view account, std::uint64_t id, bool accept, Then<std::monostate> then);
|
||||
std::vector<BunqScheduledPayment> ScheduledPayments(std::string_view account);
|
||||
[[nodiscard]] Pending ScheduledPayments(std::string_view account, Then<std::vector<BunqScheduledPayment>> then);
|
||||
void CancelScheduledPayment(std::string_view account, std::uint64_t id);
|
||||
[[nodiscard]] Pending CancelScheduledPayment(std::string_view account, std::uint64_t id, Then<std::monostate> then);
|
||||
std::vector<BunqDraftPayment> DraftPayments(std::string_view account);
|
||||
[[nodiscard]] Pending DraftPayments(std::string_view account, Then<std::vector<BunqDraftPayment>> then);
|
||||
void RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept);
|
||||
[[nodiscard]] Pending RespondToDraftPayment(std::string_view account, std::uint64_t id, bool accept, Then<std::monostate> then);
|
||||
std::vector<ConnectorCards> Cards();
|
||||
[[nodiscard]] Pending Cards(Then<std::vector<ConnectorCards>> then);
|
||||
// "ACTIVE", "DEACTIVATED", "LOST" or "STOLEN"; returns the card as bunq has it afterwards.
|
||||
BunqCard SetCardStatus(std::uint64_t card, std::string_view status);
|
||||
[[nodiscard]] Pending SetCardStatus(std::uint64_t card, std::string_view status, Then<BunqCard> then);
|
||||
|
||||
// The account apps' checkouts (Nostro.Checkout) pay from: none until one is chosen, or once it is gone.
|
||||
std::optional<Account> DefaultCheckoutAccount();
|
||||
[[nodiscard]] Pending DefaultCheckoutAccount(Then<std::optional<Account>> then);
|
||||
// Chooses it, and returns it. An account reached through OAuth cannot pay a shop, and is refused.
|
||||
Account SetDefaultCheckoutAccount(std::string_view account);
|
||||
[[nodiscard]] Pending SetDefaultCheckoutAccount(std::string_view account, Then<Account> then);
|
||||
|
||||
// The library's warnings from the calls so far, taken out.
|
||||
// The library's warnings from the blocking calls so far, taken out.
|
||||
std::vector<std::string> TakeNotes();
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -102,6 +102,12 @@ export namespace Nostro {
|
|||
return std::chrono::system_clock::from_time_t(std::mktime(&local));
|
||||
}
|
||||
|
||||
// The last moment a date ends a range at: for "YYYY-MM-DD" the end of that day, so a range up to 2026-10-31 includes
|
||||
// that day; for "YYYY-MM-DD hh:mm" that minute.
|
||||
inline std::chrono::system_clock::time_point ParseLocalDateEnd(std::string_view text) {
|
||||
return text.size() == 10 ? ParseLocalDate(text, 1) - std::chrono::microseconds(1) : ParseLocalDate(text);
|
||||
}
|
||||
|
||||
// "YYYY-MM-DD hh:mm" in the local time zone.
|
||||
inline std::string FormatLocalTime(std::chrono::system_clock::time_point time) {
|
||||
const std::time_t seconds = std::chrono::system_clock::to_time_t(time);
|
||||
|
|
|
|||
341
interfaces/Nostro.App-App.cppm
Normal file
341
interfaces/Nostro.App-App.cppm
Normal file
|
|
@ -0,0 +1,341 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App:App;
|
||||
|
||||
import std;
|
||||
import Nostro;
|
||||
import Nostro.Ui;
|
||||
import :Banks;
|
||||
import :Format;
|
||||
import :Forms;
|
||||
import :Errors;
|
||||
|
||||
// Nostro's window without the window: what it knows of the user's banks, where the user is in it, what is being
|
||||
// filled in and what is under way, and the screens built from that. It asks nostrod through the asynchronous Client
|
||||
// and never waits for it: a call's answer changes the state and asks for a new frame. Nothing here draws or reads a
|
||||
// device (that is Nostro.Ui.Shell's), so a test builds a frame, reads what it says and taps it by name.
|
||||
//
|
||||
// The rules that keep a bank's client honest are here. A press acts on what it went down on, and every action names
|
||||
// what it is about by value: an IBAN or the bank's id, never a place in a list. One change at a time: while one is
|
||||
// under way its sheet covers the screen, so nothing else can be asked for and nothing is sent twice. A payment from
|
||||
// the form is sent by the form's own button, with no question of the app's before it: what confirms it is the system's
|
||||
// password dialog, which nostrod words itself. What the app does ask about first (a request to send, a request or a
|
||||
// draft to accept, a card to report) it shows built from the same values that are then sent. And an answer that leaves
|
||||
// open whether money moved says so, and offers no button to try again.
|
||||
//
|
||||
// Which bank an account is with makes no difference to any of it. A bank's own is in two places: what :Banks says of
|
||||
// it, and the forms a connection to it is made with, which are the last pages of adding a connection in the settings
|
||||
// (Nostro.App-Bunq.cpp has bunq's). Everything else says "your bank", and asks :Banks what a connection can do.
|
||||
|
||||
export namespace Nostro::App {
|
||||
// What the app needs of the program around it.
|
||||
struct Host {
|
||||
// A client for nostrod, attached to the program's event loop. Throws when there is no system bus.
|
||||
std::function<Client()> connect;
|
||||
// Asks the desktop to open an address in the user's browser; `then` hears whether it took the request.
|
||||
std::function<void(std::string_view uri, std::function<void(bool taken)> then)> openUri;
|
||||
// The time, for "today" and for what is in the past. The system's clock when left empty.
|
||||
std::function<std::chrono::system_clock::time_point()> now;
|
||||
// A new state for an OAuth request, which ties the browser's answer to it. Random when left empty.
|
||||
std::function<std::string()> oauthState;
|
||||
std::string version;
|
||||
};
|
||||
|
||||
// How far the asking for something is.
|
||||
enum class LoadState : std::uint8_t {
|
||||
Idle, // not asked for yet
|
||||
Loading,
|
||||
Loaded,
|
||||
Failed
|
||||
};
|
||||
|
||||
// Something nostrod is asked for.
|
||||
template<typename T>
|
||||
struct Loadable {
|
||||
LoadState state = LoadState::Idle;
|
||||
T value{};
|
||||
Guidance problem; // why it failed
|
||||
bool refreshing = false; // loaded, and asked for again: the old value shows meanwhile
|
||||
};
|
||||
|
||||
// A bank connection with its accounts: Connectors() and Accounts() joined.
|
||||
struct Connection {
|
||||
std::string name;
|
||||
std::string bank; // as nostrod calls it; BankName() says it as the bank does
|
||||
std::string environment; // "production" or "sandbox"
|
||||
std::string auth; // the way it was made, as nostrod calls that; WayName() says it in words
|
||||
bool working = false;
|
||||
std::vector<Account> accounts;
|
||||
// It pays between the user's own accounts and nobody else, by what :Banks knows of the way it was made.
|
||||
bool ownAccountsOnly = false;
|
||||
};
|
||||
|
||||
// One account, found by its key.
|
||||
struct AccountRef {
|
||||
const Connection* connection = nullptr;
|
||||
const Account* account = nullptr;
|
||||
std::string key; // names it in the app: its IBAN, or its name where the bank gave none
|
||||
};
|
||||
|
||||
class App {
|
||||
public:
|
||||
App(Ui::Context& context, Host host);
|
||||
App(const App&) = delete;
|
||||
App& operator=(const App&) = delete;
|
||||
|
||||
// Asks nostrod for the connections and accounts; the first frame shows them loading.
|
||||
void Start();
|
||||
// Builds the screen the user is on.
|
||||
void Build(Ui::Frame& frame);
|
||||
|
||||
// Whether nostrod is still being waited for: for whoever runs the loop until the app has settled.
|
||||
bool Busy() const noexcept;
|
||||
// Whether a change is under way. The window may close then, and the process must not: the answer says whether
|
||||
// money moved. onSettled is called when it has come.
|
||||
bool Changing() const noexcept;
|
||||
std::function<void()> onSettled;
|
||||
// An answer came, to the method of that name, and whether it was an error: for a log or a debugger. Called once
|
||||
// the app has taken the answer in, so what it shows is what the answer led to.
|
||||
std::function<void(std::string_view member, bool failed)> onAnswer;
|
||||
|
||||
// Goes to a place by name: "accounts", "cards", "settings", "connections", "checkout", "about", "account:<IBAN>",
|
||||
// or one of the steps of adding a connection: "add" for the banks, "add:<bank>" for a bank's ways and
|
||||
// "add:<bank>:<way>" for a way's form, as in "add:bunq:key". False for a name that is none of them, and while
|
||||
// a change is under way, which the screen waits out.
|
||||
bool Navigate(std::string_view route);
|
||||
// Where the user is, as a name of the same kind, with what is open over it after a '+': "account:NL…+filter".
|
||||
std::string Screen() const;
|
||||
// The connections and accounts as they are known.
|
||||
const Loadable<std::vector<Connection>>& Overview() const noexcept;
|
||||
// The state of the OAuth request that is being made, for a test that plays the browser.
|
||||
std::string_view OAuthState() const noexcept;
|
||||
|
||||
private:
|
||||
// -- where the user is ---------------------------------------------------------------------------------------
|
||||
enum class Place : std::uint8_t {
|
||||
Accounts,
|
||||
Cards,
|
||||
Settings
|
||||
};
|
||||
enum class Page : std::uint8_t {
|
||||
Account,
|
||||
Pay,
|
||||
Request,
|
||||
Connections,
|
||||
Banks, // adding a connection: the banks to choose from
|
||||
Ways, // the ways to connect to the bank that was chosen
|
||||
Connect, // the form of the way that was chosen
|
||||
Checkout,
|
||||
About
|
||||
};
|
||||
struct Route {
|
||||
Page page = Page::Account;
|
||||
std::string account; // the key of the account it is about
|
||||
const Bank* bank = nullptr; // of Ways and Connect
|
||||
const Way* way = nullptr; // of Connect
|
||||
};
|
||||
enum class Tab : std::uint8_t {
|
||||
Activity,
|
||||
Requests,
|
||||
Incoming,
|
||||
Scheduled,
|
||||
Drafts
|
||||
};
|
||||
|
||||
// -- what is known -------------------------------------------------------------------------------------------
|
||||
// One account's lists, each asked for when its tab is first shown.
|
||||
struct AccountData {
|
||||
Tab tab = Tab::Activity;
|
||||
FilterForm filter;
|
||||
TransactionQuery query; // the filter as it was last asked for
|
||||
Loadable<std::vector<Transaction>> activity;
|
||||
bool more = false; // the list is as long as was asked for: there may be more
|
||||
Loadable<std::vector<BunqPaymentRequest>> requests;
|
||||
Loadable<std::vector<BunqIncomingRequest>> incoming;
|
||||
Loadable<std::vector<BunqScheduledPayment>> scheduled;
|
||||
Loadable<std::vector<BunqDraftPayment>> drafts;
|
||||
};
|
||||
|
||||
// One kind of call at a time, and at most one waiting behind it: the newest. nostrod's worker makes its calls
|
||||
// to the bank one after another, and an answer dropped here does not stop it, so a queue of searches the user
|
||||
// has already typed past would only use up the bank's patience.
|
||||
struct Lane {
|
||||
Client::Pending pending;
|
||||
bool busy = false;
|
||||
std::function<void()> next;
|
||||
};
|
||||
|
||||
// -- what is open over the screen ----------------------------------------------------------------------------
|
||||
struct Line {
|
||||
std::string label;
|
||||
std::string value;
|
||||
// What a press on the line puts on the clipboard, where that is not the value as it is shown: an IBAN
|
||||
// without the spaces it is read with.
|
||||
std::string copied;
|
||||
};
|
||||
struct Choice {
|
||||
std::string key;
|
||||
std::string label;
|
||||
Ui::ButtonKind kind = Ui::ButtonKind::Tonal;
|
||||
bool enabled = true;
|
||||
std::string reason; // why it cannot be chosen
|
||||
std::function<void()> onTap;
|
||||
};
|
||||
// What is asked or told over the screen: a thing to look at with what can be done about it, a question before
|
||||
// a change, the wait for its answer, and how it went.
|
||||
struct Dialog {
|
||||
enum class Kind : std::uint8_t {
|
||||
Detail, // a row's own sheet
|
||||
Ask, // before a change: what will be done, to confirm
|
||||
Working, // the change is under way
|
||||
Done, // it worked
|
||||
Problem // it did not, or may not have
|
||||
};
|
||||
Kind kind = Kind::Detail;
|
||||
std::string name; // for Screen(): "transaction", "confirm", "working"
|
||||
std::string title;
|
||||
std::vector<std::string> text;
|
||||
std::vector<Line> lines;
|
||||
std::vector<Choice> choices;
|
||||
std::string link; // an address to copy or open
|
||||
std::function<void()> onClose; // after it is sent away
|
||||
};
|
||||
enum class Picking : std::uint8_t {
|
||||
None,
|
||||
PayFrom,
|
||||
RequestInto
|
||||
};
|
||||
|
||||
// -- nostrod -------------------------------------------------------------------------------------------------
|
||||
Client& Nostrod();
|
||||
void Reconnect();
|
||||
void Note(const std::vector<std::string>& notes);
|
||||
void Answered(std::string_view member, bool failed);
|
||||
// A reading: `start` makes the call, given what to do with its answer, in the lane of that name.
|
||||
template<typename T>
|
||||
void Fetch(std::string lane, std::string member, std::function<Client::Pending(Client&, Client::Then<T>)> start, std::function<void(Answer<T>)> then);
|
||||
// A change: shows the wait, makes the call, and shows how it went. `done` says what to tell the user; what it
|
||||
// throws is told as the change having failed. `account` is the account it is about, if any.
|
||||
template<typename T>
|
||||
void Change(Operation operation, std::string member, std::string title, std::string account, std::function<Client::Pending(Client&, Client::Then<T>)> start, std::function<void(T&)> done);
|
||||
void Failed(const Guidance& guidance, Operation operation, const std::string& account);
|
||||
void Wait(Operation operation, std::string title);
|
||||
|
||||
void Refresh(bool again = false);
|
||||
void LoadAccounts(std::vector<ConnectorSummary> summaries, bool again);
|
||||
void OverviewFailed(const Guidance& guidance);
|
||||
void ListFailed(const Guidance& guidance);
|
||||
void LoadActivity(const std::string& key, bool stored = false);
|
||||
template<typename T>
|
||||
void LoadList(const std::string& key, std::string_view member, Loadable<std::vector<T>> AccountData::* list, Client::Pending (Client::* call)(std::string_view, Client::Then<std::vector<T>>));
|
||||
void LoadTab(const std::string& key, Tab tab, bool again = false);
|
||||
void LoadCards(bool again = false);
|
||||
void LoadCheckout();
|
||||
void Changed(const std::string& key);
|
||||
|
||||
// -- going places --------------------------------------------------------------------------------------------
|
||||
void Go(Place to);
|
||||
void Push(Page page, std::string account = {}, const Bank* bank = nullptr, const Way* way = nullptr);
|
||||
void Back();
|
||||
void Show(Dialog shown);
|
||||
void Close();
|
||||
void Tell(std::string_view notice);
|
||||
std::chrono::system_clock::time_point Today() const;
|
||||
std::optional<AccountRef> Find(std::string_view key) const;
|
||||
std::vector<AccountRef> Accounts() const;
|
||||
AccountData& Data(const std::string& key);
|
||||
bool Own(std::string_view iban) const;
|
||||
|
||||
// -- what the user asks for ----------------------------------------------------------------------------------
|
||||
void OpenPay(std::string account);
|
||||
void OpenRequest(std::string account);
|
||||
void SubmitPayment();
|
||||
void SendPayment(const std::string& key, Currency currency, const PaymentOrder& order);
|
||||
void ReviewRequest();
|
||||
void SendRequest(const std::string& key, Currency currency, const RequestOrder& order);
|
||||
void ShowTransaction(const std::string& key, const Transaction& transaction);
|
||||
void ShowRequest(const std::string& key, const BunqPaymentRequest& request);
|
||||
void ShowIncoming(const std::string& key, const BunqIncomingRequest& request);
|
||||
void ShowScheduled(const std::string& key, const BunqScheduledPayment& scheduled);
|
||||
void ShowDraft(const std::string& key, const BunqDraftPayment& draft);
|
||||
void ShowCard(const BunqCard& card);
|
||||
void ShowConnection(const Connection& connection);
|
||||
// A change that needs a yes and no more: asks `question`, makes the change, and tells `told`.
|
||||
void Simple(Operation operation, std::string member, std::string question, std::string detail, std::string confirm, std::string told, std::string account, std::function<Client::Pending(Client&, Client::Then<std::monostate>)> start);
|
||||
void SetCard(std::uint64_t card, std::string lastFour, Operation operation, std::string status);
|
||||
void RemoveConnection(const Connection& connection);
|
||||
// What every way's form ends in: nostrod is asked to add the connection, as `start` asks it, and the user is
|
||||
// told how that went. For the form's own button: which bank and which way it is are the page's that is open.
|
||||
void Connect(std::string member, std::function<Client::Pending(Client&, Client::Then<AddedConnector>)> start);
|
||||
void Connected(const AddedConnector& added, const Way& way);
|
||||
void ForgetConnecting();
|
||||
// bunq's two ways (Nostro.App-Bunq.cpp).
|
||||
void ConnectBunqKey();
|
||||
void BeginBunqOAuth();
|
||||
void ConnectBunqOAuth();
|
||||
void ChooseCheckout(const std::string& key, const std::string& name);
|
||||
void Search(const std::string& key, bool now);
|
||||
|
||||
// -- the screens ---------------------------------------------------------------------------------------------
|
||||
// A form's column inside a page: the page's rows run from edge to edge, a field keeps clear of the edges.
|
||||
static Ui::Box Padded(const Ui::Frame& frame, Ui::Box box);
|
||||
Ui::Box Scaffold(Ui::Frame& frame);
|
||||
std::function<void()> Refresher();
|
||||
std::string Title() const;
|
||||
void Content(Ui::Frame& frame, Ui::Box area);
|
||||
void Notes(Ui::Frame& frame, Ui::Column& column);
|
||||
void Trouble(Ui::Frame& frame, Ui::Column& column, const Guidance& guidance, std::string_view key, std::function<void()> retry);
|
||||
void Waiting(Ui::Frame& frame, Ui::Column& column, std::uint32_t rows);
|
||||
void Empty(Ui::Frame& frame, Ui::Column& column, std::string_view text);
|
||||
void Section(Ui::Frame& frame, Ui::Column& column, std::string_view title);
|
||||
void HomePage(Ui::Frame& frame, Ui::Column& column);
|
||||
void Welcome(Ui::Frame& frame, Ui::Column& column);
|
||||
void AccountPage(Ui::Frame& frame, Ui::Column& column, const AccountRef& account, Ui::Box viewport);
|
||||
void ActivityTab(Ui::Frame& frame, Ui::Column& column, const AccountRef& account, AccountData& data, Ui::Box viewport);
|
||||
template<typename T, typename Make>
|
||||
void ListTab(Ui::Frame& frame, Ui::Column& column, const std::string& key, Tab tab, Loadable<std::vector<T>>& list, std::string_view none, Ui::Box viewport, Make&& row);
|
||||
void PayPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void RequestPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void AccountField(Ui::Frame& frame, Ui::Column& column, std::string_view key, std::string_view label, const std::string& account, Picking picking);
|
||||
void CardsPage(Ui::Frame& frame, Ui::Column& column, Ui::Box viewport);
|
||||
void SettingsPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void ConnectionsPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void BanksPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void WaysPage(Ui::Frame& frame, Ui::Column& column, const Bank& bank);
|
||||
void BunqKeyPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void BunqOAuthPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void CheckoutPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void AboutPage(Ui::Frame& frame, Ui::Column& column);
|
||||
void FilterSheet(Ui::Frame& frame, const AccountRef& account);
|
||||
void PickSheet(Ui::Frame& frame);
|
||||
void DialogSheet(Ui::Frame& frame);
|
||||
|
||||
Ui::Context& context;
|
||||
Host host;
|
||||
std::optional<Client> client;
|
||||
std::map<std::string, Lane, std::less<>> lanes;
|
||||
Client::Pending change; // the one change under way
|
||||
bool changing = false;
|
||||
std::vector<std::string> notes;
|
||||
|
||||
Loadable<std::vector<Connection>> overview;
|
||||
std::map<std::string, AccountData, std::less<>> accounts;
|
||||
Loadable<std::vector<ConnectorCards>> cards;
|
||||
Loadable<std::optional<Account>> checkout;
|
||||
|
||||
Place place = Place::Accounts;
|
||||
std::vector<Route> stack;
|
||||
std::optional<Dialog> dialog;
|
||||
std::string filtering; // the account whose filter sheet is open
|
||||
Picking picking = Picking::None;
|
||||
|
||||
PaymentForm payment;
|
||||
std::string payFrom;
|
||||
RequestForm request;
|
||||
std::string requestInto;
|
||||
BunqKeyForm bunqKey;
|
||||
BunqOAuthForm bunqOAuth;
|
||||
std::string bunqOAuthError; // what was wrong with the address the browser was sent to
|
||||
};
|
||||
}
|
||||
57
interfaces/Nostro.App-Banks.cppm
Normal file
57
interfaces/Nostro.App-Banks.cppm
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App:Banks;
|
||||
|
||||
import std;
|
||||
|
||||
// The banks the app can connect, and what is a bank's own: its name, the ways a connection to it is made, and what
|
||||
// each way lets a connection do. The settings are built from this: adding a connection lists the banks, a bank lists
|
||||
// its ways, and each way has a form of its own (bunq's are in Nostro.App-Bunq.cpp). Nothing else in the app names a
|
||||
// bank or a way: every other screen says "your bank", and asks here what a connection can do. A bank and a way are
|
||||
// found by what nostrod calls them, and one that nostrod has and this app does not know is shown by that name.
|
||||
|
||||
export namespace Nostro::App {
|
||||
// The form a way is filled in on. One for each way of each bank: what a bank asks for is its own.
|
||||
enum class ConnectForm : std::uint8_t {
|
||||
BunqKey,
|
||||
BunqOAuth
|
||||
};
|
||||
|
||||
// One way to connect to a bank.
|
||||
struct Way {
|
||||
std::string_view key; // names it in a route and a region: "key"
|
||||
std::string_view auth; // what nostrod calls a connection made this way (ConnectorSummary::auth): "api key"
|
||||
std::string_view title; // "API key"
|
||||
std::string_view about; // what it gives and what it takes, in a line for under its title
|
||||
std::string_view revoke; // what stays valid at the bank once a connection is removed, and where it is revoked
|
||||
ConnectForm form = ConnectForm::BunqKey;
|
||||
// A connection made this way pays between the user's own accounts and nobody else. What it cannot pay it can
|
||||
// save as a draft, so this is for a bank that has drafts.
|
||||
bool ownAccountsOnly = false;
|
||||
};
|
||||
|
||||
struct Bank {
|
||||
std::string_view id; // what nostrod calls it (ConnectorSummary::bank): "bunq"
|
||||
std::string_view name; // as the bank writes its own name
|
||||
std::span<const Way> ways;
|
||||
};
|
||||
|
||||
// The banks the app can connect, in the order they are offered.
|
||||
std::span<const Bank> Banks() noexcept;
|
||||
// The bank nostrod calls `id`; null for one this app does not know.
|
||||
const Bank* FindBank(std::string_view id) noexcept;
|
||||
// The way a connection to that bank was made, by what nostrod calls the bank and the way; null for one this app
|
||||
// does not know.
|
||||
const Way* FindWay(std::string_view bank, std::string_view auth) noexcept;
|
||||
|
||||
// A bank's name as it writes it, and what nostrod calls it for one this app does not know.
|
||||
std::string BankName(std::string_view id);
|
||||
// How a connection was made, in words: "API key", and what nostrod calls it for a way this app does not know.
|
||||
std::string WayName(std::string_view bank, std::string_view auth);
|
||||
// Whether a connection made that way pays between the user's own accounts only. Not that the app knows of, for a
|
||||
// way it does not know: nostrod refuses what such a connection cannot do.
|
||||
bool OwnAccountsOnly(std::string_view bank, std::string_view auth) noexcept;
|
||||
// What a connection was made with stays valid at the bank when the connection is removed: where to revoke it.
|
||||
std::string Revoking(std::string_view bank, std::string_view auth);
|
||||
}
|
||||
73
interfaces/Nostro.App-Errors.cppm
Normal file
73
interfaces/Nostro.App-Errors.cppm
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App:Errors;
|
||||
|
||||
import std;
|
||||
import Nostro;
|
||||
|
||||
// What went wrong, said so that the user knows what happened to their money and what to do next. nostrod's errors
|
||||
// are told apart by their names and by what was being done: a payment that got no clear answer may have been made,
|
||||
// which a listing that got none never is.
|
||||
|
||||
export namespace Nostro::App {
|
||||
// What nostrod is asked to do, as far as that decides whether it asks for a password and what an error to it means.
|
||||
enum class Operation : std::uint8_t {
|
||||
Load, // any reading: accounts, payments, cards
|
||||
Send,
|
||||
Schedule,
|
||||
Draft,
|
||||
Request,
|
||||
RevokeRequest,
|
||||
PayRequest,
|
||||
RejectRequest,
|
||||
CancelSchedule,
|
||||
AcceptDraft,
|
||||
RejectDraft,
|
||||
FreezeCard,
|
||||
UnfreezeCard,
|
||||
BlockCard,
|
||||
AddConnection,
|
||||
RemoveConnection,
|
||||
SetCheckoutAccount
|
||||
};
|
||||
|
||||
// The polkit action nostrod checks before it does that (Bus::action…).
|
||||
std::string_view ActionOf(Operation operation) noexcept;
|
||||
// Whether the system asks for the user's password before it, by the policy nostro installs: in the user's own
|
||||
// session only what moves money, unfreezes or blocks a card, or changes the connections. An administrator's rules
|
||||
// may ask for more or less; this decides nothing but what the user is told to expect.
|
||||
bool AsksPassword(Operation operation) noexcept;
|
||||
// Whether money leaves an account by it.
|
||||
bool MovesMoney(Operation operation) noexcept;
|
||||
|
||||
enum class Problem : std::uint8_t {
|
||||
Input, // refused before anything was sent
|
||||
NotConfirmed, // no password was given, or nothing could ask for one
|
||||
NotAllowed, // the system's policy refused
|
||||
CannotAsk, // nostrod was not allowed to show a password dialog
|
||||
OwnAccountsOnly, // the bank lets this connection pay between the user's own accounts only
|
||||
Bank, // the bank refused
|
||||
Unknown, // no clear answer: it may have been carried out
|
||||
NoService, // nostrod is not there and cannot be started
|
||||
Late, // a reading that got no answer in time
|
||||
Failed // anything else
|
||||
};
|
||||
|
||||
// There is no connecting to the system bus, which nostrod is talked to over: what the app's host threw when asked
|
||||
// for a client.
|
||||
struct NoSystemBus : std::runtime_error {
|
||||
using std::runtime_error::runtime_error;
|
||||
};
|
||||
|
||||
struct Guidance {
|
||||
Problem problem = Problem::Failed;
|
||||
std::string title;
|
||||
std::string detail;
|
||||
bool mayHaveHappened = false; // look before asking again
|
||||
bool retry = false; // asking again is safe, and may work
|
||||
};
|
||||
|
||||
// What an answer's error means for what was asked.
|
||||
Guidance Explain(const std::exception_ptr& error, Operation operation);
|
||||
}
|
||||
53
interfaces/Nostro.App-Format.cppm
Normal file
53
interfaces/Nostro.App-Format.cppm
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App:Format;
|
||||
|
||||
import std;
|
||||
import Nostro;
|
||||
|
||||
// How the app words what a bank gives it: money, dates as a person reads them in their own time zone, and a
|
||||
// bank's capitals in plain words. English only; every such wording is here, and none of it names a bank.
|
||||
|
||||
export namespace Nostro::App {
|
||||
// An IBAN in groups of four, as it is printed on a card; anything else as it is.
|
||||
std::string GroupIban(std::string_view iban);
|
||||
// What an account is found by, in the app and by nostrod: its IBAN, or its name where the bank gave it none.
|
||||
std::string AccountKey(const Account& account);
|
||||
// Who a payment is to or from, as a person would say it: by name, else by IBAN or address, and "Unknown" where
|
||||
// the bank gives neither, as it does for someone it knows by a phone number alone.
|
||||
std::string PartyName(const BunqLabel& party);
|
||||
std::string PartyName(const BunqCounterparty& party);
|
||||
// An amount as a person reads it, its thousands apart: "€12,500.00", "-€3.00".
|
||||
std::string Money(std::int64_t minor, Currency currency);
|
||||
// The same with a plus on what came in, as a list of payments shows it: "+€12.50", "-€3.00", "€0.00".
|
||||
std::string SignedMoney(std::int64_t minor, Currency currency);
|
||||
|
||||
// The day a moment falls on in local time, as one number: moments of one day have the same.
|
||||
std::int64_t LocalDay(std::chrono::system_clock::time_point time);
|
||||
// That day as a list's heading: "Today", "Yesterday", "Monday 3 November", and with its year when that is not
|
||||
// `now`'s.
|
||||
std::string DayLabel(std::chrono::system_clock::time_point time, std::chrono::system_clock::time_point now);
|
||||
// "3 Nov", and with its year when that is not `now`'s.
|
||||
std::string ShortDate(std::chrono::system_clock::time_point time, std::chrono::system_clock::time_point now);
|
||||
// "14:05".
|
||||
std::string ClockTime(std::chrono::system_clock::time_point time);
|
||||
// "3 November 2026, 14:05".
|
||||
std::string FullTime(std::chrono::system_clock::time_point time);
|
||||
// "2026-11-03", as a date field takes it.
|
||||
std::string DateInput(std::chrono::system_clock::time_point time);
|
||||
|
||||
// What kind of payment the bank says one is ("MASTERCARD/PAYMENT"), in words; the bank's own for one not known.
|
||||
std::string KindLabel(std::string_view kind);
|
||||
// A status as the bank writes it ("PIN_TRIES_EXCEEDED"), in words: "Pin tries exceeded".
|
||||
std::string StatusLabel(std::string_view status);
|
||||
// A card's product ("MASTERCARD_DEBIT"), in words: "Mastercard debit".
|
||||
std::string CardProduct(std::string_view productType);
|
||||
// When a card expires, as its face has it: "11/30" for the bank's "2030-11-30"; anything else as the bank says it.
|
||||
std::string CardExpiry(std::string_view expiry);
|
||||
// When a scheduled payment runs: "Once on 3 Nov", "Every 2 weeks from 3 Nov until 1 Jan 2027".
|
||||
std::string ScheduleLabel(const BunqSchedule& schedule, std::chrono::system_clock::time_point now);
|
||||
|
||||
// A secret said without saying it: how it begins and its last four characters, enough to tell two keys apart.
|
||||
std::string Fingerprint(std::string_view secret);
|
||||
}
|
||||
175
interfaces/Nostro.App-Forms.cppm
Normal file
175
interfaces/Nostro.App-Forms.cppm
Normal file
|
|
@ -0,0 +1,175 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App:Forms;
|
||||
|
||||
import std;
|
||||
import Nostro;
|
||||
import Nostro.Ui;
|
||||
|
||||
// What the user fills in, and what it is read as. A form keeps its fields as they were typed and is checked as a
|
||||
// whole: each field's error is a line for under that field, and a form without errors gives an order, the values
|
||||
// nostrod is asked with. Nothing here talks to nostrod; nostrod checks everything again. The forms a payment, a
|
||||
// request and a filter are filled in on are any bank's; what a bank asks for to be connected is its own, and so is
|
||||
// the form for it, named for its bank.
|
||||
|
||||
export namespace Nostro::App {
|
||||
// What a field's text was read as, or why it could not be.
|
||||
template<typename T>
|
||||
struct Read {
|
||||
std::optional<T> value;
|
||||
std::string error;
|
||||
};
|
||||
|
||||
// An amount as a person writes it, "12,50", "12.50" or "12", in the currency's smallest unit; above zero.
|
||||
Read<std::int64_t> ReadAmount(std::string_view text, Currency currency);
|
||||
// Who is paid or asked: an IBAN, an e-mail address or a phone number starting with +.
|
||||
Read<BunqCounterparty> ReadParty(std::string_view text);
|
||||
// What kind of address that is, in words, for beside the field while it is typed; empty for none yet.
|
||||
std::string PartyKind(std::string_view text);
|
||||
// A day "YYYY-MM-DD" in local time, with a time "hh:mm" on it when one is given; the day's last moment when
|
||||
// `end` and no time is given.
|
||||
Read<std::chrono::system_clock::time_point> ReadDay(std::string_view day, std::string_view time = {}, bool end = false);
|
||||
// A whole number above zero.
|
||||
Read<std::uint32_t> ReadCount(std::string_view text);
|
||||
|
||||
// How often a scheduled payment runs, as the form offers it and as nostrod is told it.
|
||||
struct ScheduleUnit {
|
||||
std::string_view label;
|
||||
std::string_view unit;
|
||||
};
|
||||
inline constexpr std::array<ScheduleUnit, 6> scheduleUnits = {{
|
||||
{"Once", "ONCE"}, {"Hourly", "HOURLY"}, {"Daily", "DAILY"}, {"Weekly", "WEEKLY"}, {"Monthly", "MONTHLY"}, {"Yearly", "YEARLY"},
|
||||
}};
|
||||
|
||||
enum class When : std::uint8_t {
|
||||
Now,
|
||||
Scheduled,
|
||||
Draft // made now, paid once someone accepts it
|
||||
};
|
||||
|
||||
// A payment as nostrod is asked for it.
|
||||
struct PaymentOrder {
|
||||
BunqCounterparty to;
|
||||
std::int64_t amount = 0;
|
||||
std::string description;
|
||||
When when = When::Now;
|
||||
BunqSchedule schedule; // for When::Scheduled
|
||||
};
|
||||
|
||||
struct PaymentForm {
|
||||
Ui::EditState to{Ui::FieldKind::Plain};
|
||||
Ui::EditState name{Ui::FieldKind::Text};
|
||||
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||||
Ui::EditState description{Ui::FieldKind::Text};
|
||||
When when = When::Now;
|
||||
Ui::EditState day{Ui::FieldKind::Plain};
|
||||
Ui::EditState time{Ui::FieldKind::Plain};
|
||||
std::size_t unit = 0; // of scheduleUnits
|
||||
Ui::EditState every{Ui::FieldKind::Digits};
|
||||
Ui::EditState until{Ui::FieldKind::Plain};
|
||||
// Errors show once the user has tried to go on: an empty form is not a list of mistakes.
|
||||
bool tried = false;
|
||||
|
||||
struct Errors {
|
||||
std::string to;
|
||||
std::string name;
|
||||
std::string amount;
|
||||
std::string day;
|
||||
std::string time;
|
||||
std::string every;
|
||||
std::string until;
|
||||
bool Any() const noexcept;
|
||||
};
|
||||
// Every field's error, and into `order` what the form asks for when there is none.
|
||||
Errors Check(Currency currency, std::chrono::system_clock::time_point now, PaymentOrder& order) const;
|
||||
void Clear();
|
||||
};
|
||||
|
||||
// A request for money as nostrod is asked for it.
|
||||
struct RequestOrder {
|
||||
BunqCounterparty from;
|
||||
std::int64_t amount = 0;
|
||||
std::string description;
|
||||
};
|
||||
|
||||
struct RequestForm {
|
||||
Ui::EditState from{Ui::FieldKind::Plain};
|
||||
Ui::EditState name{Ui::FieldKind::Text};
|
||||
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||||
Ui::EditState description{Ui::FieldKind::Text};
|
||||
bool tried = false;
|
||||
|
||||
struct Errors {
|
||||
std::string from;
|
||||
std::string name;
|
||||
std::string amount;
|
||||
bool Any() const noexcept;
|
||||
};
|
||||
Errors Check(Currency currency, RequestOrder& order) const;
|
||||
void Clear();
|
||||
};
|
||||
|
||||
// Which of an account's payments are listed.
|
||||
struct FilterForm {
|
||||
Ui::EditState search{Ui::FieldKind::Text};
|
||||
Ui::EditState amount{Ui::FieldKind::Decimal};
|
||||
bool received = false; // the amount came in; it went out otherwise
|
||||
Ui::EditState from{Ui::FieldKind::Plain};
|
||||
Ui::EditState to{Ui::FieldKind::Plain};
|
||||
|
||||
struct Errors {
|
||||
std::string amount;
|
||||
std::string from;
|
||||
std::string to;
|
||||
bool Any() const noexcept;
|
||||
};
|
||||
// The errors, and into `query` what the form asks for when there is none; its limit is left as it is.
|
||||
Errors Check(Currency currency, TransactionQuery& query) const;
|
||||
// What is asked for besides the search, each as a few words: "Paid €12.50", "From 3 Nov".
|
||||
std::vector<std::string> Chips(Currency currency, std::chrono::system_clock::time_point now) const;
|
||||
// Empties everything but the search.
|
||||
void Reset();
|
||||
};
|
||||
|
||||
// A connection to bunq made with an API key. The key says itself which of bunq's environments it is of, by how it
|
||||
// starts, so there is none to choose: nostrod takes the key's own.
|
||||
struct BunqKeyForm {
|
||||
Ui::EditState key{Ui::FieldKind::Secret};
|
||||
Ui::EditState ips{Ui::FieldKind::Plain};
|
||||
bool advanced = false; // the allowed addresses are shown
|
||||
bool tried = false;
|
||||
|
||||
BunqKeyForm();
|
||||
// Why the form cannot be sent; empty when it can.
|
||||
std::string Check() const;
|
||||
void Clear();
|
||||
};
|
||||
|
||||
// A connection to bunq made through OAuth: the client's registration first, then the address the browser ends
|
||||
// up at.
|
||||
struct BunqOAuthForm {
|
||||
Ui::EditState clientId{Ui::FieldKind::Plain};
|
||||
Ui::EditState redirectUri{Ui::FieldKind::Plain};
|
||||
Ui::EditState secret{Ui::FieldKind::Secret};
|
||||
bool sandbox = false;
|
||||
Ui::EditState redirected{Ui::FieldKind::Plain};
|
||||
// Ties the browser's answer to this request; made when the address to open is.
|
||||
std::string state;
|
||||
std::string address; // where the user approves, once the first step is filled in
|
||||
bool tried = false;
|
||||
|
||||
struct Errors {
|
||||
std::string clientId;
|
||||
std::string redirectUri;
|
||||
std::string secret;
|
||||
bool Any() const noexcept;
|
||||
};
|
||||
Errors Check() const;
|
||||
// Makes the address to open, with a new state. The form must be without errors.
|
||||
void Begin(std::string newState);
|
||||
// The code in the address the browser was sent to; throws what BunqConnector::OAuthCodeFromRedirect does.
|
||||
std::string Code() const;
|
||||
void Clear();
|
||||
};
|
||||
}
|
||||
15
interfaces/Nostro.App.cppm
Normal file
15
interfaces/Nostro.App.cppm
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.App;
|
||||
|
||||
// Nostro's window as far as it can be without one: what the app knows, what the user fills in, how an error is
|
||||
// worded, and every screen, built with Nostro.Ui from what nostrod answers through the asynchronous Client. The
|
||||
// tests drive it against a real nostrod on a fake bus; a window (Nostro.Ui.Shell) only shows what it builds. What is
|
||||
// a bank's own is :Banks' to say and its forms' to ask for; nothing else in the app tells one bank from another.
|
||||
|
||||
export import :Banks;
|
||||
export import :Format;
|
||||
export import :Forms;
|
||||
export import :Errors;
|
||||
export import :App;
|
||||
62
interfaces/Nostro.DebugCommands.cppm
Normal file
62
interfaces/Nostro.DebugCommands.cppm
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.DebugCommands;
|
||||
|
||||
import std;
|
||||
import Crafter.Debug;
|
||||
import Nostro.Ui;
|
||||
import Nostro.Ui.Shell;
|
||||
import Nostro.App;
|
||||
|
||||
// What an agent can call, read and wait for in a build with --debug-api: the window driven by the names of what is on
|
||||
// it, what a screen says as text, and what the app is doing said as it happens. project.cpp builds this module only
|
||||
// then, so no other build has any of it, or of Crafter.Debug.
|
||||
//
|
||||
// A build with it can be driven and read by every program of the user's, and what it is told is kept in a log. It is
|
||||
// for the made-up system (`crafter-build … run fake`) and for bunq's sandbox, never for a real key: a field that hides
|
||||
// what it holds takes only a key of the sandbox's kind through here.
|
||||
|
||||
export namespace Nostro {
|
||||
// What the debugger acts on. All of it outlives the Debugger.
|
||||
struct DebugTargets {
|
||||
Ui::Shell::Loop& loop;
|
||||
Ui::Context& context;
|
||||
// Gone once it was closed while a change was under way.
|
||||
std::optional<Ui::Shell::Window>& window;
|
||||
// Nothing where the toolkit's gallery is shown in the app's place.
|
||||
App::App* app = nullptr;
|
||||
// A directory of the app's own, for a screenshot on its way to whoever asked.
|
||||
std::filesystem::path scratch;
|
||||
// Sets the look: "light", "dark", or "system" for the desktop's.
|
||||
std::function<void(std::string_view scheme)> setTheme;
|
||||
};
|
||||
|
||||
class Debugger {
|
||||
public:
|
||||
// Starts Crafter.Debug's session, and the thread pool its transport runs on, registers the commands and the
|
||||
// watches, and takes the window's and the app's hooks, after whoever had them. Throws what the session does.
|
||||
explicit Debugger(DebugTargets targets);
|
||||
// Hands the hooks back, and ends the session before the pool it runs on.
|
||||
~Debugger();
|
||||
Debugger(const Debugger&) = delete;
|
||||
Debugger& operator=(const Debugger&) = delete;
|
||||
|
||||
private:
|
||||
void Register();
|
||||
// Gives Crafter.Debug the loop's thread, and says what changed since the last time.
|
||||
void Pump();
|
||||
// The screens as they are now, to act on or to read; an error where the window is gone.
|
||||
Crafter::Debug::Result<> Ready();
|
||||
void Drawn();
|
||||
std::vector<std::byte> Capture();
|
||||
|
||||
DebugTargets targets;
|
||||
std::optional<Crafter::Debug::Session> session;
|
||||
Ui::Shell::Timer pump;
|
||||
std::function<void()> frameBefore;
|
||||
std::function<void(std::string_view, bool)> answerBefore;
|
||||
std::string screen; // as it was last said
|
||||
bool changing = false;
|
||||
};
|
||||
}
|
||||
310
interfaces/Nostro.Ui-Context.cppm
Normal file
310
interfaces/Nostro.Ui-Context.cppm
Normal file
|
|
@ -0,0 +1,310 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Context;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
import :Text;
|
||||
import :Edit;
|
||||
import :Gesture;
|
||||
import :Draw;
|
||||
import :Theme;
|
||||
import :Input;
|
||||
|
||||
// A window's screens without the window. A screen is built into a Frame: it draws into the frame's list and names
|
||||
// the regions that can be pressed, each with what a press does. The Context keeps what outlives a frame (the focus,
|
||||
// each list's scroll position, the press that is down, a menu that is open) and turns what the window reports, in
|
||||
// pixels and key names, into those regions' actions. Nothing here draws to a screen or reads a device, so a test
|
||||
// builds a frame, reads what it says and taps it by name.
|
||||
//
|
||||
// A region's action runs at once, from the event that asks for it, against the frame built last. So a frame must
|
||||
// still be true when it is used: whoever changes what the screens are built from calls Invalidate, which drops the
|
||||
// frame's regions (an event in between acts on nothing), and builds a new frame before the next event is read. An
|
||||
// action names what it acts on by value, an IBAN or an id, never by its place in a list that may be read again.
|
||||
// Before an action runs, the word an input method is still composing is taken into its field: the action reads every
|
||||
// field as the user sees it.
|
||||
//
|
||||
// What a sheet covers is out of reach, of a finger and of the keyboard alike: a frame that puts a sheet over what had
|
||||
// the focus takes the focus from it. So a button whose press brings a sheet, such as one that sends something and then
|
||||
// shows it under way, cannot be pressed again from under that sheet, by a second Enter either.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
// What a region is: what a press on it means, and what is under it.
|
||||
enum class Role : std::uint8_t {
|
||||
Button, // acts when tapped
|
||||
Row, // a list's row: acts when tapped, may open a menu when held
|
||||
Field, // a text field
|
||||
Scroll, // an area whose content a drag or the wheel moves; what is drawn in it is above it
|
||||
Scrim, // what a sheet covers: a tap there closes the sheet, and nothing under it is reached
|
||||
Panel // a sheet's or a bar's own ground: inert, and nothing under it is reached
|
||||
};
|
||||
|
||||
// Where a field's text sits, for the tap that places the cursor and for the input method.
|
||||
struct FieldGeometry {
|
||||
float textX = 0; // where the first code point shown starts
|
||||
FontFace face = FontFace::Regular;
|
||||
float px = 0;
|
||||
std::size_t first = 0; // the byte of the shown text the field starts drawing at, when it is too long for it
|
||||
bool masked = false; // drawn as one dot per code point
|
||||
Box caret;
|
||||
TextPurpose purpose = TextPurpose::Normal;
|
||||
std::uint32_t hints = TextHint::none;
|
||||
};
|
||||
|
||||
struct Region {
|
||||
Box box;
|
||||
std::string key; // names it across frames: the focus, a press and a test go by it
|
||||
Role role = Role::Button;
|
||||
std::string label; // what it says, for whoever lists a screen's controls
|
||||
bool enabled = true;
|
||||
bool focusable = false; // reached with Tab
|
||||
std::function<void()> onTap;
|
||||
std::function<void()> onLongPress; // held, or the secondary button
|
||||
std::function<bool(Key, Modifiers)> onKey; // a key while it has the focus; true when it took it
|
||||
// A field: what it shows changed. Not called when the word being composed is taken into the field's value, which
|
||||
// changes nothing of what it shows; an action reads its fields when it runs, and finds them whole (see Context).
|
||||
std::function<void()> onChange;
|
||||
std::function<void()> onSubmit; // a field: Enter
|
||||
std::function<void()> onRefresh; // a scrolling area: pulled down at its top, and let go
|
||||
EditState* edit = nullptr; // a field's text
|
||||
FieldGeometry field;
|
||||
// Set as the frame takes it:
|
||||
Box hit; // the part of the box that can be pressed: what its scrolling area shows of it
|
||||
Layer layer = Layer::Background;
|
||||
std::string scroll; // the scrolling area it is in, if any,
|
||||
float scrollTop = 0; // and where that area's viewport begins
|
||||
};
|
||||
|
||||
// The text a field shows: what it holds with the composing text at the cursor, or a dot for each of its code points.
|
||||
std::string FieldText(const EditState& edit, bool masked);
|
||||
// Whether a caret moved only because the window grew under it: its box as it was but for its place, which went
|
||||
// down by what the window gained. That is a field at the window's lower edge after the on-screen keyboard gave its
|
||||
// room back (the keyboard's own hide key). Told at once, the move would be a new text-input state, and a state
|
||||
// committed while the input is on brings the keyboard straight back (seen on Plasma Mobile), so it is held until the
|
||||
// caret moves for another reason.
|
||||
bool CaretFollowedGrowth(Box told, float toldHeight, Box caret, float height) noexcept;
|
||||
|
||||
struct MenuItem {
|
||||
std::string label;
|
||||
std::function<void()> action;
|
||||
bool danger = false;
|
||||
};
|
||||
|
||||
class Context;
|
||||
|
||||
// One frame, as a screen builds it.
|
||||
class Frame {
|
||||
public:
|
||||
Frame(Context& context, Box bounds, float scale, std::int64_t nowMs);
|
||||
|
||||
Context& context;
|
||||
DrawList draw;
|
||||
Box bounds; // the window
|
||||
Metrics metrics;
|
||||
SizeClass sizeClass;
|
||||
std::int64_t nowMs;
|
||||
|
||||
float Dp(float dp) const noexcept;
|
||||
const Theme& Colors() const noexcept;
|
||||
const TextMeasure& Measure() const noexcept;
|
||||
WordWrapper& Wrapper() noexcept;
|
||||
|
||||
// A region, in the layer being drawn and the scrolling area that is open.
|
||||
void Add(Region region);
|
||||
// A key that acts whatever has the focus, unless that took the key itself. The one added last wins, and a scrim
|
||||
// added after it takes it away: a sheet's keys are its own.
|
||||
void Shortcut(Key key, Modifiers modifiers, std::function<void()> action);
|
||||
// Opens a scrolling area: what is drawn and added until EndScroll is cut to `viewport` and moves with the area's
|
||||
// offset, which is returned, in whole pixels. EndScroll says how tall the content turned out to be.
|
||||
float BeginScroll(std::string_view key, Box viewport);
|
||||
void EndScroll(float contentHeight);
|
||||
// Between the two: the area that is open can be read again by a finger. Dragged down while it is at its top it
|
||||
// gives way, by half the finger's way, and let go at Size::pull or further it runs `refresh`.
|
||||
void Refreshable(std::function<void()> refresh);
|
||||
// How far the area that is open is pulled down, in pixels: its content is drawn that much lower, and what shows
|
||||
// above it says what letting go will do.
|
||||
float Pulled() const noexcept;
|
||||
|
||||
bool Hovered(std::string_view key) const noexcept;
|
||||
bool Pressed(std::string_view key) const noexcept;
|
||||
bool Focused(std::string_view key) const noexcept;
|
||||
// Whether the focus is to be shown on something that is no field: it came by the keyboard.
|
||||
bool FocusRing(std::string_view key) const noexcept;
|
||||
// The caret's blink: whether it is in its lit half.
|
||||
bool CaretOn() const noexcept;
|
||||
|
||||
struct KeyAction {
|
||||
Key key = Key::None;
|
||||
Modifiers modifiers;
|
||||
std::function<void()> action;
|
||||
};
|
||||
std::vector<Region> regions;
|
||||
std::vector<KeyAction> shortcuts;
|
||||
|
||||
private:
|
||||
struct OpenScroll {
|
||||
std::string key;
|
||||
Box viewport;
|
||||
};
|
||||
std::vector<OpenScroll> scrolls;
|
||||
};
|
||||
|
||||
class Context {
|
||||
public:
|
||||
// What the window is to do after an event or a frame.
|
||||
struct Wants {
|
||||
bool redraw = false; // what is shown is no longer what a frame would draw
|
||||
bool animating = false; // and will not be at the next frame either: a list is moving
|
||||
std::optional<std::int64_t> wakeAtMs; // call Tick then, if nothing comes before
|
||||
TextInputRequest textInput;
|
||||
std::optional<std::string> copy; // put this on the clipboard
|
||||
bool paste = false; // fetch the clipboard and hand it to Paste
|
||||
};
|
||||
|
||||
explicit Context(TextMeasure measure);
|
||||
Context(const Context&) = delete;
|
||||
Context& operator=(const Context&) = delete;
|
||||
|
||||
Theme theme;
|
||||
|
||||
// A frame to build into, and its end: the regions it named are the ones events act on from here.
|
||||
Frame& Begin(Box bounds, float scale, std::int64_t nowMs);
|
||||
void End();
|
||||
// Whether a frame was ended since the last Invalidate.
|
||||
bool Built() const noexcept;
|
||||
// The frame ended last: what the window shows.
|
||||
const Frame& Shown() const;
|
||||
// Its regions, from the bottom up.
|
||||
std::span<const Region> Regions() const noexcept;
|
||||
const Region* Find(std::string_view key) const noexcept;
|
||||
|
||||
// What the screens are built from changed: the regions go, with the press that is down and a menu that is open.
|
||||
void Invalidate();
|
||||
void Redraw() noexcept;
|
||||
|
||||
// -- what the window reports ---------------------------------------------------------------------------------
|
||||
void Pointer(PointerPhase phase, Point at, std::int64_t tMs);
|
||||
// The pointer moved with no button down, or left the window: what it is over is drawn so.
|
||||
void Hover(std::optional<Point> at);
|
||||
// The secondary button: what a long press does.
|
||||
void Secondary(Point at);
|
||||
// Wheel motion, in detents: positive scrolls down.
|
||||
void Wheel(Point at, float detents);
|
||||
// A key went down; `repeat` when it is still held from before. A held key edits and moves on, and acts once.
|
||||
void KeyDown(Key key, Modifiers modifiers, bool repeat = false);
|
||||
// Text a key typed or the input method committed, its composing text, and its deletion around the cursor.
|
||||
void Text(std::string_view utf8);
|
||||
void Preedit(std::string_view utf8);
|
||||
void DeleteAround(std::size_t beforeBytes, std::size_t afterBytes);
|
||||
// The clipboard's text, asked for through Wants::paste.
|
||||
void Paste(std::string_view utf8);
|
||||
// Time passed: a held press becomes a long press, the caret blinks, a flung list moves on, a notice goes.
|
||||
void Tick(std::int64_t nowMs);
|
||||
|
||||
// -- for a screen, a test or a debugger ----------------------------------------------------------------------
|
||||
// As a tap, or a long press, on the region of that name; false when there is none that can take it.
|
||||
bool Tap(std::string_view key);
|
||||
bool LongPress(std::string_view key);
|
||||
// Gives a region the focus, as Tab reaching it would; it need not be built yet. An empty key takes the focus away.
|
||||
void Focus(std::string_view key);
|
||||
std::string_view Focused() const noexcept;
|
||||
// Where the press is that the action now running answers to: where a menu it opens belongs. The middle of the
|
||||
// region for an action that a key or a name asked for.
|
||||
Point Pointed() const noexcept;
|
||||
// A list of choices at a point, above everything, until one is taken or it is dismissed.
|
||||
void OpenMenu(Point at, std::vector<MenuItem> items);
|
||||
void CloseMenu();
|
||||
bool MenuOpen() const noexcept;
|
||||
// A line shown over the window's bottom for a moment.
|
||||
void Notice(std::string text, std::int64_t forMs = 2500);
|
||||
void Copy(std::string text);
|
||||
// The input method starts afresh on the field that has the focus: its text was replaced by something else than
|
||||
// its own edits.
|
||||
void RestartInput() noexcept;
|
||||
ScrollState& Scroll(std::string_view key);
|
||||
// The time of the last Tick or frame: what "in a moment" is counted from.
|
||||
std::int64_t Now() const noexcept;
|
||||
// Runs `action` once from the Tick that reaches `atMs`, which the window is woken for: what a screen puts off, as
|
||||
// a search until the typing has stopped. One of the same name that was still waiting is replaced, and Forget
|
||||
// drops one.
|
||||
void At(std::string_view name, std::int64_t atMs, std::function<void()> action);
|
||||
void Forget(std::string_view name);
|
||||
|
||||
// What the window is to do, taken out: the next call says only what came since.
|
||||
Wants Take();
|
||||
|
||||
const TextMeasure& Measure() const noexcept;
|
||||
WordWrapper& Wrapper() noexcept;
|
||||
|
||||
private:
|
||||
friend class Frame;
|
||||
|
||||
const Region* HitTest(Point at) const noexcept;
|
||||
const Region* ScrollUnder(Point at) const noexcept;
|
||||
const Region* FocusedRegion() const noexcept;
|
||||
bool UnderSheet(const Region& region) const noexcept;
|
||||
void Stale();
|
||||
void Handle(const GestureEvent& event);
|
||||
void Activate(const Region& region, Point at);
|
||||
void Held(const Region& region, Point at);
|
||||
void TapField(const Region& region, Point at);
|
||||
void FieldMenu(const Region& region, Point at);
|
||||
void EndComposition();
|
||||
void MoveFocus(std::string_view key, bool byKeyboard);
|
||||
void StepFocus(std::int32_t by);
|
||||
void Reveal(const Region& region);
|
||||
void Edited(const Region& field);
|
||||
bool EditKeyDown(const Region& field, Key key, Modifiers modifiers);
|
||||
void ShowCaret() noexcept;
|
||||
void DrawMenu(Frame& frame);
|
||||
void DrawNotice(Frame& frame);
|
||||
void UpdateTextInput(float windowHeight);
|
||||
float Dp(float dp) const noexcept;
|
||||
|
||||
WordWrapper wrapper;
|
||||
std::optional<Frame> frame; // the frame being built, then the one shown
|
||||
std::vector<Region> regions; // the shown frame's, from the bottom up
|
||||
std::vector<Frame::KeyAction> shortcuts;
|
||||
bool built = false;
|
||||
float scale = 1;
|
||||
Box window;
|
||||
float builtHeight = 0; // the window's height at the frame before
|
||||
std::int64_t now = 0;
|
||||
std::int64_t eventTime = 0; // the time of the pointer event being handled
|
||||
|
||||
GestureDetector gestures;
|
||||
std::string pressedKey; // what the press that is down went down on
|
||||
std::string scrollKey; // the scrolling area under it
|
||||
float pull = 0; // how far that area is pulled down past its top
|
||||
bool pressTaken = false; // the press became a long press or a drag: its release is no tap
|
||||
std::string hoverKey;
|
||||
Point pointed; // where the press is that the running action answers to
|
||||
std::string focusKey;
|
||||
bool focusByKeyboard = false;
|
||||
std::map<std::string, ScrollState, std::less<>> scrolls;
|
||||
std::int64_t lastTick = 0;
|
||||
std::int64_t caretSince = 0; // when the caret was last shown at once: it blinks from there
|
||||
std::int64_t caretPhase = 0; // the half second of its blink the last tick found it in
|
||||
static constexpr std::int64_t caretIdleMs = 10000; // it stops blinking so long after, lit
|
||||
|
||||
struct Menu {
|
||||
Point at;
|
||||
std::vector<MenuItem> items;
|
||||
};
|
||||
std::optional<Menu> menu;
|
||||
std::string notice;
|
||||
std::int64_t noticeUntil = 0;
|
||||
struct Later {
|
||||
std::int64_t atMs = 0;
|
||||
std::function<void()> action;
|
||||
};
|
||||
std::map<std::string, Later, std::less<>> laters;
|
||||
|
||||
std::size_t inputSerial = 1; // one per time a field's input starts afresh
|
||||
TextInputRequest told; // what the window was last asked for
|
||||
float toldHeight = 0; // the window's height then: lower since, the on-screen keyboard is up
|
||||
bool keyboardGone = false; // taller since: the keyboard gave its room back while the field kept the focus
|
||||
Wants wants;
|
||||
};
|
||||
}
|
||||
120
interfaces/Nostro.Ui-Draw.cppm
Normal file
120
interfaces/Nostro.Ui-Draw.cppm
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Draw;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
import :Text;
|
||||
|
||||
// What a frame draws, as a list the window turns into its own items and the tests read: rounded boxes, circles and
|
||||
// lines of text, in layers. The window library draws a batch in one pass in which every shape comes before every
|
||||
// text, so a shape meant to cover text drawn earlier needs a pass of its own; and a pass is cut to one box. The list
|
||||
// keeps the order things were drawn in by starting a new batch where one pass could not show it: where the clip
|
||||
// changes, and where a shape would land on text already in the batch. Each batch is a pass over the whole window, so
|
||||
// a screen is drawn with few of them: its layers, and a clip per scrolling area.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
enum class TextAlign : std::uint8_t {
|
||||
Left, // x is where the text starts
|
||||
Center, // x is its middle
|
||||
Right // x is where it ends
|
||||
};
|
||||
|
||||
// Drawn in this order, whatever order a screen draws them in.
|
||||
enum class Layer : std::uint8_t {
|
||||
Background,
|
||||
Content, // what scrolls
|
||||
Chrome, // the bars around it
|
||||
Overlay, // a sheet or a dialog, and the scrim under it
|
||||
Popup // a menu or a notice, above everything
|
||||
};
|
||||
inline constexpr std::size_t layerCount = 5;
|
||||
|
||||
// A box's corner radii, clockwise from the top left.
|
||||
struct Corners {
|
||||
float topLeft = 0;
|
||||
float topRight = 0;
|
||||
float bottomRight = 0;
|
||||
float bottomLeft = 0;
|
||||
|
||||
static constexpr Corners All(float radius) noexcept {
|
||||
return {radius, radius, radius, radius};
|
||||
}
|
||||
static constexpr Corners Top(float radius) noexcept {
|
||||
return {radius, radius, 0, 0};
|
||||
}
|
||||
friend bool operator==(const Corners&, const Corners&) = default;
|
||||
};
|
||||
|
||||
struct RectItem {
|
||||
Box box;
|
||||
Color color;
|
||||
Corners corners;
|
||||
float outline = 0; // the width of a line along its edge, inside the box
|
||||
Color outlineColor;
|
||||
};
|
||||
|
||||
struct CircleItem {
|
||||
Point center;
|
||||
float radius = 0;
|
||||
Color color;
|
||||
float outline = 0;
|
||||
Color outlineColor;
|
||||
};
|
||||
|
||||
struct TextItem {
|
||||
std::string text;
|
||||
float x = 0;
|
||||
float baseline = 0;
|
||||
FontFace face = FontFace::Regular;
|
||||
float px = 0;
|
||||
Color color;
|
||||
TextAlign align = TextAlign::Left;
|
||||
Box bounds; // the line's box: its width, from its ascent to a line's height below that
|
||||
};
|
||||
|
||||
// One pass: the shapes, then the text over them, all cut to `clip`.
|
||||
struct Batch {
|
||||
std::optional<Box> clip; // nothing: the whole window
|
||||
std::vector<RectItem> rects;
|
||||
std::vector<CircleItem> circles;
|
||||
std::vector<TextItem> texts;
|
||||
};
|
||||
|
||||
class DrawList {
|
||||
public:
|
||||
// `bounds` is the window: what lies outside it, or outside the clip, is not kept.
|
||||
DrawList(const TextMeasure& measure, Box bounds);
|
||||
|
||||
// What is drawn from here on goes to this layer.
|
||||
void SetLayer(Layer layer) noexcept;
|
||||
Layer CurrentLayer() const noexcept;
|
||||
// Cuts what is drawn from here on, in any layer, to `box` within the clip there was, until the matching PopClip.
|
||||
void PushClip(Box box);
|
||||
void PopClip();
|
||||
|
||||
void Rect(const RectItem& item);
|
||||
void Rect(Box box, Color color, float radius = 0);
|
||||
void Circle(const CircleItem& item);
|
||||
void Circle(Point center, float radius, Color color);
|
||||
// One line; returns its width.
|
||||
float Text(std::string_view text, float x, float baseline, FontFace face, float px, Color color, TextAlign align = TextAlign::Left);
|
||||
|
||||
std::span<const Batch> Batches(Layer layer) const noexcept;
|
||||
std::size_t BatchCount() const noexcept;
|
||||
// Every line of text in the list, in the order it is drawn: what a screen says.
|
||||
std::vector<std::string> Texts() const;
|
||||
|
||||
private:
|
||||
// The batch a shape that covers `box`, or a text, goes into.
|
||||
Batch& BatchFor(std::optional<Box> shape);
|
||||
bool Visible(Box box) const noexcept;
|
||||
|
||||
const TextMeasure* measure;
|
||||
Box bounds;
|
||||
Layer layer = Layer::Background;
|
||||
std::vector<Box> clips;
|
||||
std::array<std::vector<Batch>, layerCount> layers;
|
||||
};
|
||||
}
|
||||
86
interfaces/Nostro.Ui-Edit.cppm
Normal file
86
interfaces/Nostro.Ui-Edit.cppm
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
// 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);
|
||||
};
|
||||
}
|
||||
126
interfaces/Nostro.Ui-Geometry.cppm
Normal file
126
interfaces/Nostro.Ui-Geometry.cppm
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Geometry;
|
||||
|
||||
import std;
|
||||
|
||||
// Points, boxes and colours, and the two things that decide how a screen is laid out: the scale that turns the design's
|
||||
// sizes into the window's pixels, and how wide the window is. Everything the toolkit positions is in the window's
|
||||
// physical pixels; a size in the design's units (dp) exists only as a constant, turned into pixels by Metrics::Dp.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
struct Point {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
friend bool operator==(const Point&, const Point&) = default;
|
||||
};
|
||||
|
||||
// Space taken off a box's sides.
|
||||
struct Insets {
|
||||
float left = 0;
|
||||
float top = 0;
|
||||
float right = 0;
|
||||
float bottom = 0;
|
||||
|
||||
static constexpr Insets All(float each) noexcept {
|
||||
return {each, each, each, each};
|
||||
}
|
||||
static constexpr Insets Symmetric(float horizontal, float vertical) noexcept {
|
||||
return {horizontal, vertical, horizontal, vertical};
|
||||
}
|
||||
};
|
||||
|
||||
struct Box {
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
float w = 0;
|
||||
float h = 0;
|
||||
|
||||
constexpr float Right() const noexcept {
|
||||
return x + w;
|
||||
}
|
||||
constexpr float Bottom() const noexcept {
|
||||
return y + h;
|
||||
}
|
||||
constexpr Point Center() const noexcept {
|
||||
return {x + w / 2, y + h / 2};
|
||||
}
|
||||
constexpr bool Empty() const noexcept {
|
||||
return w <= 0 || h <= 0;
|
||||
}
|
||||
// The left and top edges are inside, the right and bottom ones are not, so boxes that share an edge share no point.
|
||||
constexpr bool Contains(Point p) const noexcept {
|
||||
return p.x >= x && p.x < x + w && p.y >= y && p.y < y + h;
|
||||
}
|
||||
// The box within the insets; one they leave nothing of is empty at its place, never inside out.
|
||||
constexpr Box Inset(Insets by) const noexcept {
|
||||
return {x + by.left, y + by.top, std::max(0.0f, w - by.left - by.right), std::max(0.0f, h - by.top - by.bottom)};
|
||||
}
|
||||
// What two boxes share, or an empty box when they share nothing.
|
||||
constexpr Box Intersect(Box other) const noexcept {
|
||||
const float left = std::max(x, other.x);
|
||||
const float top = std::max(y, other.y);
|
||||
const float right = std::min(Right(), other.Right());
|
||||
const float bottom = std::min(Bottom(), other.Bottom());
|
||||
if(right <= left || bottom <= top) {
|
||||
return {};
|
||||
}
|
||||
return {left, top, right - left, bottom - top};
|
||||
}
|
||||
constexpr bool Overlaps(Box other) const noexcept {
|
||||
return !Intersect(other).Empty();
|
||||
}
|
||||
friend bool operator==(const Box&, const Box&) = default;
|
||||
};
|
||||
|
||||
// Red, green, blue and opacity, each from 0 to 1.
|
||||
struct Color {
|
||||
float r = 0;
|
||||
float g = 0;
|
||||
float b = 0;
|
||||
float a = 1;
|
||||
|
||||
constexpr Color WithAlpha(float alpha) const noexcept {
|
||||
return {r, g, b, alpha};
|
||||
}
|
||||
// This colour with `over` laid on it at `amount` of its strength: how a pressed or hovered control is tinted.
|
||||
constexpr Color Mixed(Color over, float amount) const noexcept {
|
||||
return {r + (over.r - r) * amount, g + (over.g - g) * amount, b + (over.b - b) * amount, a};
|
||||
}
|
||||
// How bright the eye takes it for, from 0 to 1: what decides whether text on it is dark or light.
|
||||
constexpr float Luminance() const noexcept {
|
||||
return 0.2126f * r + 0.7152f * g + 0.0722f * b;
|
||||
}
|
||||
friend bool operator==(const Color&, const Color&) = default;
|
||||
};
|
||||
|
||||
// The window's scale: how many of its pixels one of the design's units (dp) is.
|
||||
struct Metrics {
|
||||
float scale = 1;
|
||||
|
||||
// A size of the design in whole pixels, so edges land on the pixel grid and stay sharp.
|
||||
float Dp(float dp) const noexcept {
|
||||
return std::round(dp * scale);
|
||||
}
|
||||
};
|
||||
|
||||
// How much room a window gives its screens: a phone held upright, a phone on its side or a narrow window, a desktop.
|
||||
enum class SizeClass : std::uint8_t {
|
||||
Compact,
|
||||
Medium,
|
||||
Expanded
|
||||
};
|
||||
|
||||
inline constexpr float mediumFromDp = 600;
|
||||
inline constexpr float expandedFromDp = 900;
|
||||
|
||||
// The class of a window `widthPx` pixels wide at `scale`.
|
||||
constexpr SizeClass SizeClassFor(float widthPx, float scale) noexcept {
|
||||
const float dp = widthPx / (scale > 0 ? scale : 1.0f);
|
||||
if(dp < mediumFromDp) {
|
||||
return SizeClass::Compact;
|
||||
}
|
||||
return dp < expandedFromDp ? SizeClass::Medium : SizeClass::Expanded;
|
||||
}
|
||||
}
|
||||
124
interfaces/Nostro.Ui-Gesture.cppm
Normal file
124
interfaces/Nostro.Ui-Gesture.cppm
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Gesture;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
|
||||
// What a finger or a mouse did, decided from raw pointer phases; the scroll physics of a list; and the frames a
|
||||
// window that draws only on demand still owes. Pure arithmetic over points and times, so the tests feed it by hand.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
enum class PointerPhase : std::uint8_t {
|
||||
Down,
|
||||
Move,
|
||||
Up,
|
||||
Cancel // the compositor took the touch for a gesture of its own: nothing was tapped
|
||||
};
|
||||
|
||||
struct GestureEvent {
|
||||
enum class Kind : std::uint8_t {
|
||||
None,
|
||||
Tap,
|
||||
LongPress,
|
||||
DragStart,
|
||||
Drag,
|
||||
Fling,
|
||||
Release
|
||||
};
|
||||
Kind kind = Kind::None;
|
||||
Point at; // the press for Tap, LongPress and DragStart; the current position for Drag, Fling and Release
|
||||
Point delta; // DragStart and Drag: the movement since the previous event
|
||||
Point velocity; // Fling: pixels per second
|
||||
};
|
||||
|
||||
// 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, 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 landed on has no long press, a press
|
||||
// held as long is still a tap at its release.
|
||||
class GestureDetector {
|
||||
public:
|
||||
float slopPx = 8;
|
||||
std::int64_t longPressMs = 500;
|
||||
float flingMinPxPerS = 60;
|
||||
// Whether the press that follows can be a long press: the host's word, before each Down.
|
||||
bool longPress = true;
|
||||
|
||||
GestureEvent Feed(PointerPhase phase, Point p, std::int64_t tMs);
|
||||
// The host's timer at Down + longPressMs.
|
||||
GestureEvent Timeout(std::int64_t tMs);
|
||||
bool Pressed() const noexcept;
|
||||
bool Dragging() const noexcept;
|
||||
// When the timer for the press that is down is due; nothing while no press waits for one.
|
||||
std::optional<std::int64_t> LongPressDue() const noexcept;
|
||||
|
||||
private:
|
||||
struct Sample {
|
||||
Point p;
|
||||
std::int64_t tMs;
|
||||
};
|
||||
|
||||
GestureEvent LongPressed();
|
||||
void Forget(std::int64_t tMs);
|
||||
|
||||
bool pressed = false;
|
||||
bool dragging = false;
|
||||
bool longPressed = false;
|
||||
Point down;
|
||||
Point last;
|
||||
std::int64_t downAt = 0;
|
||||
std::vector<Sample> samples;
|
||||
};
|
||||
|
||||
// A scrollable extent: the offset is how far the content moved up.
|
||||
struct ScrollState {
|
||||
float offset = 0;
|
||||
float velocity = 0; // pixels per second, positive scrolls further down
|
||||
float content = 0;
|
||||
float viewport = 0;
|
||||
|
||||
float Max() const noexcept;
|
||||
void Clamp() noexcept;
|
||||
void ScrollBy(float dy) noexcept;
|
||||
void Fling(float pxPerS) noexcept;
|
||||
// Advances the fling by dt seconds; true while still moving.
|
||||
bool Tick(float dt) noexcept;
|
||||
// Scrolls as little as it takes to bring the span from `top` to `bottom` of the content into view, `margin`
|
||||
// clear of the viewport's edges where the content allows it: what keeps a field that got the focus on screen
|
||||
// when the on-screen keyboard takes half the window. True when that moved anything.
|
||||
bool EnsureVisible(float top, float bottom, float margin) noexcept;
|
||||
};
|
||||
|
||||
// The time of a pointer or touch event on the host's own clock. The compositor stamps each event with the time it
|
||||
// happened, in milliseconds from a base of its own, wrapping at 2^32; events queued while the loop was busy are
|
||||
// dispatched together when it comes back, so by the host's clock at dispatch a swipe's backlog is a burst, read as a
|
||||
// fling at many times the finger's speed, and a press and its release that both waited out a stall happen at once.
|
||||
// The stamps, unwrapped, tick like the host's clock a constant apart, and no event is dispatched before it
|
||||
// happened: the smallest difference seen is the nearest to that constant.
|
||||
struct EventClock {
|
||||
// `stamp` is the compositor's time of the event, 0 when it gave none; `hostNowMs` the host's clock now.
|
||||
std::int64_t At(std::uint32_t stamp, std::int64_t hostNowMs) noexcept;
|
||||
|
||||
private:
|
||||
std::uint32_t lastStamp = 0;
|
||||
std::int64_t unwrapped = 0;
|
||||
std::optional<std::int64_t> ahead; // the host's clock minus the unwrapped stamps
|
||||
};
|
||||
|
||||
// The frames a screen owes the font atlas. A glyph that found no cell drew blank and gets one when a later frame
|
||||
// begins, from the glyphs not drawn in the last `patience` frames (the frames in flight plus one), and a frame comes
|
||||
// only when asked for: Again(waiting) after each frame asks for another while glyphs wait, until `patience` frames in
|
||||
// a row gained no cell, when the screen needs more cells than the atlas holds. A change in the count (a gain, a
|
||||
// screen change) starts the count over; Reset does on any other change of what is drawn.
|
||||
struct FrameWait {
|
||||
std::uint32_t patience = 4;
|
||||
std::uint32_t last = 0; // the previous frame's waiting glyphs
|
||||
std::uint32_t gainless = 0; // frames in a row that gained no cell
|
||||
|
||||
bool Again(std::uint32_t waiting) noexcept;
|
||||
void Reset() noexcept;
|
||||
};
|
||||
}
|
||||
82
interfaces/Nostro.Ui-Input.cppm
Normal file
82
interfaces/Nostro.Ui-Input.cppm
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Input;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
|
||||
// What the window tells the toolkit of the keyboard, and what the toolkit tells the window of the text field that
|
||||
// has the focus, in terms of neither: the window's own key codes and the compositor's protocol stay on its side.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
// The keys the toolkit acts on. A letter is the key that types it on the user's layout, not a place on the keyboard.
|
||||
enum class Key : std::uint8_t {
|
||||
None,
|
||||
Enter,
|
||||
Escape,
|
||||
Tab,
|
||||
Space,
|
||||
Backspace,
|
||||
Delete,
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
Home,
|
||||
End,
|
||||
PageUp,
|
||||
PageDown,
|
||||
F5,
|
||||
A,
|
||||
C,
|
||||
F,
|
||||
R,
|
||||
V,
|
||||
X
|
||||
};
|
||||
|
||||
struct Modifiers {
|
||||
bool shift = false;
|
||||
bool control = false;
|
||||
friend bool operator==(const Modifiers&, const Modifiers&) = default;
|
||||
};
|
||||
|
||||
// What a text field holds, by which an on-screen keyboard picks its layout.
|
||||
enum class TextPurpose : std::uint8_t {
|
||||
Normal,
|
||||
Digits, // a whole number
|
||||
Number, // a number with a decimal separator
|
||||
Phone,
|
||||
Url,
|
||||
Email,
|
||||
Name,
|
||||
Password
|
||||
};
|
||||
|
||||
// How an input method treats what is typed, or-ed together. The values are text-input-v3's content hints, so the
|
||||
// window passes them on as they are.
|
||||
namespace TextHint {
|
||||
inline constexpr std::uint32_t none = 0x0;
|
||||
inline constexpr std::uint32_t completion = 0x1;
|
||||
inline constexpr std::uint32_t spellcheck = 0x2;
|
||||
inline constexpr std::uint32_t autoCapitalization = 0x4;
|
||||
inline constexpr std::uint32_t lowercase = 0x8;
|
||||
inline constexpr std::uint32_t hiddenText = 0x40; // show nothing of what is typed
|
||||
inline constexpr std::uint32_t sensitiveData = 0x80; // store and learn nothing of it
|
||||
inline constexpr std::uint32_t prose = completion | spellcheck | autoCapitalization;
|
||||
inline constexpr std::uint32_t secret = hiddenText | sensitiveData;
|
||||
}
|
||||
|
||||
// The text field that has the focus, for the compositor's input method: whether there is one, where its caret is
|
||||
// (an on-screen keyboard keeps clear of it), and what it holds. `field` tells one field from the next: when it
|
||||
// changes, the input method starts afresh.
|
||||
struct TextInputRequest {
|
||||
bool active = false;
|
||||
Box caret;
|
||||
TextPurpose purpose = TextPurpose::Normal;
|
||||
std::uint32_t hints = TextHint::none;
|
||||
std::size_t field = 0;
|
||||
friend bool operator==(const TextInputRequest&, const TextInputRequest&) = default;
|
||||
};
|
||||
}
|
||||
73
interfaces/Nostro.Ui-Layout.cppm
Normal file
73
interfaces/Nostro.Ui-Layout.cppm
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Layout;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
|
||||
// Boxes handed out one after the other: all the layout a screen of rows and forms needs. A screen says how tall each
|
||||
// thing is, or asks the thing itself, and the cursor moves on.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
// Down a column: each box is as wide as the column and starts where the one before ended, plus the gap.
|
||||
struct Column {
|
||||
Box area;
|
||||
float gap = 0;
|
||||
float cursor = 0; // the top of the next box
|
||||
|
||||
constexpr Column(Box area, float gap = 0) noexcept : area(area), gap(gap), cursor(area.y) {
|
||||
}
|
||||
|
||||
// The box the next thing may use: from the cursor down to the area's end.
|
||||
constexpr Box Next() const noexcept {
|
||||
return {area.x, cursor, area.w, std::max(0.0f, area.Bottom() - cursor)};
|
||||
}
|
||||
// The next thing turned out this tall.
|
||||
constexpr void Advance(float height) noexcept {
|
||||
cursor += height + gap;
|
||||
}
|
||||
// The next box, `height` tall. It may reach past the area's end: a column that scrolls is as long as its content.
|
||||
constexpr Box Take(float height) noexcept {
|
||||
const Box box{area.x, cursor, area.w, height};
|
||||
Advance(height);
|
||||
return box;
|
||||
}
|
||||
// How tall everything taken so far is, without the gap after the last.
|
||||
constexpr float Used() const noexcept {
|
||||
return std::max(0.0f, cursor - gap - area.y);
|
||||
}
|
||||
};
|
||||
|
||||
// Along a row: boxes are taken from its left or its right end, and what is left between them is the rest.
|
||||
struct Row {
|
||||
Box area;
|
||||
float gap = 0;
|
||||
float left = 0;
|
||||
float right = 0;
|
||||
|
||||
constexpr Row(Box area, float gap = 0) noexcept : area(area), gap(gap), left(area.x), right(area.Right()) {
|
||||
}
|
||||
|
||||
constexpr Box TakeLeft(float width) noexcept {
|
||||
const Box box{left, area.y, width, area.h};
|
||||
left += width + gap;
|
||||
return box;
|
||||
}
|
||||
constexpr Box TakeRight(float width) noexcept {
|
||||
right -= width;
|
||||
const Box box{right, area.y, width, area.h};
|
||||
right -= gap;
|
||||
return box;
|
||||
}
|
||||
constexpr Box Rest() const noexcept {
|
||||
return {left, area.y, std::max(0.0f, right - left), area.h};
|
||||
}
|
||||
};
|
||||
|
||||
// A box at most `maxWidth` wide in the middle of an area's width: what keeps a form readable in a wide window.
|
||||
constexpr Box Centered(Box area, float maxWidth) noexcept {
|
||||
const float width = std::min(area.w, maxWidth);
|
||||
return {area.x + (area.w - width) / 2, area.y, width, area.h};
|
||||
}
|
||||
}
|
||||
74
interfaces/Nostro.Ui-Text.cppm
Normal file
74
interfaces/Nostro.Ui-Text.cppm
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Text;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
|
||||
// Text as the toolkit handles it: UTF-8 by code point, measured through a font the window supplies, wrapped and cut
|
||||
// to a width. The window library shapes one line at a time and knows no line breaks, so the lines are broken here; and
|
||||
// since the font's advances come through TextMeasure, the tests pass a constant one and know every offset and width.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
// Which of the app's fonts a text is set in.
|
||||
enum class FontFace : std::uint8_t {
|
||||
Regular,
|
||||
Medium, // the same family, heavier: titles, amounts, buttons
|
||||
Icons // a symbol font: one code point is one icon
|
||||
};
|
||||
|
||||
// The code point at text[i], advancing i past it: 0 at the end, U+FFFD for a malformed sequence (at least one byte
|
||||
// consumed, so a loop always ends).
|
||||
std::uint32_t DecodeUtf8(std::string_view text, std::size_t& i);
|
||||
// The byte offset of the code point before `at`, and of the one after it; `at` at either end stays there.
|
||||
std::size_t PreviousCodepoint(std::string_view text, std::size_t at);
|
||||
std::size_t NextCodepoint(std::string_view text, std::size_t at);
|
||||
std::size_t CountCodepoints(std::string_view text);
|
||||
|
||||
// A font's sizes at a size in pixels, as the window's fonts give them: how far a code point moves the pen, how far
|
||||
// the baseline is below a line's top, and how far apart baselines are.
|
||||
struct TextMeasure {
|
||||
std::function<float(FontFace face, std::uint32_t codepoint, float px)> advance;
|
||||
std::function<float(FontFace face, float px)> ascent;
|
||||
std::function<float(FontFace face, float px)> lineHeight;
|
||||
};
|
||||
|
||||
// A line's width as it is drawn: the font's advances as they are, none for a newline, so a caret or a run that
|
||||
// follows lands where the glyphs end.
|
||||
float TextWidth(const TextMeasure& measure, std::string_view text, FontFace face, float px);
|
||||
// The byte offset in a line nearest to `x` pixels from its start, by the same widths: where a tap puts the cursor.
|
||||
std::size_t NearestByte(const TextMeasure& measure, std::string_view line, FontFace face, float px, float x);
|
||||
// The baseline that puts one line of text in the middle of a box's height.
|
||||
float CenteredBaseline(const TextMeasure& measure, Box box, FontFace face, float px);
|
||||
// The text that fits in the width, "…" in place of what was cut; a line break cuts too, since this is for one line.
|
||||
std::string Ellipsize(const TextMeasure& measure, std::string_view text, FontFace face, float px, float maxWidthPx);
|
||||
|
||||
struct WrappedLine {
|
||||
std::size_t begin = 0; // byte offsets into the text
|
||||
std::size_t end = 0; // exclusive; a space or a newline broken at is not part of the line, a hyphen is
|
||||
float width = 0;
|
||||
friend bool operator==(const WrappedLine&, const WrappedLine&) = default;
|
||||
};
|
||||
|
||||
// Greedy word wrap: a break after a space or a hyphen, a hard break at '\n', a word wider than the line split
|
||||
// between code points; the run of spaces where a line wraps goes to neither line. Results are kept per text, font,
|
||||
// size and width, since a screen wraps the same texts every frame.
|
||||
class WordWrapper {
|
||||
public:
|
||||
static constexpr std::size_t cacheLimit = 512;
|
||||
|
||||
explicit WordWrapper(TextMeasure measure);
|
||||
|
||||
// Valid until the next Wrap: a full cache is emptied.
|
||||
const std::vector<WrappedLine>& Wrap(std::string_view text, FontFace face, float px, float maxWidthPx);
|
||||
std::size_t CacheSize() const noexcept;
|
||||
const TextMeasure& Measure() const noexcept;
|
||||
|
||||
private:
|
||||
std::vector<WrappedLine> Compute(std::string_view text, FontFace face, float px, float maxWidthPx) const;
|
||||
|
||||
TextMeasure measure;
|
||||
std::map<std::string, std::vector<WrappedLine>> cache;
|
||||
};
|
||||
}
|
||||
66
interfaces/Nostro.Ui-Theme.cppm
Normal file
66
interfaces/Nostro.Ui-Theme.cppm
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Theme;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
|
||||
// The colours and sizes every widget draws with. The colours follow the system: light or dark, and its accent
|
||||
// colour. The sizes are the design's (dp), turned into pixels where they are used.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
struct Theme {
|
||||
Color background{0.96f, 0.96f, 0.97f}; // behind everything
|
||||
Color surface{1.0f, 1.0f, 1.0f}; // bars, cards, sheets, fields
|
||||
Color surfaceAlt{0.93f, 0.93f, 0.94f}; // what stands out from a surface: a chip, a selected tab
|
||||
Color text{0.11f, 0.11f, 0.12f};
|
||||
Color muted{0.43f, 0.43f, 0.45f}; // secondary text
|
||||
Color accent{0.18f, 0.44f, 0.93f};
|
||||
Color onAccent{1.0f, 1.0f, 1.0f}; // text on the accent colour
|
||||
Color divider{0.88f, 0.88f, 0.89f};
|
||||
Color positive{0.10f, 0.53f, 0.25f}; // money coming in
|
||||
Color warning{1.0f, 0.95f, 0.80f}; // a notice's ground
|
||||
Color onWarning{0.40f, 0.30f, 0.01f};
|
||||
Color danger{0.80f, 0.16f, 0.16f}; // an error, a step that cannot be undone
|
||||
Color onDanger{1.0f, 1.0f, 1.0f};
|
||||
Color scrim{0.0f, 0.0f, 0.0f, 0.35f}; // over what a sheet covers
|
||||
bool dark = false;
|
||||
|
||||
static Theme Light();
|
||||
static Theme Dark();
|
||||
// The system's accent colour, with text on it dark or light as its brightness asks.
|
||||
void Accent(Color color);
|
||||
// A control's colour while the pointer is over it, and while it is pressed: tinted towards the text colour, so
|
||||
// the change shows on a light theme and on a dark one.
|
||||
Color Hovered(Color color) const;
|
||||
Color Pressed(Color color) const;
|
||||
// The one taken of a few choices that stand on the quiet ground: lighter than that ground, as what is raised
|
||||
// from it would be, on a light theme and on a dark one.
|
||||
Color Taken() const;
|
||||
};
|
||||
|
||||
// Text sizes, in dp.
|
||||
namespace TextSize {
|
||||
inline constexpr float display = 32; // a balance
|
||||
inline constexpr float title = 22; // a screen's title
|
||||
inline constexpr float heading = 18;
|
||||
inline constexpr float body = 16;
|
||||
inline constexpr float label = 14;
|
||||
inline constexpr float caption = 12;
|
||||
}
|
||||
|
||||
// Distances, in dp.
|
||||
namespace Size {
|
||||
inline constexpr float unit = 4; // every gap is a multiple of it
|
||||
inline constexpr float gutter = 16; // between content and the window's edge
|
||||
inline constexpr float touch = 48; // the least a finger can hit
|
||||
inline constexpr float control = 48; // a button's and a field's height
|
||||
inline constexpr float row = 64; // a list row with two lines
|
||||
inline constexpr float bar = 56; // the top and bottom bars
|
||||
inline constexpr float radius = 12; // cards and sheets
|
||||
inline constexpr float controlRadius = 8;
|
||||
inline constexpr float contentMax = 640; // a form or a list does not grow wider than this
|
||||
inline constexpr float pull = 64; // how far down a list is pulled before letting go reads it again
|
||||
}
|
||||
}
|
||||
188
interfaces/Nostro.Ui-Widgets.cppm
Normal file
188
interfaces/Nostro.Ui-Widgets.cppm
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui:Widgets;
|
||||
|
||||
import std;
|
||||
import :Geometry;
|
||||
import :Text;
|
||||
import :Edit;
|
||||
import :Draw;
|
||||
import :Theme;
|
||||
import :Input;
|
||||
import :Context;
|
||||
|
||||
// What a screen is built from. Each widget is a function: given a frame and a box, it draws itself there and names
|
||||
// the regions that can be pressed. It keeps nothing; what must outlive the frame (a field's text, the focus, a
|
||||
// list's position) is the caller's or the Context's. Sizes are the design's, turned into pixels by the frame.
|
||||
|
||||
export namespace Nostro::Ui {
|
||||
struct TextStyle {
|
||||
FontFace face = FontFace::Regular;
|
||||
float sizeDp = TextSize::body;
|
||||
Color color;
|
||||
TextAlign align = TextAlign::Left;
|
||||
};
|
||||
|
||||
// The styles most text is set in, in the frame's colours.
|
||||
TextStyle Body(const Frame& frame);
|
||||
TextStyle Strong(const Frame& frame); // the body's size, heavier
|
||||
TextStyle Muted(const Frame& frame); // secondary text, a size down
|
||||
TextStyle Caption(const Frame& frame);
|
||||
TextStyle Heading(const Frame& frame);
|
||||
|
||||
// One line in the middle of the box's height, cut with an ellipsis where it does not fit. A Center or Right
|
||||
// alignment sets it against the box's middle or its right edge.
|
||||
void Label(Frame& frame, Box box, std::string_view text, const TextStyle& style);
|
||||
// Text wrapped to the box's width from its top; returns how tall it came out, whatever the box's height.
|
||||
float Paragraph(Frame& frame, Box box, std::string_view text, const TextStyle& style);
|
||||
float ParagraphHeight(Frame& frame, float width, std::string_view text, const TextStyle& style);
|
||||
|
||||
enum class ButtonKind : std::uint8_t {
|
||||
Filled, // the one thing to do here
|
||||
Tonal, // another thing to do
|
||||
Plain, // text alone: an action in a bar or beside a button
|
||||
Danger // a step that cannot be undone
|
||||
};
|
||||
|
||||
struct ButtonOptions {
|
||||
std::string key;
|
||||
std::string label;
|
||||
ButtonKind kind = ButtonKind::Filled;
|
||||
bool enabled = true;
|
||||
std::function<void()> onTap;
|
||||
};
|
||||
// Drawn in the box. The area that takes a press is the box and, for one lower than a finger needs, as much above
|
||||
// and below it as makes up for that: leave that room between two of them.
|
||||
void Button(Frame& frame, Box box, const ButtonOptions& options);
|
||||
// How wide a button is at least, for its label.
|
||||
float ButtonWidth(const Frame& frame, std::string_view label);
|
||||
|
||||
struct RowOptions {
|
||||
std::string key;
|
||||
std::string title;
|
||||
std::string subtitle; // a second line, muted
|
||||
std::string trailing; // at the right: an amount
|
||||
std::string trailingSub; // under it: a date
|
||||
std::optional<Color> trailingColor;
|
||||
std::string badge; // a small mark after the title: "Sandbox"
|
||||
std::optional<bool> selected; // a choice among rows, drawn as a radio button
|
||||
bool chevron = false; // leads somewhere
|
||||
bool enabled = true;
|
||||
bool divider = true;
|
||||
std::function<void()> onTap;
|
||||
std::function<void()> onLongPress;
|
||||
};
|
||||
void ListRow(Frame& frame, Box box, const RowOptions& options);
|
||||
|
||||
struct ValueOptions {
|
||||
std::string key;
|
||||
std::string label;
|
||||
std::string value;
|
||||
float labelWidth = 112; // the room the label has before the value, in dp
|
||||
std::function<void()> onTap; // nothing: it is only read
|
||||
std::function<void()> onLongPress;
|
||||
};
|
||||
// A label and beside it what it stands for, wrapped to the room there is: a line of a sheet that says what
|
||||
// something is. With an action it is a row that can be pressed, a finger tall in a window as narrow as a phone and
|
||||
// closer to the next under a pointer. The text keeps clear of the box's sides, which a press is drawn up to. Drawn
|
||||
// at the box's top and as wide as it; returns how tall it came out, which ValueRowHeight says beforehand.
|
||||
float ValueRow(Frame& frame, Box box, const ValueOptions& options);
|
||||
float ValueRowHeight(Frame& frame, float width, const ValueOptions& options);
|
||||
|
||||
struct FieldOptions {
|
||||
std::string key;
|
||||
std::string label;
|
||||
std::string placeholder;
|
||||
std::string prefix; // before the text, inside the field: a currency's symbol
|
||||
std::string error; // under the field, in place of the helper
|
||||
std::string helper;
|
||||
std::optional<TextPurpose> purpose; // by default what the field's kind asks for
|
||||
std::optional<std::uint32_t> hints;
|
||||
bool enabled = true;
|
||||
std::function<void()> onChange;
|
||||
std::function<void()> onSubmit;
|
||||
};
|
||||
// A text field with its label above and its error or helper below, at the box's top and as wide as it; returns how
|
||||
// tall it came out.
|
||||
float TextField(Frame& frame, Box box, EditState& edit, const FieldOptions& options);
|
||||
// How tall a field with these options comes out in a box that wide, for what must know before it is drawn.
|
||||
float FieldHeight(Frame& frame, float width, const FieldOptions& options);
|
||||
|
||||
struct ChoiceOptions {
|
||||
std::string key;
|
||||
std::vector<std::string> labels;
|
||||
std::size_t selected = 0;
|
||||
bool enabled = true;
|
||||
std::function<void(std::size_t)> onSelect;
|
||||
};
|
||||
// A few choices side by side, one of them taken: a box of segments.
|
||||
void Segmented(Frame& frame, Box box, const ChoiceOptions& options);
|
||||
// The sections of a screen, one of them shown: a strip with a line under the one that is.
|
||||
void Tabs(Frame& frame, Box box, const ChoiceOptions& options);
|
||||
|
||||
enum class BannerKind : std::uint8_t {
|
||||
Info,
|
||||
Warning,
|
||||
Error
|
||||
};
|
||||
|
||||
struct BannerOptions {
|
||||
std::string key;
|
||||
BannerKind kind = BannerKind::Info;
|
||||
std::string text;
|
||||
std::string action; // a thing to do about it
|
||||
std::function<void()> onAction;
|
||||
std::function<void()> onDismiss; // nothing: it cannot be closed
|
||||
};
|
||||
// A notice across the box's width at its top; returns how tall it came out.
|
||||
float Banner(Frame& frame, Box box, const BannerOptions& options);
|
||||
|
||||
// A small rounded mark: a status. ChipWidth is how wide it comes out.
|
||||
void Chip(Frame& frame, Box box, std::string_view text, Color ground, Color color);
|
||||
float ChipWidth(const Frame& frame, std::string_view text);
|
||||
// A line along the box's bottom edge.
|
||||
void Divider(Frame& frame, Box box);
|
||||
// The ground where something is still being fetched: a bar of the box's width, less for a `fraction` under 1.
|
||||
void Skeleton(Frame& frame, Box box, float fraction = 1.0f);
|
||||
|
||||
struct BarAction {
|
||||
std::string key;
|
||||
std::string label;
|
||||
bool enabled = true;
|
||||
std::function<void()> onTap;
|
||||
};
|
||||
|
||||
struct TopBarOptions {
|
||||
std::string title;
|
||||
std::function<void()> onBack; // nothing: no way back from here
|
||||
std::vector<BarAction> actions;
|
||||
};
|
||||
// The bar along an area's top; returns the area under it.
|
||||
Box TopBar(Frame& frame, Box area, const TopBarOptions& options);
|
||||
|
||||
struct NavItem {
|
||||
std::string key;
|
||||
std::string label;
|
||||
std::string detail; // beside the label, where there is room: a balance
|
||||
bool selected = false;
|
||||
bool header = false; // names a group, and cannot be pressed
|
||||
std::function<void()> onTap;
|
||||
};
|
||||
// The app's places along an area's bottom, for a window as narrow as a phone; returns the area above it.
|
||||
Box NavBar(Frame& frame, Box area, std::span<const NavItem> items);
|
||||
// The same down an area's left side, `widthDp` wide, for a wider window; returns the area beside it.
|
||||
Box Sidebar(Frame& frame, Box area, float widthDp, std::span<const NavItem> items);
|
||||
|
||||
struct SheetOptions {
|
||||
std::string key;
|
||||
std::string title;
|
||||
float contentHeight = 0; // how tall the body wants to be, in pixels
|
||||
std::function<void()> onDismiss; // a tap beside it, or Escape
|
||||
};
|
||||
// What is asked or shown over a screen: a sheet from the bottom edge in a narrow window, a dialog in the middle of a
|
||||
// wider one, with what it covers dimmed and out of reach. `body` draws into the box it is given.
|
||||
void Sheet(Frame& frame, const SheetOptions& options, const std::function<void(Box)>& body);
|
||||
// How wide the box is that a sheet gives its body in this frame, for working out how tall the body wants to be.
|
||||
float SheetBodyWidth(const Frame& frame);
|
||||
}
|
||||
157
interfaces/Nostro.Ui.Shell-Canvas.cppm
Normal file
157
interfaces/Nostro.Ui.Shell-Canvas.cppm
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
#include "vulkan/vulkan.h"
|
||||
export module Nostro.Ui.Shell:Canvas;
|
||||
|
||||
import std;
|
||||
import Crafter.Graphics;
|
||||
import Crafter.Event;
|
||||
import Nostro.Ui;
|
||||
|
||||
// The window and what turns a frame's draw list into what the window library draws. The library draws from compute
|
||||
// shaders, one pass over the whole window per dispatch, in which every box comes before every glyph and everything is
|
||||
// cut to one box: that is a Batch of the draw list, so a frame is one dispatch per batch, in the list's order. A
|
||||
// dispatch reads its items from buffers of its own, from their start, so each batch needs a pair of buffers, and a
|
||||
// frame still on the GPU reads its buffers as it runs, so each frame in flight has a set of pairs: a pool, made once.
|
||||
|
||||
export namespace Nostro::Ui::Shell {
|
||||
// The app's fonts. They are read from files and measured without a window, so the toolkit's Context, which
|
||||
// measures every text through them, is made before the window and may outlive it.
|
||||
class Fonts {
|
||||
public:
|
||||
// Reads NotoSans-Regular.ttf and NotoSans-Medium.ttf from the directory; throws when one is not there.
|
||||
explicit Fonts(const std::filesystem::path& directory);
|
||||
Fonts(const Fonts&) = delete;
|
||||
Fonts& operator=(const Fonts&) = delete;
|
||||
|
||||
Crafter::Font& Face(FontFace face) noexcept;
|
||||
// The fonts' sizes for the toolkit; good for as long as the fonts are.
|
||||
TextMeasure Measure();
|
||||
|
||||
private:
|
||||
Crafter::Font regular;
|
||||
Crafter::Font medium;
|
||||
};
|
||||
|
||||
struct CanvasOptions {
|
||||
// The window's size where the compositor leaves it to the app, as a desktop does and a phone does not.
|
||||
std::uint32_t widthDp = 1024;
|
||||
std::uint32_t heightDp = 720;
|
||||
std::string title;
|
||||
std::string appId; // what the compositor finds the desktop file, and so the icon, by
|
||||
std::filesystem::path assets; // where the window library's shaders are
|
||||
std::filesystem::path cache; // where compiled shaders are kept between runs
|
||||
// For looking at another kind of display than the one at hand, a phone's on a desktop: everything is drawn as
|
||||
// at this scale, in a window of the size above times it in pixels, whatever the display's own scale is and
|
||||
// whatever size the compositor asks for.
|
||||
std::optional<float> scale;
|
||||
};
|
||||
|
||||
class Canvas {
|
||||
public:
|
||||
// Sets the graphics device up, once per process, and makes the window. Throws when there is no compositor to
|
||||
// connect to, no graphics device that can draw, or a shader is missing.
|
||||
Canvas(Fonts& fonts, const CanvasOptions& options);
|
||||
// Waits for the frames still being drawn, then lets go of everything.
|
||||
~Canvas();
|
||||
Canvas(const Canvas&) = delete;
|
||||
Canvas& operator=(const Canvas&) = delete;
|
||||
|
||||
Crafter::Window& Native() noexcept;
|
||||
const Crafter::Window& Native() const noexcept;
|
||||
// How many pixels a dp is: the scale of the display the window is on, or the one it was told to draw at.
|
||||
float Scale() const noexcept;
|
||||
// The window, in pixels.
|
||||
Box Bounds() const noexcept;
|
||||
// Gives the window the size it should have at the display's scale, where the window library has not: the size
|
||||
// the compositor asked for last, or the app's own until it asks for one. The library passes over the size that
|
||||
// comes with a new window, which is the whole screen on a phone, and applies a new scale only with the next
|
||||
// new size. True when the size changed, which the library says through onResize too.
|
||||
bool Reconcile();
|
||||
// Asks for another size, in dp: the window takes it at the next Reconcile, and keeps it until the compositor
|
||||
// asks for one of its own, which a compositor that tiles or maximises the window does at once.
|
||||
void SetSize(std::uint32_t widthDp, std::uint32_t heightDp) noexcept;
|
||||
// Draws one frame: the list over the background colour. Throws what the window library does at a graphics
|
||||
// error, after which the window draws nothing more.
|
||||
void Render(const DrawList& list, Color background);
|
||||
// The next frame is also written to this file, as a PNG.
|
||||
void Capture(const std::filesystem::path& png);
|
||||
|
||||
// What the last frame took, for whoever watches the pool: a frame that needed more than there is drew less
|
||||
// than it was given.
|
||||
struct Stats {
|
||||
std::uint32_t batches = 0;
|
||||
std::uint32_t boxes = 0;
|
||||
std::uint32_t glyphs = 0;
|
||||
std::uint32_t glyphsWaiting = 0; // drawn blank for want of a cell in the font atlas: there after a later frame
|
||||
bool overflowed = false;
|
||||
};
|
||||
const Stats& Last() const noexcept;
|
||||
std::uint64_t Frames() const noexcept;
|
||||
// The graphics device's name, and how it reaches what it draws: for a log.
|
||||
std::string Device() const;
|
||||
|
||||
// The frames a glyph without a cell waits at most: the frames in flight and one.
|
||||
static constexpr std::uint32_t atlasPatience = Crafter::Window::numFrames + 1;
|
||||
|
||||
private:
|
||||
// A batch's buffers: boxes (circles are boxes with round corners) and glyphs.
|
||||
struct Pair {
|
||||
Crafter::GraphicsBuffer<Crafter::QuadItem, true> boxes;
|
||||
Crafter::GraphicsBuffer<Crafter::GlyphItem, true> glyphs;
|
||||
std::optional<Crafter::BufferSlot> boxSlot;
|
||||
std::optional<Crafter::BufferSlot> glyphSlot;
|
||||
std::uint32_t boxCap = 0;
|
||||
std::uint32_t glyphCap = 0;
|
||||
};
|
||||
// Most batches are a bar or a sheet; a list is one batch of many items. So a frame has many small pairs and a
|
||||
// few large ones.
|
||||
static constexpr std::uint32_t smallPairs = 20;
|
||||
static constexpr std::uint32_t smallBoxes = 128;
|
||||
static constexpr std::uint32_t smallGlyphs = 1024;
|
||||
static constexpr std::uint32_t largePairs = 4;
|
||||
static constexpr std::uint32_t largeBoxes = 2048;
|
||||
static constexpr std::uint32_t largeGlyphs = 8192;
|
||||
struct FrameSet {
|
||||
std::vector<Pair> small;
|
||||
std::vector<Pair> large;
|
||||
std::size_t smallUsed = 0;
|
||||
std::size_t largeUsed = 0;
|
||||
};
|
||||
// In the order they are made, which is the reverse of the order they go in: the pairs hand their slots back to
|
||||
// the heap, the renderer holds slots of its own, and the heap holds a view of the atlas's image.
|
||||
struct Parts {
|
||||
Parts(std::uint32_t width, std::uint32_t height, std::string_view title);
|
||||
|
||||
Crafter::Window window;
|
||||
Crafter::FontAtlas atlas;
|
||||
Crafter::GraphicsDescriptorHeap heap;
|
||||
Crafter::UIRenderer renderer;
|
||||
std::optional<Crafter::EventListener<Crafter::UIBuildArgs>> build;
|
||||
std::array<FrameSet, Crafter::Window::numFrames> pool;
|
||||
};
|
||||
|
||||
void Release() noexcept;
|
||||
void MakePairs(std::vector<Pair>& pairs, std::uint32_t count, std::uint32_t boxCap, std::uint32_t glyphCap);
|
||||
void Build(VkCommandBuffer commands, std::uint32_t frameIndex);
|
||||
// One dispatch: the batch, over `under` when there is one.
|
||||
void Submit(VkCommandBuffer commands, FrameSet& set, const Batch& batch, const RectItem* under = nullptr);
|
||||
Pair* Take(FrameSet& set, std::size_t boxes, std::size_t glyphs) noexcept;
|
||||
|
||||
Fonts& fonts;
|
||||
// The size the window should have, in the compositor's units, and the size the compositor was last seen to ask
|
||||
// for, (0, 0) when it left the choice to the app.
|
||||
std::int32_t logicalWidth;
|
||||
std::int32_t logicalHeight;
|
||||
std::int32_t askedWidth = 0;
|
||||
std::int32_t askedHeight = 0;
|
||||
std::optional<float> pretend; // CanvasOptions::scale
|
||||
std::optional<Parts> parts;
|
||||
const DrawList* drawing = nullptr; // the list of the frame being drawn
|
||||
Color background;
|
||||
Stats stats;
|
||||
std::uint64_t frames = 0;
|
||||
};
|
||||
}
|
||||
164
interfaces/Nostro.Ui.Shell-Loop.cppm
Normal file
164
interfaces/Nostro.Ui.Shell-Loop.cppm
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
#include <systemd/sd-event.h>
|
||||
#include <wayland-client.h>
|
||||
export module Nostro.Ui.Shell:Loop;
|
||||
|
||||
import std;
|
||||
|
||||
// One thread and one sd-event loop serve everything the app waits for: the compositor, nostrod's answers on the
|
||||
// system bus, the portal on the session bus, and every timer. The window library's own loop (Window::StartSync) serves
|
||||
// the compositor alone and would starve the buses, so the Wayland display is a source of this loop instead, and the
|
||||
// app asks for its own frames: a frame is drawn when the compositor says the window is about to be shown, and only
|
||||
// when something asked for one. An idle window, a hidden one and a locked phone draw nothing.
|
||||
//
|
||||
// A callback sd-event or libwayland calls must not throw: the exception would unwind through C. Every callback here
|
||||
// runs through Loop::Guard, which turns what is thrown into a line on stderr and the loop's end with status 1.
|
||||
|
||||
export namespace Nostro::Ui::Shell {
|
||||
// The clock timers are set by and frames are dated with: milliseconds, monotonic.
|
||||
std::int64_t NowMs() noexcept;
|
||||
|
||||
class Loop {
|
||||
public:
|
||||
// Blocks SIGINT and SIGTERM for the process, which is why a Loop is made before anything starts a thread, and
|
||||
// ends the loop with status 0 when one arrives.
|
||||
Loop();
|
||||
~Loop();
|
||||
Loop(const Loop&) = delete;
|
||||
Loop& operator=(const Loop&) = delete;
|
||||
|
||||
sd_event* Event() const noexcept;
|
||||
// Runs until Exit; returns what Exit was given.
|
||||
std::int32_t Run();
|
||||
void Exit(std::int32_t status) noexcept;
|
||||
// Runs `body`. What it throws is said on stderr, with `what` for what was being done, and ends the loop.
|
||||
void Guard(std::string_view what, const std::function<void()>& body) noexcept;
|
||||
|
||||
// Called after every source of the loop that ran, whoever's it was, before the loop waits again: where the
|
||||
// window looks at what the callbacks left to do.
|
||||
std::function<void()> onSettle;
|
||||
// Where Guard says what was thrown; stderr without one.
|
||||
std::function<void(std::string_view line)> onError;
|
||||
|
||||
private:
|
||||
static std::int32_t OnPost(sd_event_source* source, void* data);
|
||||
void Failed(std::string_view line) noexcept;
|
||||
|
||||
sd_event* event = nullptr;
|
||||
sd_event_source* post = nullptr;
|
||||
};
|
||||
|
||||
// Calls back once at a time on the loop's clock, until it is set again.
|
||||
class Timer {
|
||||
public:
|
||||
Timer(Loop& loop, std::function<void()> fire);
|
||||
~Timer();
|
||||
Timer(const Timer&) = delete;
|
||||
Timer& operator=(const Timer&) = delete;
|
||||
|
||||
// At that time by NowMs, at once when it has passed; replaces the time set before.
|
||||
void At(std::int64_t ms);
|
||||
void After(std::int64_t ms);
|
||||
void Cancel() noexcept;
|
||||
bool Armed() const noexcept;
|
||||
|
||||
private:
|
||||
static std::int32_t OnTime(sd_event_source* source, std::uint64_t usec, void* data);
|
||||
|
||||
Loop& loop;
|
||||
std::function<void()> fire;
|
||||
sd_event_source* source = nullptr;
|
||||
bool armed = false;
|
||||
};
|
||||
|
||||
// Calls back once from the loop itself, soon: for what must not run inside the callback that asks for it.
|
||||
class Defer {
|
||||
public:
|
||||
Defer(Loop& loop, std::function<void()> run);
|
||||
~Defer();
|
||||
Defer(const Defer&) = delete;
|
||||
Defer& operator=(const Defer&) = delete;
|
||||
|
||||
void Schedule() noexcept;
|
||||
void Cancel() noexcept;
|
||||
|
||||
private:
|
||||
static std::int32_t OnDefer(sd_event_source* source, void* data);
|
||||
|
||||
Loop& loop;
|
||||
std::function<void()> run;
|
||||
sd_event_source* source = nullptr;
|
||||
};
|
||||
|
||||
// The compositor's connection as a source of the loop.
|
||||
//
|
||||
// libwayland wants a reader to announce itself (prepare_read), wait for the socket, and then read or step back, and
|
||||
// two readers on one thread deadlock: the second waits for the first to read. The window library and the Vulkan
|
||||
// driver both read the display themselves, from inside this thread's callbacks (a clipboard read, a swapchain
|
||||
// waiting for the compositor). So no announced read here outlives a callback: when the socket is readable, one
|
||||
// callback announces, reads and dispatches; and before the loop waits, what such a foreign read left queued for
|
||||
// this thread is dispatched and what was written is sent.
|
||||
class WaylandSource {
|
||||
public:
|
||||
WaylandSource(Loop& loop, wl_display* display);
|
||||
~WaylandSource();
|
||||
WaylandSource(const WaylandSource&) = delete;
|
||||
WaylandSource& operator=(const WaylandSource&) = delete;
|
||||
|
||||
// The connection broke, which a process cannot mend: called once, and the source stops.
|
||||
std::function<void()> onLost;
|
||||
|
||||
// Reads and dispatches what the compositor has already sent, without waiting for more: for a timer that must
|
||||
// not get ahead of an event that is waiting to be read.
|
||||
void Pump();
|
||||
|
||||
private:
|
||||
static std::int32_t OnPrepare(sd_event_source* source, void* data);
|
||||
static std::int32_t OnReady(sd_event_source* source, std::int32_t fd, std::uint32_t revents, void* data);
|
||||
static std::int32_t OnNudge(sd_event_source* source, void* data);
|
||||
void Flush();
|
||||
void Lost();
|
||||
|
||||
Loop& loop;
|
||||
wl_display* display;
|
||||
sd_event_source* source = nullptr;
|
||||
sd_event_source* nudge = nullptr; // a source that does nothing, for the loop to go round once more
|
||||
bool writing = false; // the socket was full: its room to write is waited for too
|
||||
bool lost = false;
|
||||
};
|
||||
|
||||
// Frames when the compositor wants them: a request is a frame callback and a commit that changes nothing else, the
|
||||
// compositor's answer draws one frame, and nothing asks again unless something changed meanwhile or is moving. A
|
||||
// surface that is not shown is never answered, so it draws nothing.
|
||||
class FrameDriver {
|
||||
public:
|
||||
FrameDriver(Loop& loop, wl_surface* surface);
|
||||
~FrameDriver();
|
||||
FrameDriver(const FrameDriver&) = delete;
|
||||
FrameDriver& operator=(const FrameDriver&) = delete;
|
||||
|
||||
// Draws one frame.
|
||||
std::function<void()> onFrame;
|
||||
|
||||
// A frame as soon as the compositor allows one.
|
||||
void RequestRedraw();
|
||||
// Frames keep coming while on: something on the screen is moving.
|
||||
void SetAnimating(bool on);
|
||||
bool Animating() const noexcept;
|
||||
// Whether a request is out that the compositor has not answered.
|
||||
bool Waiting() const noexcept;
|
||||
|
||||
private:
|
||||
static void OnDone(void* data, wl_callback* callback, std::uint32_t time);
|
||||
static constexpr wl_callback_listener listener = {OnDone};
|
||||
|
||||
Loop& loop;
|
||||
wl_surface* surface;
|
||||
wl_callback* pending = nullptr;
|
||||
bool dirty = false;
|
||||
bool animating = false;
|
||||
};
|
||||
}
|
||||
65
interfaces/Nostro.Ui.Shell-Portal.cppm
Normal file
65
interfaces/Nostro.Ui.Shell-Portal.cppm
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
#include <systemd/sd-bus.h>
|
||||
export module Nostro.Ui.Shell:Portal;
|
||||
|
||||
import std;
|
||||
import Nostro.Ui;
|
||||
import :Loop;
|
||||
|
||||
// The desktop portal on the session bus, for what an app in a sandbox may ask of the desktop without a permission of
|
||||
// its own: how the system looks (dark or light, and its accent colour), and opening an address in the user's browser.
|
||||
// Without a session bus or a portal nothing is known and nothing opens, and the app goes on as it is.
|
||||
|
||||
export namespace Nostro::Ui::Shell {
|
||||
struct Appearance {
|
||||
bool known = false; // the portal gave a colour scheme
|
||||
bool dark = false;
|
||||
std::optional<Color> accent; // none: the system has no accent colour, or says none
|
||||
friend bool operator==(const Appearance&, const Appearance&) = default;
|
||||
};
|
||||
|
||||
class Portal {
|
||||
public:
|
||||
// Connects to the session bus and reads the appearance, waiting up to `patience` for the portal's answer: a
|
||||
// window drawn before it would open light and turn dark. An answer that comes later is told as a change.
|
||||
explicit Portal(Loop& loop, std::chrono::milliseconds patience = std::chrono::milliseconds(1000));
|
||||
~Portal();
|
||||
Portal(const Portal&) = delete;
|
||||
Portal& operator=(const Portal&) = delete;
|
||||
|
||||
bool Connected() const noexcept;
|
||||
const Appearance& Current() const noexcept;
|
||||
// The appearance changed, or was learnt after the start.
|
||||
std::function<void(const Appearance&)> onAppearance;
|
||||
|
||||
// Asks the desktop to open an address with the user's own program for it. `then` hears whether the desktop
|
||||
// took the request, from the loop; it hears false at once when there is no portal to ask.
|
||||
void OpenUri(std::string_view uri, std::function<void(bool taken)> then);
|
||||
|
||||
private:
|
||||
static std::int32_t OnSettings(sd_bus_message* reply, void* data, sd_bus_error* error);
|
||||
static std::int32_t OnChanged(sd_bus_message* signal, void* data, sd_bus_error* error);
|
||||
static std::int32_t OnOpened(sd_bus_message* reply, void* data, sd_bus_error* error);
|
||||
// Takes ReadAll's answer in; true when it changed what is known.
|
||||
bool TakeSettings(sd_bus_message* reply);
|
||||
bool TakeSetting(std::string_view key, sd_bus_message* value);
|
||||
void Told();
|
||||
|
||||
struct Opening {
|
||||
Portal* portal = nullptr;
|
||||
std::function<void(bool)> then;
|
||||
sd_bus_slot* slot = nullptr;
|
||||
};
|
||||
|
||||
Loop& loop;
|
||||
sd_bus* bus = nullptr;
|
||||
sd_bus_slot* changed = nullptr;
|
||||
sd_bus_slot* late = nullptr; // a ReadAll the start did not wait out
|
||||
std::list<Opening> openings;
|
||||
Appearance current;
|
||||
bool answered = false; // ReadAll's answer came, whatever it was
|
||||
};
|
||||
}
|
||||
118
interfaces/Nostro.Ui.Shell-Window.cppm
Normal file
118
interfaces/Nostro.Ui.Shell-Window.cppm
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// 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;
|
||||
};
|
||||
}
|
||||
14
interfaces/Nostro.Ui.Shell.cppm
Normal file
14
interfaces/Nostro.Ui.Shell.cppm
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui.Shell;
|
||||
|
||||
// What puts the toolkit's screens (Nostro.Ui) on a display: one event loop, a window on the Catcrafts window library,
|
||||
// and the desktop portal. It is the one part of the app that needs a compositor and a graphics device to run, so it
|
||||
// is kept thin: it holds no state of the screens and decides nothing about them. The toolkit and the app's own logic
|
||||
// are tested without it; this is checked by looking at what it draws.
|
||||
|
||||
export import :Loop;
|
||||
export import :Canvas;
|
||||
export import :Window;
|
||||
export import :Portal;
|
||||
19
interfaces/Nostro.Ui.cppm
Normal file
19
interfaces/Nostro.Ui.cppm
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
export module Nostro.Ui;
|
||||
|
||||
// The toolkit the Nostro app's screens are built with: text, editing, gestures, a list of what a frame draws, and the
|
||||
// frame's regions with what a press on each does. It knows nothing of banks and nothing of a window, so its tests
|
||||
// need neither; Nostro.Ui.Shell puts it on a Crafter.Graphics window.
|
||||
|
||||
export import :Geometry;
|
||||
export import :Text;
|
||||
export import :Edit;
|
||||
export import :Gesture;
|
||||
export import :Draw;
|
||||
export import :Theme;
|
||||
export import :Input;
|
||||
export import :Context;
|
||||
export import :Layout;
|
||||
export import :Widgets;
|
||||
Loading…
Reference in a new issue