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 for (row_index, row) in galley.rows.iter().enumerate() {
44 let row_id = parent_id.with(row_index);
45 #[cfg(feature = "accesskit")]
46 ctx.register_accesskit_parent(row_id, parent_id);
47 ctx.accesskit_node_builder(row_id, |builder| {
48 builder.set_role(accesskit::Role::TextRun);
49 let rect = global_from_galley * row.rect_without_leading_space();
50 builder.set_bounds(accesskit::Rect {
51 x0: rect.min.x.into(),
52 y0: rect.min.y.into(),
53 x1: rect.max.x.into(),
54 y1: rect.max.y.into(),
55 });
56 builder.set_text_direction(accesskit::TextDirection::LeftToRight);
57 let glyph_count = row.glyphs.len();
61 let mut value = String::new();
62 value.reserve(glyph_count);
63 let mut character_lengths = Vec::<u8>::with_capacity(glyph_count);
64 let mut character_positions = Vec::<f32>::with_capacity(glyph_count);
65 let mut character_widths = Vec::<f32>::with_capacity(glyph_count);
66 let mut word_lengths = Vec::<u8>::new();
67 let mut was_at_word_end = false;
68 let mut last_word_start = 0usize;
69
70 for glyph in &row.glyphs {
71 let is_word_char = is_word_char(glyph.chr);
72 if is_word_char && was_at_word_end {
73 word_lengths.push((character_lengths.len() - last_word_start) as _);
74 last_word_start = character_lengths.len();
75 }
76 was_at_word_end = !is_word_char;
77 let old_len = value.len();
78 value.push(glyph.chr);
79 character_lengths.push((value.len() - old_len) as _);
80 character_positions.push(glyph.pos.x - row.pos.x);
81 character_widths.push(glyph.advance_width);
82 }
83
84 if row.ends_with_newline {
85 value.push('\n');
86 character_lengths.push(1);
87 character_positions.push(row.size.x);
88 character_widths.push(0.0);
89 }
90 word_lengths.push((character_lengths.len() - last_word_start) as _);
91
92 builder.set_value(value);
93 builder.set_character_lengths(character_lengths);
94 builder.set_character_positions(character_positions);
95 builder.set_character_widths(character_widths);
96 builder.set_word_lengths(word_lengths);
97 });
98 }
99}