diff --git a/apps/leaf-dioxus/Cargo.lock b/apps/leaf-dioxus/Cargo.lock index 475086b..b96bb66 100644 --- a/apps/leaf-dioxus/Cargo.lock +++ b/apps/leaf-dioxus/Cargo.lock @@ -100,7 +100,7 @@ dependencies = [ [[package]] name = "accesskit_xplat" version = "0.2.1" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "accesskit", "accesskit_macos", @@ -545,7 +545,7 @@ checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" [[package]] name = "blitz-dom" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "accesskit", "anyrender", @@ -594,7 +594,7 @@ dependencies = [ [[package]] name = "blitz-net" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "blitz-traits", "data-url", @@ -606,7 +606,7 @@ dependencies = [ [[package]] name = "blitz-paint" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "anyrender", "anyrender_svg", @@ -628,7 +628,7 @@ dependencies = [ [[package]] name = "blitz-shell" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "accesskit", "accesskit_xplat", @@ -649,7 +649,7 @@ dependencies = [ [[package]] name = "blitz-traits" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "atomic_refcell", "bitflags", @@ -1107,7 +1107,7 @@ checksum = "be1e0bca6c3637f992fc1cc7cbc52a78c1ef6db076dbf1059c4323d6a2048376" [[package]] name = "debug_timer" version = "0.1.3" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" [[package]] name = "derive_more" @@ -2716,6 +2716,7 @@ dependencies = [ name = "leaf-dioxus" version = "0.0.0" dependencies = [ + "accesskit", "arboard", "dioxus", "dioxus-native", @@ -2734,6 +2735,7 @@ dependencies = [ name = "leaf-dioxus-app" version = "0.5.1" dependencies = [ + "accesskit", "accesskit_xplat", "anyrender", "anyrender_vello", @@ -4894,7 +4896,7 @@ dependencies = [ [[package]] name = "stylo_taffy" version = "0.3.0-beta.4" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "bitflags", "stylo", @@ -6650,7 +6652,7 @@ dependencies = [ [[package]] name = "winit-appkit" version = "0.31.0-beta.3" -source = "git+https://github.com/diaryx-org/blitz?branch=leaf#d78620c598e07a51050f8e700620df8778470c95" +source = "git+https://github.com/diaryx-org/blitz?branch=leaf#9ee8db179bc5f3e73d1b6296f0af40b9e3134230" dependencies = [ "bitflags", "block2 0.6.2", diff --git a/apps/leaf-dioxus/Cargo.toml b/apps/leaf-dioxus/Cargo.toml index 71fc10c..de98593 100644 --- a/apps/leaf-dioxus/Cargo.toml +++ b/apps/leaf-dioxus/Cargo.toml @@ -68,6 +68,8 @@ blitz-traits = "=0.3.0-beta.4" # `View::accessibility_action_events` through the app's own window events: # the version blitz-shell's `accessibility` uses. accesskit_xplat = "0.2" +# The tree itself, where the editor's text goes in (`access.rs`). +accesskit = "0.25" # Export as PDF and Print (`pdf.rs`): Blitz's paint recorded, and drawn again # as PDF pages by krilla, glyphs' characters read back from their fonts. anyrender = "0.14" diff --git a/apps/leaf-dioxus/README.md b/apps/leaf-dioxus/README.md index 9d429da..1568204 100644 --- a/apps/leaf-dioxus/README.md +++ b/apps/leaf-dioxus/README.md @@ -98,6 +98,25 @@ app carries out through its own handling of window events. The tree is rebuilt a rebuilds it only on a poll that finds changes, which the app's own resolves have usually taken first. The iOS side is the fork's `examples/ios-a11y`. +The document is a text area (`AXTextArea`), not static text. Each tree +built passes through a hook (`access.rs`, blitz-shell's +`View::set_accessibility_tree_hook`) that hands the editor's element to the +editor (`Editor::describe`, the widget's `access.rs`): its value is the +whole text, and its children are text runs, one per visual line, with each +character's length, position and width, where its words start, and the +selection. In the rendered view the text is the text as shown; in the +source view it is the Markdown. So assistive tech reads AXValue, +AXNumberOfCharacters, AXSelectedText and AXSelectedTextRange, AXInsertionPointLineNumber, +AXRangeForLine, AXLineForIndex, AXStringForRange and AXBoundsForRange, and +VoiceOver reads by line and word. Setting AXValue or AXSelectedTextRange +comes back as AccessKit's `SetValue` or `SetTextSelection`, which the +editor carries out through the document as typing does +(`Editor::accessibility_action`): a new value replaces only the stretch +that differs, typed over. The text and its runs are kept while the +document, the caret and the rows' layouts are unchanged, so a frame that +changes neither costs about a millisecond (120,000 characters, debug +build). + **winit is patched.** Its macOS backend is kept, patched, in the Blitz fork's `patches/winit-appkit` (`PATCH.md` there says what and why; `upstream/` is the change prepared for winit), and the app takes it by git @@ -446,13 +465,15 @@ An agent can test the app without taking the screen from whoever is using it `open`, which brings the app forward; `additional_arguments` passes it arguments. - **Reading it.** `get_window_state` captures one window even when it is - covered or minimized. The tree holds only the window's own buttons and the - menu bar: the page is drawn by Blitz and has no accessibility elements. - Read frames with `list_windows` or `System Events` (`position`, `size`). + covered or minimized. The document is an `AXTextArea` whose value is its + text (see "Accessibility" above), and the toolbar's controls are buttons + and checkboxes. Read frames with `list_windows` or `System Events` + (`position`, `size`). - **Acting on it.** Menu items through `invoke_menu`, which does not activate the app; window buttons by their element tokens; text with - `type_text` and a point in the page, which reaches the editor in a covered - window. A pixel click in the background does not move the caret, and keys + `set_value` on the text area's element token, read back from its value + (checked on 2026-10-22, in a covered window), or `type_text` and a point in + the page. A pixel click in the background does not move the caret, and keys to a minimized window are refused. `delivery_mode: "foreground"` brings the app forward and moves the real pointer: not for a screen someone is using. `set_window_frame` moves and sizes a window, between displays too. diff --git a/apps/leaf-dioxus/src/access.rs b/apps/leaf-dioxus/src/access.rs new file mode 100644 index 0000000..ec8d652 --- /dev/null +++ b/apps/leaf-dioxus/src/access.rs @@ -0,0 +1,82 @@ +//! The document's text in each window's accessibility tree. +//! +//! Blitz builds the tree from the page, where the editor's rows are divs of +//! static text. Each tree built passes through the hook installed here, +//! which hands the editor's element ([`leaf_dioxus::TEXT_ID`]) to the +//! editor to make a text area of — its value, text runs and selection — in +//! place of the nodes under it. What assistive tech asks of that text area +//! comes back through [`action`]. + +use std::collections::HashSet; + +use accesskit::{ActionRequest, NodeId, TreeUpdate}; +use blitz_dom::BaseDocument; +use blitz_shell::View; +use dioxus_native::winit::window::WindowId; +use leaf_dioxus::{TEXT_ID, is_text_run}; + +use crate::app::page; +use crate::renderer::Renderer; + +/// What AccessKit calls the window: the root Blitz gives the tree. +const WINDOW: NodeId = NodeId(u64::MAX); + +/// Pass every tree built for window `id` through [`describe`]. +pub fn install(view: &mut View, id: WindowId) { + view.set_accessibility_tree_hook(move |doc, tree| describe(id, doc, tree)); +} + +/// Make the editor's element in `tree` the document's text area, if the +/// window has an editor. +fn describe(id: WindowId, doc: &BaseDocument, tree: &mut TreeUpdate) { + let Some(ed) = page(id).and_then(|p| p.editor()) else { + return; + }; + let Some(text) = doc.get_element_by_id(TEXT_ID) else { + return; + }; + let text_id = NodeId(text.as_u64()); + let Some(at) = tree.nodes.iter().position(|(n, _)| *n == text_id) else { + return; + }; + let Some(runs) = ed.describe(&mut tree.nodes[at].1) else { + return; + }; + // The rows' nodes go: the runs stand for them. + let mut under = HashSet::new(); + let mut stack: Vec<_> = doc + .get_node(text) + .map_or(Vec::new(), |n| n.children.to_vec()); + while let Some(n) = stack.pop() { + under.insert(n.as_u64()); + if let Some(node) = doc.get_node(n) { + stack.extend(node.children.iter().copied()); + } + } + tree.nodes.retain(|(n, _)| !under.contains(&n.0)); + tree.nodes.extend(runs); + // The editor has the keys unless one of the page's controls has them, + // so it has the focus too: the caret VoiceOver follows is its. + let focus = tree.focus; + let root = NodeId(doc.root_node().id.as_u64()); + if focus == WINDOW + || focus == root + || under.contains(&focus.0) + || !tree.nodes.iter().any(|(n, _)| *n == focus) + { + tree.focus = text_id; + } +} + +/// Carry out what assistive tech asks of the editor's text area or one of +/// its runs, in window `id`: whether it was the editor's to do. +pub fn action(view: &View, id: WindowId, request: &ActionRequest) -> bool { + let text = view.doc.inner().get_element_by_id(TEXT_ID); + let target = request.target_node; + if !is_text_run(target) && text.map(|t| NodeId(t.as_u64())) != Some(target) { + return false; + } + page(id) + .and_then(|p| p.with_editor(|ed, _| ed.accessibility_action(request))) + .unwrap_or(false) +} diff --git a/apps/leaf-dioxus/src/app.rs b/apps/leaf-dioxus/src/app.rs index d6f69b0..e5a22c1 100644 --- a/apps/leaf-dioxus/src/app.rs +++ b/apps/leaf-dioxus/src/app.rs @@ -349,6 +349,7 @@ impl App { }); doc.initial_build(); } + crate::access::install(&mut view, window.id()); view.resume(); view.request_redraw(); self.blitz.windows.insert(window.id(), view); @@ -587,9 +588,18 @@ impl ApplicationHandler for App { // A press assistive tech asks for is carried out as a click, // through this app's own handling of window events, so the page // and the editor see it as they see a hand's. + // What it asks of the document's text is the editor's. if let BlitzShellEvent::Accessibility { window_id, data } = &event && let accesskit_xplat::WindowEvent::ActionRequested(request) = &**data { + let edited = self + .blitz + .windows + .get(window_id) + .is_some_and(|view| crate::access::action(view, *window_id, request)); + if edited { + continue; + } let events = self .blitz .windows diff --git a/apps/leaf-dioxus/src/main.rs b/apps/leaf-dioxus/src/main.rs index ee9fd0b..022836e 100644 --- a/apps/leaf-dioxus/src/main.rs +++ b/apps/leaf-dioxus/src/main.rs @@ -24,6 +24,7 @@ //! `LEAF_TRACE=1` logs keys, pointer events and repaint timings to stderr; //! `LEAF_OUTLINE=1` outlines every visual line and run. +mod access; mod app; mod commands; mod context; diff --git a/crates/leaf-dioxus/Cargo.lock b/crates/leaf-dioxus/Cargo.lock index 561bcd1..d8ca590 100644 --- a/crates/leaf-dioxus/Cargo.lock +++ b/crates/leaf-dioxus/Cargo.lock @@ -2339,6 +2339,7 @@ dependencies = [ name = "leaf-dioxus" version = "0.0.0" dependencies = [ + "accesskit", "arboard", "dioxus", "dioxus-native", diff --git a/crates/leaf-dioxus/Cargo.toml b/crates/leaf-dioxus/Cargo.toml index 65d4ffd..9ede7f9 100644 --- a/crates/leaf-dioxus/Cargo.toml +++ b/crates/leaf-dioxus/Cargo.toml @@ -87,6 +87,9 @@ parley = "=0.12.0" unicode-width = "0.2" # Grapheme clusters, the unit a column advances by, as core walks them. unicode-segmentation = "1.13" +# The document as assistive tech reads and edits it (`access.rs`): the +# AccessKit Blitz's accessibility tree is made of, at the version it uses. +accesskit = "0.25" # Block pictures (`media.rs`): a picture's size off its header, an SVG's off # its canvas, so the block's height is known before Blitz has decoded it. Both diff --git a/crates/leaf-dioxus/README.md b/crates/leaf-dioxus/README.md index afad4b3..d7f0909 100644 --- a/crates/leaf-dioxus/README.md +++ b/crates/leaf-dioxus/README.md @@ -134,6 +134,14 @@ is the Swift editor's defaults. follow. It does not clip to the box: Blitz will not repaint a clip that changes, so the stylesheet still clips at `body`, and the host covers what lies outside the box with its own chrome, in a stacking layer above it. +- **Assistive tech** reads the document as text when the host hands the + editor its element's node. Blitz's tree has the page's rows as static + text; a host with blitz-shell's `View::set_accessibility_tree_hook` finds + the element by its id, `TEXT_ID`, gives its node to `Editor::describe`, + which makes it a text area and answers its text runs, and puts the runs in + the tree in place of the nodes under it. The requests that come back for + that node or a run (`is_text_run`) go to `Editor::accessibility_action`. + `apps/leaf-dioxus/src/access.rs` does all three. - **Under a finger** (iOS), a tap places the caret and raises the keyboard, a drag scrolls, and a sideways drag, where `Editor::set_turns` allows it, slides the page and is `Host::turn` when let go far enough. The keyboard is diff --git a/crates/leaf-dioxus/src/access.rs b/crates/leaf-dioxus/src/access.rs new file mode 100644 index 0000000..0ce1b82 --- /dev/null +++ b/crates/leaf-dioxus/src/access.rs @@ -0,0 +1,832 @@ +//! The document as assistive tech reads and edits it: a text area, as +//! AccessKit describes one, for VoiceOver and for agents that drive the +//! app through the accessibility API. +//! +//! The page draws the document as rows of runs, which Blitz's tree would +//! give as static text with no value, selection or caret. So the host hands +//! the editor's node to [`Editor::describe`], which makes it a +//! `MultilineTextInput`: its value the whole text, one `TextRun` child per +//! visual line with each character's length, position and width and where +//! its words start, and the selection. What assistive tech then asks of it +//! (set the value, replace the selection, select a range) the host hands to +//! [`Editor::accessibility_action`], which edits through the document as +//! typing does. +//! +//! **The text is the text as shown.** In the rendered view that is each +//! row's drawn glyphs, a line per row, the rows that hold no text (a table's +//! rules, the gap between blocks) left out; in the source view it is the +//! Markdown source. A position in it maps to the source through the view's +//! own (row, column) map ([`crate::motion::pos_of`], +//! [`crate::motion::offset_at`]), the map a click goes through. A character +//! is a grapheme cluster, the unit the caret moves by. + +use std::ops::Range; +use std::rc::Rc; + +use accesskit::{ + Action, ActionData, ActionRequest, Node, NodeId, Rect, Role, TextDirection, TextPosition, + TextSelection, +}; +use dioxus::prelude::*; +use leaf_core::{Doc, View}; +use unicode_segmentation::UnicodeSegmentation; +use unicode_width::UnicodeWidthStr; + +use crate::editor::{Editor, shown_caret}; +use crate::motion::{offset_at, pos_of}; + +/// The element [`Editor::render`] draws the document's rows in, by its DOM +/// id: the node a host finds in Blitz's tree and hands to +/// [`Editor::describe`]. +pub const TEXT_ID: &str = "leaf-text"; + +/// The first of the ids [`Editor::describe`] gives the text runs, clear of +/// the ids Blitz gives a document's nodes (their slab indices). +pub const RUN_IDS: u64 = 0x4c45_4146 << 32; + +/// The most characters a run holds: AccessKit counts a run's word starts in +/// a byte each. A longer line is split into runs on the same line. +const RUN_MAX: usize = u8::MAX as usize; + +/// One line of the text: a row of the view. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Line { + /// The view's row it is. + row: usize, + text: String, + /// Each character's first byte in `text`, and its display column. + chars: Vec<(usize, usize)>, + /// The display column past the last character. + end_col: usize, + /// The source offset just past each character: where a range that ends + /// with it ends. Not always the next character's offset, which in the + /// rendered view may be past hidden markup (`**` after a bold word). + ends: Vec, +} + +impl Line { + /// Line `row`, its text `text`, and the source offset just past each + /// of its `char`s, in order. + fn new(row: usize, text: String, char_ends: &[usize]) -> Self { + let mut col = 0; + let mut seen = 0; + let mut ends = Vec::new(); + let chars = text + .grapheme_indices(true) + .map(|(b, g)| { + let at = (b, col); + col += UnicodeWidthStr::width(g); + seen += g.chars().count(); + ends.push(char_ends.get(seen - 1).copied().unwrap_or(0)); + at + }) + .collect(); + Self { + row, + text, + chars, + end_col: col, + ends, + } + } + + fn len(&self) -> usize { + self.chars.len() + } + + /// The bytes of character `ch`; at the end, the empty range there. + fn bytes(&self, ch: usize) -> Range { + let start = self.chars.get(ch).map_or(self.text.len(), |c| c.0); + let end = self.chars.get(ch + 1).map_or(self.text.len(), |c| c.0); + start..end + } + + /// The display column of character `ch`, or past the last. + fn col(&self, ch: usize) -> usize { + self.chars.get(ch).map_or(self.end_col, |c| c.1) + } + + /// The character at display column `col`: the first that starts at or + /// past it, or the end. + fn char_at_col(&self, col: usize) -> usize { + self.chars.partition_point(|c| c.1 < col) + } +} + +/// The document's text as shown: its lines, a line break between each two. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Text { + lines: Vec, +} + +impl Text { + /// The text of `doc` in the view it shows, as of its last build (the + /// rendered view's map, the source view's rows). + pub(crate) fn of(doc: &Doc) -> Self { + let mut lines: Vec = match doc.view { + View::Source => { + let mut start = 0; + doc.source + .split('\n') + .enumerate() + .map(|(r, l)| { + let ends: Vec = l + .char_indices() + .map(|(b, c)| start + b + c.len_utf8()) + .collect(); + start += l.len() + 1; + Line::new(r, l.to_string(), &ends) + }) + .collect() + } + // A glyph a character, so each one's end is its own. + View::Wysiwyg => doc + .vmap + .rows + .iter() + .enumerate() + .filter(|(_, r)| !r.decoration) + .map(|(r, row)| { + let ends: Vec = + row.glyphs.iter().map(|g| g.src + g.ch.len_utf8()).collect(); + Line::new(r, row.glyphs.iter().map(|g| g.drawn()).collect(), &ends) + }) + .collect(), + }; + if lines.is_empty() { + lines.push(Line::new(0, String::new(), &[])); + } + Self { lines } + } + + pub(crate) fn value(&self) -> String { + let mut v = String::new(); + for (i, l) in self.lines.iter().enumerate() { + if i > 0 { + v.push('\n'); + } + v.push_str(&l.text); + } + v + } + + /// The (line, character) a source offset is shown at. + pub(crate) fn position(&self, doc: &Doc, offset: usize) -> (usize, usize) { + let (row, col) = pos_of(doc, offset); + let line = self + .lines + .partition_point(|l| l.row < row) + .min(self.lines.len() - 1); + let l = &self.lines[line]; + if l.row != row { + // A row the text leaves out: the start of the next line. + return (line, 0); + } + (line, l.char_at_col(col)) + } + + /// The source offset of (line, character). + pub(crate) fn offset(&self, doc: &Doc, line: usize, ch: usize) -> usize { + let l = &self.lines[line.min(self.lines.len() - 1)]; + offset_at(doc, l.row, l.col(ch.min(l.len()))) + } + + /// The source offset where a range ending before (line, character) + /// ends: just past the character before it, or the line's start. + pub(crate) fn end(&self, doc: &Doc, line: usize, ch: usize) -> usize { + let l = &self.lines[line.min(self.lines.len() - 1)]; + match ch.min(l.len()).checked_sub(1) { + Some(before) => l.ends[before], + None => self.offset(doc, line, 0), + } + } + + /// The (line, character) at byte `at` of [`value`](Self::value): the + /// character that starts at or after it. + pub(crate) fn locate(&self, at: usize) -> (usize, usize) { + let mut start = 0; + for (i, l) in self.lines.iter().enumerate() { + let end = start + l.text.len(); + if at <= end || i + 1 == self.lines.len() { + let within = at.saturating_sub(start); + return (i, l.chars.partition_point(|c| c.0 < within)); + } + start = end + 1; + } + (0, 0) + } +} + +/// One text run: a stretch of a line, on one visual line. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Run { + line: usize, + chars: Range, + /// It is its line's last, and the line break is its last character. + breaks: bool, + /// The run after it is on the same visual line: a line split at + /// [`RUN_MAX`]. + continues: bool, + /// Which of its row's visual lines it is on. + visual: usize, +} + +/// The text as last described, with its runs: what assistive tech's next +/// request names positions in. +#[derive(Clone, Debug, PartialEq)] +pub(crate) struct Described { + text: Text, + runs: Vec, +} + +impl Described { + /// `text` in runs, each line split where `wraps` says its visual lines + /// start (as characters of it, after the first), and at [`RUN_MAX`]. + pub(crate) fn new(text: Text, wraps: impl Fn(&Line) -> Vec) -> Self { + let mut runs = Vec::new(); + let last = text.lines.len() - 1; + for (i, l) in text.lines.iter().enumerate() { + // Visual line `k` runs up to where line `k + 1` starts. + let mut cuts = wraps(l); + cuts.push(l.len()); + let mut from = 0; + let first = runs.len(); + for (visual, cut) in cuts.into_iter().enumerate() { + let cut = cut.clamp(from, l.len()); + while cut - from > RUN_MAX { + runs.push(Run { + line: i, + chars: from..from + RUN_MAX, + breaks: false, + continues: true, + visual, + }); + from += RUN_MAX; + } + if cut > from || runs.len() == first && cut == l.len() { + runs.push(Run { + line: i, + chars: from..cut, + breaks: false, + continues: false, + visual, + }); + } + from = cut; + } + runs.last_mut().expect("a run per line").breaks = i < last; + } + Self { text, runs } + } + + /// The run and the index within it of (line, character): at a soft + /// wrap, the start of the next visual line; at a line's end, its break. + fn text_position(&self, line: usize, ch: usize) -> TextPosition { + let first = self.runs.partition_point(|r| r.line < line); + let mut i = first; + while i + 1 < self.runs.len() + && self.runs[i + 1].line == line + && self.runs[i].chars.end <= ch + { + i += 1; + } + let r = &self.runs[i]; + TextPosition { + node: run_id(i), + character_index: ch.clamp(r.chars.start, r.chars.end) - r.chars.start, + } + } + + /// The (line, character) of a position in a run; `None` for a node not + /// a run of this text. + fn line_char(&self, p: TextPosition) -> Option<(usize, usize)> { + let i = p.node.0.checked_sub(RUN_IDS)? as usize; + let r = self.runs.get(i)?; + Some((r.line, (r.chars.start + p.character_index).min(r.chars.end))) + } + + /// Run `i` as AccessKit's node, without its geometry. + fn run_node(&self, i: usize) -> Node { + let r = &self.runs[i]; + let l = &self.text.lines[r.line]; + let mut node = Node::new(Role::TextRun); + let bytes = l.bytes(r.chars.start).start..l.bytes(r.chars.end).start; + let mut value = l.text[bytes.clone()].to_string(); + let mut lengths: Vec = r + .chars + .clone() + .map(|c| l.bytes(c).len().min(u8::MAX as usize) as u8) + .collect(); + if r.breaks { + value.push('\n'); + lengths.push(1); + } + // Where a word starts: a word-bound segment that opens on a letter + // or a digit, as the caret's word motion stops. + let starts: Vec = l + .text + .split_word_bound_indices() + .filter(|(b, w)| { + bytes.contains(b) && w.chars().next().is_some_and(char::is_alphanumeric) + }) + .map(|(b, _)| (l.chars.partition_point(|c| c.0 < b) - r.chars.start) as u8) + .collect(); + node.set_value(value); + node.set_character_lengths(lengths); + node.set_word_starts(starts); + node.set_text_direction(TextDirection::LeftToRight); + if r.continues { + node.set_next_on_line(run_id(i + 1)); + } + if i > 0 && self.runs[i - 1].continues { + node.set_previous_on_line(run_id(i - 1)); + } + node + } + + /// The selection of `doc`, from its anchor to its caret. + fn selection(&self, doc: &Doc) -> TextSelection { + let caret = match doc.view { + View::Wysiwyg => shown_caret(doc, doc.caret), + View::Source => doc.caret, + }; + let at = |offset| { + let (line, ch) = self.text.position(doc, offset); + self.text_position(line, ch) + }; + let focus = at(caret); + TextSelection { + anchor: doc.anchor.map_or(focus, at), + focus, + } + } + + /// The editor's node, made the text area: its value, its runs as its + /// children, and the selection. + fn text_area(&self, doc: &Doc, node: &mut Node) { + node.set_role(Role::MultilineTextInput); + node.set_value(self.text.value()); + node.clear_children(); + node.clear_labelled_by(); + for i in 0..self.runs.len() { + node.push_child(run_id(i)); + } + node.set_text_selection(self.selection(doc)); + node.add_action(Action::Focus); + if doc.read_only() { + node.set_read_only(); + } else { + node.add_action(Action::SetValue); + node.add_action(Action::ReplaceSelectedText); + } + node.add_action(Action::SetTextSelection); + } + + /// What `request` asks of `doc`, done: whether it was this text's to do. + pub(crate) fn act(&self, doc: &mut Doc, request: &ActionRequest) -> bool { + match (request.action, &request.data) { + (Action::SetTextSelection, Some(ActionData::SetTextSelection(s))) => { + let (Some(a), Some(f)) = (self.line_char(s.anchor), self.line_char(s.focus)) else { + return false; + }; + // The range's start is where its first character is, and + // its end just past its last. + let (anchor, focus) = match a.cmp(&f) { + std::cmp::Ordering::Less => ( + self.text.offset(doc, a.0, a.1), + self.text.end(doc, f.0, f.1), + ), + std::cmp::Ordering::Greater => ( + self.text.end(doc, a.0, a.1), + self.text.offset(doc, f.0, f.1), + ), + std::cmp::Ordering::Equal => { + let at = self.text.offset(doc, a.0, a.1); + (at, at) + } + }; + doc.select_range(anchor, focus); + true + } + (Action::ReplaceSelectedText, Some(ActionData::Value(text))) => { + doc.insert(text); + true + } + (Action::SetValue, Some(ActionData::Value(text))) => { + self.set_value(doc, text); + true + } + (Action::Focus, _) => true, + _ => false, + } + } + + /// Bring the text to `new`. In the source view the source is the text, + /// written exactly. In the rendered view the stretch that differs is + /// selected and `new`'s part of it typed over it, as a hand would. + fn set_value(&self, doc: &mut Doc, new: &str) { + if doc.view == View::Source { + doc.replace_source(new); + return; + } + let old = self.text.value(); + let mut prefix = old + .bytes() + .zip(new.bytes()) + .take_while(|(a, b)| a == b) + .count(); + while !old.is_char_boundary(prefix) || !new.is_char_boundary(prefix) { + prefix -= 1; + } + let room = old.len().min(new.len()) - prefix; + let mut suffix = old + .bytes() + .rev() + .zip(new.bytes().rev()) + .take(room) + .take_while(|(a, b)| a == b) + .count(); + while !old.is_char_boundary(old.len() - suffix) || !new.is_char_boundary(new.len() - suffix) + { + suffix -= 1; + } + let (from, to) = (prefix, old.len() - suffix); + let with = &new[prefix..new.len() - suffix]; + if from == to && with.is_empty() { + return; + } + let (l0, c0) = self.text.locate(from); + let (l1, c1) = self.text.locate(to); + let start = self.text.offset(doc, l0, c0); + let end = if to > from { + self.text.end(doc, l1, c1) + } else { + start + }; + doc.select_range(start, end); + doc.insert(with); + } +} + +/// What a described text was built from; see [`Editor::describe`]. +#[derive(PartialEq)] +struct Key { + revision: u64, + source: bool, + caret: usize, + anchor: Option, + /// Each row's layout, by its address. + layouts: Vec, +} + +/// A text as last described, its runs' nodes made but for their geometry. +pub(crate) struct Cached { + key: Key, + described: Described, + nodes: Vec, +} + +fn run_id(i: usize) -> NodeId { + NodeId(RUN_IDS + i as u64) +} + +/// Whether node `id` is one [`Editor::describe`] made: a text run, whose +/// requests are the editor's ([`Editor::accessibility_action`]). +pub fn is_text_run(id: NodeId) -> bool { + id.0 >= RUN_IDS +} + +impl Editor { + /// Make `node` — the editor's in the host's accessibility tree, the + /// element [`TEXT_ID`] names — the document's text area, and answer its + /// text runs, which the host puts in the tree in place of the nodes it + /// had under `node`. Bounds are in physical pixels from the window's + /// top-left, as Blitz's are. `None` while the editor has its document + /// borrowed: `node` is then left as it was. + pub fn describe(&self, node: &mut Node) -> Option> { + let t0 = std::time::Instant::now(); + let doc = self.st.doc.try_borrow().ok()?; + let lays = self.sig.lays.peek(); + // The text and its runs hold while the document, the caret (which + // reveals markup in the rendered view) and the rows' layouts do: + // across the frames a blinking caret or a scroll draws. + let key = Key { + revision: doc.revision(), + source: doc.view == View::Source, + caret: doc.caret, + anchor: doc.anchor, + layouts: lays.iter().map(|l| Rc::as_ptr(&l.0) as usize).collect(), + }; + let cached = self.st.described.borrow().clone().filter(|c| c.key == key); + let cached = cached.unwrap_or_else(|| { + let text = Text::of(&doc); + let described = Described::new(text, |l| match lays.get(l.row) { + // Each visual line after the first starts a run. + Some(lay) if !self.in_block(l.row) => lay + .0 + .line_cols() + .into_iter() + .skip(1) + .map(|col| l.char_at_col(col)) + .collect(), + _ => Vec::new(), + }); + let nodes = (0..described.runs.len()) + .map(|i| described.run_node(i)) + .collect(); + let c = Rc::new(Cached { + key, + described, + nodes, + }); + *self.st.described.borrow_mut() = Some(c.clone()); + c + }); + let described = &cached.described; + described.text_area(&doc, node); + + let scale = self.st.window.scale_factor(); + let layout = self.sig.layout.peek(); + let page_top = self.sig.origin.peek().1 + self.sig.bar.peek().height(); + let y0 = page_top - *self.sig.top.peek(); + let page_bottom = page_top + self.view_height(); + let (left, width) = (self.left(), *self.sig.width.peek() as f64); + let px = |x: f64, y: f64, w: f64, h: f64| { + Rect::new(x * scale, y * scale, (x + w) * scale, (y + h) * scale) + }; + let runs = (0..described.runs.len()) + .map(|i| { + let mut n = cached.nodes[i].clone(); + let r = &described.runs[i]; + let l = &described.text.lines[r.line]; + let row = l.row; + let lay = lays.get(row).filter(|_| !self.in_block(row)).map(|l| &*l.0); + if row >= layout.rows() { + return (run_id(i), n); + } + let Some(lay) = lay else { + // A block (a table, a picture) draws its rows its own + // way: the run is its row's share of it. + let top = layout.top(row); + n.set_bounds(px(left, y0 + top, width, layout.bottom(row) - top)); + return (run_id(i), n); + }; + let vl = r.visual; + let Some(line) = lay.lines.get(vl) else { + return (run_id(i), n); + }; + let y = y0 + layout.top(row) + line.top as f64; + let h = line.height as f64; + n.set_bounds(px(left, y, width, h)); + // Where each character is, for the runs on screen; measuring + // the rest would cost a layout walk each for nothing seen. + if y + h > page_top && y < page_bottom { + let xs: Vec = r + .chars + .clone() + .chain(std::iter::once(r.chars.end)) + .map(|c| { + let (at, x) = lay.caret(l.col(c)); + if at == vl { x } else { f32::NAN } + }) + .collect(); + let mut positions = Vec::with_capacity(xs.len()); + let mut widths = Vec::with_capacity(xs.len()); + for k in 0..r.chars.len() { + let x = xs[k]; + // The last character before a soft wrap ends where + // the next visual line starts: as wide as the one + // before it. + let w = if xs[k + 1].is_nan() { + widths.last().copied().unwrap_or(0.0) / scale as f32 + } else { + xs[k + 1] - x + }; + positions.push(x * scale as f32); + widths.push(w.max(0.0) * scale as f32); + } + if r.breaks { + let end = xs.last().copied().filter(|x| !x.is_nan()); + positions.push(end.unwrap_or(0.0) * scale as f32); + widths.push(0.0); + } + n.set_character_positions(positions); + n.set_character_widths(widths); + } + (run_id(i), n) + }) + .collect(); + drop(doc); + crate::trace::trace(|| format!("described the text: {:.1?}", t0.elapsed())); + Some(runs) + } + + /// What assistive tech asks of the text area or one of its runs: set its + /// value, replace its selection, select a range. Done through the + /// document as typing is, then repainted. Whether it was the editor's to + /// do: `false` for any other action, and before the first + /// [`describe`](Self::describe). + pub fn accessibility_action(&self, request: &ActionRequest) -> bool { + let st = &*self.st; + let Some(cached) = st.described.borrow().clone() else { + return false; + }; + let done = { + let mut doc = st.doc.borrow_mut(); + st.composer.borrow_mut().settle(&mut doc); + cached.described.act(&mut doc, request) + }; + if done { + st.goal.set(None); + self.refresh(); + self.ime_area(); + } + done + } +} + +#[cfg(test)] +mod tests { + use accesskit::TreeId; + use leaf_core::Format; + + use super::*; + + fn rendered(src: &str) -> Doc { + let mut d = Doc::from_source(src.into(), Format::Markdown).expect("a document"); + d.set_inline_pictures(true); + d.build_visual_unwrapped(); + d + } + + fn source(src: &str) -> Doc { + let mut d = Doc::from_source(src.into(), Format::Markdown).expect("a document"); + d.toggle_view(); + d.build_source(); + d + } + + /// The runs, a line each. + fn lines(d: &Doc) -> Described { + Described::new(Text::of(d), |_| Vec::new()) + } + + fn request(action: Action, data: ActionData) -> ActionRequest { + ActionRequest { + action, + target_tree: TreeId::ROOT, + target_node: NodeId(1), + data: Some(data), + } + } + + fn at(node: usize, character_index: usize) -> TextPosition { + TextPosition { + node: run_id(node), + character_index, + } + } + + #[test] + fn the_rendered_text_is_the_text_shown_a_line_a_row() { + let d = rendered("# Title\n\nSome **bold** text.\n"); + let t = Text::of(&d); + assert_eq!(t.value(), "Title\nSome bold text."); + let r = lines(&d); + let first = r.run_node(0); + assert_eq!(first.role(), Role::TextRun); + // The line break ends the line's run, a character of its own. + assert_eq!(first.value(), Some("Title\n")); + assert_eq!(first.character_lengths(), &[1, 1, 1, 1, 1, 1]); + assert_eq!(r.run_node(1).value(), Some("Some bold text.")); + assert_eq!(r.run_node(1).word_starts(), &[0, 5, 10]); + } + + #[test] + fn the_source_view_text_is_the_markdown() { + let d = source("# Title\n\nSome **bold** text."); + assert_eq!(Text::of(&d).value(), "# Title\n\nSome **bold** text."); + } + + #[test] + fn a_character_is_a_grapheme_cluster() { + let d = source("e\u{301}🙂x"); + let r = lines(&d); + assert_eq!(r.run_node(0).character_lengths(), &[3, 4, 1]); + } + + #[test] + fn the_selection_is_shown_where_the_caret_is() { + let mut d = rendered("# Title\n\nSome **bold** text.\n"); + let src = d.source.clone(); + let b = src.find("bold").unwrap(); + d.select_range(b, b + 4); + d.build_visual_unwrapped(); + let r = lines(&d); + let s = r.selection(&d); + assert_eq!(s.anchor, at(1, 5)); + assert_eq!(s.focus, at(1, 9)); + } + + #[test] + fn a_caret_at_a_lines_end_is_on_its_break() { + let mut d = source("ab\ncd"); + d.select_range(2, 2); + let s = lines(&d).selection(&d); + assert_eq!(s.focus, at(0, 2)); + assert_eq!(s.anchor, s.focus); + } + + #[test] + fn selecting_a_range_selects_its_source() { + let mut d = rendered("Some **bold** text.\n"); + let r = lines(&d); + assert!(r.act( + &mut d, + &request( + Action::SetTextSelection, + ActionData::SetTextSelection(TextSelection { + anchor: at(0, 5), + focus: at(0, 9), + }), + ), + )); + assert_eq!(d.selected_text(), Some("bold")); + } + + #[test] + fn replacing_the_selection_types_over_it() { + let mut d = rendered("Some **bold** text.\n"); + let b = d.source.find("bold").unwrap(); + d.select_range(b, b + 4); + let r = lines(&d); + assert!(r.act( + &mut d, + &request(Action::ReplaceSelectedText, ActionData::Value("big".into())) + )); + assert_eq!(d.source, "Some **big** text.\n"); + } + + #[test] + fn setting_the_value_edits_only_what_differs() { + let mut d = rendered("# Title\n\nSome **bold** text.\n"); + let r = lines(&d); + assert!(r.act( + &mut d, + &request( + Action::SetValue, + ActionData::Value("Title\nSome bold text, and more.".into()) + ), + )); + assert_eq!(d.source, "# Title\n\nSome **bold** text, and more.\n"); + } + + #[test] + fn setting_the_source_views_value_writes_it_exactly() { + let mut d = source("# Title\n"); + let r = lines(&d); + assert!(r.act( + &mut d, + &request(Action::SetValue, ActionData::Value("## Other *x*\n".into())) + )); + assert_eq!(d.source, "## Other *x*\n"); + } + + #[test] + fn a_long_line_is_runs_on_one_line() { + let long = "word ".repeat(120); + let d = source(long.trim_end()); + let r = lines(&d); + assert_eq!(r.runs.len(), 3); + assert_eq!(r.run_node(0).next_on_line(), Some(run_id(1))); + assert_eq!(r.run_node(1).previous_on_line(), Some(run_id(0))); + assert_eq!(r.run_node(2).next_on_line(), None); + // A position past the first run's end is in the second. + assert_eq!(r.text_position(0, 300), at(1, 45)); + } + + #[test] + fn a_soft_wrap_starts_a_run() { + let d = source("one two three"); + let r = Described::new(Text::of(&d), |_| vec![4, 8]); + let values: Vec<_> = (0..r.runs.len()) + .map(|i| r.run_node(i).value().unwrap().to_string()) + .collect(); + assert_eq!(values, ["one ", "two ", "three"]); + // At the wrap, the caret is at the next visual line's start. + assert_eq!(r.text_position(0, 4), at(1, 0)); + assert_eq!(r.text_position(0, 13), at(2, 5)); + } + + #[test] + fn an_empty_document_is_one_empty_line() { + let d = rendered(""); + let r = lines(&d); + assert_eq!(r.runs.len(), 1); + assert_eq!(r.run_node(0).value(), Some("")); + let mut node = Node::new(Role::GenericContainer); + r.text_area(&d, &mut node); + assert_eq!(node.role(), Role::MultilineTextInput); + assert_eq!(node.children(), &[run_id(0)]); + } +} diff --git a/crates/leaf-dioxus/src/editor.rs b/crates/leaf-dioxus/src/editor.rs index 49f2afc..bafeb5b 100644 --- a/crates/leaf-dioxus/src/editor.rs +++ b/crates/leaf-dioxus/src/editor.rs @@ -238,6 +238,9 @@ pub struct State { /// The runtime and the component the editor was made in, where a glide /// runs: it ends with them. pub home: Option<(Rc, dioxus::core::ScopeId)>, + /// The text as assistive tech was last given it, whose positions its + /// requests name; see `access.rs`. + pub(crate) described: RefCell>>, } /// What the render reads. Each is a signal, so the page re-renders when an @@ -430,6 +433,7 @@ impl Editor { caught: Cell::new(false), home: dioxus::core::Runtime::try_current() .and_then(|rt| rt.try_current_scope_id().map(|id| (rt, id))), + described: RefCell::default(), }; Self { st: Rc::new(st), diff --git a/crates/leaf-dioxus/src/lib.rs b/crates/leaf-dioxus/src/lib.rs index c1b3e98..8a02b34 100644 --- a/crates/leaf-dioxus/src/lib.rs +++ b/crates/leaf-dioxus/src/lib.rs @@ -48,6 +48,7 @@ //! fork (`patches/winit-appkit`); a `[patch]` here would not reach a host's //! build. +mod access; mod blocks; mod bridge; mod dispatch; @@ -73,6 +74,7 @@ mod trace; mod view; mod viewport; +pub use access::{TEXT_ID, is_text_run}; pub use blocks::Directive; pub use editor::{Bounds, Editor, Look, STATUS, Status, use_editor, use_shared_editor}; pub use host::{Clipboard, Host, Request}; diff --git a/crates/leaf-dioxus/src/motion.rs b/crates/leaf-dioxus/src/motion.rs index c78114c..1daaf45 100644 --- a/crates/leaf-dioxus/src/motion.rs +++ b/crates/leaf-dioxus/src/motion.rs @@ -165,7 +165,7 @@ impl Editor { self.sig.lays.peek().get(row)?.0.font_at(col) } - fn in_block(&self, row: usize) -> bool { + pub(crate) fn in_block(&self, row: usize) -> bool { self.sig .blocks .peek() diff --git a/crates/leaf-dioxus/src/text.rs b/crates/leaf-dioxus/src/text.rs index 5482167..c28346c 100644 --- a/crates/leaf-dioxus/src/text.rs +++ b/crates/leaf-dioxus/src/text.rs @@ -530,6 +530,21 @@ impl RowLayout { (line, self.prefix_w + x) } + /// The display column each visual line starts at. + pub fn line_cols(&self) -> Vec { + self.lines + .iter() + .enumerate() + .map(|(i, l)| { + if i == 0 { + 0 + } else { + self.prefix_cols + cols(&self.body.text[..l.bytes.start]) + } + }) + .collect() + } + /// The display column at the start or end of visual line `line`. pub fn edge(&self, line: usize, end: bool) -> usize { let line = line.min(self.lines.len() - 1); diff --git a/crates/leaf-dioxus/src/view.rs b/crates/leaf-dioxus/src/view.rs index 796b9f0..5c36c38 100644 --- a/crates/leaf-dioxus/src/view.rs +++ b/crates/leaf-dioxus/src/view.rs @@ -311,7 +311,9 @@ fn render(ed: &Editor) -> Element { // text selection, drawn over leaf's, and the focus changes that made // `onkeydown` unreliable (see `keys.rs`). Not `onpointerdown`: // cancelling the pointer event stops Blitz dispatching `mousedown`. - div { class: "leaf", style: "width: {column}px; margin-left: {margin}px; position: relative; left: {slide}px", + // The element assistive tech is given the document's text as + // (`access.rs`), by its id. + div { class: "leaf", id: crate::access::TEXT_ID, style: "width: {column}px; margin-left: {margin}px; position: relative; left: {slide}px", // The wheel and the trackpad, in CSS pixels: a line is 20. onwheel: move |e: WheelEvent| { e.prevent_default();