use super::{area::AreaState, doc::Env, text_input::Fingerprint, *};
use crate::{
accessibility::text::{TextModel, TextRef},
context::Context,
text::TextLayout,
AccessAction, AccessActionKind as Kind, AccessRole,
};
use std::sync::Arc;
const MAX_VALUE: usize = 256 * 1024;
pub(super) struct Lines {
pub text: TextRef,
pub origin: Vec2,
pub selection: (usize, usize),
}
pub(crate) type LineCache = Option<(Arc<TextLayout>, TextRef)>;
#[derive(Clone, Copy, PartialEq)]
pub(crate) struct AreaKey {
fingerprint: Fingerprint,
first: usize,
last: usize,
top: u32,
env: Env,
}
pub(super) fn requests(ctx: &mut Context, id: Id, events: &mut Vec<TextEditInput>) {
for action in ctx.take_access_actions(id) {
events.push(match action {
AccessAction::SetValue(text) => TextEditInput::SetValue(text),
AccessAction::ReplaceSelectedText(text) => TextEditInput::Commit(text),
AccessAction::SetTextSelection { anchor, focus } => {
TextEditInput::Select(anchor, focus)
}
_ => continue,
});
}
}
pub(super) fn line(cache: &mut LineCache, shown: &str, layout: &Arc<TextLayout>) -> TextRef {
if let Some((_, text)) = cache.as_ref().filter(|(old, _)| Arc::ptr_eq(old, layout)) {
return text.clone();
}
let mut model = TextModel::default();
model.push_lines(shown, 0, &layout.lines, Vec2::ZERO, "");
let text = TextRef::from(model);
*cache = Some((Arc::clone(layout), text.clone()));
text
}
impl TextEdit<'_> {
pub(super) fn area_lines(
&self,
ctx: &mut Context,
area: &mut AreaState,
(first, last): (usize, usize),
composing: bool,
) -> TextRef {
let doc = &mut area.doc;
let key = AreaKey {
fingerprint: Fingerprint::of(self.text),
first,
last,
top: doc.top(first).to_bits(),
env: doc.env(),
};
if let Some((_, text)) = area
.access
.as_ref()
.filter(|(old, _)| *old == key && !composing)
{
return text.clone();
}
let text: &str = self.text;
let mut model = TextModel::default();
let mut shift = 0;
for i in first..=last {
let layout = doc.layout(ctx, text, i);
let top = doc.top(i);
let para = &doc.paras[i];
let shown = doc.shown(text, i);
model.push_lines(
shown,
para.start + shift,
&layout.lines,
Vec2::new(0.0, top),
&text[para.end()..para.next_start()],
);
shift += shown.len().saturating_sub(para.len);
}
let text = TextRef::from(model);
area.access = (!composing).then(|| (key, text.clone()));
text
}
pub(super) fn describe(
&self,
ui: &mut Ui<'_>,
id: Id,
rect: Rect,
lines: Option<Lines>,
shown: Option<&str>,
) {
let multiline = self.area.is_some();
let role = if multiline {
AccessRole::MultilineTextInput
} else {
AccessRole::TextInput
};
let invalid = ui.field_status(self.status) == crate::SemanticStatus::Error;
ui.a11y(id, rect, role, |node| {
node.placeholder(self.placeholder.as_str())
.read_only(self.read_only)
.disabled(!self.enabled)
.invalid(invalid)
.action(Kind::SetTextSelection);
if !self.read_only {
node.action(Kind::SetValue)
.action(Kind::ReplaceSelectedText);
}
let value = shown.unwrap_or(self.text);
if !multiline || value.len() <= MAX_VALUE {
node.value(value);
}
if let Some(lines) = lines {
node.text(lines.text, Some(lines.selection))
.text_origin(lines.origin);
}
});
}
}