use crate::ui::{Model, theme};
pub const HINT: &str = "ESC";
pub const OUT_HELP: &str = "closes this — the same as pressing Esc";
pub const OUT: &str = "the overlay's way out was clicked";
pub const BOX: &str = "the overlay's box";
pub const FIRST: &str = "the overlay's first control takes the keyboard";
pub fn ceiling(ui: &egui::Ui) -> f32 {
(ui.max_rect().height() - theme::space::XS).max(0.0)
}
pub fn float(ui: &mut egui::Ui, name: &'static str, height: f32, body: impl FnOnce(&mut egui::Ui)) {
let room = ui.max_rect();
let span = egui::Rangef::new(
room.left() + theme::space::L,
room.right() - theme::space::L,
);
float_at(ui, name, span, height, body);
}
pub fn float_at(
ui: &mut egui::Ui,
name: &'static str,
span: egui::Rangef,
height: f32,
body: impl FnOnce(&mut egui::Ui),
) -> egui::Rect {
let room = ui.max_rect();
let height = height.min(ceiling(ui));
let rect = egui::Rect::from_min_max(
egui::pos2(span.min, room.bottom() - height),
egui::pos2(span.max, room.bottom()),
);
let mut child = ui.new_child(
egui::UiBuilder::new()
.max_rect(rect)
.layout(egui::Layout::top_down(egui::Align::Min))
.id_salt(name),
);
egui::Frame::none()
.fill(theme::SURFACE)
.stroke(egui::Stroke::new(1.0, theme::HAIRLINE))
.rounding(theme::RADIUS)
.inner_margin(egui::Margin::symmetric(theme::space::S, theme::space::XS))
.show(&mut child, |ui| {
let inner = rect.size() - egui::vec2(2.0 * theme::space::S, 2.0 * theme::space::XS);
ui.set_min_size(inner);
ui.set_max_height(inner.y);
ui.data_mut(|d| d.insert_temp(egui::Id::new(BOX), ui.max_rect()));
egui::ScrollArea::vertical()
.id_salt(name)
.auto_shrink(false)
.show(ui, body);
});
rect
}
pub fn name(ui: &mut egui::Ui, word: &str) {
about(ui, word, None);
}
pub fn way_out(ui: &mut egui::Ui) {
let first = egui::Id::new(FIRST);
let held = ui.ctx().data_mut(|d| d.remove_temp::<bool>(first));
if theme::paint::pointer_link(ui, HINT, theme::INK_WEAK, OUT_HELP).clicked() {
ui.ctx()
.data_mut(|d| d.insert_temp(egui::Id::new(OUT), true));
ui.ctx().request_repaint();
}
if let Some(held) = held {
ui.ctx().data_mut(|d| d.insert_temp(first, held));
}
}
pub fn about(ui: &mut egui::Ui, word: &str, subject: Option<&str>) {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
way_out(ui);
ui.allocate_ui_with_layout(
egui::vec2(ui.available_width(), theme::ROW),
egui::Layout::left_to_right(egui::Align::Center),
|ui| {
ui.label(egui::RichText::new(word).color(theme::INK));
if let Some(subject) = subject {
let subject = egui::RichText::new(subject).color(theme::INK_WEAK);
ui.add(egui::Label::new(subject).truncate());
}
},
);
});
ui.add_space(theme::space::XS);
}
pub fn stand(ui: &mut egui::Ui, model: &mut Model) {
if !model.covered() {
return;
}
let first = egui::Id::new(FIRST);
let wants = !model.switching && ui.ctx().memory(egui::Memory::focused).is_none();
ui.ctx().data_mut(|d| d.insert_temp(first, wants));
float(ui, "the overlay", ceiling(ui), |ui| {
use crate::ui::{
board, clear, commands, find, fleet, prices, queue, records, settings, switcher, trail,
unmake,
};
let floating = !model.docked();
let _ = switcher::render(ui, model)
|| settings::render(ui, model)
|| (floating && records::render(ui, model))
|| queue::render(ui, model)
|| (floating && (trail::render(ui, model) || clear::render(ui, model)))
|| board::render(ui, model)
|| fleet::render(ui, model)
|| commands::render(ui, model)
|| find::render(ui, model)
|| prices::render(ui, model)
|| unmake::render(ui, model);
});
ui.ctx().data_mut(|d| d.remove_temp::<bool>(first));
}
#[cfg(test)]
mod tests;