1// Screen readers for pkg::alloy::ui (REFOCUS.md, R6f, question2// 3B): an app installs them, and from then on the platform's screen3// readers see it, through the tree its widgets describe4// (`Widget::semantics`).5//6// ```text7// import pkg::alloy::ui;8// import pkg::alloy::ui_access as ui_access;9//10// fn main() -> i32 {11// return app(&WindowConfig::titled(&"Counter"), (ui) => {12// ui_access::install(&var ui);13// const count = ui.signal(0);14// return ui.add(Button::text(&"Add one", (ui) => count.update(&var ui, (n) => n += 1)));15// });16// }17// ```18//19// Installing lets the window's screen readers reach it (pkg::alloy::window's20// `install_access`: AT-SPI on Linux, UI Automation on Windows,21// NSAccessibility on macOS, the page's accessibility layer behind its22// "Turn on accessibility" button); once one asks, each frame drawn sends23// the window the nodes changed (`access.alloy`), fields and texts of24// several lines with the maps screen readers read them by25// (`text_maps.alloy`), and what screen readers ask comes back as the26// elements' input. An app not installing them links none of it, and27// screen readers find only a window.28//29// ## How it was built30//31// Notes kept from each stage as it was finished, the sizes and numbers32// of their day; what changed since is in REVIEW.md and the history.33//34// **R6f as built** (2026-10-26; REFOCUS.md, question 3B):35// screen readers opt in, moved out of `ui`.36// - `ui_access::install(&var ui)` (the package imported `as ui_access`)37// installs `Access`, an extension (`ui`'s question 8A) holding what38// `Ui` held: the nodes as last sent and the focus (`sent`, `focus`, from39// `access_sent` and `access_focus`) and the text maps' `revision` (from40// `map_revision`). Its `event` takes `Event::AccessRequested` (the41// tree's start, everything laid out again with the maps, a frame asked42// for) and `Event::Action`, which left `running.alloy`; its `show_in`43// calls the window's `install_access`, sends the tree's changes after a44// frame was drawn, as `running.alloy` did after rendering, and gives the45// window what `ui.announce` queued; its `text_map` makes the maps.46// `ui.access_update()` installs it when called, for tests.47// - Moved: `access.alloy` (the walk, node ids, actions; 314 lines) and48// `text_maps.alloy` (123) from `ui`, reaching the core through its49// inner modules' `exp` names (`tree`, `events`, `scrolling`, `layers`,50// `extensions`, `fields`' `text_map_of`, `languages`' `spoken_language`51// and `reading_flow`).52// - What the core keeps: `Widget::semantics`, `parts` and each widget's53// description, linked only where something walks them (this package,54// ui_app's forms and requests); `ui.access_on` and `access_stale`;55// `Extension::text_map` with `TextLook`, which `Text` of several lines56// and `TextField` ask while `access_on`; `ui.announce`'s queue, emptied57// after each event. `ui.input_secret()` reads `TextField.secret` rather58// than the semantics. Without the install the window's screen readers59// are never started: they find a window with nothing in it, and60// nothing announced is said.61// - Sizes, as `bench.sh` builds them: the counter 974,384 bytes natively62// and 671,036 of wasm with nothing installed (1,217,312 and 707,61663// before); with screen readers (`bench/access_counter.alloy`,64// `access_counter_native_bytes`) 1,207,056 and 705,849: AT-SPI and65// text ranges 71 KB, the platform's part of it 37 KB, D-Bus 35 KB,66// `ui`'s walk and descriptions 25 KB, std 17 KB, the words' tables67// 7.5 KB, this package 6.9 KB, Ryu's tables 10.7 KB, the settings68// portal 1.5 KB.69// - Tests (`tests.alloy`, 13): the twelve of `ui/tests.alloy` reading the70// tree, its actions and the maps, installing first, and an app not71// installing that sends no tree, says nothing and takes no action72// though asked. ui_controls' and ui_app's tests import this package73// where they read the tree; `tests/atspi`, `tests/automation`,74// `tests/reading` and `samples/tickets` install it.75//76// **F6a as built** (2026-10-13), the part ui_access holds since R6f (the77// rest in pkg::alloy::window's notes):78// - Nothing is built until `AccessRequested`; then each drawn frame walks79// the elements, compares each node with what was sent and sends those80// changed, gone, or the focus moved (`access.alloy`). The walk is81// every element per frame while a screen reader reads, which F6b can82// narrow to the elements renewed if it shows in the benchmarks.83// - Actions (`ui.access_action`): `Focus` focuses with the ring,84// `ScrollIntoView` reveals, `Click` is a click at the element's middle,85// `Increment` and `Decrement` the Up and Down keys, and `SetValue` and86// `SetSelection` come as the new `Input::Action`, which `Slider` (a87// number) and `TextField` take.88//89// **F14b as built** (2026-10-20; decisions/F14.md, "Text for90// screen readers"), the part ui_access holds since R6f (the rest in91// pkg::alloy::ui's and pkg::alloy::window's notes):92// - Maps (`text_maps.alloy`, 5B, 6A): while a screen reader reads93// (`ui.access_on`), a `TextField` makes its `TextMap` as it is laid94// out and as its caret scrolls it (`remap`), kept in `Editing.map`,95// and a `Text` laid out on more than one line keeps one in `Text.map`96// (read only, a heading's said so); `access.alloy` sends it with the97// node. Graphemes come from text's new `Layout::graphemes_in_order`,98// with the line breaks it draws nothing for at the end of their lines;99// words from `text::word_starts`; boxes at the Ui's scale; the format's100// family is the first face's PostScript name before its style, its101// language the Ui's. Each map takes the next `Ui.map_revision`, so the102// access walk sends a node again only when its map was made again: on103// an edit, a scroll or a new layout, never on a frame where nothing104// changed, and not during a composition (7A: the map before it, as the105// value). A secret field maps the dots it shows. The window asking for106// the tree lays everything out again, so maps exist from the first107// update. Never on the web (`target()`), whose browser reads its own108// elements.109110import std::string;111import std::vector;112import std::option;113import std::map;114import std::process;115import pkg::alloy::window;116import pkg::alloy::draw;117import pkg::alloy::text as text;118119import pkg::alloy::ui;120import pkg::alloy::ui::tree;121import pkg::alloy::ui::extensions;122import access;123import text_maps;124125// Lets the platform's screen readers see the app, from now on.126exp fn install(ui: &var Ui) {127 ui.access_state();128}129130// What the Ui keeps for screen readers: the nodes as last sent and the131// focus, the last text map's revision, and what the window is given132// after the event: a frame to draw the tree after, and the tree.133pub type Access : Extension = struct {134 pub sent: Map<u64, AccessNode> = Map<u64, AccessNode>::empty(),135 pub focus: u64 = 0,136 pub revision: u32 = 0,137 pub redraw: bool = false,138 pub drawn: bool = false,139};140141pub fn access_state(self ui: &var Ui) -> &var Access {142 if (ui.extension<Access>() is ::None) {143 ui.install(new Access {});144 }145 if (ui.extension_var<Access>() is ::Some |&var a|) {146 return &var a;147 }148 panic "pkg::alloy::ui_access: not installed";149}150151pub fn event(self a: &var Access, ui: &var Ui, event: &Event) {152 match (event) {153 ::AccessRequested {154 ui.access_on = true;155 ui.access_stale = true;156 const state = &var ui.access_state();157 state.sent.clear();158 state.redraw = true;159 // laid out again, fields and texts of several lines now making160 // the maps screen readers read them by161 if (target() != "web-wasm32") {162 ui.invalidate_all();163 }164 }165 ::Action |&request| {166 ui.access_action(&request);167 }168 ::Redraw {169 ui.access_state().drawn = true;170 }171 else {}172 }173}174175// The window, once a frame was drawn, gets the tree's changes, and what176// screen readers are to say.177pub fn show_in(self a: &var Access, ui: &var Ui, window: &var Window) {178 window.install_access();179 const state = &var ui.access_state();180 if (state.redraw) {181 state.redraw = false;182 window.request_redraw();183 }184 const drawn = state.drawn;185 state.drawn = false;186 if (drawn) {187 ui.send_access(&var window);188 }189 for (ui.announced.items()) |&var said| {190 window.announce(move said.text, said.assertive);191 }192}193194pub fn text_map(195 self a: &var Access,196 ui: &var Ui,197 laid: &text::Layout,198 origin: Point,199 look: TextLook200) -> Option<TextMap> {201 var format = ui.text_format(&laid, look.size, look.weight, look.color);202 format.heading = look.heading;203 format.read_only = look.read_only;204 return ::Some(ui.text_map(&laid, origin, move format));205}