use crate::settings::{KNOBS, Kind, Knob, Knobbed, Settings};
use crate::ui::{Model, theme};
pub const PINNED: &str = "bar pinned open";
pub const PINNED_HELP: &str = "keeps the bar open whatever the pointer does — the pin at its head";
pub const SLIDER: f32 = 200.0;
pub const DEFAULTS: &str = "defaults";
pub const DEFAULTS_HELP: &str =
"puts every knob on the seat's three pages back to its default; the pin stays";
pub const STACK: f32 = 2.0 * SLIDER;
pub fn row(
ui: &mut egui::Ui,
label: &str,
help: &str,
control: impl FnOnce(&mut egui::Ui),
) -> egui::Response {
let words = |ui: &mut egui::Ui| {
ui.label(egui::RichText::new(label).color(theme::INK));
ui.add(egui::Label::new(egui::RichText::new(help).color(theme::INK_WEAK)).wrap());
};
let laid = if ui.available_width() >= STACK {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Min), |ui| {
control(ui);
ui.add_space(theme::space::S);
ui.vertical(words);
})
} else {
ui.vertical(|ui| {
words(ui);
control(ui);
})
};
ui.add_space(theme::space::M);
laid.response
}
pub fn page(ui: &mut egui::Ui, model: &mut Model, seat: crate::settings::Page) {
for knob in KNOBS.iter().filter(|knob| knob.page == seat) {
self::knob(ui, &mut model.settings, &mut model.seeking.jump, knob);
}
if seat == crate::settings::Page::Bar {
row(ui, PINNED, PINNED_HELP, |ui| {
theme::paint::toggle(ui, &mut model.bar.pinned, "", PINNED_HELP);
});
}
if theme::paint::link(ui, DEFAULTS, theme::INK_WEAK, DEFAULTS_HELP).clicked() {
model.settings = Settings::default();
}
}
enum Control {
On(bool),
At(f32, &'static str, (f32, f32, f32)),
}
pub fn knob(
ui: &mut egui::Ui,
settings: &mut Settings,
jump: &mut Option<&'static str>,
knob: &Knob,
) {
let control = match (knob.kind, (knob.get)(settings)) {
(Kind::Toggle, Knobbed::On(on)) => Control::On(on),
(
Kind::Number {
unit,
floor,
ceiling,
step,
},
Knobbed::At(at),
) => Control::At(at, unit, (floor, ceiling, step)),
_ => return,
};
let help = if knob.key == crate::settings::SCALE_KEY {
crate::ui::keys::cited(
knob.help,
&[
crate::ui::keys::Act::ScaleUp,
crate::ui::keys::Act::ScaleDown,
crate::ui::keys::Act::ScaleReset,
],
)
} else {
knob.help.to_owned()
};
let mut written = None;
let seat = row(ui, knob.label, &help, |ui| match control {
Control::On(mut on) => {
if theme::paint::toggle(ui, &mut on, "", &help).changed() {
written = Some(Knobbed::On(on));
}
}
Control::At(mut at, unit, bounds) => {
ui.allocate_ui_with_layout(
egui::vec2(SLIDER, theme::ROW),
egui::Layout::left_to_right(egui::Align::Center),
|ui| {
if theme::paint::slider(ui, &mut at, bounds, unit, &help).changed() {
written = Some(Knobbed::At(at));
}
},
);
}
});
if let Some(value) = written {
(knob.set)(settings, value);
}
if *jump == Some(knob.key) {
seat.scroll_to_me(Some(egui::Align::Min));
*jump = None;
}
}