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