Nostro/tests/ShouldLayOutWidgets/main.cpp

466 lines
32 KiB
C++
Raw Normal View History

feat(app): add the Nostro app, a window for phones and desktops Linux phones have no banking apps, and a command line is no answer on a phone. The app does everything the nostro command does and, like it, only talks to nostrod: it holds no key and no token. - Nostro.Ui, a toolkit whose screens are functions that fill a frame, ported from imsd-messages ca5cd72d; Nostro.App, the app's state, forms and screens; Nostro.Ui.Shell, which puts them on a Wayland window with Crafter.Graphics; nostro-app; and Nostro.DebugCommands for the --debug-api build. - Client: every method also in an asynchronous form for an sd-event loop, with DaemonMissing and OutcomeUnknown. - nostrod's Flatpak is net.catcrafts.Nostrod now and the app takes net.catcrafts.Nostro; install.sh moves the state. The app's Flatpak, both Flatpaks cross-built for aarch64, and the licence texts they carry. - nostro-fake-system, a made-up bus and bank with a real nostrod on them, to look at the app against. The README's screenshots are taken on it. - The nostro command: an API key's environment is the key's own to say, so --sandbox is for bunq-oauth alone, and a virtual card is listed as VIRTUAL. The published contract changes in three places, before any release (design rule 10): - Transactions' query takes `stored`: answered from what nostrod has stored, without asking the bank. The app searches with it; before, every search was a call to bunq. - AddBunq's `environment` no longer chooses over what the key says of itself: a key of the other environment is refused. - SetCardStatus refuses to freeze or unfreeze a virtual card. bunq reports one as DEACTIVATED however well it pays, and leaves it as it is when asked, without an error. The app is GPL-3.0-only, the library stays LGPL-3.0-only, and no code moved from one to the other (design rule 13). Transaction's schema is unchanged. Verified: the 17 tests pass with every line, function and region of what they link run, and the five lint runs are clean. Every screen was looked at on the made-up system, at a phone's and a desktop's sizes in the light and the dark look, on Plasma under Wayland. What is said of bunq's cards comes from its sandbox, with throwaway users, and from one real virtual card. Known to have been used against production: connecting, and reading accounts, payments and cards. Not known to have been tried: a payment from the app against bunq, OAuth against bunq, and anything on a phone. Building the app takes --graphics=, a Crafter.Graphics checkout on its branch claude/nostro-app, until that branch is merged and pinned here. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-19 12:27:14 +01:00
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// Nostro.Ui's layout cursors and its widgets, each in every state it has: what it draws, where, in which colours, and
// what a press on it does. The font is a fixed one (Ui.inc); the window is 400 by 800 at a scale of one unless a
// check says otherwise, so a size of the design is that many pixels.
import std;
import Nostro.Ui;
#include "../Support/Check.inc"
#include "../Support/Ui.inc"
using namespace Nostro::Ui;
using Test::Check;
using Test::Equal;
using Test::Hides;
using Test::Shows;
using Test::TapAt;
namespace {
constexpr Box window{0, 0, 400, 800};
template<typename Scene>
void Build(Context& ui, Scene&& scene, Box bounds = window, float scale = 1) {
Frame& frame = ui.Begin(bounds, scale, 0);
scene(frame);
ui.End();
}
// The line of text that reads `text`, or nothing.
const TextItem* Line(const Context& ui, std::string_view text) {
for(std::size_t layer = 0; layer < layerCount; layer++) {
for(const Batch& batch : ui.Shown().draw.Batches(static_cast<Layer>(layer))) {
for(const TextItem& item : batch.texts) {
if(item.text == text) {
return &item;
}
}
}
}
return nullptr;
}
// The boxes drawn in a colour, in the order they are drawn.
std::vector<RectItem> Rects(const Context& ui, Color color) {
std::vector<RectItem> rects;
for(std::size_t layer = 0; layer < layerCount; layer++) {
for(const Batch& batch : ui.Shown().draw.Batches(static_cast<Layer>(layer))) {
for(const RectItem& item : batch.rects) {
if(item.color == color) {
rects.push_back(item);
}
}
}
}
return rects;
}
std::size_t Circles(const Context& ui) {
std::size_t count = 0;
for(std::size_t layer = 0; layer < layerCount; layer++) {
for(const Batch& batch : ui.Shown().draw.Batches(static_cast<Layer>(layer))) {
count += batch.circles.size();
}
}
return count;
}
// Whether a line is drawn around a box: the keyboard's focus on it.
bool Ringed(const Context& ui, Box box) {
const std::vector<RectItem> rings = Rects(ui, ui.theme.accent.WithAlpha(0));
return std::ranges::any_of(rings, [box](const RectItem& ring) { return ring.box == box && ring.outline == 2; });
}
void Cursors() {
Column column({10, 20, 100, 300}, 8);
Check(column.Next() == Box{10, 20, 100, 300} && column.Used() == 0, "a column starts at its area's top, with nothing used");
Check(column.Take(40) == Box{10, 20, 100, 40} && column.Take(60) == Box{10, 68, 100, 60}, "each box starts under the one before, the gap between them");
Check(column.Next() == Box{10, 136, 100, 184} && column.Used() == 108, "what is used is the boxes and the gaps between them, not the gap after the last");
column.Advance(500);
Check(column.Next().h == 0 && column.Take(10).y == 644, "a column may run past its area: one that scrolls is as long as its content");
Row row({10, 20, 300, 40}, 4);
Check(row.TakeLeft(50) == Box{10, 20, 50, 40} && row.TakeRight(60) == Box{250, 20, 60, 40} && row.Rest() == Box{64, 20, 182, 40}, "a row gives boxes from its ends, and the rest between them");
row.TakeLeft(400);
Equal(row.Rest().w, 0.0f, "nothing is left of a row that is taken up");
Check(Centered({0, 0, 1000, 50}, 640) == Box{180, 0, 640, 50} && Centered({20, 0, 300, 50}, 640) == Box{20, 0, 300, 50}, "a box no wider than asked, in the middle of the area");
}
void Words() {
Context ui(Test::FixedMeasure());
float tall = 0;
float measured = 0;
Build(ui, [&](Frame& frame) {
Check(Body(frame).sizeDp == 16 && Strong(frame).face == FontFace::Medium && Muted(frame).color == ui.theme.muted && Caption(frame).sizeDp == 12 && Heading(frame).sizeDp == 18, "the styles text is set in");
Label(frame, {10, 0, 200, 40}, "left", Body(frame));
Label(frame, {10, 40, 200, 40}, "middle", {FontFace::Regular, 16, ui.theme.text, TextAlign::Center});
Label(frame, {10, 80, 200, 40}, "right", {FontFace::Regular, 16, ui.theme.text, TextAlign::Right});
Label(frame, {10, 120, 64, 40}, "much too long", Body(frame));
tall = Paragraph(frame, {10, 200, 80, 0}, "one two three four", Body(frame));
Paragraph(frame, {10, 400, 200, 0}, "in the middle", {FontFace::Regular, 16, ui.theme.text, TextAlign::Center});
Paragraph(frame, {10, 440, 200, 0}, "at the end", {FontFace::Regular, 16, ui.theme.text, TextAlign::Right});
measured = ParagraphHeight(frame, 80, "one two three four", Body(frame));
});
Check(Line(ui, "left")->x == 10 && Line(ui, "left")->baseline == 10 + 12.8f && Line(ui, "middle")->x == 110 && Line(ui, "right")->x == 210, "a label sits in the middle of its box's height, at the edge its alignment names");
Shows(ui, "much to…", "and is cut where it does not fit");
Check(Line(ui, "one two") != nullptr && Line(ui, "three four") != nullptr && Line(ui, "three four")->baseline == Line(ui, "one two")->baseline + 20 && tall == 40 && measured == 40, "a paragraph wraps to its box's width, and says how tall it came out");
Check(Line(ui, "in the middle")->x == 110 && Line(ui, "at the end")->x == 210, "its lines go by the same alignment");
}
void Buttons() {
Context ui(Test::FixedMeasure());
std::int32_t taps = 0;
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
Button(frame, {10, 10, 120, 48}, {.key = "filled", .label = "Pay", .onTap = [&taps] { taps++; }});
Button(frame, {10, 70, 120, 48}, {.key = "tonal", .label = "Request", .kind = ButtonKind::Tonal});
Button(frame, {10, 130, 120, 48}, {.key = "plain", .label = "Cancel", .kind = ButtonKind::Plain});
Button(frame, {10, 190, 120, 48}, {.key = "danger", .label = "Remove", .kind = ButtonKind::Danger});
Button(frame, {10, 250, 120, 48}, {.key = "off", .label = "Not yet", .enabled = false, .onTap = [&taps] { taps++; }});
Button(frame, {10, 328, 120, 32}, {.key = "low", .label = "More", .kind = ButtonKind::Plain, .onTap = [&taps] { taps += 10; }});
Check(ButtonWidth(frame, "Pay") == 3 * 8 + 32, "a button is as wide as its label and a gutter each side");
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(Rects(ui, theme.accent).size() == 1 && Rects(ui, theme.accent)[0].box == Box{10, 10, 120, 48} && Line(ui, "Pay")->color == theme.onAccent && Line(ui, "Pay")->face == FontFace::Medium && Line(ui, "Pay")->x == 70, "a filled button: the accent, its label in the middle");
Check(Rects(ui, theme.surfaceAlt).size() == 2 && Line(ui, "Request")->color == theme.text && Line(ui, "Not yet")->color == theme.muted, "a tonal button and a disabled one stand on the quiet ground");
Check(Rects(ui, theme.surface).empty() && Line(ui, "Cancel")->color == theme.accent, "a plain one is its label alone");
Check(Rects(ui, theme.danger).size() == 1 && Line(ui, "Remove")->color == theme.onDanger, "a dangerous one says so by its colour");
Check(ui.Find("filled")->focusable && ui.Find("filled")->label == "Pay" && !ui.Find("off")->enabled && ui.Find("filled")->box == Box{10, 10, 120, 48}, "each is a region with its label, as large as it is drawn");
TapAt(ui, {50, 30});
TapAt(ui, {50, 270});
Equal(taps, 1, "a tap presses it, unless it is disabled");
TapAt(ui, {50, 322});
TapAt(ui, {50, 366});
TapAt(ui, {50, 370});
Check(taps == 21 && ui.Find("low")->box == Box{10, 320, 120, 48}, "a button lower than a finger needs takes a press from as much above and below it as makes up for that");
ui.Hover(Point{50, 150});
Build(ui, scene);
Equal(Rects(ui, theme.Hovered(theme.surface)).size(), std::size_t{1}, "under the pointer a plain button gets a ground");
ui.Pointer(PointerPhase::Down, {50, 30}, 0);
ui.Hover(Point{50, 270});
Build(ui, scene);
Check(Rects(ui, theme.Pressed(theme.accent)).size() == 1 && Rects(ui, theme.accent).empty() && Rects(ui, theme.surfaceAlt).size() == 2, "a pressed button is drawn pressed; a disabled one feels nothing");
ui.Pointer(PointerPhase::Cancel, {50, 30}, 10);
ui.Focus("tonal");
Build(ui, scene);
Check(Ringed(ui, {10, 70, 120, 48}) && !Ringed(ui, {10, 10, 120, 48}), "the keyboard's focus is a line around it");
}
void Rows() {
Context ui(Test::FixedMeasure());
std::vector<std::string> seen;
const Color green{0, 0.5f, 0};
// 43 characters, and room for 37 beside the mark: the row is 368 wide within its gutters, the mark 58 and a gap of 8.
const std::string longTitle = "A connection with quite a long name, indeed";
const std::string cutTitle = Ellipsize(Test::FixedMeasure(), longTitle, FontFace::Regular, 16, 302);
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
ListRow(frame, {0, 0, 400, 64}, {.key = "main", .title = "Main account", .subtitle = "NL23 BUNQ 2025 1234 56", .trailing = "€12.50", .trailingSub = "today", .chevron = true, .onTap = [&seen] { seen.push_back("main"); }, .onLongPress = [&seen] { seen.push_back("main held"); }});
ListRow(frame, {0, 64, 400, 56}, {.key = "plain", .title = "Just a title", .divider = false});
ListRow(frame, {0, 120, 400, 64}, {.key = "in", .title = "Bob", .trailing = "+€5.00", .trailingColor = green});
ListRow(frame, {0, 184, 400, 64}, {.key = "dated", .title = "Carol", .trailingSub = "1 Nov"});
ListRow(frame, {0, 248, 400, 64}, {.key = "sandbox", .title = longTitle, .badge = "Sandbox"});
ListRow(frame, {0, 312, 400, 64}, {.key = "chosen", .title = "Savings", .selected = true, .onTap = [&seen] { seen.push_back("chosen"); }});
ListRow(frame, {0, 376, 400, 64}, {.key = "other", .title = "Joint", .selected = false, .enabled = false, .onTap = [&seen] { seen.push_back("other"); }});
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(Line(ui, "Main account")->x == 16 && Line(ui, "NL23 BUNQ 2025 1234 56")->color == theme.muted && Line(ui, "NL23 BUNQ 2025 1234 56")->baseline > Line(ui, "Main account")->baseline, "a row's title, and under it its second line");
Check(Line(ui, "€12.50")->align == TextAlign::Right && Line(ui, "€12.50")->x == 400 - 16 - 20 && Line(ui, "today")->x == 364 && Line(ui, "today")->baseline > Line(ui, "€12.50")->baseline && Line(ui, "›") != nullptr, "what trails it at the right, before the mark that it leads somewhere");
Check(Line(ui, "Just a title")->baseline == 64 + (56 - 20) / 2.0f + 12.8f, "a title alone is in the middle of the row");
Check(Line(ui, "+€5.00")->color == green && Line(ui, "+€5.00")->baseline == Line(ui, "Bob")->baseline && Line(ui, "1 Nov")->baseline == 184 + (64 - 15) / 2.0f + 9.6f, "an amount in its own colour; one trailing line alone is in the row's middle");
Check(cutTitle != longTitle && Line(ui, cutTitle) != nullptr && Line(ui, "Sandbox") != nullptr && Line(ui, "Sandbox")->bounds.x > Line(ui, cutTitle)->bounds.Right(), "a mark follows the title, which makes room for it");
Check(Circles(ui) == 3 && Line(ui, "Savings")->x == 16 + 36 && Line(ui, "Joint")->color == theme.muted, "a choice among rows is a ring, with a dot in the one chosen; a disabled row is muted");
Check(Rects(ui, theme.divider).size() == 6 && Rects(ui, theme.divider)[0].box == Box{16, 63, 384, 1}, "a line under every row but the one that asked for none, from its title to the edge of a window as narrow as a phone");
Check(ui.Find("main")->role == Role::Row && ui.Find("main")->focusable && !ui.Find("plain")->focusable && ui.Find("main")->label == "Main account", "a row is a region; one that does nothing takes no focus");
TapAt(ui, {200, 30});
TapAt(ui, {200, 400});
Check(ui.LongPress("main") && seen == std::vector<std::string>{"main", "main held"}, "a tap and a long press act; a disabled row does not");
ui.Hover(Point{200, 30});
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surface)).size() == 1 && Rects(ui, theme.Hovered(theme.surface))[0].box == Box{0, 0, 400, 64}, "the pointer over a row that acts tints it");
ui.Hover(Point{200, 90});
ui.Pointer(PointerPhase::Down, {200, 340}, 0);
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surface)).empty() && Rects(ui, theme.Pressed(theme.surface)).size() == 1, "one that does nothing is not; a pressed one is drawn pressed");
ui.Pointer(PointerPhase::Cancel, {200, 340}, 10);
ui.Focus("chosen");
Build(ui, scene);
Check(Ringed(ui, {2, 314, 396, 60}), "the keyboard's focus is a line just inside it");
Build(ui, scene, {0, 0, 1000, 800});
Check(Rects(ui, theme.divider)[0].box == Box{16, 63, 368, 1}, "in a wider window the line under a row ends where the row's text does");
}
// A line of a sheet that says what something is: a label, and beside it a value that a press can copy.
void Values() {
Context ui(Test::FixedMeasure());
std::vector<std::string> seen;
std::array<float, 3> heights{};
float foretold = 0;
// 74 characters, and room for 34 on a line: the row is 400 wide, its text 8 clear of either side, the label 112.
const std::string longValue = "A description that is long enough to need a second line in the room it has";
const std::string longLabel = "A label that is too long for its room";
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
const ValueOptions iban{.key = "iban", .label = "IBAN", .value = "NL23 BUNQ 2025 1234 56", .onTap = [&seen] { seen.push_back("tap"); }, .onLongPress = [&seen] { seen.push_back("held"); }};
foretold = ValueRowHeight(frame, frame.bounds.w, iban);
heights[0] = ValueRow(frame, {0, 0, frame.bounds.w, 0}, iban);
heights[1] = ValueRow(frame, {0, 100, 400, 0}, {.key = "long", .label = "Description", .value = longValue, .onTap = [&seen] { seen.push_back("long"); }});
heights[2] = ValueRow(frame, {0, 300, 400, 0}, {.key = "read", .label = longLabel, .value = "Read only"});
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(heights[0] == 48 && foretold == 48 && heights[2] == 48, "on a phone a line is as tall as a finger needs, which can be asked beforehand");
Check(Line(ui, "IBAN")->x == 8 && Line(ui, "IBAN")->px == 14 && Line(ui, "IBAN")->color == theme.muted && Line(ui, "NL23 BUNQ 2025 1234 56")->x == 8 + 112, "the label, and beside it the value, clear of the row's sides");
Check(Line(ui, "NL23 BUNQ 2025 1234 56")->baseline == 14 + 12.8f && Line(ui, "IBAN")->baseline == 14 + 12.8f, "the value in the middle of the row's height, the label on its line");
Check(heights[1] == 3 * 20 + 12 && Line(ui, "A description that is long enough") != nullptr && Line(ui, "it has")->baseline == 100 + 6 + 40 + 12.8f && Line(ui, "Description")->baseline == 100 + 6 + 12.8f, "a value too long for its room wraps, the row grows with it, and the label stays on its first line");
Check(Line(ui, Ellipsize(Test::FixedMeasure(), longLabel, FontFace::Regular, 14, 104)) != nullptr && Line(ui, longLabel) == nullptr, "a label too long for its room is cut");
Check(ui.Find("iban")->role == Role::Row && ui.Find("iban")->focusable && ui.Find("iban")->box == Box{0, 0, 400, 48} && ui.Find("iban")->label == "IBAN" && ui.Find("read") == nullptr, "a line with something to do is a region; one that is only read is not");
Check(ui.Tap("iban") && ui.LongPress("iban") && ui.Tap("long") && seen == std::vector<std::string>{"tap", "held", "long"}, "a tap and a long press act");
ui.Hover(Point{200, 20});
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surface)).size() == 1 && Rects(ui, theme.Hovered(theme.surface))[0].box == Box{0, 0, 400, 48}, "under the pointer the whole row is tinted");
ui.Hover(Point{200, 320});
ui.Pointer(PointerPhase::Down, {200, 120}, 0);
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surface)).empty() && Rects(ui, theme.Pressed(theme.surface)).size() == 1, "a line that is only read is not; a pressed one is drawn pressed");
ui.Pointer(PointerPhase::Cancel, {200, 120}, 10);
ui.Focus("long");
Build(ui, scene);
Check(Ringed(ui, {0, 100, 400, 72}), "the keyboard's focus is a line around the row");
Build(ui, scene, {0, 0, 1000, 800});
Check(heights[0] == 36 && foretold == 36 && ui.Find("iban")->box == Box{0, 0, 1000, 36}, "in a wider window, under a pointer, the lines are closer together");
}
void Choosing() {
Context ui(Test::FixedMeasure());
std::vector<std::size_t> picked;
std::size_t selected = 1;
bool enabled = true;
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
Segmented(frame, {10, 10, 300, 40}, {.key = "when", .labels = {"Now", "Scheduled", "Draft"}, .selected = selected, .enabled = enabled, .onSelect = [&](std::size_t i) { picked.push_back(i); }});
Segmented(frame, {10, 60, 300, 40}, {.key = "idle", .labels = {"One", "Two"}});
Tabs(frame, {0, 200, 400, 48}, {.key = "tabs", .labels = {"Activity", "Requests", "Incoming", "Scheduled", "Drafts"}, .selected = selected, .enabled = enabled, .onSelect = [&](std::size_t i) { picked.push_back(i); }});
Tabs(frame, {0, 300, 300, 48}, {.key = "tight", .labels = {"Activity", "Requests", "Incoming", "Scheduled", "Drafts"}, .selected = 0});
Segmented(frame, {10, 400, 300, 40}, {.key = "none"});
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(Line(ui, "Scheduled")->face == FontFace::Medium && Line(ui, "Now")->face == FontFace::Regular && Rects(ui, theme.surface).size() == 2, "of a row of segments the one taken stands out");
Check(ui.Find("when.0")->box == Box{10, 10, 100, 40} && ui.Find("when.2")->box == Box{210, 10, 100, 40} && ui.Find("when.1")->label == "Scheduled" && ui.Find("none.0") == nullptr, "each segment is an equal part, and a region");
Check(ui.Tap("when.2") && ui.Tap("tabs.4") && picked == std::vector<std::size_t>{2, 4} && ui.Tap("idle.1"), "a tap on one says which; one nobody listens to can still be pressed");
// The tabs: 80 wide each in the wide strip, 60 in the tight one, where "Scheduled" at 14 pixels wants 71.
const std::span<const Batch> content = ui.Shown().draw.Batches(Layer::Content);
std::vector<float> sizes;
for(const Batch& batch : content) {
for(const TextItem& item : batch.texts) {
if(item.text == "Scheduled" && item.baseline > 200) {
sizes.push_back(item.px);
}
}
}
Equal(sizes == std::vector<float>{14, 12}, true, "tabs whose labels do not all fit beside each other are set a size down");
Check(Rects(ui, theme.accent).size() == 2 && Rects(ui, theme.accent)[0].box == Box{80 + 8, 245, 64, 3}, "a line under the tab that is shown");
ui.Hover(Point{50, 30});
ui.Pointer(PointerPhase::Down, {40, 220}, 0);
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surfaceAlt)).size() == 1 && Rects(ui, theme.Pressed(theme.surface)).size() == 1, "a segment under the pointer and a tab that is pressed are drawn so");
ui.Pointer(PointerPhase::Cancel, {40, 220}, 10);
ui.Hover(Point{160, 30});
Build(ui, scene);
Check(Rects(ui, theme.Hovered(theme.surfaceAlt)).empty(), "the segment that is taken does not change under the pointer");
ui.Focus("when.0");
Build(ui, scene);
Check(Ringed(ui, {13, 13, 94, 34}), "the focus on a segment");
ui.Focus("tabs.2");
Build(ui, scene);
Check(Ringed(ui, {162, 202, 76, 44}), "and on a tab");
enabled = false;
ui.Hover(Point{50, 30});
ui.Pointer(PointerPhase::Down, {40, 220}, 0);
Build(ui, scene);
Check(Line(ui, "Now")->color == theme.muted && Rects(ui, theme.Hovered(theme.surfaceAlt)).empty() && Rects(ui, theme.Pressed(theme.surface)).empty() && !ui.Tap("when.0"), "disabled, they are muted, feel nothing and take no tap");
ui.Pointer(PointerPhase::Cancel, {40, 220}, 10);
// A dark theme's surface is darker than the ground the segments stand on: the one taken is lighter than both.
ui.theme = Theme::Dark();
Build(ui, scene);
Check(Rects(ui, ui.theme.Taken()).size() == 2 && Rects(ui, ui.theme.surface).empty(), "on a dark theme the segment taken still stands out as the lighter");
}
void Notices() {
Context ui(Test::FixedMeasure());
std::vector<std::string> seen;
std::array<float, 4> heights{};
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
heights[0] = Banner(frame, {10, 0, 380, 0}, {.key = "info", .text = "A connection could not be reached"});
heights[1] = Banner(frame, {10, 100, 380, 0}, {.key = "warn", .kind = BannerKind::Warning, .text = "It may have been carried out: check your activity before trying again", .action = "Open activity", .onAction = [&seen] { seen.push_back("action"); }, .onDismiss = [&seen] { seen.push_back("dismiss"); }});
heights[2] = Banner(frame, {10, 300, 380, 0}, {.key = "error", .kind = BannerKind::Error, .text = "bunq refused", .onDismiss = [&seen] { seen.push_back("gone"); }});
heights[3] = Banner(frame, {10, 400, 380, 0}, {.key = "act", .text = "Nothing is connected yet", .action = "Add a bank"});
Chip(frame, {10, 600, ChipWidth(frame, "PENDING"), 20}, "PENDING", ui.theme.surfaceAlt, ui.theme.muted);
Divider(frame, {0, 700, 400, 20});
Skeleton(frame, {10, 740, 200, 16});
Skeleton(frame, {10, 770, 200, 16}, 0.5f);
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(heights[0] == 24 + 17.5f && Rects(ui, theme.surfaceAlt)[0].box == Box{10, 0, 380, 41.5f} && Line(ui, "A connection could not be reached")->color == theme.text, "a notice is as tall as its text and its padding");
Check(Rects(ui, theme.warning).size() == 2 && Line(ui, "×") != nullptr && Line(ui, "Open activity")->color == theme.onWarning && heights[1] == 24 + 35 + 40, "a warning in its own colours, wrapped beside the mark that closes it, its action under the text");
Check(Rects(ui, theme.danger).size() == 1 && Line(ui, "bunq refused")->color == theme.onDanger && heights[2] == 48, "an error; a notice that can be closed is at least as tall as the mark");
Check(ui.Tap("warn.action") && ui.Tap("warn.dismiss") && ui.Tap("error.dismiss") && ui.Tap("act.action") && seen == std::vector<std::string>{"action", "dismiss", "gone"}, "its action and its closing can be pressed");
Check(ui.Find("info.dismiss") == nullptr && ui.Find("info.action") == nullptr && ui.Find("warn.dismiss")->label == "Dismiss", "a notice without them has neither");
Check(Line(ui, "PENDING")->px == 12 && Rects(ui, theme.surfaceAlt)[3].corners == Corners::All(10) && ChipWidth(ui.Shown(), "PENDING") == 7 * 6 + 16, "a chip is a rounded mark around its text");
Check(Rects(ui, theme.divider)[0].box == Box{0, 719, 400, 1}, "a divider is a line along its box's bottom edge");
Check(Rects(ui, theme.surfaceAlt)[4].box == Box{10, 740, 200, 16} && Rects(ui, theme.surfaceAlt)[5].box == Box{10, 770, 100, 16}, "a skeleton is a bar, or part of one");
ui.Focus("warn.action");
ui.Hover(Point{50, 170});
Build(ui, scene);
Check(Ringed(ui, ui.Find("warn.action")->box.Inset(Insets::Symmetric(0, 6))) && ui.Find("warn.action")->box.h == 48 && Rects(ui, theme.Hovered(theme.warning)).size() == 1, "the action takes the focus and feels the pointer, and a press from a little above and below what is drawn: a finger's height");
ui.Focus("warn.dismiss");
Build(ui, scene);
Check(Ringed(ui, ui.Find("warn.dismiss")->box.Inset(Insets::All(6))), "and so does the mark");
}
void Bars() {
Context ui(Test::FixedMeasure());
std::vector<std::string> seen;
bool back = true;
Box under;
Box above;
Box beside;
float sideWidth = 240;
const std::vector<NavItem> places = {
{.key = "nav.accounts", .label = "Accounts", .selected = true, .onTap = [&seen] { seen.push_back("accounts"); }},
{.key = "nav.cards", .label = "Cards", .onTap = [&seen] { seen.push_back("cards"); }},
{.key = "nav.settings", .label = "Settings", .onTap = [&seen] { seen.push_back("settings"); }},
};
const std::vector<NavItem> side = {
{.key = "side.accounts", .label = "Accounts", .selected = true, .onTap = [&seen] { seen.push_back("side accounts"); }},
{.key = "side.head", .label = "Alice at bunq", .header = true},
{.key = "side.main", .label = "Main", .detail = "€1,000.00", .onTap = [&seen] { seen.push_back("side main"); }},
};
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
under = TopBar(frame, frame.bounds, {.title = "A title that is far too long to fit beside two actions", .onBack = back ? std::function<void()>([&seen] { seen.push_back("back"); }) : nullptr, .actions = {{.key = "bar.refresh", .label = "Refresh", .onTap = [&seen] { seen.push_back("refresh"); }}, {.key = "bar.add", .label = "Add", .enabled = false}}});
above = NavBar(frame, under, places);
beside = Sidebar(frame, above, sideWidth, side);
Check(frame.draw.CurrentLayer() == Layer::Content, "a bar leaves the layer as it found it");
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(under == Box{0, 56, 400, 744} && above == Box{0, 56, 400, 688} && beside == Box{240, 56, 160, 688}, "each bar returns the area it leaves");
Check(ui.Find("bar")->role == Role::Panel && ui.Find("bar")->layer == Layer::Chrome && ui.Find("bar.back")->box == Box{4, 0, 48, 56} && Line(ui, "‹") != nullptr, "the top bar, with the way back at its left");
Check(ui.Find("bar.add")->box.Right() == 396 && ui.Find("bar.refresh")->box.Right() == ui.Find("bar.add")->box.x - 4 && !ui.Find("bar.add")->enabled, "its actions from the right, in the order they were named");
Shows(ui, "…", "and its title in what room is left");
Check(ui.Tap("bar.back") && ui.Tap("bar.refresh") && seen == std::vector<std::string>{"back", "refresh"}, "which can be pressed");
ui.KeyDown(Key::Escape, {});
Equal(seen.back(), std::string("back"), "Escape is the way back");
Check(ui.Find("nav")->box == Box{0, 744, 400, 56} && ui.Find("nav.cards")->box == Box{400 / 3.0f, 744, 400 / 3.0f, 56}, "the places along the bottom, in equal parts");
Check(Line(ui, "Cards")->color == theme.muted && Rects(ui, theme.accent.WithAlpha(0.14f)).size() == 2, "the place the window is at stands out, in each bar");
Check(ui.Tap("nav.cards") && seen.back() == "cards", "a place can be pressed");
Check(ui.Find("side")->box == Box{0, 56, 240, 688} && ui.Find("side.scroll")->role == Role::Scroll && ui.Find("side.head") == nullptr && Line(ui, "Alice at bunq")->px == 12, "the places down the side, a heading among them that is no region");
Check(Line(ui, "€1,000.00") != nullptr && Line(ui, "€1,000.00")->align == TextAlign::Right && ui.Tap("side.main") && seen.back() == "side main", "a place with its detail beside it");
ui.Hover(Point{20, 20});
Build(ui, scene);
Equal(Rects(ui, theme.Hovered(theme.surface)).size(), std::size_t{1}, "the way back feels the pointer");
ui.Hover(Point{200, 770});
Build(ui, scene);
Equal(Rects(ui, theme.Hovered(theme.surface)).size(), std::size_t{1}, "so does a place along the bottom");
ui.Pointer(PointerPhase::Down, {100, 160}, 0);
ui.Hover(std::nullopt);
Build(ui, scene);
Equal(Rects(ui, theme.Pressed(theme.surface)).size(), std::size_t{1}, "and one down the side a press");
ui.Pointer(PointerPhase::Cancel, {100, 160}, 10);
ui.Focus("bar.back");
Build(ui, scene);
Check(Ringed(ui, {4, 4, 48, 48}), "the focus on the way back");
ui.Focus("nav.cards");
Build(ui, scene);
Check(Rects(ui, theme.accent.WithAlpha(0)).size() == 1, "on a place along the bottom");
ui.Focus("side.main");
Build(ui, scene);
Check(Rects(ui, theme.accent.WithAlpha(0)).size() == 1, "and on one down the side");
back = false;
sideWidth = 120;
Build(ui, scene);
Check(ui.Find("bar.back") == nullptr && Line(ui, "‹") == nullptr, "a screen with no way back shows none");
Check(Line(ui, "€1,000.00") == nullptr && Line(ui, "Main") != nullptr, "a detail gives way where the side bar is too narrow for both");
seen.clear();
ui.KeyDown(Key::Escape, {});
Check(seen.empty(), "and Escape goes nowhere");
// The dark theme tints the place the window is at more strongly.
ui.theme = Theme::Dark();
Build(ui, scene);
Equal(Rects(ui, ui.theme.accent.WithAlpha(0.28f)).size(), std::size_t{2}, "on a dark ground it stands out more strongly");
}
void Sheets() {
Context ui(Test::FixedMeasure());
std::vector<std::string> seen;
Box content;
std::string title = "Report this card stolen?";
bool dismissable = true;
float wanted = 200;
const auto scene = [&](Frame& frame) {
frame.draw.SetLayer(Layer::Content);
Button(frame, {10, 10, 100, 48}, {.key = "under", .label = "Under", .onTap = [&seen] { seen.push_back("under"); }});
Sheet(frame, {.key = "sheet", .title = title, .contentHeight = wanted, .onDismiss = dismissable ? std::function<void()>([&seen] { seen.push_back("dismissed"); }) : nullptr}, [&](Box box) {
content = box;
Button(frame, {box.x, box.y, 100, 48}, {.key = "sheet.ok", .label = "Report", .kind = ButtonKind::Danger, .onTap = [&seen] { seen.push_back("ok"); }});
});
Check(frame.draw.CurrentLayer() == Layer::Content, "a sheet leaves the layer as it found it");
};
Build(ui, scene);
const Theme& theme = ui.theme;
Check(ui.Find("sheet.panel")->box == Box{0, 800 - 272, 400, 272} && content == Box{16, 528 + 16 + 40, 368, 200} && ui.Find("sheet.panel")->layer == Layer::Overlay && SheetBodyWidth(ui.Shown()) == 368, "in a narrow window a sheet rises from the bottom edge, as tall as its title and its body");
Check(Rects(ui, theme.scrim).size() == 1 && Rects(ui, theme.surface).back().corners == Corners::Top(12), "over a scrim, its top corners rounded");
Shows(ui, "Report this card stolen?", "with its title");
Check(!ui.Tap("under") && ui.Tap("sheet.ok") && seen.back() == "ok", "what it covers is out of reach; its own button is not");
TapAt(ui, {200, 100});
ui.KeyDown(Key::Escape, {});
Equal(seen == std::vector<std::string>{"ok", "dismissed", "dismissed"}, true, "a tap beside it or Escape dismisses it");
title.clear();
dismissable = false;
wanted = 5000;
Build(ui, scene, {0, 0, 1000, 700});
Check(ui.Find("sheet.panel")->box == Box{260, 24, 480, 652} && content == Box{276, 40, 448, 620} && Rects(ui, theme.surface).back().corners == Corners::All(12), "in a wide window it is a dialog in the middle, no taller than the window leaves room for");
Check(SheetBodyWidth(ui.Shown()) == 448, "how wide its body is can be asked beforehand");
TapAt(ui, {50, 50});
ui.KeyDown(Key::Escape, {});
Equal(seen.size(), std::size_t{3}, "one that cannot be dismissed stays");
}
}
int main() {
Cursors();
Words();
Buttons();
Rows();
Values();
Choosing();
Notices();
Bars();
Sheets();
return Test::Result();
}