use crate::ui::model::Engine;
use crate::ui::{Aim, Model, Stand, theme};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Stand(Stand),
Wall(Aim),
Conversation(String),
}
impl Step {
pub fn holding(&self, model: &Model) -> bool {
match self {
Self::Stand(stand) => model.standing.as_ref() == Some(stand),
Self::Wall(aim) => {
model.standing.is_none()
&& model.conversation.is_none()
&& model.aim.as_ref() == Some(aim)
}
Self::Conversation(root_id) => {
model.standing.is_none() && model.conversation.as_deref() == Some(root_id.as_str())
}
}
}
}
pub fn track(model: &Model) -> Vec<Step> {
let open = model.engine_open();
let mut rows = Vec::new();
for engine in model.engine_rows() {
let name = engine.name().to_owned();
rows.push(Step::Stand(Stand::Engine(name.clone())));
if open.as_deref() != Some(name.as_str()) {
continue;
}
for wall in super::ordered(&engine.walls()) {
let Some(aim) = engine.aim(&wall) else {
continue;
};
let aimed = model.aim.as_ref() == Some(&aim);
rows.push(Step::Wall(aim.clone()));
if aimed {
rows.extend(
model
.rows()
.into_iter()
.map(|row| Step::Conversation(row.root_id)),
);
}
rows.push(Step::Stand(Stand::Begin(aim)));
}
if let Some(channel) = founds(&engine) {
rows.push(Step::Stand(Stand::Found(channel.to_owned())));
}
}
rows
}
pub fn render(ui: &mut egui::Ui, model: &mut Model, engine: &Engine, open: bool, reveal: bool) {
let name = engine.name().to_owned();
let standing = model.standing == Some(Stand::Engine(name.clone()));
let seat = theme::paint::row(
ui,
&engine.header(),
theme::INK,
standing.then_some(theme::BRAND),
open,
0.0,
);
if standing && reveal {
seat.scroll_to_me(None);
}
if seat.gained_focus() {
model.standing = Some(Stand::Engine(name.clone()));
}
if seat.clicked() {
model.open_engine(&name);
}
}
pub const FOUND: &str = "+ new workspace";
pub fn found(ui: &mut egui::Ui, model: &mut Model, engine: &Engine, reveal: bool) {
let Some(channel) = founds(engine) else {
return;
};
if model.covered() && !model.switching {
return;
}
let stand = Stand::Found(channel.to_owned());
if super::wall::standing_row(ui, model, FOUND, stand, 0.0, reveal) {
model.begin_founding(channel);
model.chose();
}
}
fn founds(engine: &Engine) -> Option<&str> {
let chunk = &engine.lead;
(chunk.channel.name == crate::seat::OWN && !matches!(chunk.held, crate::ui::Held::Unheld(_)))
.then_some(chunk.channel.name.as_str())
}