module ui_controlssource
The controls of pkg::alloy::ui (REFOCUS.md, R6e, question 9A): widgets on the core's layers, what an app's menus, choices and notices and a game's pause screens want, with nothing of the app services.
import pkg::alloy::ui;
import pkg::alloy::ui_controls;
fn saving(ui: &var Ui) -> Element {
return ui.add(Button::primary(&"Save", (ui) => ui.toast(Toast { .text = String::from(&"Saved") })));
}
Each keeps its state as an extension of the Ui (Extension), installed
as it is first used, so a program not using one carries none of its
code.
## How it was built
**R6e as built** (2026-10-26): moved out of ui, whose notes of F11a,
F11b, F11c, F11e and F12c tell how each was first built, with their
notes here: choices.alloy (Select, ChoiceList, Radios, Tabs),
menus.alloy (ui.menu, MenuButton), tooltips.alloy,
toasts.alloy, progress.alloy (Spinner, ProgressBar). Hooks became
extensions: Menus (the pointer onto an item, Tab in a menu), Tips
(the pointer and the focus), Toasts (their state, ticked as each
update begins, their texts given a replay). The layer mechanism they
stand on (ui.open, Overlay, Place, Popup) stays in the core.
**F11a as built** (2026-10-15; decisions/F11.md, "Layers"), the part ui_controls holds since R6e:
- Menus (
menus.alloy):ui.menu(place, items),MenuItem(label, shortcut shown at the end,enabled,separated,submenu,on_select). Up, Down, Home, End, Page Up and Down, a letter moving to the next item starting with it, Enter, Space and Right choosing or opening a submenu, Left and Escape closing it; hovering an item focuses it and opens its submenu after 250 ms. A context menu opened by the right button's press chooses the item it is released on after a drag of more than 4 pixels.on_context_menuonColumn,RowandStacktakes the right button, the Menu key and Shift+F10, and Control and a click on macOS;uipasses the right button on. Tooltip(tooltips.alloy) wraps its children, showing its text in a passive layer above them (below with no room) after 500 ms of rest, or at once when the keyboard's focus comes in; it stays while the pointer is over either, and Escape or a press hides it.- Toasts (
toasts.alloy):ui.toast(Toast)(text,action,kindInfo, Success or Error,duration, 5 s), in one layer in the bottom end corner, three at most with the rest waiting, each going after its time, paused under the pointer or the focus; an error stays until closed, and so does one with an action while a screen reader reads. Each is spoken as it shows (ui.announce(text, assertive)).
**F11b as built** (2026-10-15; decisions/F11.md, "Choices", questions 3A, 4A, 15A and 16A):
Choice<T: Equal>(a label, the value,enabled;Choice::of), and inchoices.alloySelect<T>,ChoiceList<T>andRadios<T>, each one element. Only finding the chosen value and giving one back is generic; laying out, painting, the keys and type-ahead are written once over aLabelsinterface object, so each type of value costs little more than its glue. A program's enum or struct satisfiesEqualwith no marker when==compares it (question 17).Select: a field showing the chosen label or the placeholder and a chevron (a mask made on the canvas as the first Select lays out,Marks.chevron), as wide as its longest label, measured once a change of choices. The press opens its list and lets the pointer go, so it follows the rows and a release after a drag chooses (press_opened, F11a's menu press made any popup's: the element under the release in that layer is clicked); Space, Enter, Up and Down open it too. The list is aChoiceListin a popup layer below it, above with more room there, at least as wide and at most twelve rows of the tallest label tall or the room, the chosen row highlighted in view; moving there chooses nothing, Enter, Space, a click or Tab choose (Tab is offered first to what has the focus in atabbedoverlay, closing it, then moves on from the Select), the value already chosen closing it unheard, and Escape and a press outside close. The list closes when a binding gives the Select other choices, a chosen value orenabled(renew_nodeasks aW: Popupwhat to close).ChoiceListon its own is a list box whose choice follows Up, Down, Home, End, Page Up and Page Down; it scrolls itself (the wheel, its bar), laying out only the rows in view, and shows a check mark by the chosen row; the pointer's hover highlights only a Select's.- Type-ahead (
TypeAhead): characters typed within a second of each other make a prefix, the next enabled label starting with it found after simple case folding (pkg::alloy::text's newstarts_folded, CaseFolding.txt in its tables), one letter again going through those starting with it; closed, a Select chooses at once. Radios: circles and labels in a column or a row, one Tab stop on the chosen radio, the arrows choosing the next enabled one round the group, Space the focused one, a click the one under it.Widget::parts()gives the items an element draws asParts (semantics, bounds,focused), each a node under the element's, its id the window's bit, 11 bits of the element's generation, the part's index in 20 bits and the element's index; the focused part holds the tree's focus, and actions on one come asInput::Part(PartRequest)(Focus focusing the element first, ScrollIntoView revealing it). A Select is a combo box (expanded), its list a list box of options (selectedthe highlighted one,position), radios a radio group of radio buttons (checked). A plain layer of no label is no node.Input::Hovergives the pointer's moves over an element with no button held, through a hook a list or radios set as they lay out.MenuButton(question 15A): a button openingitemsbelow it on the press, a drag to an item and a release choosing it, Enter, Space or Down opening it with its first item focused; held down while open, to a screen reader a button withexpanded. Thedialogssample's "⋯" buttons are MenuButtons.ui.click_at(at)presses and releases at a point, for tests and benchmarks.- Size (question 16A, under 30 KB a stage): the counter grew by 6.0 KB
natively (1,792,752 bytes) and 5.0 KB on the web (1,288,174): the
window's new roles in each tree, the parts in the access tree, the
hover hook and Tab's offer; the widgets come only with a program
using them (
choices.alloyis 1.94 MB in release). - Tests: nine in
ui/tests.alloy(a Select by the pointer, by the keys, by a drag, its type-ahead, closing on new options; a list alone; radios; a menu button; the tree's combo box, options and radios and a screen reader choosing); goldenschoices.png,choices_dark.pngandselect_open.png;tests/atspiandtests/automationopen a Select and choose an option and a radio. Benchmarkui_select: a Select of 1,000 choices opened and closed, 0.105 ms. - Sample
choices.alloy: a ticket order of 300 performances, a tier among radios, a quantity, seats in a list, a summary, the dark theme.
**F11c as built** (2026-10-15; decisions/F11.md, "Screens", questions 5A and 6B), the part ui_controls holds since R6e:
Tabs<T: Equal>(inchoices.alloy,selected: T): labels with 16 by 10 pixels around, the selected one's label and a 2-pixel underline in the primary color, a line across in the border color, the hovered tab tinted; one Tab stop, Left and Right selecting the next enabled tab round, Home and End, a click; a tab list of tab parts. Radios' state is the sharedItemState.
MenuItemMenuItem::ofmenuMenuButtonChoiceChoice::ofSelectChoiceListRadiosTabsToastToastActionToastAction::ofToastKindtoastSpinnerProgressBarTooltip
type MenuItemsource
exp type MenuItem = struct
An action of a menu.
Fields
exp label: Stringexp shortcut: String = String::empty()shown at the end, "Ctrl+D"; the app handles the keys
exp enabled: bool = trueexp separated: bool = falsea line above it
exp checked: Option<bool> = ::Nonea setting it turns on or off: a check mark beside it while true, and to screen readers a checked menu item
opened beside it, by Right or a rest
exp on_select: |=| (&var Ui) = (ui) {}
Functions
fn MenuItem::ofsource
exp fn MenuItem::of(label: &[u8], on_select: |=| (&var Ui)) -> MenuItem
type MenuButtonsource
exp type MenuButton : Widget = struct
A button opening a menu of the items below it on the press, so a drag to an item and a release there chooses it, as on Windows and macOS; or by Enter, Space or Down, the first item focused. To a screen reader a button with a popup, expanded while its menu is open.
Fields
exp label: Stringexp accessible: String = String::empty()what a screen reader says, the label when empty
exp items: Vector<MenuItem> = Vector<MenuItem>::empty()exp variant: ButtonVariant = ButtonVariant::Neutralexp enabled: bool = trueexp padding: Padding = Padding::symmetric(14.0, 8.0)exp sizing: Sizing = Sizing {}exp open: Option<Layer> = ::Noneits menu, while open
exp laid: Option<*text::Layout> = ::None
Functions
type Choicesource
exp type Choice<T: Equal> = struct
A value to choose, with its label.
Fields
exp label: Stringexp value: Texp enabled: bool = true
Functions
fn Choice::ofsource
exp fn Choice::of(label: &[u8], value: T) -> Choice<T>
type Selectsource
exp type Select<T: Equal> : Widget + Popup = struct
A field showing the chosen label (or the placeholder) and a chevron, as wide as its longest choice unless sized. A press, Space, Enter, Alt+Down, Up or Down opens its list below it (above without room), the chosen item highlighted; there Up, Down, Home, End, Page Up and Page Down move, Enter, Space or a click choose and close, a drag from the press and a release choose, Escape closes, Tab chooses and moves on. Typed characters move to the next item starting with them, forgotten after a second, the same letter again going through those starting with it; closed, that item is chosen at once. To a screen reader a combo box, its list a list box of options.
Fields
exp choices: Vector<Choice<T>>exp selected: Option<T> = ::NoneNone shows the placeholder
exp placeholder: String = String::empty()exp accessible: String = String::empty()exp on_change: |=| (&var Ui, T) = (ui, value) {}exp enabled: bool = trueexp error: String = String::empty()what is wrong with the choice, none when empty: a border in the theme's danger color, and to screen readers invalid, the message its description;
requireda form needs one chosenexp required: bool = falseexp sizing: Sizing = Sizing {}exp open: Option<Layer> = ::Noneits own state: the list it opened
exp laid: Option<*text::Layout> = ::Nonethe chosen label or the placeholder
Functions
exp fn layout<T: Equal>(self s: &var Select<T>, arrange: &var Arrange, limits: Limits) -> Extentexp fn paint<T: Equal>(self s: &Select<T>, look: &Look, paint: &var Paint)exp fn event<T: Equal>(self s: &var Select<T>, ui: &var Ui, input: &Input) -> boolexp fn handle<T: Equal>( self s: &var Select<T>, ui: &var Ui, input: &Input, closed: (&var Ui, Element) ) -> boolexp fn take_closing<T: Equal>(self s: &var Select<T>) -> Option<Layer>exp fn renew<T: Equal>(self s: &var Select<T>, next: Select<T>) -> Changeexp fn sizing<T: Equal>(self s: &Select<T>) -> Sizingexp fn interactive<T: Equal>(self s: &Select<T>) -> boolexp fn focusable<T: Equal>(self s: &Select<T>) -> boolexp fn clips<T: Equal>(self s: &Select<T>) -> boolexp fn semantics<T: Equal>(self s: &Select<T>) -> Semantics
type ChoiceListsource
exp type ChoiceList<T: Equal> : Widget = struct
The choices in a list, one chosen: a list box on its own, Up and Down choosing the next, Home, End, Page Up and Page Down further, a click or typed characters as a Select's; as tall as its choices unless sized, scrolling when not. A Select opens one.
Fields
exp choices: Vector<Choice<T>>exp selected: Option<T> = ::Noneexp accessible: String = String::empty()exp on_change: |=| (&var Ui, T) = (ui, value) {}exp enabled: bool = trueexp error: String = String::empty()as a Select's
exp required: bool = falseexp sizing: Sizing = Sizing {}
Functions
type Radiossource
exp type Radios<T: Equal> : Widget = struct
A circle and a label for each choice, the chosen one's filled: one element and one Tab stop, on the chosen radio or the first; Up, Down, Left and Right choose the next enabled one, Space the focused one, a click the one under it. To a screen reader a radio group.
Fields
exp choices: Vector<Choice<T>>exp selected: Option<T> = ::Noneexp accessible: String = String::empty()the group's label
exp on_change: |=| (&var Ui, T) = (ui, value) {}exp horizontal: bool = falseside by side rather than one under the other
exp gap: f32 = 8.0exp enabled: bool = trueexp error: String = String::empty()what is wrong with the choice, none when empty: the circles in the theme's danger color, and to screen readers invalid, the message its description;
requireda form needs one chosenexp required: bool = falseexp sizing: Sizing = Sizing {}
Functions
type Tabssource
exp type Tabs<T: Equal> : Widget = struct
A row of tabs, the selected one underlined in the primary color: one
element and one Tab stop, Left and Right selecting the next enabled
tab, Home and End the first and last, a click the one under it. What
a tab shows is the program's, beside it (ui.switch). To a screen
reader a tab list of tabs.
Fields
exp tabs: Vector<Choice<T>>exp selected: Texp accessible: String = String::empty()the tab list's label
exp on_change: |=| (&var Ui, T) = (ui, value) {}exp enabled: bool = trueexp sizing: Sizing = Sizing {}
Functions
type Toastsource
exp type Toast = struct
What a toast says, and how.
Fields
exp text: Stringexp action: Option<ToastAction> = ::Noneexp kind: ToastKind = ToastKind::Infoexp duration: Duration = Duration::from_seconds(5.0)
type ToastActionsource
exp type ToastAction = struct
A toast's button: the toast closes after its function runs.
Fields
exp label: Stringexp on_press: |=| (&var Ui)
Functions
fn ToastAction::ofsource
exp fn ToastAction::of(label: &[u8], on_press: |=| (&var Ui)) -> ToastAction
type ToastKindsource
exp type ToastKind = enum
Variants
InfoSuccessErrorstays until closed
fn toastsource
exp fn toast(self ui: &var Ui, toast: Toast)
Tells the user something happened, without taking the focus: in the window's bottom end corner, gone after its duration, its time paused while the pointer is over it or the focus in it; spoken by screen readers, an error at once.
type Spinnersource
exp type Spinner : Widget = struct
A looping sign of work under way: dots round a circle, the brightest going round once a second; under reduced motion they pulse together, slowly. To a screen reader its label, "Loading".
Fields
exp size: f32 = 24.0exp color: Ink = Ink::Primaryexp accessible: String = String::empty()exp sizing: Sizing = Sizing {}
Functions
type ProgressBarsource
exp type ProgressBar : Widget = struct
How much of a piece of work is done: a track filling from its start to
value, from 0 to 1, or, while that is not known (None), a stretch
going across it once a second, pulsing under reduced motion. To a
screen reader a progress bar, its label accessible, its value in
percent.
Fields
exp value: Option<f32> = ::Noneexp accessible: String = String::empty()exp color: Ink = Ink::Primaryexp width: f32 = 200.0exp height: f32 = 6.0exp sizing: Sizing = Sizing {}
Functions
type Tooltipsource
exp type Tooltip : Widget = struct
Text shown over the element it holds when the pointer rests on it or the keyboard's focus comes to it.
Fields
exp text: Stringexp children: &[Element] = &[]exp sizing: Sizing = Sizing {}