use std::time::Duration;
use glam::Vec2;
use super::popup::OpenPopup;
use super::store::NodeKey;
use super::{Jump, Ui};
use crate::edit::Edit;
pub const TYPEAHEAD_TIMEOUT: Duration = Duration::from_secs(1);
#[derive(Clone, Debug, Default)]
pub(super) struct Typeahead {
list: Option<NodeKey>,
prefix: String,
typed_at: Duration,
pub built: Option<NodeKey>,
}
pub(super) struct ListItem<'a> {
pub key: NodeKey,
pub label: &'a str,
pub enabled: bool,
}
impl Ui {
#[must_use]
pub fn popup_list_open(&self) -> bool {
self.engaged().is_none()
&& self
.popups
.last()
.is_some_and(|open| self.typeahead.built == Some(open.anchor))
}
pub(super) fn jump_in_popup(&mut self, jump: Jump) {
let Some(&OpenPopup { anchor, .. }) = self.popups.last() else {
return;
};
let root = Self::popup_key(anchor);
let order = self.focusable_in(Some(root));
let at = self
.focused()
.and_then(|focused| order.iter().position(|&key| key == focused));
let rects: Vec<(Vec2, Vec2)> = order
.iter()
.map(|&key| {
self.store
.by_key(key)
.map_or((Vec2::ZERO, Vec2::ZERO), |node| node.rect)
})
.collect();
let (view_top, view_bottom) = self.store.by_key(root).map_or((0.0, 0.0), |node| {
let (min, max) = node.content_box();
(min.y, max.y)
});
let page = view_bottom - view_top;
let target = match (jump, at) {
(Jump::First, _) | (Jump::PageDown, None) => (!order.is_empty()).then_some(0),
(Jump::Last, _) | (Jump::PageUp, None) => order.len().checked_sub(1),
(Jump::PageDown, Some(at)) => {
let last_in_view = (at..order.len())
.take_while(|&index| rects[index].1.y <= view_bottom)
.last()
.filter(|&last| last > at);
last_in_view.or_else(|| {
let limit = rects[at].1.y + page;
(at + 1..order.len())
.take_while(|&index| rects[index].1.y <= limit)
.last()
.or_else(|| (at + 1 < order.len()).then_some(at + 1))
})
}
(Jump::PageUp, Some(at)) => {
let first_in_view = (0..=at)
.rev()
.take_while(|&index| rects[index].0.y >= view_top)
.last()
.filter(|&first| first < at);
first_in_view.or_else(|| {
let limit = rects[at].0.y - page;
(0..at)
.rev()
.take_while(|&index| rects[index].0.y >= limit)
.last()
.or_else(|| at.checked_sub(1))
})
}
};
if let Some(index) = target {
self.move_focus(order[index]);
}
}
pub(super) fn typeahead(&mut self, anchor: NodeKey, items: &[ListItem<'_>]) {
if self.popups.last().is_none_or(|open| open.anchor != anchor) {
return;
}
self.typeahead.built = Some(anchor);
if self.engaged().is_some() {
return;
}
let typed: Vec<char> = self
.text_frame
.edits
.iter()
.filter_map(|edit| match edit {
Edit::Insert(text) => Some(text.chars()),
_ => None,
})
.flatten()
.filter(|character| !character.is_whitespace() && !character.is_control())
.collect();
if typed.is_empty() {
return;
}
let state = &mut self.typeahead;
if state.list != Some(anchor)
|| self.text_clock.saturating_sub(state.typed_at) > TYPEAHEAD_TIMEOUT
{
state.prefix.clear();
}
state.list = Some(anchor);
state.typed_at = self.text_clock;
let focused = self.focused();
let mut at = items.iter().position(|item| Some(item.key) == focused);
let mut moved = None;
for character in typed {
self.typeahead.prefix.push(character);
let prefix = self.typeahead.prefix.to_lowercase();
let mut letters = prefix.chars();
let first = letters.next();
let (needle, start) = if letters.all(|letter| Some(letter) == first) {
let needle: String = first.into_iter().collect();
(needle, at.map_or(0, |at| at + 1))
} else {
(prefix, at.unwrap_or(0))
};
let found = (0..items.len())
.map(|offset| (start + offset) % items.len())
.find(|&index| {
items[index].enabled && items[index].label.to_lowercase().starts_with(&needle)
});
if let Some(index) = found {
at = Some(index);
moved = Some(items[index].key);
}
}
if let Some(key) = moved
&& Some(key) != focused
{
self.set_focus(key);
}
}
}