use crate::settings::KNOBS;
use crate::ui::{Model, Page, Seeking, theme};
pub const HINT: &str = "search settings";
pub const HELP: &str = "finds a row by its label or its help, on every page";
pub const NOTHING: &str = "no row says that";
pub const OPEN: &str = "open";
const BOX: &str = "the settings' search box";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Hit {
pub label: &'static str,
pub help: &'static str,
pub page: Page,
pub key: Option<&'static str>,
}
pub fn render(ui: &mut egui::Ui, model: &mut Model) -> bool {
theme::paint::field(
ui,
egui::Id::new(BOX),
&mut model.seeking.text,
HINT,
None,
HELP,
);
ui.add_space(theme::space::XS);
let needle = model.seeking.text.trim().to_lowercase();
if needle.is_empty() {
return false;
}
let found = hits(&needle);
if found.is_empty() {
ui.colored_label(theme::INK_WEAK, NOTHING);
return true;
}
let mut picked = None;
for hit in found {
super::rows::row(ui, hit.label, hit.help, |ui| {
let door = theme::paint::link(ui, OPEN, theme::INK_WEAK, &super::tree::path(hit.page));
if door.clicked() {
picked = Some(hit.clone());
}
});
}
if let Some(hit) = picked {
model.seeking = Seeking {
text: String::new(),
jump: hit.key,
};
model.turn_to(hit.page);
}
true
}
pub fn hits(needle: &str) -> Vec<Hit> {
let says = |label: &str, help: &str| {
label.to_lowercase().contains(needle) || help.to_lowercase().contains(needle)
};
let pin = Hit {
label: super::rows::PINNED,
help: super::rows::PINNED_HELP,
page: Page::Seat(crate::settings::Page::Bar),
key: None,
};
KNOBS
.iter()
.map(|knob| Hit {
label: knob.label,
help: knob.help,
page: Page::Seat(knob.page),
key: Some(knob.key),
})
.chain([pin])
.chain(Page::ALL.into_iter().map(|page| Hit {
label: page.label(),
help: page.help(),
page,
key: None,
}))
.filter(|hit| says(hit.label, hit.help))
.collect()
}