use crate::ui::slash::Row;
use crate::ui::{Model, Selector, theme};
pub mod diagnostics;
pub mod dropup;
mod fit;
pub mod seat;
pub mod selectors;
pub mod waiting;
pub use diagnostics::{READY, state};
pub use fit::{elide, fits, fitting, width};
pub use selectors::NO_MODEL;
pub const LEAD: &str = theme::glyph::LEAD;
pub const SEP: &str = theme::glyph::SEP;
pub const SERVERS: &str = "servers";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Door {
Row(Row),
Drop(Selector),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Run {
pub text: String,
pub ink: egui::Color32,
pub door: Option<Door>,
pub mark: Option<(&'static str, egui::Color32)>,
}
pub fn run(text: impl Into<String>, ink: egui::Color32) -> Run {
Run {
text: text.into(),
ink,
door: None,
mark: None,
}
}
pub fn door(model: &Model, word: &str) -> Option<Row> {
crate::ui::slash::rows(model)
.into_iter()
.find(|row| row.word == word)
}
pub fn selected(model: &Model) -> Option<crate::reply::convs::ConvRow> {
let id = model.conversation.as_ref()?;
model.rows().into_iter().find(|row| row.root_id == *id)
}
pub fn name(model: &Model) -> Run {
let text = match (selected(model), &model.aim) {
(Some(row), _) => row.display,
(None, Some(aim)) => aim.address.clone(),
(None, None) => SERVERS.to_owned(),
};
Run {
door: door(model, crate::ui::switcher::WORD).map(Door::Row),
..run(text, theme::INK)
}
}
pub fn runs(model: &Model) -> (Vec<Run>, usize) {
let (mut out, kept) = selectors::selectors(model);
out.extend(diagnostics::diagnostics(model));
(out, kept)
}
fn help(door: Door) -> String {
match door {
Door::Row(row) => row.summary.to_owned(),
Door::Drop(selector) => crate::ui::keys::cited(
selectors::help(selector),
match selector {
crate::ui::Selector::Model => &[crate::ui::keys::Act::Model],
_ => &[],
},
),
}
}
pub(crate) fn paint(ui: &mut egui::Ui, run: &Run, words: &str) -> (bool, egui::Rect) {
let Some(door) = run.door else {
let seat = ui.label(egui::RichText::new(words).small().color(run.ink));
return (false, seat.rect);
};
let seat = match run.mark {
Some(mark) => theme::paint::link_marked(ui, words, run.ink, mark, &help(door)),
None => theme::paint::link(ui, words, run.ink, &help(door)),
};
crate::ui::act::tag(&seat, selectors::ops(door));
(seat.clicked(), seat.rect)
}
pub fn render(ui: &mut egui::Ui, model: &mut Model) {
let (mut runs, kept) = runs(model);
let name = name(model);
let room = ui.available_width();
let named = elide(ui, &name.text, room / 4.0);
let gap = ui.spacing().item_spacing.x;
let reserve = width(ui, &named) + seat::width(ui, model) + gap;
let dial = seat::dial(model);
let beside = dial
.as_ref()
.map(|said| width(ui, said) + gap)
.filter(|w| fits(ui, &runs, kept, reserve + w, room));
let faint = |ui: &mut egui::Ui, text: &str| {
ui.label(egui::RichText::new(text).small().color(theme::INK_FAINT));
};
let wrapped = |ui: &mut egui::Ui, said: &str, ink| {
let words = egui::RichText::new(said).small().color(ink);
ui.add(egui::Label::new(words).wrap());
};
if let (Some(said), None) = (&dial, beside) {
wrapped(ui, said, theme::accent(theme::State::Error));
}
let reserve = reserve + beside.unwrap_or(0.0);
let noticed = model.notice.as_ref().map(|_| state(model));
if let Some(said) = noticed
&& fitting(ui, &runs, kept, reserve, room).0 < runs.len()
{
wrapped(ui, &said.text, said.ink);
if let Some(slot) = runs.iter_mut().find(|run| **run == said) {
*slot = diagnostics::calm(model);
}
}
let (shown, cut) = fitting(ui, &runs, kept, reserve, room);
let mut fired = None;
let mut asked = false;
ui.horizontal(|ui| {
faint(ui, LEAD);
for (at, run) in runs.iter().take(shown).enumerate() {
if at > 0 {
faint(ui, SEP);
}
let words = if at < kept {
elide(ui, &run.text, cut)
} else {
run.text.clone()
};
let (clicked, rect) = paint(ui, run, &words);
if let Some(Door::Drop(selector)) = run.door {
dropup::anchor(ui, model, selector, rect);
}
fired = clicked.then_some(run.door).flatten().or(fired);
}
faint(ui, LEAD);
fired = paint(ui, &name, &named)
.0
.then_some(name.door)
.flatten()
.or(fired);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
asked = seat::render(ui, model, beside.and(dial.as_deref()));
});
});
if asked {
crate::ui::slash::fire(model, crate::ui::queue::WORD, "");
}
match fired {
Some(Door::Row(row)) => {
crate::ui::slash::fire(model, row.word, "");
}
Some(Door::Drop(selector)) => dropup::toggle(ui, model, selector),
None => {}
}
}
#[cfg(test)]
mod tests;