1// The controls of pkg::alloy::ui (REFOCUS.md, R6e, question 9A):2// widgets on the core's layers, what an app's menus, choices and notices3// and a game's pause screens want, with nothing of the app services.4//5// ```text6// import pkg::alloy::ui;7// import pkg::alloy::ui_controls;8//9// fn saving(ui: &var Ui) -> Element {10// return ui.add(Button::primary(&"Save", (ui) => ui.toast(Toast { .text = String::from(&"Saved") })));11// }12// ```13//14// Each keeps its state as an extension of the Ui (`Extension`), installed15// as it is first used, so a program not using one carries none of its16// code.17//18// ## How it was built19//20// **R6e as built** (2026-10-26): moved out of `ui`, whose notes of F11a,21// F11b, F11c, F11e and F12c tell how each was first built, with their22// notes here: `choices.alloy` (`Select`, `ChoiceList`, `Radios`, `Tabs`),23// `menus.alloy` (`ui.menu`, `MenuButton`), `tooltips.alloy`,24// `toasts.alloy`, `progress.alloy` (`Spinner`, `ProgressBar`). Hooks became25// extensions: `Menus` (the pointer onto an item, Tab in a menu), `Tips`26// (the pointer and the focus), `Toasts` (their state, ticked as each27// update begins, their texts given a replay). The layer mechanism they28// stand on (`ui.open`, `Overlay`, `Place`, `Popup`) stays in the core.29//30// **F11a as built** (2026-10-15; decisions/F11.md, "Layers"),31// the part ui_controls holds since R6e:32// - Menus (`menus.alloy`): `ui.menu(place, items)`, `MenuItem` (label,33// shortcut shown at the end, `enabled`, `separated`, `submenu`,34// `on_select`). Up, Down, Home, End, Page Up and Down, a letter moving35// to the next item starting with it, Enter, Space and Right choosing or36// opening a submenu, Left and Escape closing it; hovering an item37// focuses it and opens its submenu after 250 ms. A context menu opened38// by the right button's press chooses the item it is released on after39// a drag of more than 4 pixels. `on_context_menu` on `Column`, `Row`40// and `Stack` takes the right button, the Menu key and Shift+F10, and41// Control and a click on macOS; `ui` passes the right button on.42// - `Tooltip` (`tooltips.alloy`) wraps its children, showing its text in43// a passive layer above them (below with no room) after 500 ms of rest,44// or at once when the keyboard's focus comes in; it stays while the45// pointer is over either, and Escape or a press hides it.46// - Toasts (`toasts.alloy`): `ui.toast(Toast)` (text, `action`, `kind`47// Info, Success or Error, `duration`, 5 s), in one layer in the bottom48// end corner, three at most with the rest waiting, each going after its49// time, paused under the pointer or the focus; an error stays until50// closed, and so does one with an action while a screen reader reads.51// Each is spoken as it shows (`ui.announce(text, assertive)`).52//53// **F11b as built** (2026-10-15; decisions/F11.md, "Choices",54// questions 3A, 4A, 15A and 16A):55// - `Choice<T: Equal>` (a label, the value, `enabled`; `Choice::of`), and56// in `choices.alloy` `Select<T>`, `ChoiceList<T>` and `Radios<T>`, each57// one element. Only finding the chosen value and giving one back is58// generic; laying out, painting, the keys and type-ahead are written59// once over a `Labels` interface object, so each type of value costs60// little more than its glue. A program's enum or struct satisfies61// `Equal` with no marker when `==` compares it (question 17).62// - `Select`: a field showing the chosen label or the placeholder and a63// chevron (a mask made on the canvas as the first Select lays out,64// `Marks.chevron`), as wide as its longest label, measured once a65// change of choices. The press opens its list and lets the pointer go,66// so it follows the rows and a release after a drag chooses67// (`press_opened`, F11a's menu press made any popup's: the element68// under the release in that layer is clicked); Space, Enter, Up and69// Down open it too. The list is a `ChoiceList` in a popup layer below70// it, above with more room there, at least as wide and at most twelve71// rows of the tallest label tall or the room, the chosen row72// highlighted in view; moving there chooses nothing, Enter, Space, a73// click or Tab choose (Tab is offered first to what has the focus in a74// `tabbed` overlay, closing it, then moves on from the Select), the75// value already chosen closing it unheard, and Escape and a press76// outside close. The77// list closes when a binding gives the Select other choices, a chosen78// value or `enabled` (`renew_node` asks a `W: Popup` what to close).79// - `ChoiceList` on its own is a list box whose choice follows Up, Down,80// Home, End, Page Up and Page Down; it scrolls itself (the wheel, its81// bar), laying out only the rows in view, and shows a check mark by the82// chosen row; the pointer's hover highlights only a Select's.83// - Type-ahead (`TypeAhead`): characters typed within a second of each84// other make a prefix, the next enabled label starting with it found85// after simple case folding (pkg::alloy::text's new `starts_folded`,86// CaseFolding.txt in its tables), one letter again going through those87// starting with it; closed, a Select chooses at once.88// - `Radios`: circles and labels in a column or a row, one Tab stop on89// the chosen radio, the arrows choosing the next enabled one round the90// group, Space the focused one, a click the one under it.91// - `Widget::parts()` gives the items an element draws as `Part`s92// (semantics, bounds, `focused`), each a node under the element's, its93// id the window's bit, 11 bits of the element's generation, the part's94// index in 20 bits and the element's index; the focused part holds the95// tree's focus, and actions on one come as `Input::Part(PartRequest)`96// (Focus focusing the element first, ScrollIntoView revealing it). A97// Select is a combo box (`expanded`), its list a list box of options98// (`selected` the highlighted one, `position`), radios a radio group of99// radio buttons (`checked`). A plain layer of no label is no node.100// - `Input::Hover` gives the pointer's moves over an element with no101// button held, through a hook a list or radios set as they lay out.102// - `MenuButton` (question 15A): a button opening `items` below it on103// the press, a drag to an item and a release choosing it, Enter, Space104// or Down opening it with its first item focused; held down while open,105// to a screen reader a button with `expanded`. The `dialogs` sample's106// "⋯" buttons are MenuButtons.107// - `ui.click_at(at)` presses and releases at a point, for tests and108// benchmarks.109// - Size (question 16A, under 30 KB a stage): the counter grew by 6.0 KB110// natively (1,792,752 bytes) and 5.0 KB on the web (1,288,174): the111// window's new roles in each tree, the parts in the access tree, the112// hover hook and Tab's offer; the widgets come only with a program113// using them (`choices.alloy` is 1.94 MB in release).114// - Tests: nine in `ui/tests.alloy` (a Select by the pointer, by the keys,115// by a drag, its type-ahead, closing on new options; a list alone;116// radios; a menu button; the tree's combo box, options and radios and a117// screen reader choosing); goldens `choices.png`, `choices_dark.png`118// and `select_open.png`; `tests/atspi` and `tests/automation` open a119// Select and choose an option and a radio. Benchmark `ui_select`: a120// Select of 1,000 choices opened and closed, 0.105 ms.121// - Sample `choices.alloy`: a ticket order of 300 performances, a tier122// among radios, a quantity, seats in a list, a summary, the dark theme.123//124// **F11c as built** (2026-10-15; decisions/F11.md, "Screens",125// questions 5A and 6B), the part ui_controls holds since R6e:126// - `Tabs<T: Equal>` (in `choices.alloy`, `selected: T`): labels with 16127// by 10 pixels around, the selected one's label and a 2-pixel underline128// in the primary color, a line across in the border color, the hovered129// tab tinted; one Tab stop, Left and Right selecting the next enabled130// tab round, Home and End, a click; a tab list of tab parts. Radios'131// state is the shared `ItemState`.132133import std::string;134import std::vector;135import std::option;136import std::time;137import std::equal;138import pkg::alloy::window;139import pkg::alloy::draw;140import pkg::alloy::text as text;141142import pkg::alloy::ui;143import pkg::alloy::ui::store;144import pkg::alloy::ui::tree;145import pkg::alloy::ui::layout;146import pkg::alloy::ui::painting;147import pkg::alloy::ui::events;148import pkg::alloy::ui::widgets;149import pkg::alloy::ui::fields;150import pkg::alloy::ui::scrolling;151import pkg::alloy::ui::running;152import pkg::alloy::ui::timers;153import pkg::alloy::ui::layers;154import pkg::alloy::ui::motion;155import pkg::alloy::ui::transitions;156import pkg::alloy::ui::languages;157import pkg::alloy::ui::extensions;158import pkg::alloy::ui::views;159import choices;160import menus;161import tooltips;162import toasts;163import progress;164165// An action of a menu.166exp type MenuItem = struct {167 exp label: String,168 exp shortcut: String = String::empty(), // shown at the end, "Ctrl+D"; the app handles the keys169 exp enabled: bool = true,170 exp separated: bool = false, // a line above it171 // a setting it turns on or off: a check mark beside it while true, and172 // to screen readers a checked menu item173 exp checked: Option<bool> = ::None,174 exp submenu: Vector<MenuItem> = Vector<MenuItem>::empty(), // opened beside it, by Right or a rest175 exp on_select: |=| (&var Ui) = (ui) {},176};177178exp fn MenuItem::of(label: &[u8], on_select: |=| (&var Ui)) -> MenuItem => MenuItem {179 .label = String::from(&label),180 .on_select = move on_select181};182183// Opens a menu of the items: Up and Down move through them, Home and End184// to the ends, a letter to the next starting with it, Enter and Space185// choose, Right opens a submenu and Left or Escape closes it; choosing186// closes every menu, then runs the item's function.187exp fn menu(self ui: &var Ui, place: Place, items: Vector<MenuItem>) -> Layer {188 return ui.open_menu(place, move items, false);189}190191// A button opening a menu of the items below it on the press, so a drag192// to an item and a release there chooses it, as on Windows and macOS; or193// by Enter, Space or Down, the first item focused. To a screen reader a194// button with a popup, expanded while its menu is open.195exp type MenuButton : Widget = struct {196 exp label: String,197 exp accessible: String = String::empty(), // what a screen reader says, the label when empty198 exp items: Vector<MenuItem> = Vector<MenuItem>::empty(),199 exp variant: ButtonVariant = ButtonVariant::Neutral,200 exp enabled: bool = true,201 exp padding: Padding = Padding::symmetric(14.0, 8.0),202 exp sizing: Sizing = Sizing {},203 exp open: Option<Layer> = ::None, // its menu, while open204 exp laid: Option<*text::Layout> = ::None,205};206207/* --- Choices (decisions/F11.md, F11b) --- */208209// A value to choose, with its label.210exp type Choice<T: Equal> = struct {211 exp label: String,212 exp value: T,213 exp enabled: bool = true,214};215216exp fn Choice::of(label: &[u8], value: T) -> Choice<T> => Choice<T> {217 .label = String::from(&label),218 .value = move value219};220221// A field showing the chosen label (or the placeholder) and a chevron,222// as wide as its longest choice unless sized. A press, Space, Enter,223// Alt+Down, Up or Down opens its list below it (above without room), the224// chosen item highlighted; there Up, Down, Home, End, Page Up and Page225// Down move, Enter, Space or a click choose and close, a drag from the226// press and a release choose, Escape closes, Tab chooses and moves on.227// Typed characters move to the next item starting with them, forgotten228// after a second, the same letter again going through those starting229// with it; closed, that item is chosen at once. To a screen reader a230// combo box, its list a list box of options.231exp type Select<T: Equal> : Widget + Popup = struct {232 exp choices: Vector<Choice<T>>,233 exp selected: Option<T> = ::None, // None shows the placeholder234 exp placeholder: String = String::empty(),235 exp accessible: String = String::empty(),236 exp on_change: |=| (&var Ui, T) = (ui, value) {},237 exp enabled: bool = true,238 // what is wrong with the choice, none when empty: a border in the239 // theme's danger color, and to screen readers invalid, the message its240 // description; `required` a form needs one chosen241 exp error: String = String::empty(),242 exp required: bool = false,243 exp sizing: Sizing = Sizing {},244 exp open: Option<Layer> = ::None, // its own state: the list it opened245 pub typed: TypeAhead = TypeAhead {},246 exp laid: Option<*text::Layout> = ::None, // the chosen label or the placeholder247 pub widest: Option<f32> = ::None, // the longest label's width, measured once a change248 pub tallest: f32 = 0.0, // and the tallest label's line, which the list's rows take249 pub closing: Option<Layer> = ::None, // the list to close, its options having changed250};251252// The choices in a list, one chosen: a list box on its own, Up and Down253// choosing the next, Home, End, Page Up and Page Down further, a click254// or typed characters as a Select's; as tall as its choices unless255// sized, scrolling when not. A Select opens one.256exp type ChoiceList<T: Equal> : Widget = struct {257 exp choices: Vector<Choice<T>>,258 exp selected: Option<T> = ::None,259 exp accessible: String = String::empty(),260 exp on_change: |=| (&var Ui, T) = (ui, value) {},261 exp enabled: bool = true,262 exp error: String = String::empty(), // as a Select's263 exp required: bool = false,264 exp sizing: Sizing = Sizing {},265 // a Select's list: moving leaves the choice alone, and choosing266 // closes the list's layer before calling `on_change`267 pub popup: bool = false,268 pub rows: ListState = ListState {}, // its own state269};270271// A circle and a label for each choice, the chosen one's filled: one272// element and one Tab stop, on the chosen radio or the first; Up, Down,273// Left and Right choose the next enabled one, Space the focused one, a274// click the one under it. To a screen reader a radio group.275exp type Radios<T: Equal> : Widget = struct {276 exp choices: Vector<Choice<T>>,277 exp selected: Option<T> = ::None,278 exp accessible: String = String::empty(), // the group's label279 exp on_change: |=| (&var Ui, T) = (ui, value) {},280 exp horizontal: bool = false, // side by side rather than one under the other281 exp gap: f32 = 8.0,282 exp enabled: bool = true,283 // what is wrong with the choice, none when empty: the circles in the284 // theme's danger color, and to screen readers invalid, the message its285 // description; `required` a form needs one chosen286 exp error: String = String::empty(),287 exp required: bool = false,288 exp sizing: Sizing = Sizing {},289 pub state: ItemState = ItemState {}, // its own290};291292// A row of tabs, the selected one underlined in the primary color: one293// element and one Tab stop, Left and Right selecting the next enabled294// tab, Home and End the first and last, a click the one under it. What295// a tab shows is the program's, beside it (`ui.switch`). To a screen296// reader a tab list of tabs.297exp type Tabs<T: Equal> : Widget = struct {298 exp tabs: Vector<Choice<T>>,299 exp selected: T,300 exp accessible: String = String::empty(), // the tab list's label301 exp on_change: |=| (&var Ui, T) = (ui, value) {},302 exp enabled: bool = true,303 exp sizing: Sizing = Sizing {},304 pub state: ItemState = ItemState {}, // its own305};306307// What a toast says, and how.308exp type Toast = struct {309 exp text: String,310 exp action: Option<ToastAction> = ::None,311 exp kind: ToastKind = ToastKind::Info,312 exp duration: Duration = Duration::from_seconds(5.0),313};314315// A toast's button: the toast closes after its function runs.316exp type ToastAction = struct {317 exp label: String,318 exp on_press: |=| (&var Ui),319};320321exp fn ToastAction::of(label: &[u8], on_press: |=| (&var Ui)) -> ToastAction => ToastAction {322 .label = String::from(&label),323 .on_press = move on_press324};325326exp type ToastKind = enum {327 Info,328 Success,329 Error, // stays until closed330};331332// Tells the user something happened, without taking the focus: in the333// window's bottom end corner, gone after its duration, its time paused334// while the pointer is over it or the focus in it; spoken by screen335// readers, an error at once.336exp fn toast(self ui: &var Ui, toast: Toast) {337 ui.show_toast(move toast);338}339340// A looping sign of work under way: dots round a circle, the brightest341// going round once a second; under reduced motion they pulse together,342// slowly. To a screen reader its label, "Loading".343exp type Spinner : Widget = struct {344 exp size: f32 = 24.0,345 exp color: Ink = Ink::Primary,346 exp accessible: String = String::empty(),347 exp sizing: Sizing = Sizing {},348};349350// How much of a piece of work is done: a track filling from its start to351// `value`, from 0 to 1, or, while that is not known (None), a stretch352// going across it once a second, pulsing under reduced motion. To a353// screen reader a progress bar, its label `accessible`, its value in354// percent.355exp type ProgressBar : Widget = struct {356 exp value: Option<f32> = ::None,357 exp accessible: String = String::empty(),358 exp color: Ink = Ink::Primary,359 exp width: f32 = 200.0,360 exp height: f32 = 6.0,361 exp sizing: Sizing = Sizing {},362};363364// Text shown over the element it holds when the pointer rests on it or365// the keyboard's focus comes to it.366exp type Tooltip : Widget = struct {367 exp text: String,368 exp children: &[Element] = &[],369 exp sizing: Sizing = Sizing {},370};