use crate::ui::status::{self, Door, Run};
use crate::ui::{Model, Selector, theme};
pub struct Send {
pub live: bool,
pub help: &'static str,
pub ops: &'static [&'static str],
}
pub struct Trail {
pills: Vec<Run>,
cap: f32,
send: Option<Send>,
}
#[derive(Default)]
pub struct Clicks {
picked: Option<Selector>,
seats: Vec<(Selector, egui::Rect)>,
pub sent: bool,
}
const AIR: egui::Vec2 = egui::vec2(theme::space::S, theme::space::XXS);
impl Trail {
pub fn new(ui: &egui::Ui, model: &Model, pilled: bool, send: Option<Send>) -> Self {
let pills = if pilled {
status::selectors::pills(model)
} else {
Vec::new()
};
Trail {
pills,
cap: (ui.available_width() - 2.0 * theme::space::S) / 4.0,
send,
}
}
fn words(&self, ui: &egui::Ui, run: &Run) -> String {
status::elide(ui, &run.text, self.cap)
}
fn pill(&self, ui: &egui::Ui, run: &Run) -> f32 {
let mark = run
.mark
.map_or(String::new(), |(glyph, _)| format!(" {glyph}"));
status::width(ui, &format!("{}{mark}", self.words(ui, run))) + 2.0 * AIR.x
}
pub fn width(&self, ui: &egui::Ui) -> f32 {
let gap = ui.spacing().item_spacing.x;
let pills: f32 = self.pills.iter().map(|run| self.pill(ui, run) + gap).sum();
pills + self.send.as_ref().map_or(0.0, |_| theme::ROW + gap)
}
pub fn paint(&self, ui: &mut egui::Ui, clicks: &mut Clicks) {
let wrap = |ui: &mut egui::Ui, wide: f32| {
if wide > ui.available_size_before_wrap().x {
ui.end_row();
}
};
for run in &self.pills {
wrap(ui, self.pill(ui, run));
let words = self.words(ui, run);
let stood = egui::Frame::none()
.stroke(egui::Stroke::new(1.0, theme::HAIRLINE))
.rounding(theme::ROW / 2.0)
.inner_margin(egui::Margin::symmetric(AIR.x, AIR.y))
.show(ui, |ui| status::paint(ui, run, &words))
.inner;
if let Some(Door::Drop(selector)) = run.door {
clicks.seats.push((selector, stood.1));
clicks.picked = stood.0.then_some(selector).or(clicks.picked);
}
}
if let Some(send) = &self.send {
wrap(ui, theme::ROW);
let seat = theme::paint::send(ui, send.live, send.help);
crate::ui::act::tag(&seat, send.ops);
clicks.sent = seat.clicked();
}
}
}
pub fn spend(ui: &egui::Ui, model: &mut Model, clicks: Clicks) -> bool {
for (selector, rect) in clicks.seats {
status::dropup::anchor(ui, model, selector, rect);
}
if let Some(selector) = clicks.picked {
status::dropup::toggle(ui, model, selector);
}
clicks.sent
}