use egui::accesskit::{self, Live, Role, TextPosition, TextSelection};
use egui::{Context, Id, Rect, Sense, Ui, UiBuilder};
const RUN_MAX: usize = 255;
pub struct Row {
pub value: String,
lens: Vec<u8>,
xs: Vec<f32>,
ws: Vec<f32>,
offs: Vec<u64>,
pub start: u64,
pub end: u64,
pub rect: Rect,
}
impl Row {
pub fn new(start: u64, end: u64, rect: Rect) -> Row {
Row { value: String::new(), lens: Vec::new(), xs: Vec::new(), ws: Vec::new(), offs: Vec::new(), start, end, rect }
}
pub fn push(&mut self, ch: char, off: u64, x: f32, w: f32) {
let n = self.value.len();
self.value.push(ch);
self.lens.push((self.value.len() - n) as u8);
self.xs.push(x);
self.ws.push(w);
self.offs.push(off);
}
fn index_of(&self, off: u64) -> usize {
self.offs.iter().take_while(|&&o| o < off).count()
}
}
pub fn enabled(ctx: &Context, id: Id) -> bool {
ctx.accesskit_node_builder(id, |_| ()).is_some()
}
pub struct TextWidget<'a> {
pub label: &'a str,
pub position: String,
pub read_only: bool,
pub rows: &'a [Row],
pub anchor: u64,
pub caret: u64,
}
fn run_id(id: Id, row: usize, chunk: usize) -> Id {
id.with(("a11y-run", row, chunk))
}
fn position(id: Id, row: usize, k: usize) -> TextPosition {
let chunk = if k > 0 && k.is_multiple_of(RUN_MAX) { k / RUN_MAX - 1 } else { k / RUN_MAX };
TextPosition { node: run_id(id, row, chunk).accesskit_id(), character_index: k - chunk * RUN_MAX }
}
fn locate(rows: &[Row], off: u64) -> Option<(usize, usize)> {
rows.iter().position(|r| off >= r.start && off <= r.end).map(|i| (i, rows[i].index_of(off)))
}
pub fn text_widget(ui: &mut Ui, id: Id, w: &TextWidget) -> String {
let ctx = ui.ctx().clone();
let runs = ui.new_child(UiBuilder::new().accessibility_parent(id));
let sel = locate(w.rows, w.caret).map(|focus| (locate(w.rows, w.anchor).unwrap_or(focus), focus));
ctx.accesskit_node_builder(id, |b| {
b.set_role(Role::MultilineTextInput);
b.set_label(w.label);
b.set_description(w.position.as_str());
if w.read_only {
b.set_read_only();
}
if let Some((a, f)) = sel {
b.set_text_selection(TextSelection { anchor: position(id, a.0, a.1), focus: position(id, f.0, f.1) });
}
});
for (i, r) in w.rows.iter().enumerate() {
let chunks = r.lens.len().div_ceil(RUN_MAX).max(1);
let mut byte = 0;
for c in 0..chunks {
let (a, z) = (c * RUN_MAX, ((c + 1) * RUN_MAX).min(r.lens.len()));
let len: usize = r.lens[a..z].iter().map(|&l| l as usize).sum();
let value = &r.value[byte..byte + len];
byte += len;
let rid = run_id(id, i, c);
runs.interact(Rect::NOTHING, rid, Sense::hover());
ctx.accesskit_node_builder(rid, |b| {
b.set_role(Role::TextRun);
b.set_text_direction(accesskit::TextDirection::LeftToRight);
let x0 = r.xs.get(a).copied().unwrap_or(0.0);
let x1 = if z > a { r.xs[z - 1] + r.ws[z - 1] } else { x0 };
b.set_bounds(accesskit::Rect { x0: (r.rect.left() + x0).into(), y0: r.rect.top().into(), x1: (r.rect.left() + x1).into(), y1: r.rect.bottom().into() });
b.set_value(value);
b.set_character_lengths(&r.lens[a..z]);
b.set_character_positions(r.xs[a..z].iter().map(|x| x - x0).collect::<Vec<f32>>());
b.set_character_widths(&r.ws[a..z]);
if c > 0 {
b.set_previous_on_line(run_id(id, i, c - 1).accesskit_id());
}
if c + 1 < chunks {
b.set_next_on_line(run_id(id, i, c + 1).accesskit_id());
}
});
}
}
let first = w.rows.first().map(|r| r.value.trim_end_matches('\n')).unwrap_or_default();
format!("{} rows; {}; first: {first}; selection {sel:?}", w.rows.len(), w.position)
}
pub fn live_region(ctx: &Context, id: Id, text: &str) {
ctx.accesskit_node_builder(id, |b| {
b.set_role(Role::Status);
b.set_live(Live::Polite);
b.set_value(text);
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn positions_split_long_rows() {
let id = Id::new("v");
assert_eq!(position(id, 0, 0).node, run_id(id, 0, 0).accesskit_id());
assert_eq!((position(id, 2, 255).node, position(id, 2, 255).character_index), (run_id(id, 2, 0).accesskit_id(), 255));
assert_eq!((position(id, 2, 256).node, position(id, 2, 256).character_index), (run_id(id, 2, 1).accesskit_id(), 1));
let mut r = Row::new(10, 14, Rect::ZERO);
for (i, ch) in "añb\n".chars().enumerate() {
r.push(ch, 10 + i as u64, i as f32 * 8.0, 8.0);
}
assert_eq!(r.lens, [1, 2, 1, 1]);
assert_eq!(locate(std::slice::from_ref(&r), 12), Some((0, 2)));
assert_eq!(locate(std::slice::from_ref(&r), 99), None);
}
}