use super::{Fold, Kind, Row, code, fold};
use crate::ui::theme::{self, Bubble, Speaker, State, glyph, space};
pub const STEP_HELP: &str = "one step of the working; opens it whole in place";
pub const SHUT_HELP: &str = "folds this step back to its one line";
pub(super) fn line(row: &Row, open: bool) -> theme::paint::Row {
let red = theme::accent(State::Error);
let weak = theme::INK_WEAK;
let mark = if is_call(row) {
if row.failed {
(glyph::FAILED, red)
} else {
(glyph::DONE, weak)
}
} else if row.weight == Speaker::Model && row.kind == Kind::Said {
(glyph::MODEL, weak)
} else {
(glyph::NOTE, weak)
};
let first = row.said.lines().next().unwrap_or_default();
let words = if open || is_call(row) {
row.who.clone()
} else {
format!("{} {} {first}", row.who, glyph::NOTE)
};
theme::paint::Row {
mark: (mark.0.to_owned(), mark.1),
words,
ink: if row.failed { red } else { weak },
state: row.failed.then_some(red),
chosen: false,
indent: 0.0,
aside: row.exit.map(badge),
beneath: Vec::new(),
tail: None,
door: None,
help: if open { SHUT_HELP } else { STEP_HELP }.to_owned(),
}
}
fn badge(code: i32) -> (String, Option<State>) {
let state = if code == 0 {
State::Attention
} else {
State::Error
};
(format!("exit {code}"), Some(state))
}
pub(super) fn is_call(row: &Row) -> bool {
matches!(row.kind, Kind::Call | Kind::Answered | Kind::Result)
}
pub(super) fn step(ui: &mut egui::Ui, at: usize, row: &Row, door: bool, bubbles: bool) -> bool {
let shut = if door {
line(row, true).paint(ui).clicked()
} else {
flying(ui, row);
false
};
if row.said.is_empty() {
return shut;
}
if matches!(row.kind, Kind::Answered | Kind::Result) {
inset(ui, (space::XS, space::XS), |ui| {
let role = bubbles.then_some(Bubble::Card);
theme::paint::nested(ui, role, space::S, |ui| output(ui, at, row));
});
} else {
inset(ui, (theme::RULE + space::M, space::S), |ui| {
match row.kind {
Kind::Said => code::paint(ui, at, &row.said, theme::INK),
_ => {
theme::paint::prose(ui, &row.said, theme::INK_WEAK);
}
}
});
}
shut
}
fn flying(ui: &mut egui::Ui, row: &Row) {
let (mark, ink) = match row.kind {
Kind::Call => (glyph::CALL, theme::BRAND),
Kind::Said if row.weight == Speaker::Model => (glyph::MODEL, theme::INK_WEAK),
_ => (glyph::NOTE, theme::INK_WEAK),
};
let who = match row.kind {
Kind::Said => theme::speaker(row.weight),
_ => theme::INK_WEAK,
};
ui.horizontal(|ui| {
ui.set_min_height(theme::ROW);
ui.add_space(theme::RULE + space::M);
ui.label(egui::RichText::new(mark).color(ink));
ui.label(egui::RichText::new(&row.who).color(who));
});
}
fn inset(ui: &mut egui::Ui, (left, right): (f32, f32), body: impl FnOnce(&mut egui::Ui)) {
ui.horizontal_top(|ui| {
ui.add_space(left);
ui.vertical(|ui| {
ui.set_width(ui.available_width() - right);
body(ui);
});
});
ui.add_space(space::XS);
}
fn output(ui: &mut egui::Ui, at: usize, row: &Row) {
match &row.fold {
Some(fold) => folded(ui, at, row, fold),
None => mono(ui, &row.said, theme::INK_WEAK),
}
}
fn mono(ui: &mut egui::Ui, words: &str, ink: egui::Color32) {
ui.add(egui::Label::new(egui::RichText::new(words).monospace().color(ink)).wrap());
}
fn folded(ui: &mut egui::Ui, at: usize, row: &Row, fold: &Fold) {
let id = ui.make_persistent_id(at);
let mut open = ui.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
let help = if open {
fold::FOLD_HELP
} else {
fold::UNFOLD_HELP
};
if theme::paint::button(
ui,
if open { fold::UNFOLD } else { &fold.whole },
true,
help,
)
.clicked()
{
open = !open;
ui.data_mut(|d| d.insert_temp(id, open));
}
if open {
mono(ui, &row.said, theme::INK);
} else {
mono(ui, &fold.head, theme::INK_WEAK);
}
}