use crate::reply::convs::ConvRow;
use crate::ui::{Model, convs, theme};
pub const EMPTY: &str = "no conversations here yet — + new begins one";
pub const NOTHING_AIMED: &str = "pick a workspace below";
pub const KEPT: &str = "PINNED";
pub const KEPT_HINT: &str = "shift-click a conversation to pin";
pub const NOTHING_MATCHES: &str = "nothing matches";
pub const SHIFT_KEEPS: &str = "; Shift+click keeps it at the top of this bar";
#[derive(Default)]
struct Asked {
picked: Option<String>,
kept: Option<String>,
trashed: Option<(String, bool)>,
}
pub(super) fn render(ui: &mut egui::Ui, model: &mut Model, open: bool) {
let rows = model.rows();
if rows.is_empty() {
if open {
let said = match &model.aim {
None => NOTHING_AIMED,
Some(aim) if model.answered.as_ref() == Some(aim) => EMPTY,
Some(_) => convs::NOT_ANSWERED,
};
theme::paint::empty(ui, said);
}
return;
}
let kept = model.kept();
let sift = model.bar.sift.clone();
let (top, rest): (Vec<&ConvRow>, Vec<&ConvRow>) = rows
.iter()
.filter(|row| !open || super::verbs::sifted(row, &sift))
.partition(|row| open && kept.contains(&row.root_id));
let now = convs::epoch::now();
let mut asked = Asked::default();
egui::ScrollArea::vertical()
.id_salt("bar")
.auto_shrink(false)
.show(ui, |ui| {
if open {
heading(ui, KEPT, theme::BRAND);
if kept.is_empty() {
inset(ui, KEPT_HINT, theme::INK_WEAK);
}
for row in &top {
listed(ui, model, row, open, &mut asked);
}
ui.add_space(theme::space::S);
if rest.is_empty() && !sift.trim().is_empty() {
theme::paint::empty(ui, NOTHING_MATCHES);
}
}
let mut last = None;
for row in &rest {
if open && row.depth == 0 {
let epoch = convs::epoch::epoch(now, row.age_secs);
if last.as_ref() != Some(&epoch) {
parting(ui, &epoch);
last = Some(epoch);
}
}
listed(ui, model, row, open, &mut asked);
}
});
if let Some(id) = asked.picked {
model.select(&id);
}
if let Some(id) = asked.kept {
model.toggle_kept(&id);
}
if let Some((id, at_once)) = asked.trashed {
if at_once {
model.post_delete(&id);
} else {
model.begin_deleting(&id);
}
}
}
fn heading(ui: &mut egui::Ui, word: &str, ink: egui::Color32) {
inset(ui, &word.to_uppercase(), ink);
}
fn inset(ui: &mut egui::Ui, words: &str, ink: egui::Color32) {
let left = theme::RULE + theme::space::M;
let font = egui::TextStyle::Small.resolve(ui.style());
let galley = ui.painter().layout(
words.to_owned(),
font,
ink,
(ui.available_width() - left).max(0.0),
);
let tall = galley.size().y;
ui.painter()
.galley(ui.cursor().min + egui::vec2(left, 0.0), galley, ink);
ui.add_space(tall + ui.spacing().item_spacing.y);
}
fn parting(ui: &mut egui::Ui, epoch: &str) {
ui.add_space(theme::space::XS);
heading(ui, epoch, theme::INK_WEAK);
let at = ui.cursor().min;
ui.painter().hline(
at.x..=at.x + ui.available_width(),
at.y,
egui::Stroke::new(1.0, theme::HAIRLINE),
);
ui.add_space(1.0 + ui.spacing().item_spacing.y);
}
fn listed(ui: &mut egui::Ui, model: &mut Model, row: &ConvRow, open: bool, asked: &mut Asked) {
let (seat, trash) = one(ui, model, row, open);
if seat.clicked() {
if ui.input(|i| i.modifiers.shift) {
asked.kept = Some(row.root_id.clone());
} else {
asked.picked = Some(row.root_id.clone());
}
}
if trash.is_some_and(|door| door.clicked()) {
let at_once = ui.input(|i| i.modifiers.command);
asked.trashed = Some((row.root_id.clone(), at_once));
}
if let Some(aim) = model.aim.clone() {
convs::menu::show(&seat, model, &aim, row);
}
}
fn one(
ui: &mut egui::Ui,
model: &Model,
row: &ConvRow,
open: bool,
) -> (egui::Response, Option<egui::Response>) {
let selected = model.conversation.as_ref() == Some(&row.root_id);
let ink = if selected { theme::BRAND } else { theme::INK };
let (glyph, glyph_ink) = convs::mark(row);
let age = convs::age(row.age_secs);
let preview = row.preview.lines().next().unwrap_or_default();
let beneath = if open {
vec![(preview.to_owned(), theme::INK_WEAK, egui::TextStyle::Small)]
} else {
Vec::new()
};
let name = open
.then(|| row.name.clone().filter(|name| *name != row.display))
.flatten();
let (seat, trash) = theme::paint::Row {
mark: (glyph, glyph_ink),
words: row.display.clone(),
ink,
state: convs::rule(row),
chosen: selected,
indent: if open {
convs::indent(row.depth) - convs::STEP
} else {
0.0
},
aside: open.then_some((age, None)),
beneath,
tail: name,
door: open.then_some((theme::glyph::TRASH, convs::TRASH_HELP)),
help: convs::help(row) + if open { SHIFT_KEEPS } else { "" },
}
.paint_doored(ui);
crate::ui::act::tag(
&seat,
&[crate::verbs::TRANSCRIPT.word, crate::verbs::FOLLOW.word],
);
if let Some(trash) = &trash {
crate::ui::act::tag(trash, &[crate::verbs::DELETE_AGENT.word]);
}
(seat, trash)
}