use super::{HEADING, choices, confirm, down, moved, pick, row};
use crate::ui::Model;
const DIGITS: [egui::Key; 9] = [
egui::Key::Num1,
egui::Key::Num2,
egui::Key::Num3,
egui::Key::Num4,
egui::Key::Num5,
egui::Key::Num6,
egui::Key::Num7,
egui::Key::Num8,
egui::Key::Num9,
];
pub fn keys(ctx: &egui::Context, model: &mut Model) {
let field = egui::Id::new(crate::ui::keys::BOX_ID);
let here = ctx
.memory(egui::Memory::focused)
.is_none_or(|id| id == field);
if !here || !model.draft.is_empty() || row(model).is_none() || down(model).is_some() {
return;
}
let took = |key| ctx.input_mut(|i| i.consume_key(egui::Modifiers::NONE, key));
for (key, step) in [(egui::Key::ArrowUp, -1), (egui::Key::ArrowDown, 1)] {
if took(key) {
moved(ctx, model, step);
}
}
if took(egui::Key::Enter) {
confirm(ctx, model);
}
for (n, key) in (1..).zip(DIGITS) {
if took(key) {
let typed = n.to_string();
ctx.input_mut(|i| {
i.events
.retain(|event| !matches!(event, egui::Event::Text(t) if *t == typed));
});
pick(model, n);
}
}
}
pub fn hint(model: &Model) -> String {
if let Some(why) = down(model) {
return why;
}
let digits = match row(model).map(|asked| choices(&asked.1).len()) {
Some(1) => "1 answers, or ".to_owned(),
Some(n) => format!("1–{n} answers, or "),
None => String::new(),
};
format!("{HEADING} — {digits}{}", crate::ui::composer::HINT)
}