egui/text_selection/
accesskit_text.rs

1use crate::{Context, Galley, Id, Pos2};
2
3use super::{text_cursor_state::is_word_char, CursorRange};
4
5/// Update accesskit with the current text state.
6pub fn update_accesskit_for_text_widget(
7    ctx: &Context,
8    widget_id: Id,
9    cursor_range: Option<CursorRange>,
10    role: accesskit::Role,
11    galley_pos: Pos2,
12    galley: &Galley,
13) {
14    let parent_id = ctx.accesskit_node_builder(widget_id, |builder| {
15        let parent_id = widget_id;
16
17        if let Some(cursor_range) = &cursor_range {
18            let anchor = &cursor_range.secondary.rcursor;
19            let focus = &cursor_range.primary.rcursor;
20            builder.set_text_selection(accesskit::TextSelection {
21                anchor: accesskit::TextPosition {
22                    node: parent_id.with(anchor.row).accesskit_id(),
23                    character_index: anchor.column,
24                },
25                focus: accesskit::TextPosition {
26                    node: parent_id.with(focus.row).accesskit_id(),
27                    character_index: focus.column,
28                },
29            });
30        }
31
32        builder.set_role(role);
33
34        parent_id
35    });
36
37    let Some(parent_id) = parent_id else {
38        return;
39    };
40
41    ctx.with_accessibility_parent(parent_id, || {
42        for (row_index, row) in galley.rows.iter().enumerate() {
43            let row_id = parent_id.with(row_index);
44            ctx.accesskit_node_builder(row_id, |builder| {
45                builder.set_role(accesskit::Role::TextRun);
46                let rect = row.rect.translate(galley_pos.to_vec2());
47                builder.set_bounds(accesskit::Rect {
48                    x0: rect.min.x.into(),
49                    y0: rect.min.y.into(),
50                    x1: rect.max.x.into(),
51                    y1: rect.max.y.into(),
52                });
53                builder.set_text_direction(accesskit::TextDirection::LeftToRight);
54                // TODO(mwcampbell): Set more node fields for the row
55                // once AccessKit adapters expose text formatting info.
56
57                let glyph_count = row.glyphs.len();
58                let mut value = String::new();
59                value.reserve(glyph_count);
60                let mut character_lengths = Vec::<u8>::with_capacity(glyph_count);
61                let mut character_positions = Vec::<f32>::with_capacity(glyph_count);
62                let mut character_widths = Vec::<f32>::with_capacity(glyph_count);
63                let mut word_lengths = Vec::<u8>::new();
64                let mut was_at_word_end = false;
65                let mut last_word_start = 0usize;
66
67                for glyph in &row.glyphs {
68                    let is_word_char = is_word_char(glyph.chr);
69                    if is_word_char && was_at_word_end {
70                        word_lengths.push((character_lengths.len() - last_word_start) as _);
71                        last_word_start = character_lengths.len();
72                    }
73                    was_at_word_end = !is_word_char;
74                    let old_len = value.len();
75                    value.push(glyph.chr);
76                    character_lengths.push((value.len() - old_len) as _);
77                    character_positions.push(glyph.pos.x - row.rect.min.x);
78                    character_widths.push(glyph.advance_width);
79                }
80
81                if row.ends_with_newline {
82                    value.push('\n');
83                    character_lengths.push(1);
84                    character_positions.push(row.rect.max.x - row.rect.min.x);
85                    character_widths.push(0.0);
86                }
87                word_lengths.push((character_lengths.len() - last_word_start) as _);
88
89                builder.set_value(value);
90                builder.set_character_lengths(character_lengths);
91                builder.set_character_positions(character_positions);
92                builder.set_character_widths(character_widths);
93                builder.set_word_lengths(word_lengths);
94            });
95        }
96    });
97}