ui_access

source of access

1// The accessibility tree (PLAN.md, F6): each element described2// by its widget's semantics, the elements without a role left out and3// their children given to the nearest described ancestor, under a node4// for the window. Nothing is built until a screen reader asks; then5// each frame drawn sends the window the nodes changed since the last,6// and what the screen reader asks comes back as the element's input.78import std::string;9import std::vector;10import std::option;11import std::map;12import std::process;13import pkg::alloy::window;14import pkg::alloy::draw;1516import pkg::alloy::ui;17import pkg::alloy::ui::tree;18import pkg::alloy::ui::events;19import pkg::alloy::ui::scrolling;20import pkg::alloy::ui::layers;21import pkg::alloy::ui::extensions;22import pkg::alloy::ui::fields;23import pkg::alloy::ui::languages;24import ui_access;2526// The window's node, above every element's.27exp const WINDOW_NODE: u64 = 1 << 63;2829const ORIGIN: Point = Point { .x = 0.0, .y = 0.0 };3031// An element's node: its generation and its index plus one, so no32// element's is 0.33exp fn access_id(self ui: &Ui, index: u32) -> u64 {34    return (ui.node(index).generation to u64 << 32) | (index to u64 + 1);35}3637// A part's node (Widget::parts): the window's bit, 11 bits of its38// element's generation, its index among the parts in 20 bits and its39// element's index plus one.40pub fn part_id(self ui: &Ui, index: u32, part: u64) -> u64 {41    const generation = ui.node(index).generation to u64 & 0x7FF;42    return WINDOW_NODE | generation << 52 | (part & 0xFFFFF) << 32 | (index to u64 + 1);43}4445// The element and the part a part's node stands for, while it is there.46fn access_part(self ui: &Ui, id: u64) -> Option<[u32 : 2]> {47    const low = id & 0xFFFFFFFF;48    if ((id & WINDOW_NODE) == 0 || low == 0 || low > ui.nodes.length()) {49        return ::None;50    }51    const index = (low - 1) to u32;52    const node = &ui.node(index);53    if (!node.alive || (node.generation to u64 & 0x7FF) != (id >> 52 & 0x7FF)) {54        return ::None;55    }56    return ::Some([index, (id >> 32 & 0xFFFFF) to u32]);57}5859// The part of the element holding the focus, if any.60fn focused_part(self ui: &Ui, index: u32) -> Option<u64> {61    if (ui.node(index).widget is ::Some |&widget|) {62        const parts = widget.parts();63        for (parts.view(), [..parts.length()]) |&part, k| {64            if (part.focused) {65                return ::Some(ui.part_id(index, k));66            }67        }68    }69    return ::None;70}7172// The element a node stands for, if it is still there.73fn access_element(self ui: &Ui, id: u64) -> Option<u32> {74    const low = id & 0xFFFFFFFF;75    if (id == WINDOW_NODE || low == 0) {76        return ::None;77    }78    const element = Element { .index = (low - 1) to u32, .generation = (id >> 32) to u32 };79    return if (ui.exists(element)) ::Some(element.index) else ::None;80}8182// Gives the window the nodes changed since last sent, once a screen83// reader asked.84pub fn send_access(self ui: &var Ui, window: &var Window) {85    // nothing to look at unless an element was laid out or painted since86    if (!ui.access_on || !ui.access_stale) {87        return;88    }89    ui.access_stale = false;90    if (ui.access_update() is ::Some |move update|) {91        window.update_access(move update);92    }93}9495// The nodes changed since last sent, as sent; none when nothing changed.96exp fn access_update(self ui: &var Ui) -> Option<TreeUpdate> {97    // the elements showing a resource loading98    ui.extensions_describing();99    var nodes = Map<u64, AccessNode>::empty();100    var top = Vector<u64>::empty();101    const none = String::empty();102    if (ui.root is ::Some |root|) {103        ui.describe(root, ORIGIN, &none, ui.reading_flow(), &var nodes, &var top);104    }105    // each layer after the content, in the order they opened106    for (ui.layers.view()) |&slot| {107        ui.describe(slot.host.index, ORIGIN, &none, ui.reading_flow(), &var nodes, &var top);108    }109    nodes.insert(WINDOW_NODE, AccessNode {110        .id = WINDOW_NODE,111        .semantics = Semantics {112            .role = Role::Window,113            .busy = if (ui.root is ::Some |root|) ui.node(root).busy else false114        },115        .bounds = Area {116            .x = 0.0,117            .y = 0.0,118            .width = ui.viewport.width * ui.scale,119            .height = ui.viewport.height * ui.scale120        },121        .children = move top122    });123    var focus = WINDOW_NODE;124    if (ui.focused is ::Some |focused| && nodes.contains(&ui.access_id(focused))) {125        focus = ui.access_id(focused);126        // a list's highlighted option, a group's focused radio127        if (ui.focused_part(focused) is ::Some |part|) {128            focus = part;129        }130    }131    var update = TreeUpdate {132        .nodes = Vector<AccessNode>::empty(),133        .removed = Vector<u64>::empty(),134        .root = WINDOW_NODE,135        .focus = ::Some(focus)136    };137    const state = &var ui.access_state();138    for (nodes.entries()) |&entry| {139        if (!same(&state.sent, &entry.value)) {140            update.nodes.push(copy entry.value);141        }142    }143    for (state.sent.entries()) |&entry| {144        if (!nodes.contains(&entry.key)) {145            update.removed.push(entry.key);146        }147    }148    const first = state.sent.length() == 0;149    if (150        !first151        && update.nodes.length() == 0152        && update.removed.length() == 0153        && focus == state.focus154    ) {155        return ::None;156    }157    state.sent = move nodes;158    state.focus = focus;159    return ::Some(move update);160}161162// Whether the node went as it is.163fn same(sent: &Map<u64, AccessNode>, node: &AccessNode) -> bool {164    if (sent.try_get(&node.id) is ::Some |&was|) {165        return was == node;166    }167    return false;168}169170// Adds the element's node, or its children's when it has no role, to171// the nodes, its id to `into`; `above` is its parent's origin, `tip` a172// tooltip's text, the description of the first node under it, and173// `reading` the direction of the node holding it, which a node reading174// the other way (`directed`) says it does.175fn describe(176    self ui: &Ui,177    index: u32,178    above: Point,179    tip: &String,180    reading: LayoutDirection,181    nodes: &var Map<u64, AccessNode>,182    into: &var Vector<u64>183) {184    const node = &ui.node(index);185    if (node.hidden) {186        return;187    }188    const origin = Point { .x = above.x + node.position.x, .y = above.y + node.position.y };189    var semantics = Semantics {};190    var parts = Vector<Part>::empty();191    var described = &tip;192    var map: Option<TextMap> = ::None;193    if (!node.transparent && node.widget is ::Some |&widget|) {194        semantics = widget.semantics();195        parts = widget.parts();196        if (target() != "web-wasm32") {197            map = text_map_of(&widget);198        }199    }200    if (semantics.role is ::Unknown) {201        // what stands for nothing may describe what it holds: a tooltip202        if (semantics.description.length() > 0) {203            described = &semantics.description;204        }205        for (node.children.view()) |child| {206            ui.describe(child, origin, &described, reading, &var nodes, &var into);207        }208        return;209    }210    if (node.direction != reading) {211        semantics.right_to_left = ::Some(node.direction is ::RightToLeft);212    }213    if (semantics.description.length() == 0) {214        semantics.description = copy described;215    }216    semantics.busy = semantics.busy || node.busy;217    const none = String::empty();218    const s = ui.scale;219    var children = Vector<u64>::empty();220    // the items it draws, then its children221    for (parts.items(), [..parts.length()]) |&var part, k| {222        const id = ui.part_id(index, k);223        const b = part.bounds;224        nodes.insert(id, AccessNode {225            .id = id,226            .semantics = move part.semantics,227            .bounds = Area {228                .x = (origin.x + b.x) * s,229                .y = (origin.y + b.y) * s,230                .width = b.width * s,231                .height = b.height * s232            },233            .children = Vector<u64>::empty()234        });235        children.push(id);236    }237    for (node.children.view()) |child| {238        ui.describe(child, origin, &none, node.direction, &var nodes, &var children);239    }240    const id = ui.access_id(index);241    nodes.insert(id, AccessNode {242        .id = id,243        .semantics = move semantics,244        .bounds = Area {245            .x = origin.x * s,246            .y = origin.y * s,247            .width = node.size.width * s,248            .height = node.size.height * s249        },250        .children = move children,251        .text = move map252    });253    into.push(id);254}255256// Does what a screen reader asks: focusing and scrolling into view257// itself, a click as a click at the element's middle, stepping as the258// arrow keys, the rest as Input::Action.259exp fn access_action(self ui: &var Ui, request: &ActionRequest) {260    if (ui.access_part(request.node) is ::Some |found|) {261        ui.part_action(found[0], found[1], copy request.action);262        return;263    }264    const index = if (ui.access_element(request.node) is ::Some |i|) i else {265        return;266    };267    match (request.action) {268        ::Focus {269            ui.focus_node(::Some(index), true);270        }271        ::ScrollIntoView {272            ui.reveal(index);273        }274        ::Click {275            const origin = ui.origin_of(index);276            const size = ui.node(index).size;277            const at = Point {278                .x = origin.x + size.width / 2.0,279                .y = origin.y + size.height / 2.0280            };281            ui.dispatch(index, Input::Click(at));282        }283        ::Dismiss {284            ui.extensions_dismissed(index);285        }286        ::Increment, ::Decrement {287            const key = if (request.action is ::Increment) Key::Up else Key::Down;288            for ([true, false]) |pressed| {289                ui.dispatch(index, Input::Key(KeyEvent {290                    .key = key,291                    .pressed = pressed,292                    .repeat = false,293                    .modifiers = Modifiers::none()294                }));295            }296        }297        else {298            ui.dispatch(index, Input::Action(copy request.action));299        }300    }301}302303// Gives the element what a screen reader asks of one of its parts,304// focusing the element first for Focus, and scrolling it into view.305fn part_action(self ui: &var Ui, index: u32, part: u32, action: Action) {306    if (action is ::Focus) {307        ui.focus_node(::Some(index), true);308    }309    const reveal = action is ::ScrollIntoView;310    ui.dispatch(index, Input::Part(PartRequest { .part = part, .action = move action }));311    if (reveal) {312        ui.reveal(index);313    }314}