310 lines
16 KiB
Text
310 lines
16 KiB
Text
|
|
// 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;
|
||
|
|
};
|
||
|
|
}
|