188 lines
8.7 KiB
Text
188 lines
8.7 KiB
Text
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
export module Nostro.Ui:Widgets;
|
||
|
|
|
||
|
|
import std;
|
||
|
|
import :Geometry;
|
||
|
|
import :Text;
|
||
|
|
import :Edit;
|
||
|
|
import :Draw;
|
||
|
|
import :Theme;
|
||
|
|
import :Input;
|
||
|
|
import :Context;
|
||
|
|
|
||
|
|
// What a screen is built from. Each widget is a function: given a frame and a box, it draws itself there and names
|
||
|
|
// the regions that can be pressed. It keeps nothing; what must outlive the frame (a field's text, the focus, a
|
||
|
|
// list's position) is the caller's or the Context's. Sizes are the design's, turned into pixels by the frame.
|
||
|
|
|
||
|
|
export namespace Nostro::Ui {
|
||
|
|
struct TextStyle {
|
||
|
|
FontFace face = FontFace::Regular;
|
||
|
|
float sizeDp = TextSize::body;
|
||
|
|
Color color;
|
||
|
|
TextAlign align = TextAlign::Left;
|
||
|
|
};
|
||
|
|
|
||
|
|
// The styles most text is set in, in the frame's colours.
|
||
|
|
TextStyle Body(const Frame& frame);
|
||
|
|
TextStyle Strong(const Frame& frame); // the body's size, heavier
|
||
|
|
TextStyle Muted(const Frame& frame); // secondary text, a size down
|
||
|
|
TextStyle Caption(const Frame& frame);
|
||
|
|
TextStyle Heading(const Frame& frame);
|
||
|
|
|
||
|
|
// One line in the middle of the box's height, cut with an ellipsis where it does not fit. A Center or Right
|
||
|
|
// alignment sets it against the box's middle or its right edge.
|
||
|
|
void Label(Frame& frame, Box box, std::string_view text, const TextStyle& style);
|
||
|
|
// Text wrapped to the box's width from its top; returns how tall it came out, whatever the box's height.
|
||
|
|
float Paragraph(Frame& frame, Box box, std::string_view text, const TextStyle& style);
|
||
|
|
float ParagraphHeight(Frame& frame, float width, std::string_view text, const TextStyle& style);
|
||
|
|
|
||
|
|
enum class ButtonKind : std::uint8_t {
|
||
|
|
Filled, // the one thing to do here
|
||
|
|
Tonal, // another thing to do
|
||
|
|
Plain, // text alone: an action in a bar or beside a button
|
||
|
|
Danger // a step that cannot be undone
|
||
|
|
};
|
||
|
|
|
||
|
|
struct ButtonOptions {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
ButtonKind kind = ButtonKind::Filled;
|
||
|
|
bool enabled = true;
|
||
|
|
std::function<void()> onTap;
|
||
|
|
};
|
||
|
|
// Drawn in the box. The area that takes a press is the box and, for one lower than a finger needs, as much above
|
||
|
|
// and below it as makes up for that: leave that room between two of them.
|
||
|
|
void Button(Frame& frame, Box box, const ButtonOptions& options);
|
||
|
|
// How wide a button is at least, for its label.
|
||
|
|
float ButtonWidth(const Frame& frame, std::string_view label);
|
||
|
|
|
||
|
|
struct RowOptions {
|
||
|
|
std::string key;
|
||
|
|
std::string title;
|
||
|
|
std::string subtitle; // a second line, muted
|
||
|
|
std::string trailing; // at the right: an amount
|
||
|
|
std::string trailingSub; // under it: a date
|
||
|
|
std::optional<Color> trailingColor;
|
||
|
|
std::string badge; // a small mark after the title: "Sandbox"
|
||
|
|
std::optional<bool> selected; // a choice among rows, drawn as a radio button
|
||
|
|
bool chevron = false; // leads somewhere
|
||
|
|
bool enabled = true;
|
||
|
|
bool divider = true;
|
||
|
|
std::function<void()> onTap;
|
||
|
|
std::function<void()> onLongPress;
|
||
|
|
};
|
||
|
|
void ListRow(Frame& frame, Box box, const RowOptions& options);
|
||
|
|
|
||
|
|
struct ValueOptions {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
std::string value;
|
||
|
|
float labelWidth = 112; // the room the label has before the value, in dp
|
||
|
|
std::function<void()> onTap; // nothing: it is only read
|
||
|
|
std::function<void()> onLongPress;
|
||
|
|
};
|
||
|
|
// A label and beside it what it stands for, wrapped to the room there is: a line of a sheet that says what
|
||
|
|
// something is. With an action it is a row that can be pressed, a finger tall in a window as narrow as a phone and
|
||
|
|
// closer to the next under a pointer. The text keeps clear of the box's sides, which a press is drawn up to. Drawn
|
||
|
|
// at the box's top and as wide as it; returns how tall it came out, which ValueRowHeight says beforehand.
|
||
|
|
float ValueRow(Frame& frame, Box box, const ValueOptions& options);
|
||
|
|
float ValueRowHeight(Frame& frame, float width, const ValueOptions& options);
|
||
|
|
|
||
|
|
struct FieldOptions {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
std::string placeholder;
|
||
|
|
std::string prefix; // before the text, inside the field: a currency's symbol
|
||
|
|
std::string error; // under the field, in place of the helper
|
||
|
|
std::string helper;
|
||
|
|
std::optional<TextPurpose> purpose; // by default what the field's kind asks for
|
||
|
|
std::optional<std::uint32_t> hints;
|
||
|
|
bool enabled = true;
|
||
|
|
std::function<void()> onChange;
|
||
|
|
std::function<void()> onSubmit;
|
||
|
|
};
|
||
|
|
// A text field with its label above and its error or helper below, at the box's top and as wide as it; returns how
|
||
|
|
// tall it came out.
|
||
|
|
float TextField(Frame& frame, Box box, EditState& edit, const FieldOptions& options);
|
||
|
|
// How tall a field with these options comes out in a box that wide, for what must know before it is drawn.
|
||
|
|
float FieldHeight(Frame& frame, float width, const FieldOptions& options);
|
||
|
|
|
||
|
|
struct ChoiceOptions {
|
||
|
|
std::string key;
|
||
|
|
std::vector<std::string> labels;
|
||
|
|
std::size_t selected = 0;
|
||
|
|
bool enabled = true;
|
||
|
|
std::function<void(std::size_t)> onSelect;
|
||
|
|
};
|
||
|
|
// A few choices side by side, one of them taken: a box of segments.
|
||
|
|
void Segmented(Frame& frame, Box box, const ChoiceOptions& options);
|
||
|
|
// The sections of a screen, one of them shown: a strip with a line under the one that is.
|
||
|
|
void Tabs(Frame& frame, Box box, const ChoiceOptions& options);
|
||
|
|
|
||
|
|
enum class BannerKind : std::uint8_t {
|
||
|
|
Info,
|
||
|
|
Warning,
|
||
|
|
Error
|
||
|
|
};
|
||
|
|
|
||
|
|
struct BannerOptions {
|
||
|
|
std::string key;
|
||
|
|
BannerKind kind = BannerKind::Info;
|
||
|
|
std::string text;
|
||
|
|
std::string action; // a thing to do about it
|
||
|
|
std::function<void()> onAction;
|
||
|
|
std::function<void()> onDismiss; // nothing: it cannot be closed
|
||
|
|
};
|
||
|
|
// A notice across the box's width at its top; returns how tall it came out.
|
||
|
|
float Banner(Frame& frame, Box box, const BannerOptions& options);
|
||
|
|
|
||
|
|
// A small rounded mark: a status. ChipWidth is how wide it comes out.
|
||
|
|
void Chip(Frame& frame, Box box, std::string_view text, Color ground, Color color);
|
||
|
|
float ChipWidth(const Frame& frame, std::string_view text);
|
||
|
|
// A line along the box's bottom edge.
|
||
|
|
void Divider(Frame& frame, Box box);
|
||
|
|
// The ground where something is still being fetched: a bar of the box's width, less for a `fraction` under 1.
|
||
|
|
void Skeleton(Frame& frame, Box box, float fraction = 1.0f);
|
||
|
|
|
||
|
|
struct BarAction {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
bool enabled = true;
|
||
|
|
std::function<void()> onTap;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct TopBarOptions {
|
||
|
|
std::string title;
|
||
|
|
std::function<void()> onBack; // nothing: no way back from here
|
||
|
|
std::vector<BarAction> actions;
|
||
|
|
};
|
||
|
|
// The bar along an area's top; returns the area under it.
|
||
|
|
Box TopBar(Frame& frame, Box area, const TopBarOptions& options);
|
||
|
|
|
||
|
|
struct NavItem {
|
||
|
|
std::string key;
|
||
|
|
std::string label;
|
||
|
|
std::string detail; // beside the label, where there is room: a balance
|
||
|
|
bool selected = false;
|
||
|
|
bool header = false; // names a group, and cannot be pressed
|
||
|
|
std::function<void()> onTap;
|
||
|
|
};
|
||
|
|
// The app's places along an area's bottom, for a window as narrow as a phone; returns the area above it.
|
||
|
|
Box NavBar(Frame& frame, Box area, std::span<const NavItem> items);
|
||
|
|
// The same down an area's left side, `widthDp` wide, for a wider window; returns the area beside it.
|
||
|
|
Box Sidebar(Frame& frame, Box area, float widthDp, std::span<const NavItem> items);
|
||
|
|
|
||
|
|
struct SheetOptions {
|
||
|
|
std::string key;
|
||
|
|
std::string title;
|
||
|
|
float contentHeight = 0; // how tall the body wants to be, in pixels
|
||
|
|
std::function<void()> onDismiss; // a tap beside it, or Escape
|
||
|
|
};
|
||
|
|
// What is asked or shown over a screen: a sheet from the bottom edge in a narrow window, a dialog in the middle of a
|
||
|
|
// wider one, with what it covers dimmed and out of reach. `body` draws into the box it is given.
|
||
|
|
void Sheet(Frame& frame, const SheetOptions& options, const std::function<void(Box)>& body);
|
||
|
|
// How wide the box is that a sheet gives its body in this frame, for working out how tall the body wants to be.
|
||
|
|
float SheetBodyWidth(const Frame& frame);
|
||
|
|
}
|