use super::{NodeKind, Tree, focus_visible, tab_navigation};
use crate::{
node::Node,
time::{Duration, Instant},
};
pub(super) const TYPEAHEAD_WINDOW: Duration = Duration::from_millis(1000);
pub(super) struct Typeahead {
typed: String,
at: Instant,
}
impl Tree {
pub(super) fn focus_group_typeahead(&mut self, key: &str) -> bool {
let mut chars = key.chars();
let (Some(typed), None) = (chars.next(), chars.next()) else {
return false;
};
if typed.is_whitespace() || typed.is_control() {
return false;
}
let (Some(focused), Some(path)) = (self.focused_node, self.focused_path.clone()) else {
return false;
};
let Some(root) = &self.root else {
return false;
};
let mut group = None;
let mut node = root;
for &index in &path {
match node.focus_group_value() {
Some(held) if held.is_none() => group = None,
Some(held) if held.typeahead => group = Some(node),
_ => {}
}
let Some(child) = node.children().get(index) else {
return false;
};
node = child;
}
if matches!(
node.node_kind(),
NodeKind::TextInput { .. } | NodeKind::Slider { .. } | NodeKind::Select { .. }
) {
return false;
}
let Some(group) = group else {
return false;
};
let activate = group.focus_group_value().is_some_and(|held| held.activate);
let now = self.now();
let typed: String = typed.to_lowercase().collect();
let prefix = match self.typeahead.take() {
Some(Typeahead { typed: before, at }) if now.duration_since(at) <= TYPEAHEAD_WINDOW => before + &typed,
_ => typed,
};
self.typeahead = Some(Typeahead {
typed: prefix.clone(),
at: now,
});
if let Some(rows) = group.focus_branch().and_then(|branch| branch.rows.clone())
&& let Some(current) = node.focus_branch().and_then(|branch| branch.row)
{
let group = group.node_id();
let target = find(rows.count(), Some(current), &prefix, |row| {
rows.enabled(row).then(|| rows.label(row)).flatten()
});
if let Some(target) = target
&& target != current
{
self.focus_row(group, rows, target, activate.then(Default::default));
}
return true;
}
let (stops, nodes) = tab_navigation::group_stop_nodes(group, &self.dropped_nodes);
let current = stops.iter().position(|stop| stop.input_id == focused);
let target = find(stops.len(), current, &prefix, |index| label(nodes[index])).map(|index| stops[index]);
if let Some(target) = target
&& Some(target.input_id) != Some(focused)
{
self.set_input_modality(focus_visible::InputModality::Keyboard);
self.focus_node(target);
self.scroll_focused_into_view();
if activate {
self.activate_focused_button(Default::default());
}
}
true
}
}
fn find(count: usize, current: Option<usize>, prefix: &str, label: impl Fn(usize) -> Option<String>) -> Option<usize> {
let mut letters = prefix.chars();
let first = letters.next()?;
let repeated = letters.all(|letter| letter == first);
let (wanted, start) = match (repeated, current) {
(true, Some(current)) => (first.to_string(), current + 1),
(false, Some(current)) => (prefix.to_owned(), current),
(_, None) => (prefix.to_owned(), 0),
};
(0..count)
.map(|step| (start + step) % count)
.find(|&index| label(index).is_some_and(|label| label.to_lowercase().starts_with(&wanted)))
}
pub(super) fn label(node: &Node) -> Option<String> {
if let Some(label) = node.focus_label() {
return Some(label.to_owned());
}
node.button_kind_value()?;
let mut texts = Vec::new();
collect_texts(node, &mut texts);
match texts.as_slice() {
[text] => Some((*text).to_owned()),
_ => None,
}
}
fn collect_texts<'a>(node: &'a Node, texts: &mut Vec<&'a str>) {
if let Some(text) = node.text_content() {
texts.push(text);
}
for child in node.children() {
collect_texts(child, texts);
}
}