1// The page's accessibility layer, which browser.alloy's `start_layer`2// starts once a program installs screen readers: hidden elements over the3// canvas, one a node, behind a "Turn on accessibility" button, made by the4// JavaScript beside this file (layer.web.js). Only the installed layer5// calls these externs, so only a program installing it bundles that6// JavaScript, which reaches the canvas, keys and focus through what7// browser.web.js puts in the glue's `alloy.page`.89import std::ffi;10import std::option;11import std::string;12import std::vector;13import std::json;1415import window;1617pub extern al_window_access_start();18pub extern al_window_access(json: c_pointer, length: u32);19pub extern al_window_announce(text: c_pointer, length: u32, assertive: u32);20pub extern al_window_language(tag: c_pointer, length: u32, right_to_left: u32);2122// The update as layer.web.js reads it: ids as strings, a node's role23// as Role's variant number, bounds as [x, y, width, height]; numbers as24// their bits, f32 as numbers and f64 as strings, so no program carries25// float formatting for the page.26pub fn access_json(update: &TreeUpdate) -> String {27 var out = String::from(&"{\"nodes\":[");28 for ([..update.nodes.length()]) |k| {29 const node = &update.nodes.get(k);30 const s = &node.semantics;31 if (k != 0) {32 out.push(',');33 }34 out.append(&f"{{\"id\":\"{node.id}\",\"role\":{s.role to u32},\"label\":".view());35 write_text(&s.label.view(), &var out);36 out.append(&",\"value\":");37 write_text(&s.value.view(), &var out);38 const b = node.bounds;39 out.append(40 &f",\"bounds\":[{b.x as u32},{b.y as u32},{b.width as u32},{b.height as u32}],\"disabled\":{s.disabled},\"focusable\":{s.focusable},\"multiline\":{s.multiline},\"checked\":".view()41 );42 if (s.checked is ::Some |c|) {43 out.append(&f"{c}".view());44 } else {45 out.append(&"null");46 }47 out.append(&f",\"modal\":{s.modal},\"expanded\":".view());48 if (s.expanded is ::Some |e|) {49 out.append(&f"{e}".view());50 } else {51 out.append(&"null");52 }53 out.append(&",\"description\":");54 write_text(&s.description.view(), &var out);55 out.append(&",\"selected\":");56 if (s.selected is ::Some |e|) {57 out.append(&f"{e}".view());58 } else {59 out.append(&"null");60 }61 out.append(&",\"position\":");62 if (s.position is ::Some |p|) {63 out.append(&f"[{p[0]},{p[1]}]".view());64 } else {65 out.append(&"null");66 }67 out.append(&",\"cell\":");68 if (s.cell is ::Some |c|) {69 out.append(&f"[{c[0]},{c[1]}]".view());70 } else {71 out.append(&"null");72 }73 out.append(74 &f",\"current\":{s.current},\"busy\":{s.busy},\"invalid\":{s.invalid},\"required\":{s.required},\"secret\":{s.secret}".view()75 );76 out.append(&",\"dir\":");77 out.append(match (s.right_to_left) {78 ::Some |rtl| if (rtl) &"\"rtl\"" else &"\"ltr\"";79 ::None &"null";80 });81 out.append(&",\"range\":");82 if (s.range is ::Some |r|) {83 out.append(84 &f"[\"{r.value as u64}\",\"{r.minimum as u64}\",\"{r.maximum as u64}\",\"{r.step as u64}\"]".view()85 );86 } else {87 out.append(&"null");88 }89 out.append(&",\"children\":[");90 for ([..node.children.length()]) |c| {91 if (c != 0) {92 out.push(',');93 }94 out.append(&f"\"{node.children.get(c)}\"".view());95 }96 out.append(&"]}");97 }98 out.append(&"],\"removed\":[");99 for ([..update.removed.length()]) |k| {100 if (k != 0) {101 out.push(',');102 }103 out.append(&f"\"{update.removed.get(k)}\"".view());104 }105 const focus = if (update.focus is ::Some |f|) f else 0; // the page's none106 out.append(&f"],\"root\":\"{update.root}\",\"focus\":\"{focus}\"}}".view());107 return move out;108}