// SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® // Nostro.Ui's frames without a window: what a draw list keeps and in which passes, the theme, and the Context, fed // what a window would report. Taps that act on what the press went down on, long presses and the secondary button, // drags, flings and the wheel, the focus and the keys, text fields with their cursor, selection, clipboard and input // method, what an action finds when it runs, what a screen puts off, the menu and the notice, and what the window is // asked for in return. // The font is a fixed one (Ui.inc), and the window is 400 by 800 at a scale of one, 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}; constexpr Color ink{0, 0, 0}; template void Build(Context& ui, Scene&& scene, std::int64_t nowMs = 0, Box bounds = window, float scale = 1) { Frame& frame = ui.Begin(bounds, scale, nowMs); scene(frame); ui.End(); } Region Pressable(std::string key, Box box, std::function onTap) { Region region; region.box = box; region.key = std::move(key); region.label = region.key; region.focusable = true; region.onTap = std::move(onTap); return region; } void Drawing() { const TextMeasure measure = Test::FixedMeasure(); DrawList list(measure, window); Check(list.CurrentLayer() == Layer::Background && list.BatchCount() == 0 && list.Texts().empty(), "a new list is empty, at the bottom layer"); list.Rect({0, 0, 100, 50}, ink, 4); list.Circle({20, 20}, 10, ink); Equal(list.Text("hello", 10, 30, FontFace::Regular, 16, ink), 40.0f, "a text returns its width"); const std::span background = list.Batches(Layer::Background); Check(background.size() == 1 && background[0].rects.size() == 1 && background[0].circles.size() == 1 && background[0].texts.size() == 1 && !background[0].clip, "shapes and the text over them are one pass"); Check(background[0].rects[0].corners == Corners::All(4) && background[0].texts[0].bounds == Box{10, 30 - 12.8f, 40, 20}, "a box's corners, and a line's bounds from its ascent down"); list.Rect({200, 200, 10, 10}, ink); Equal(list.BatchCount(), std::size_t{1}, "a shape clear of the text stays in the pass"); list.Rect({0, 20, 30, 10}, ink); Equal(list.BatchCount(), std::size_t{2}, "a shape on text drawn before it needs the next pass to cover it"); list.Text("x", 300, 300, FontFace::Regular, 16, ink); list.Circle({304, 296}, 5, ink); Equal(list.BatchCount(), std::size_t{3}, "and so does a circle"); list.Text("abcd", 100, 500, FontFace::Regular, 16, ink, TextAlign::Center); list.Text("abcd", 100, 540, FontFace::Regular, 16, ink, TextAlign::Right); const Batch& last = list.Batches(Layer::Background).back(); Check(last.texts[0].bounds.x == 84 && last.texts[1].bounds.x == 68 && last.texts[1].x == 100, "a centred line's bounds are around its x, a right-aligned one's end there"); Equal(list.Text("", 10, 10, FontFace::Regular, 16, ink), 0.0f, "no text is no width"); list.Rect({500, 0, 10, 10}, ink); list.Circle({-50, -50}, 10, ink); list.Text("gone", 0, 900, FontFace::Regular, 16, ink); Check(list.BatchCount() == 3 && list.Batches(Layer::Background).back().texts.size() == 2, "what lies outside the window is not kept"); list.SetLayer(Layer::Content); list.PushClip({0, 100, 400, 200}); list.Rect({0, 150, 10, 10}, ink); list.Rect({0, 400, 10, 10}, ink); list.Circle({5, 405}, 5, ink); list.Text("cut", 0, 420, FontFace::Regular, 16, ink); const std::span content = list.Batches(Layer::Content); Check(content.size() == 1 && content[0].clip == std::optional(Box{0, 100, 400, 200}) && content[0].rects.size() == 1 && content[0].circles.empty() && content[0].texts.empty(), "a clip cuts a pass to its box, and what lies outside it is not kept"); list.PushClip({0, 250, 400, 400}); list.Rect({0, 260, 10, 10}, ink); Check(list.Batches(Layer::Content).back().clip == std::optional(Box{0, 250, 400, 50}), "a clip within a clip is what the two share"); list.PopClip(); list.Rect({0, 150, 10, 10}, ink); list.PopClip(); list.Rect({0, 150, 10, 10}, ink); Check(list.Batches(Layer::Content).size() == 4 && !list.Batches(Layer::Content).back().clip, "every change of the clip is a pass of its own"); list.PushClip({300, 700, 400, 400}); list.Rect({350, 750, 10, 10}, ink); Check(list.Batches(Layer::Content).back().clip == std::optional(Box{300, 700, 100, 100}), "a clip reaches no further than the window"); list.PopClip(); list.SetLayer(Layer::Popup); list.Text("top", 0, 20, FontFace::Regular, 16, ink); list.SetLayer(Layer::Chrome); list.Text("bar", 0, 20, FontFace::Regular, 16, ink); Equal(list.Texts() == std::vector{"hello", "x", "abcd", "abcd", "bar", "top"}, true, "the texts in the order they are drawn: by layer, whatever order they were added in"); Equal(list.BatchCount(), std::size_t{10}, "every pass counted"); } void Colours() { const Theme light = Theme::Light(); const Theme dark = Theme::Dark(); Check(!light.dark && dark.dark && light.background.Luminance() > 0.9f && dark.background.Luminance() < 0.1f && dark.text.Luminance() > light.text.Luminance(), "a light theme and a dark one"); Theme theme; theme.Accent({0.9f, 0.9f, 0.2f, 0.5f}); Check(theme.accent == Color{0.9f, 0.9f, 0.2f, 1.0f} && theme.onAccent.Luminance() < 0.2f, "a bright accent takes dark text, and is opaque"); theme.Accent({0.1f, 0.2f, 0.6f}); Check(theme.onAccent == Color{1, 1, 1}, "a dark accent takes light text"); Check(light.Hovered(light.surface).Luminance() < light.surface.Luminance() && light.Pressed(light.surface).Luminance() < light.Hovered(light.surface).Luminance(), "on a light theme a control darkens under the pointer, more when pressed"); Check(dark.Hovered(dark.surface).Luminance() > dark.surface.Luminance(), "on a dark one it lightens"); Check(light.Taken() == light.surface && light.Taken().Luminance() > light.surfaceAlt.Luminance() && dark.Taken().Luminance() > dark.surfaceAlt.Luminance(), "the one taken of a few choices is lighter than the quiet ground they stand on, on either"); } void Pressing() { Context ui(Test::FixedMeasure()); Check(!ui.Built() && ui.Regions().empty() && ui.Find("a") == nullptr, "nothing is built at first"); std::vector taps; bool second = false; // the screen after something changed: another button where "a" was const auto scene = [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); frame.Add(Pressable(second ? "other" : "a", {0, 0, 100, 50}, [&taps, second] { taps.push_back(second ? "other" : "a"); })); Region disabled = Pressable("off", {0, 60, 100, 50}, [&taps] { taps.push_back("off"); }); disabled.enabled = false; frame.Add(std::move(disabled)); Region held = Pressable("row", {0, 120, 100, 50}, [&taps] { taps.push_back("row"); }); held.role = Role::Row; held.onLongPress = [&taps] { taps.push_back("row held"); }; frame.Add(std::move(held)); Region inert; inert.box = {0, 180, 100, 50}; inert.key = "inert"; frame.Add(std::move(inert)); Check(frame.Dp(16) == 16 && frame.sizeClass == SizeClass::Compact && frame.bounds == window && frame.Colors().accent == ui.theme.accent, "a frame knows its window, its scale and its colours"); }; Build(ui, scene); Check(ui.Built() && ui.Regions().size() == 4 && ui.Find("row") != nullptr && ui.Find("row")->role == Role::Row && ui.Shown().bounds == window, "a frame's regions are the ones events act on"); TapAt(ui, {50, 25}); Equal(taps == std::vector{"a"}, true, "a tap on a button runs its action"); TapAt(ui, {50, 85}); TapAt(ui, {50, 205}); TapAt(ui, {300, 700}); Equal(taps.size(), std::size_t{1}, "a tap on a disabled button, on one that does nothing, or beside them all acts on nothing"); // The press shows on what it went down on. ui.Pointer(PointerPhase::Down, {50, 25}, 1000); Build(ui, [&](Frame& frame) { scene(frame); Check(frame.Pressed("a") && !frame.Pressed("row"), "what is pressed is drawn pressed"); }); ui.Pointer(PointerPhase::Up, {50, 25}, 1100); Equal(taps.size(), std::size_t{2}, "a frame built during a press that keeps the button where it was leaves the tap its own"); // The screen is read again while the finger is down, and another button is where the press went down. ui.Pointer(PointerPhase::Down, {50, 25}, 2000); second = true; Build(ui, scene); ui.Pointer(PointerPhase::Up, {50, 25}, 2100); Equal(taps.size(), std::size_t{2}, "a release on something else than the press went down on acts on nothing"); TapAt(ui, {50, 25}, 2200); Equal(taps.back(), std::string("other"), "a press and a release on it do"); second = false; // Whoever changes what the screens are built from says so: the press that is down is cancelled. Build(ui, scene); ui.Pointer(PointerPhase::Down, {50, 25}, 3000); ui.Invalidate(); Check(!ui.Built() && ui.Regions().empty(), "an invalidated frame has no regions to act on"); TapAt(ui, {50, 25}, 3050); Build(ui, scene); ui.Pointer(PointerPhase::Up, {50, 25}, 3100); Equal(taps.size(), std::size_t{3}, "neither a tap in between nor the release of the press it cancelled acts"); // A long press: the window's timer, by Tick. ui.Pointer(PointerPhase::Down, {50, 145}, 4000); Equal(ui.Take().wakeAtMs == std::optional(4500), true, "a press on what can be held asks to be woken at its long-press time"); ui.Tick(4400); Equal(taps.size(), std::size_t{3}, "before that time nothing happens"); ui.Tick(4500); Equal(taps.back(), std::string("row held"), "at it, the long press"); Build(ui, [&](Frame& frame) { scene(frame); Check(!frame.Pressed("row"), "what was held is no longer drawn pressed"); }); ui.Pointer(PointerPhase::Up, {50, 145}, 4700); Equal(taps.size(), std::size_t{4}, "and its release is no tap"); ui.Pointer(PointerPhase::Down, {50, 25}, 5000); Check(!ui.Take().wakeAtMs, "a press on what cannot be held waits for nothing"); ui.Pointer(PointerPhase::Up, {50, 25}, 5900); Equal(taps.back(), std::string("a"), "and is a tap however long it was held"); ui.Secondary({50, 145}); Equal(taps.back(), std::string("row held"), "the secondary button is a long press"); ui.Secondary({50, 25}); ui.Secondary({50, 85}); ui.Secondary({300, 700}); Equal(taps.size(), std::size_t{6}, "on what has none, on a disabled region and on nothing it is nothing"); // By name, as a test or a debugger taps. Check(ui.Tap("row") && taps.back() == "row" && ui.LongPress("row") && taps.back() == "row held", "a tap and a long press by name"); Check(!ui.Tap("off") && !ui.Tap("nowhere") && !ui.LongPress("off") && !ui.LongPress("nowhere"), "not on a disabled region, nor on one that is not there"); Check(ui.Tap("inert") && ui.LongPress("a"), "a region without the action takes it and does nothing"); // The pointer over a region. ui.Hover(Point{50, 25}); Check(ui.Take().redraw, "the pointer coming over a button asks for a frame"); Build(ui, [&](Frame& frame) { scene(frame); Check(frame.Hovered("a") && !frame.Hovered("row"), "what the pointer is over is drawn so"); }); ui.Hover(Point{50, 30}); Check(!ui.Take().redraw, "moving on within it asks for none"); ui.Hover(Point{50, 85}); Build(ui, [&](Frame& frame) { scene(frame); Check(!frame.Hovered("a") && !frame.Hovered("off"), "a disabled region does not show the pointer"); }); ui.Hover(Point{50, 145}); ui.Hover(std::nullopt); Build(ui, [&](Frame& frame) { scene(frame); Check(!frame.Hovered("row"), "nor does anything once the pointer has left the window"); }); } void Layers() { Context ui(Test::FixedMeasure()); std::vector taps; bool sheet = true; const auto scene = [&](Frame& frame) { if(sheet) { // The sheet is added first, and is on top all the same: its layer is. frame.draw.SetLayer(Layer::Overlay); Region scrim; scrim.box = frame.bounds; scrim.key = "scrim"; scrim.role = Role::Scrim; scrim.onTap = [&taps] { taps.push_back("scrim"); }; frame.Add(std::move(scrim)); Region panel; panel.box = {0, 600, 400, 200}; panel.key = "panel"; panel.role = Role::Panel; frame.Add(std::move(panel)); frame.Add(Pressable("ok", {20, 700, 100, 50}, [&taps] { taps.push_back("ok"); })); frame.Add(Pressable("cancel", {140, 700, 100, 50}, [&taps] { taps.push_back("cancel"); })); } frame.draw.SetLayer(Layer::Content); frame.Add(Pressable("under", {20, 700, 100, 50}, [&taps] { taps.push_back("under"); })); frame.Add(Pressable("beside", {20, 100, 100, 50}, [&taps] { taps.push_back("beside"); })); }; Build(ui, scene); Check(ui.Regions().front().key == "under" && ui.Regions().back().key == "cancel", "the regions are kept from the bottom up, by layer"); TapAt(ui, {50, 725}); TapAt(ui, {50, 125}); TapAt(ui, {300, 650}); Equal(taps == std::vector{"ok", "scrim"}, true, "a sheet's button is reached, what it covers is not: a tap beside the sheet is the scrim's, one on its ground nobody's"); Check(!ui.Tap("under") && !ui.Tap("beside") && ui.Tap("ok"), "nor is what a sheet covers reached by name"); // The focus stays within the sheet. ui.KeyDown(Key::Tab, {}); Equal(ui.Focused(), std::string_view("ok"), "Tab reaches the sheet's first button"); ui.KeyDown(Key::Tab, {}); ui.KeyDown(Key::Tab, {}); Equal(ui.Focused(), std::string_view("ok"), "and goes round within the sheet"); ui.KeyDown(Key::Tab, {.shift = true}); Equal(ui.Focused(), std::string_view("cancel"), "Shift with Tab goes back"); ui.KeyDown(Key::Enter, {}); ui.KeyDown(Key::Enter, {}, true); ui.KeyDown(Key::Space, {}); Equal(taps.size() == 5 && taps.back() == "cancel", true, "Enter and Space press what has the focus, once for a press: a key held down is not a row of them"); Build(ui, [&](Frame& frame) { scene(frame); Check(frame.Focused("cancel") && frame.FocusRing("cancel") && !frame.FocusRing("ok"), "the focus the keyboard gave is shown"); }); TapAt(ui, {50, 725}); Build(ui, [&](Frame& frame) { scene(frame); Check(frame.Focused("cancel") && !frame.FocusRing("cancel"), "and no longer once the pointer is used"); }); sheet = false; Build(ui, scene); Equal(ui.Focused(), std::string_view(), "what had the focus is gone with the sheet: nothing has it"); ui.KeyDown(Key::Up, {}); Equal(ui.Focused(), std::string_view("beside"), "Up from nothing is the last region"); ui.KeyDown(Key::Down, {}); Equal(ui.Focused(), std::string_view("under"), "Down goes on, and round"); ui.KeyDown(Key::Up, {}, true); Equal(ui.Focused(), std::string_view("beside"), "a held arrow moves on"); ui.Focus("under"); Equal(ui.Focused(), std::string_view("under"), "the focus given by name"); ui.Focus({}); Equal(ui.Focused(), std::string_view(), "and taken away"); ui.KeyDown(Key::Enter, {}); Equal(taps.size(), std::size_t{6}, "Enter with no focus presses nothing"); Context bare(Test::FixedMeasure()); Build(bare, [](Frame&) {}); bare.KeyDown(Key::Tab, {}); Equal(bare.Focused(), std::string_view(), "with nothing to focus Tab does nothing"); } void Keys() { Context ui(Test::FixedMeasure()); std::vector seen; bool takes = true; const auto scene = [&](Frame& frame) { frame.Shortcut(Key::Escape, {}, [&seen] { seen.push_back("back"); }); frame.Shortcut(Key::F, {.control = true}, [&seen] { seen.push_back("find"); }); frame.Shortcut(Key::Escape, {}, [&seen] { seen.push_back("close"); }); Region tabs = Pressable("tabs", {0, 0, 100, 50}, [&seen] { seen.push_back("tabs"); }); tabs.onKey = [&seen, &takes](Key key, Modifiers) { if(takes && (key == Key::Left || key == Key::Right)) { seen.push_back(key == Key::Left ? "left" : "right"); return true; } return false; }; frame.Add(std::move(tabs)); }; Build(ui, scene); ui.KeyDown(Key::Escape, {}); Equal(seen.back(), std::string("close"), "a shortcut acts; of two for one key the one added last"); ui.KeyDown(Key::F, {}); ui.KeyDown(Key::F, {.shift = true, .control = true}); ui.KeyDown(Key::R, {.control = true}); ui.KeyDown(Key::Escape, {}, true); Equal(seen.size(), std::size_t{1}, "not with other modifiers, not another key, and not for a key held down"); ui.KeyDown(Key::F, {.control = true}); Equal(seen.back(), std::string("find"), "with its own modifiers it does"); ui.Focus("tabs"); ui.KeyDown(Key::Right, {}); ui.KeyDown(Key::Left, {}); Check(seen.size() == 4 && seen.back() == "left" && ui.Take().redraw, "what has the focus sees a key first, and may take it"); ui.KeyDown(Key::Escape, {}); Equal(seen.back(), std::string("close"), "a key it leaves goes on to the shortcuts"); ui.KeyDown(Key::Enter, {}); Equal(seen.back(), std::string("tabs"), "or presses it"); takes = false; ui.KeyDown(Key::Left, {}); Equal(seen.size(), std::size_t{6}, "an arrow nobody takes is nothing"); ui.Invalidate(); ui.KeyDown(Key::Escape, {}); ui.KeyDown(Key::Tab, {}); Equal(seen.size(), std::size_t{6}, "without a frame a key acts on nothing"); // What a scrim covers is out of the keyboard's reach too: a sheet that cannot be sent away leaves Escape with // nothing to do, and one that can has it to itself. const auto covered = [&](bool dismissible) { Build(ui, [&](Frame& frame) { scene(frame); frame.draw.SetLayer(Layer::Overlay); Region scrim; scrim.box = frame.bounds; scrim.key = "scrim"; scrim.role = Role::Scrim; frame.Add(std::move(scrim)); if(dismissible) { frame.Shortcut(Key::Escape, {}, [&seen] { seen.push_back("dismiss"); }); } }); }; covered(false); ui.KeyDown(Key::Escape, {}); ui.KeyDown(Key::F, {.control = true}); Equal(seen.size(), std::size_t{6}, "under a scrim the screen's shortcuts do nothing"); covered(true); ui.KeyDown(Key::Escape, {}); Equal(seen.back(), std::string("dismiss"), "and what is over it has its own"); } void Scrolling() { Context ui(Test::FixedMeasure()); std::vector taps; float contentHeight = 2000; bool covered = false; float offset = 0; const auto scene = [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); offset = frame.BeginScroll("list", {0, 100, 400, 600}); for(std::int32_t row = 0; row < 20; row++) { frame.Add(Pressable(std::format("row{}", row), {0, 100 + 100.0f * static_cast(row) - offset, 400, 100}, [&taps, row] { taps.push_back(std::format("row{}", row)); })); } frame.EndScroll(contentHeight); frame.draw.SetLayer(Layer::Chrome); Region bar; bar.box = {0, 0, 400, 100}; bar.key = "bar"; bar.role = Role::Panel; frame.Add(std::move(bar)); if(covered) { frame.draw.SetLayer(Layer::Overlay); Region scrim; scrim.box = frame.bounds; scrim.key = "scrim"; scrim.role = Role::Scrim; frame.Add(std::move(scrim)); } }; Build(ui, scene); Check(ui.Find("list") != nullptr && ui.Find("list")->role == Role::Scroll && ui.Scroll("list").viewport == 600 && ui.Scroll("list").content == 2000, "a scrolling area knows its viewport and its content"); Check(ui.Find("row0")->scroll == "list" && ui.Find("row0")->scrollTop == 100 && ui.Find("row8")->hit.Empty() && ui.Find("row6")->hit == Box{0, 700, 400, 0}.Intersect({0, 100, 400, 600}), "what is in it can be pressed only where its viewport shows it"); Check(ui.Find("row5")->hit == Box{0, 600, 400, 100} && !ui.Tap("row8") && ui.Tap("row5"), "a row scrolled out of sight is not there to tap"); // The wheel. ui.Wheel({200, 300}, 2); Check(ui.Scroll("list").offset == 96 && ui.Take().redraw, "a turn of the wheel moves the list under the pointer by 48 a detent"); ui.Wheel({200, 50}, 2); ui.Wheel({200, 750}, 2); Equal(ui.Scroll("list").offset, 96.0f, "not over a bar that lies above the list, nor beside it"); Build(ui, scene); Equal(offset, 96.0f, "the next frame is drawn at the new offset"); // A drag moves the list with the finger, and its row is not tapped. taps.clear(); ui.Hover(Point{200, 400}); ui.Pointer(PointerPhase::Down, {200, 400}, 1000); ui.Pointer(PointerPhase::Move, {200, 380}, 1016); Equal(ui.Scroll("list").offset, 116.0f, "a drag up moves the content up"); Build(ui, [&](Frame& frame) { scene(frame); Check(!frame.Pressed("row3") && !frame.Hovered("row3"), "the row the drag began on is no longer drawn pressed, nor as under the pointer"); }); ui.Pointer(PointerPhase::Move, {200, 330}, 1032); Equal(ui.Scroll("list").offset, 166.0f, "and on"); ui.Pointer(PointerPhase::Up, {200, 280}, 1048); Check(taps.empty(), "its release taps nothing"); Context::Wants wants = ui.Take(); Check(wants.animating && wants.redraw && ui.Scroll("list").velocity > 1000, "a fast release flings the list on"); ui.Tick(1064); Check(ui.Scroll("list").offset > 166 && ui.Take().animating, "which moves on with time"); ui.Tick(9000); const float flung = ui.Scroll("list").offset; Check(flung < 166 + 1000 * 0.1f + 5000 * 0.05f, "by a step of a twentieth of a second at most: after a stall it does not jump"); ui.Pointer(PointerPhase::Down, {200, 400}, 9010); Equal(ui.Scroll("list").velocity, 0.0f, "a finger down stops the fling under it"); ui.Pointer(PointerPhase::Up, {200, 400}, 9020); ui.Tick(9040); Check(!ui.Take().animating && ui.Scroll("list").offset == flung, "and nothing moves any more"); ui.Scroll("list").Fling(30); ui.Tick(9090); Check(ui.Take().animating, "a slow fling moves on"); for(std::int64_t at = 9140; at < 9600; at += 50) { ui.Tick(at); } Check(!ui.Take().animating && ui.Scroll("list").velocity == 0 && ui.Scroll("list").offset > flung, "until it comes to rest by itself"); const float rested = ui.Scroll("list").offset; // A drag that begins on a bar, or under a sheet, moves nothing. ui.Pointer(PointerPhase::Down, {200, 50}, 10000); ui.Pointer(PointerPhase::Move, {200, 90}, 10016); ui.Pointer(PointerPhase::Up, {200, 99}, 10032); covered = true; Build(ui, scene); ui.Pointer(PointerPhase::Down, {200, 400}, 11000); ui.Pointer(PointerPhase::Move, {200, 300}, 11016); ui.Pointer(PointerPhase::Up, {200, 200}, 11032); ui.Wheel({200, 400}, 3); Equal(ui.Scroll("list").offset, rested, "a drag on a bar, or on what a sheet covers, moves nothing; nor does the wheel there"); covered = false; Build(ui, scene); // The keys. ui.Scroll("list").offset = 0; ui.KeyDown(Key::PageDown, {}); Equal(ui.Scroll("list").offset, 540.0f, "Page Down turns nine tenths of a page"); ui.KeyDown(Key::PageUp, {}); Equal(ui.Scroll("list").offset, 0.0f, "Page Up turns it back"); Context bare(Test::FixedMeasure()); Build(bare, [](Frame&) {}); bare.KeyDown(Key::PageDown, {}); Check(!bare.Take().redraw, "with nothing to scroll they do nothing"); // The content got shorter than where the list was scrolled to. ui.Scroll("list").offset = 1400; contentHeight = 800; ui.Take(); Build(ui, scene); Check(ui.Scroll("list").offset == 200 && ui.Take().redraw, "a list scrolled past its new end is brought back, and drawn again"); // The focus brings its region into view. contentHeight = 2000; Build(ui, scene); ui.Focus("row15"); Equal(ui.Scroll("list").offset, 1600 + 16 - 600.0f, "the focus on a row below scrolls it up to the viewport's bottom edge, a margin clear"); Build(ui, scene); ui.Focus("row2"); Equal(ui.Scroll("list").offset, 200 - 16.0f, "and one above down to its top edge"); ui.Focus("bar"); Equal(ui.Scroll("list").offset, 184.0f, "the focus on something outside any list scrolls nothing"); // A list that came to rest between two pixels is drawn on one: its rows' text and hairlines stay sharp. ui.Scroll("list").ScrollBy(0.4f); Build(ui, scene); Check(ui.Scroll("list").offset == 184.4f && offset == 184.0f, "the content moves by whole pixels"); ui.Scroll("list").ScrollBy(0.2f); Build(ui, scene); Equal(offset, 185.0f, "to the nearest one"); } // A list that a finger can have read again: pulled down at its top, and let go. void Pulling() { Context ui(Test::FixedMeasure()); std::int32_t refreshed = 0; bool refreshable = true; float offset = 0; float pulled = 0; float otherPulled = 0; const auto scene = [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); offset = frame.BeginScroll("list", {0, 100, 400, 500}); if(refreshable) { frame.Refreshable([&refreshed] { refreshed++; }); } pulled = frame.Pulled(); frame.EndScroll(2000); frame.BeginScroll("other", {0, 600, 400, 200}); otherPulled = frame.Pulled(); frame.EndScroll(1000); }; const auto drag = [&](float from, float to, std::int64_t at) { ui.Pointer(PointerPhase::Down, {200, from}, at); ui.Pointer(PointerPhase::Move, {200, to}, at + 100); Build(ui, scene); }; Build(ui, scene); Check(ui.Find("list")->onRefresh != nullptr && ui.Find("other")->onRefresh == nullptr && pulled == 0, "a scrolling area can offer to be read again"); drag(300, 400, 1000); Check(pulled == 50 && offset == 0 && ui.Scroll("list").offset == 0 && otherPulled == 0, "dragged down at its top it gives way, by half the finger's way"); ui.Pointer(PointerPhase::Move, {200, 360}, 1200); Build(ui, scene); Equal(pulled, 30.0f, "and comes back up with the finger"); ui.Pointer(PointerPhase::Up, {200, 360}, 1300); Build(ui, scene); Check(refreshed == 0 && pulled == 0, "let go before it is far enough down, it is not read again"); drag(300, 440, 2000); Equal(pulled, 70.0f, "pulled further"); ui.Pointer(PointerPhase::Up, {200, 440}, 2200); Equal(refreshed, 1, "let go from far enough down, it is read again, once"); Build(ui, scene); Equal(pulled, 0.0f, "and is back where it was"); // Pushed back up past its top, it scrolls as any list. drag(300, 340, 3000); ui.Pointer(PointerPhase::Move, {200, 200}, 3200); Check(ui.Scroll("list").offset == 0, "the way back up first takes the pull away"); ui.Pointer(PointerPhase::Move, {200, 150}, 3300); Equal(ui.Scroll("list").offset, 50.0f, "and then moves the list"); ui.Pointer(PointerPhase::Cancel, {200, 150}, 3400); ui.Scroll("list").offset = 300; drag(300, 400, 4000); Check(ui.Scroll("list").offset == 200 && pulled == 0, "a list that is not at its top is moved, not pulled"); ui.Pointer(PointerPhase::Cancel, {200, 400}, 4200); ui.Scroll("list").offset = 0; drag(300, 440, 5000); ui.Pointer(PointerPhase::Cancel, {200, 440}, 5200); Build(ui, scene); Check(refreshed == 1 && pulled == 0, "a pull the compositor took away reads nothing"); drag(300, 440, 6000); ui.Invalidate(); ui.Pointer(PointerPhase::Up, {200, 440}, 6200); Build(ui, scene); Check(refreshed == 1 && pulled == 0, "nor one the screen changed under"); drag(300, 440, 7000); refreshable = false; Build(ui, scene); ui.Pointer(PointerPhase::Up, {200, 440}, 7200); Equal(refreshed, 1, "nor one whose list no longer offers it when the finger lets go"); drag(650, 750, 8000); Check(otherPulled == 0 && ui.Scroll("other").offset == 0, "a list that offers none stays where it is"); ui.Pointer(PointerPhase::Cancel, {200, 750}, 8200); } // A form of three fields over a ground that takes taps: a name, a key that is a secret, and an amount. struct Form { EditState name; EditState key{FieldKind::Secret}; EditState amount{FieldKind::Decimal}; std::int32_t changes = 0; std::int32_t submits = 0; bool nameEnabled = true; bool withName = true; void Draw(Frame& frame) { frame.draw.SetLayer(Layer::Content); Region ground; ground.box = frame.bounds; ground.key = "ground"; ground.role = Role::Panel; frame.Add(std::move(ground)); Column column(frame.bounds.Inset(Insets::All(16)), 12); if(withName) { column.Advance(TextField(frame, column.Next(), name, {.key = "name", .label = "Name", .placeholder = "Who", .enabled = nameEnabled, .onChange = [this] { changes++; }, .onSubmit = [this] { submits++; }})); } column.Advance(TextField(frame, column.Next(), key, {.key = "key", .label = "API key", .helper = "From the bunq app"})); column.Advance(TextField(frame, column.Next(), amount, {.key = "amount", .label = "Amount", .prefix = "€", .error = "Too much", .purpose = TextPurpose::Number, .hints = TextHint::lowercase})); } }; void Fields() { Context ui(Test::FixedMeasure()); // How tall a field comes out can be asked before it is drawn: its label, the field, and what is said under it, // a line of which is 15 pixels. EditState measured; Build(ui, [&measured](Frame& frame) { const FieldOptions bare{.key = "bare"}; const FieldOptions helped{.key = "helped", .label = "Name", .helper = "Who the account belongs to, as the bank knows them"}; const FieldOptions wrong{.key = "wrong", .label = "Amount", .error = "Too much", .helper = "Not shown"}; Check(FieldHeight(frame, 368, bare) == 48 && TextField(frame, {16, 0, 368, 0}, measured, bare) == 48, "a field alone is as tall as a control"); const float tall = FieldHeight(frame, 120, helped); Check(tall > 24 + 48 + 4 + 15 && TextField(frame, {16, 100, 120, 0}, measured, helped) == tall, "with its label above it and a helper that wraps under it, it is as tall as it is said to be beforehand"); Check(FieldHeight(frame, 368, wrong) == 24 + 48 + 4 + 15 && TextField(frame, {16, 300, 368, 0}, measured, wrong) == 24 + 48 + 4 + 15, "an error stands in the helper's place"); }, 0); Form form; const auto build = [&](std::int64_t nowMs = 0) { Build(ui, [&form](Frame& frame) { form.Draw(frame); }, nowMs); }; build(); Shows(ui, "Who", "an empty field shows its placeholder"); Shows(ui, "From the bunq app", "a field's helper is under it"); Shows(ui, "Too much", "and its error"); Check(!ui.Take().textInput.active, "with no field focused no text input is asked for"); ui.Text("lost"); ui.Preedit("lost"); ui.DeleteAround(1, 1); ui.Paste("lost"); Check(form.name.Empty() && form.changes == 0, "what is typed with no field focused goes nowhere"); // The name's field is at 16, 40, 368 by 48; its text starts at 28, eight pixels a character. TapAt(ui, {100, 64}); Equal(ui.Focused(), std::string_view("name"), "a tap on a field gives it the focus"); build(); Context::Wants wants = ui.Take(); Check(wants.textInput.active && wants.textInput.purpose == TextPurpose::Normal && wants.textInput.hints == TextHint::prose && wants.textInput.caret == Box{28, 52, 2, 24}, "and asks for text input there: prose, the caret where the text starts"); const std::size_t firstSerial = wants.textInput.field; ui.Text("hello"); Check(form.name.value == "hello" && form.changes == 1, "what is typed goes into it, and it says it changed"); build(); Shows(ui, "hello", "and is drawn"); Hides(ui, "Who", "in place of the placeholder"); wants = ui.Take(); Check(wants.textInput.caret == Box{68, 52, 2, 24} && wants.textInput.field == firstSerial, "the caret moved along, in the same field"); TapAt(ui, {28 + 17, 64}); Equal(form.name.cursor, std::size_t{2}, "a tap puts the cursor at the nearest character boundary"); ui.Text("X"); Equal(form.name.value, std::string("heXllo"), "and typing goes on there"); ui.KeyDown(Key::Backspace, {}); Check(form.name.value == "hello" && form.changes == 3, "Backspace deletes, which is a change"); ui.KeyDown(Key::Backspace, {}); ui.KeyDown(Key::Backspace, {}, true); ui.KeyDown(Key::Backspace, {}, true); Check(form.name.value == "llo" && form.changes == 5, "held down it deletes on, and at the start deletes nothing and changes nothing"); ui.KeyDown(Key::End, {}); ui.KeyDown(Key::Left, {.shift = true}); Check(form.name.Selected() == "o" && form.changes == 5, "Shift with an arrow selects, which is no change"); build(); ui.KeyDown(Key::Home, {}); ui.KeyDown(Key::Delete, {}); ui.KeyDown(Key::Right, {}); Check(form.name.value == "lo" && form.name.cursor == 1, "Home, Delete and the arrows edit"); // The clipboard. ui.KeyDown(Key::C, {.control = true}); Check(!ui.Take().copy, "with nothing selected Ctrl+C copies nothing"); ui.KeyDown(Key::A, {.control = true}); ui.KeyDown(Key::C, {.control = true}); wants = ui.Take(); Check(wants.copy == std::optional("lo") && form.name.value == "lo", "Ctrl+A selects everything and Ctrl+C asks for it to be copied"); Check(!ui.Take().copy, "once"); ui.KeyDown(Key::X, {.control = true}); wants = ui.Take(); Check(wants.copy == std::optional("lo") && form.name.Empty() && form.changes == 7, "Ctrl+X copies it and takes it out"); ui.KeyDown(Key::V, {.control = true}); wants = ui.Take(); Check(wants.paste && !ui.Take().paste, "Ctrl+V asks for the clipboard"); ui.Paste("Bob\n"); Check(form.name.value == "Bob " && form.changes == 8, "which goes in as typed text does"); bool found = false; Build(ui, [&](Frame& frame) { frame.Shortcut(Key::R, {.control = true}, [&found] { found = true; }); form.Draw(frame); }); ui.KeyDown(Key::R, {.control = true}); ui.KeyDown(Key::F5, {}); Check(found && form.name.value == "Bob ", "a key that edits nothing goes on to the shortcuts"); ui.KeyDown(Key::Enter, {}); ui.KeyDown(Key::Enter, {}, true); Equal(form.submits, 1, "Enter submits, once for a press"); // The input method's composing text. form.name.Set("hello"); ui.Preedit("ab"); build(); Shows(ui, "ab", "the composing text is drawn, in a run of its own"); Equal(ui.Take().textInput.caret.x, 28 + 7 * 8.0f, "the caret is after it"); TapAt(ui, {28 + 6 * 8 - 1, 64}); Check(form.name.preedit == "ab" && form.name.cursor == 5 && ui.Take().textInput.field == firstSerial, "a tap inside the composing text leaves it to the input method"); ui.DeleteAround(2, 0); Check(form.name.value == "hel" && form.name.preedit == "ab", "the input method's deletion takes what is around the cursor"); TapAt(ui, {28 + 8, 64}); build(); wants = ui.Take(); Check(form.name.value == "helab" && form.name.preedit.empty() && form.name.cursor == 1 && wants.textInput.field != firstSerial, "a tap outside it takes the word into the field where it was typed, moves the cursor, and has the input method start afresh"); const std::size_t secondSerial = wants.textInput.field; ui.RestartInput(); build(); Check(ui.Take().textInput.field != secondSerial, "as it does when asked to"); // A secret. ui.Preedit("xy"); TapAt(ui, {100, 148}); Check(ui.Focused() == "key" && form.name.value == "hxyelab" && form.name.preedit.empty(), "the focus moving on takes the composing word into the field it leaves"); ui.Text("sandbox aé€"); build(); wants = ui.Take(); Check(form.key.value == "sandboxaé€" && wants.textInput.purpose == TextPurpose::Password && wants.textInput.hints == TextHint::secret, "a secret asks the keyboard to show and learn nothing"); Shows(ui, "••••••••••", "it is drawn as a dot for each character"); Hides(ui, "sandbox", "and never as itself"); Equal(wants.textInput.caret.x, 28 + 10 * 8.0f, "the caret after the dots"); TapAt(ui, {28 + 8 * 8 + 3, 148}); Equal(form.key.cursor, std::size_t{8}, "a tap among the dots puts the cursor after that many characters"); TapAt(ui, {28 + 9 * 8 + 3, 148}); Equal(form.key.cursor, std::size_t{10}, "counted in the text's own bytes"); ui.KeyDown(Key::A, {.control = true}); ui.KeyDown(Key::C, {.control = true}); ui.KeyDown(Key::X, {.control = true}); Check(!ui.Take().copy && form.key.value == "sandboxaé€", "a secret is neither copied nor cut"); // The amount, whose options ask for a purpose and hints of their own. ui.KeyDown(Key::Tab, {}); build(); wants = ui.Take(); Check(ui.Focused() == "amount" && wants.textInput.purpose == TextPurpose::Number && wants.textInput.hints == TextHint::lowercase && wants.textInput.caret.x == 28 + 8 + 6.0f, "Tab moves on to the next field, whose text starts after its prefix"); ui.Text("12,50 EUR"); Equal(form.amount.value, std::string("12,50"), "an amount takes digits and a separator"); ui.KeyDown(Key::Up, {}); Equal(ui.Focused(), std::string_view("key"), "Up moves back a field"); // A text too long for the field: 43 characters fit. ui.Focus("name"); form.name.Set(std::format("{}END", std::string(50, 'a'))); build(); Shows(ui, std::format("{}END", std::string(40, 'a')), "with the focus, a long text is shown up to its cursor"); Equal(ui.Find("name")->field.first, std::size_t{10}, "from the first character that leaves the cursor room"); TapAt(ui, {20, 64}); Equal(form.name.cursor, std::size_t{10}, "a tap at the field's start is at the first character shown"); ui.Focus("key"); build(); Shows(ui, std::string(43, 'a'), "without it, from its start"); Hides(ui, "END", "as much as fits"); // Bare ground. ui.Preedit("zz"); TapAt(ui, {200, 700}); build(); Check(ui.Focused().empty() && form.key.preedit.empty() && form.key.value.ends_with("zz") && !ui.Take().textInput.active, "a tap on bare ground takes the focus away: the composing word stays in the field, and text input ends"); // A field that is disabled. form.nameEnabled = false; build(); TapAt(ui, {100, 64}); ui.KeyDown(Key::Tab, {}); Check(ui.Focused() == "key", "a disabled field takes no tap and no focus"); form.nameEnabled = true; // A field that is gone while it had the focus. ui.Focus("name"); build(); Check(ui.Take().textInput.active, "the focus by name asks for text input too"); form.withName = false; build(); Check(ui.Focused().empty() && !ui.Take().textInput.active, "a field that is no longer on the screen has no focus, and no text input"); form.withName = true; } void Carets() { Context ui(Test::FixedMeasure()); Form form; bool lit = false; const auto build = [&](std::int64_t nowMs) { Build(ui, [&](Frame& frame) { form.Draw(frame); lit = frame.CaretOn(); }, nowMs); }; build(1000); ui.Tick(1000); ui.Focus("name"); build(1000); Check(lit && ui.Take().wakeAtMs == std::optional(1500), "a field that got the focus shows its caret at once, and asks to be woken for the blink"); ui.Tick(1200); Check(!ui.Take().redraw, "within the half second nothing changes"); ui.Tick(1500); Check(ui.Take().redraw, "at it, a frame is due"); build(1500); Check(!lit && ui.Take().wakeAtMs == std::optional(2000), "in which the caret is dark, until the next half second"); ui.Tick(2000); build(2000); Check(lit, "when it is lit again"); ui.Tick(2300); ui.Text("a"); build(2300); Check(lit && ui.Take().wakeAtMs == std::optional(2800), "typing shows it at once, and the blink starts over from there"); ui.Tick(2800); build(2800); Check(!lit, "half a second on it is dark"); ui.Tick(12300); build(12300); Check(lit && ui.Take().redraw, "ten seconds after the last edit it stops blinking, lit"); ui.Tick(12800); ui.Tick(13300); Check(!ui.Take().redraw && !ui.Take().wakeAtMs, "and nothing wakes the window for it any more"); // Two things to wake for: the sooner one. ui.Text("b"); ui.Notice("Saved", 200); Equal(ui.Take().wakeAtMs == std::optional(13500), true, "a notice that goes before the caret's next blink wakes the window first"); ui.Notice("Saved", 2000); build(13300); Equal(ui.Take().wakeAtMs == std::optional(13800), true, "one that goes after it does not"); ui.Hover(Point{100, 64}); Build(ui, [&](Frame& frame) { form.Draw(frame); Check(frame.Hovered("name"), "a field shows the pointer over it"); }, 13300); } void Keyboards() { // A field that stays at the window's bottom edge, as a chat's would: the on-screen keyboard comes and goes under it. Context ui(Test::FixedMeasure()); EditState field; const auto build = [&](float height) { Build(ui, [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); Region ground; ground.box = frame.bounds; ground.key = "ground"; ground.role = Role::Panel; frame.Add(std::move(ground)); TextField(frame, {16, frame.bounds.h - 64, 368, 48}, field, {.key = "field"}); }, 0, {0, 0, 400, height}); }; build(800); TapAt(ui, {100, 760}); build(800); Context::Wants wants = ui.Take(); const TextInputRequest atFull = wants.textInput; Check(atFull.active && atFull.caret == Box{28, 748, 2, 24}, "text input is asked for at the caret"); build(500); const TextInputRequest raised = ui.Take().textInput; Check(raised.caret == Box{28, 448, 2, 24} && raised.field == atFull.field, "the keyboard came and the window shrank: the caret is told where it went"); build(800); Check(ui.Take().textInput == raised, "the keyboard went and the window grew: the caret only followed, and is not told, which would bring the keyboard back"); ui.Text("a"); build(800); Check(ui.Take().textInput.caret == Box{36, 748, 2, 24}, "once it moves for another reason it is"); build(500); build(800); TapAt(ui, {100, 760}); build(800); wants = ui.Take(); Check(wants.textInput.field != atFull.field && wants.textInput.caret == Box{36, 748, 2, 24}, "a tap on the field that kept the focus while its keyboard went asks for the keyboard again"); const std::size_t asked = wants.textInput.field; TapAt(ui, {100, 760}); build(800); Equal(ui.Take().textInput.field, asked, "with the keyboard up, a tap only places the cursor"); Check(!CaretFollowedGrowth({10, 100, 2, 24}, 500, {10, 100, 2, 24}, 500), "a window that did not grow moved no caret"); Check(!CaretFollowedGrowth({10, 100, 2, 24}, 500, {10, 100, 2, 24}, 400), "nor one that shrank"); Check(CaretFollowedGrowth({10, 100, 2, 24}, 500, {10.5f, 400, 2, 24}, 800), "a caret that went down by what the window gained followed it"); Check(!CaretFollowedGrowth({10, 100, 2, 24}, 500, {30, 400, 2, 24}, 800) && !CaretFollowedGrowth({10, 100, 2, 24}, 500, {10, 380, 2, 24}, 800) && !CaretFollowedGrowth({10, 100, 2, 24}, 500, {10, 400, 4, 24}, 800) && !CaretFollowedGrowth({10, 100, 2, 24}, 500, {10, 400, 2, 30}, 800), "one that moved along its text, by another distance, or changed its size did not"); // A field down a form that scrolls: when the keyboard takes the room it was in, it is brought into view. Context scrolled(Test::FixedMeasure()); EditState low; const auto form = [&](float height) { Build(scrolled, [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); const float offset = frame.BeginScroll("form", frame.bounds); TextField(frame, {16, 600 - offset, 368, 48}, low, {.key = "low"}); frame.EndScroll(1000); }, 0, {0, 0, 400, height}); }; form(800); scrolled.Focus("low"); form(800); Equal(scrolled.Scroll("form").offset, 0.0f, "a field in view stays where it is"); scrolled.Take(); form(400); Check(scrolled.Scroll("form").offset == 600 + 48 + 16 - 400.0f && scrolled.Take().redraw, "the window shrinking over it scrolls it back into view, and asks for the frame that shows it"); } void Menus() { Context ui(Test::FixedMeasure()); Form form; std::vector chosen; const auto build = [&] { Build(ui, [&form](Frame& frame) { form.Draw(frame); }); }; build(); Check(!ui.MenuOpen(), "no menu is open at first"); ui.CloseMenu(); Check(ui.Built(), "closing none changes nothing"); // Near the window's corner: the menu is moved inside. ui.OpenMenu({390, 790}, {{"Copy IBAN", [&chosen] { chosen.push_back("copy"); }}, {"Pay again", [&chosen] { chosen.push_back("pay"); }}, {"Remove", [&chosen] { chosen.push_back("remove"); }, true}}); Check(ui.MenuOpen() && !ui.Built(), "an open menu needs a frame before anything is pressed"); TapAt(ui, {100, 64}); Check(ui.Focused().empty(), "until then a tap reaches nothing"); build(); Shows(ui, "Pay again", "the menu's choices are drawn"); const Region* panel = ui.Find("menu.panel"); Check(panel != nullptr && panel->box == Box{400 - 8 - 160, 800 - 8 - (48 * 3 + 16), 160, 48 * 3 + 16} && panel->layer == Layer::Popup, "inside the window, above everything"); ui.Hover(Point{300, 700}); ui.Pointer(PointerPhase::Down, {300, 750}, 0); Build(ui, [&](Frame& frame) { form.Draw(frame); }); ui.Pointer(PointerPhase::Cancel, {300, 750}, 10); TapAt(ui, {100, 64}); build(); Check(!ui.MenuOpen() && ui.Focused().empty() && chosen.empty(), "a tap beside the menu sends it away, and reaches nothing else"); ui.OpenMenu({100, 100}, {{"Copy IBAN", [&chosen] { chosen.push_back("copy"); }}, {"More", [&ui, &chosen] { ui.OpenMenu({10, 10}, {{"Deeper", [&chosen] { chosen.push_back("deeper"); }}}); }}}); build(); Check(ui.Find("menu.panel")->box.x == 100 && ui.Find("menu.panel")->box.y == 100, "a menu opens at the point it was asked for"); ui.KeyDown(Key::Tab, {}); ui.KeyDown(Key::Enter, {}); ui.KeyDown(Key::Escape, {}, true); Check(ui.MenuOpen() && ui.Focused().empty() && chosen.empty(), "an open menu takes the keyboard"); Check(ui.Tap("menu.0") && chosen.back() == "copy" && !ui.MenuOpen(), "a choice acts, and the menu is gone"); ui.OpenMenu({100, 100}, {{"More", [&ui, &chosen] { ui.OpenMenu({10, 10}, {{"Deeper", [&chosen] { chosen.push_back("deeper"); }}}); }}}); build(); ui.Tap("menu.0"); build(); Check(ui.MenuOpen() && ui.Tap("menu.0") && chosen.back() == "deeper", "a choice may open another menu"); ui.OpenMenu({100, 100}, {{"Nothing", [] {}}}); build(); ui.KeyDown(Key::Escape, {}); Check(!ui.MenuOpen(), "Escape sends a menu away"); ui.OpenMenu({100, 100}, {{"Nothing", [] {}}}); ui.Invalidate(); Check(!ui.MenuOpen(), "and so does a change of what the screens are built from"); // A field's own menu: the clipboard, for a keyboard that has no key for it. build(); Check(ui.LongPress("name") && ui.Focused() == "name" && ui.MenuOpen(), "holding a field gives it the focus and opens its menu"); build(); Check(ui.Find("menu.0")->label == "Paste" && ui.Find("menu.1") == nullptr, "an empty field offers to paste"); ui.Tap("menu.0"); Check(ui.Take().paste, "which asks for the clipboard"); form.name.Set("Alice Example"); build(); ui.LongPress("name"); build(); Check(ui.Find("menu.1")->label == "Copy" && ui.Find("menu.2")->label == "Select all", "a field with text also offers to copy it and to select it"); ui.Tap("menu.1"); Equal(ui.Take().copy == std::optional("Alice Example"), true, "copying with nothing selected copies everything"); build(); ui.LongPress("name"); build(); form.name.preedit = "!"; ui.Tap("menu.2"); Check(form.name.Selected() == "Alice Example!" && form.name.preedit.empty(), "selecting takes the composing word in, and selects it all"); form.name.MoveTo(5); form.name.Apply(EditKey::Home, true); build(); ui.LongPress("name"); build(); ui.Tap("menu.1"); Equal(ui.Take().copy == std::optional("Alice"), true, "copying with a selection copies that"); form.key.Set("sandbox_key"); build(); ui.Secondary({100, 148}); build(); Check(ui.Focused() == "key" && ui.Find("menu.0")->label == "Paste" && ui.Find("menu.1")->label == "Select all" && ui.Find("menu.2") == nullptr, "a secret offers no copy"); // The field went while its menu was open: the choice acts on nothing. ui.CloseMenu(); build(); ui.LongPress("name"); form.withName = false; build(); ui.Tap("menu.1"); build(); ui.OpenMenu({0, 0}, {}); build(); Check(!ui.Take().copy && ui.MenuOpen(), "a choice for a field that is gone does nothing"); form.withName = true; ui.CloseMenu(); build(); ui.LongPress("name"); form.withName = false; build(); ui.Tap("menu.2"); Check(!form.name.HasSelection() || form.name.Selected() == "Alice", "nor does selecting in it"); } // What a sheet covers is out of the keyboard's reach, as it is out of a finger's. void Covering() { Context ui(Test::FixedMeasure()); std::vector ran; EditState field; bool sheet = false; const auto scene = [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); // A button that sends something, and then shows it under way in a sheet. frame.Add(Pressable("send", {0, 0, 100, 48}, [&] { ran.push_back("send"); sheet = true; ui.Invalidate(); })); TextField(frame, {16, 100, 368, 48}, field, {.key = "field"}); if(sheet) { frame.draw.SetLayer(Layer::Overlay); Region scrim; scrim.box = frame.bounds; scrim.key = "scrim"; scrim.role = Role::Scrim; frame.Add(std::move(scrim)); frame.Add(Pressable("ok", {0, 700, 100, 48}, [&ran] { ran.push_back("ok"); })); } }; Build(ui, scene); ui.Focus("send"); ui.KeyDown(Key::Enter, {}); ui.KeyDown(Key::Enter, {}); Equal(ran.size(), std::size_t{1}, "Enter presses the button that has the focus; a second one before the next frame presses nothing"); ui.Take(); Build(ui, scene); Check(ui.Focused().empty() && ui.Take().redraw, "the sheet its press brought takes the focus from it, and the frame that still drew the focus there is drawn again"); ui.KeyDown(Key::Enter, {}); ui.KeyDown(Key::Space, {}); Equal(ran.size(), std::size_t{1}, "so a second Enter does not press it again from under the sheet"); ui.Focus("send"); ui.KeyDown(Key::Enter, {}); Check(ran.size() == 1 && !ui.Tap("send"), "nor when the focus is put there by name; a tap by name does not reach it either"); Build(ui, scene); Check(ui.Focused().empty(), "and the next frame lets that focus go too"); ui.KeyDown(Key::Tab, {}); ui.KeyDown(Key::Enter, {}); Check(ui.Focused() == "ok" && ran.back() == "ok", "the sheet's own button takes the focus, and the key"); // A field under a sheet: what was being composed in it is kept, and nothing more is typed there. sheet = false; Build(ui, scene); TapAt(ui, {100, 124}); Build(ui, scene); ui.Text("ab"); ui.Preedit("cd"); Check(ui.Focused() == "field" && ui.Take().textInput.active, "a field with the focus has the keyboard"); sheet = true; Build(ui, scene); Check(field.value == "abcd" && field.preedit.empty() && ui.Focused().empty(), "a sheet over a field takes the focus from it, and the word being composed goes into it as it showed it"); Check(!ui.Take().textInput.active, "the on-screen keyboard is let go"); ui.Text("x"); ui.Paste("y"); ui.KeyDown(Key::Backspace, {}); Equal(field.value, std::string("abcd"), "and nothing is typed, pasted or deleted there from under the sheet"); // A menu is no sheet: it belongs to the field it was opened on, which keeps the focus and what is typed. sheet = false; Build(ui, scene); Check(ui.LongPress("field") && ui.MenuOpen(), "a field's own menu opens over it"); Build(ui, scene); ui.Text("e"); Check(ui.Focused() == "field" && field.value == "abcde", "and leaves it the focus, and what is typed"); } void Acting() { // What an action finds when it runs: every field whole, the word still being composed taken into it, and the // place of the press it answers to. Context ui(Test::FixedMeasure()); EditState field; std::vector ran; std::vector at; const auto scene = [&](Frame& frame) { frame.draw.SetLayer(Layer::Content); TextField(frame, {16, 100, 368, 48}, field, {.key = "field", .onSubmit = [&] { ran.push_back(std::format("submit {}", field.value)); }}); Region button = Pressable("button", {16, 300, 368, 48}, [&] { ran.push_back(std::format("tap {}", field.value)); at.push_back(ui.Pointed()); }); button.onLongPress = [&] { ran.push_back(std::format("held {}", field.value)); at.push_back(ui.Pointed()); }; frame.Add(std::move(button)); frame.Shortcut(Key::Escape, {}, [&] { ran.push_back(std::format("back {}", field.value)); }); }; Build(ui, scene); TapAt(ui, {100, 124}); Build(ui, scene); const std::size_t first = ui.Take().textInput.field; ui.Text("Hello "); ui.Preedit("wor"); Build(ui, scene); Equal(ui.Take().textInput.field, first, "composing a word is the same field's input"); TapAt(ui, {120, 320}, 1000); Check(ran == std::vector{"tap Hello wor"} && field.preedit.empty() && field.value == "Hello wor", "a tap's action finds the word being composed in its field"); Check(at.back() == Point{120, 320}, "and knows where the tap is"); Build(ui, scene); const std::size_t second = ui.Take().textInput.field; Check(ui.Focused() == "field" && second != first, "the field keeps the focus, and its input method starts afresh: the word is no longer its to change"); TapAt(ui, {120, 320}, 2000); Build(ui, scene); Check(ran.back() == "tap Hello wor" && ui.Take().textInput.field == second, "with nothing being composed a tap ends nothing"); ui.Preedit("ld"); Build(ui, scene); ui.Pointer(PointerPhase::Down, {130, 330}, 3000); ui.Tick(3600); ui.Pointer(PointerPhase::Up, {130, 330}, 3700); Check(ran.back() == "held Hello world" && at.back() == Point{130, 330}, "so does what a held press does"); Build(ui, scene); ui.Preedit("!"); ui.Secondary({140, 340}); Check(ran.back() == "held Hello world!" && at.back() == Point{140, 340}, "and the secondary button"); Build(ui, scene); ui.Preedit("?"); ui.KeyDown(Key::Enter, {}); Equal(ran.back(), std::string("submit Hello world!?"), "Enter submits the field with the word in it"); Build(ui, scene); ui.Preedit("!"); ui.KeyDown(Key::Escape, {}); Equal(ran.back(), std::string("back Hello world!?!"), "and a shortcut runs after it"); // An action asked for by name or by a key has no press: it answers to the middle of its region. Build(ui, scene); Check(ui.Tap("button") && at.back() == Point{200, 324}, "a tap by name is at the region's middle"); Check(ui.LongPress("button") && at.back() == Point{200, 324}, "and so is a long press by name"); at.push_back({}); ui.Focus("button"); ui.KeyDown(Key::Space, {}); Check(ran.back() == "tap Hello world!?!" && at.back() == Point{200, 324}, "and Space on what has the focus"); field.MoveTo(0); Build(ui, scene); Check(ui.Tap("field") && ui.Focused() == "field" && field.cursor == field.value.size(), "a tap by name on a field puts the cursor at the end of what it shows"); } void PuttingOff() { // What a screen puts off: run once, from the tick that reaches its time, which the window is woken for. Context ui(Test::FixedMeasure()); std::vector ran; ui.Tick(1000); Equal(ui.Now(), std::int64_t{1000}, "the context knows the time of its last tick"); ui.At("search", ui.Now() + 400, [&ran] { ran.push_back("search"); }); Check(ui.Take().wakeAtMs == std::optional(1400), "the window is woken when it is due"); ui.Tick(1399); Check(ran.empty(), "it does not run before its time"); ui.At("search", 1600, [&ran] { ran.push_back("search again"); }); ui.Tick(1500); Check(ran.empty() && ui.Take().wakeAtMs == std::optional(1600), "one of the same name takes its place, and its time"); ui.At("later", 1700, [&ran] { ran.push_back("later"); }); ui.At("sooner", 1650, [&ran, &ui] { ran.push_back("sooner"); ui.At("sooner", 5000, [&ran] { ran.push_back("sooner again"); }); }); ui.At("dropped", 1650, [&ran] { ran.push_back("dropped"); }); ui.Forget("dropped"); ui.Forget("never there"); ui.Take(); ui.Tick(1800); Check(ran == std::vector{"search again", "sooner", "later"} && ui.Take().redraw, "what is due runs in the order of its times, once, and the screen is drawn again"); Check(ui.Take().wakeAtMs == std::optional(5000), "what it put off in turn waits for its own time"); ui.Tick(6000); Equal(ran.back(), std::string("sooner again"), "and runs then"); Check(!ui.Take().wakeAtMs, "with nothing left to wake for"); } void Notices() { Context ui(Test::FixedMeasure()); const auto build = [&](std::int64_t nowMs) { Build(ui, [](Frame&) {}, nowMs); }; build(1000); ui.Tick(1000); ui.Notice("Copied"); Check(!ui.Built() && ui.Take().wakeAtMs == std::optional(3500), "a notice needs a frame, and the window woken when its time is up"); build(1000); Shows(ui, "Copied", "it is drawn"); Check(ui.Shown().draw.Batches(Layer::Popup).size() == 1, "above everything"); ui.Tick(3400); build(3400); Shows(ui, "Copied", "it stays for its time"); ui.Tick(3500); Check(!ui.Built(), "and is gone after it"); build(3500); Hides(ui, "Copied", "from the next frame on"); Check(!ui.Take().wakeAtMs, "with nothing left to wake for"); ui.Notice(std::string(200, 'w'), 100); build(3500); Shows(ui, "…", "a notice wider than the window is cut to it"); // A menu that opens while a notice shows is over it, not under. ui.Notice("Copied"); ui.OpenMenu({200, 600}, {{"Pay again", [] {}}}); build(3500); const std::vector said = ui.Shown().draw.Texts(); Check(std::ranges::find(said, "Copied") < std::ranges::find(said, "Pay again") && std::ranges::find(said, "Pay again") != said.end(), "the notice is drawn first, the menu after it"); ui.CloseMenu(); ui.Copy("NL23BUNQ2025123456"); Check(ui.Take().copy == std::optional("NL23BUNQ2025123456") && !ui.Take().copy, "what a screen asks to be copied is handed to the window once"); Build(ui, [](Frame& frame) { Check(frame.Dp(16) == 16 && frame.metrics.scale == 1, "a window that reports no scale is at a scale of one"); }, 3500, window, 0); ui.Redraw(); Check(ui.Take().redraw && !ui.Take().redraw, "and so is a frame it asks for"); Check(ui.Measure().advance(FontFace::Regular, 'x', 16) == 8 && ui.Wrapper().CacheSize() == 0, "the context gives out the font it measures with"); } } int main() { Drawing(); Colours(); Pressing(); Layers(); Keys(); Scrolling(); Pulling(); Fields(); Carets(); Keyboards(); Menus(); Covering(); Acting(); PuttingOff(); Notices(); return Test::Result(); }