Medlar/crates/app/src/views/control.rs

106 lines
4.4 KiB
Rust

//! The sheet over the list for speed, queues, the week and "when done", and what runs behind it.
use super::main::Reply;
use super::modes::{self, Change, MODES};
use super::queues;
use super::schedule::{self, Awake, Done, Level};
use crate::bridge::{Bridge, Lists};
use crate::theme::Theme;
use crate::ui::dialog;
use crate::ui::toast::Toasts;
use crate::ui::widgets::{Tab, tabs};
use eframe::egui::{Context, Ui};
use medlar_core::model::TaskId;
use medlar_core::settings::Settings;
use medlar_core::snapshot::Snapshot;
use medlar_core::tr;
use std::time::Duration;
#[derive(Default)]
pub struct Control {
pub open: bool,
pub tab: usize,
mb: bool,
queues: queues::Page,
brush: u8,
cells: Vec<u8>,
pub done: Done,
awake: Awake,
/// The hour of the week the limits were last set for.
cell: Option<usize>,
}
impl Control {
/// What runs without a screen: the plan of modes, the countdown, the lock against sleep.
pub fn tick(&mut self, ctx: &Context, t: &Theme, settings: &Settings, bridge: &mut Bridge, shot: &Snapshot, toasts: &mut Toasts<Reply>) {
use chrono::{Datelike, Timelike};
let local = chrono::Local::now().naive_local();
let cell = local.weekday().num_days_from_monday() as usize * 24 + local.hour() as usize;
if settings.mode_grid.is_empty() {
self.cell = None;
} else {
if self.cell.is_some_and(|c| c != cell) {
bridge.apply(settings);
}
self.cell = Some(cell);
ctx.request_repaint_after(Duration::from_secs(30));
}
self.awake.follow(shot.totals.running);
self.done.tick(ctx, t, shot, toasts);
}
#[allow(clippy::too_many_arguments)]
pub fn show(&mut self, ctx: &Context, t: &Theme, settings: &Settings, bridge: &Bridge, shot: &Snapshot, lists: &Lists, selected: &[TaskId], change: &mut Option<Change>) {
if !self.open && !dialog::shown(ctx, "control") {
return;
}
let mut open = self.open;
let closed = dialog::sheet(ctx, t, "control", &tr!("control.title"), open, |ui| {
let (a, b, c, d) = (tr!("control.speed"), tr!("control.queues"), tr!("control.week"), tr!("control.done"));
let options = [
Tab { icon: "", text: &a, count: None },
Tab { icon: "", text: &b, count: Some(lists.queues.len()) },
Tab { icon: "", text: &c, count: None },
Tab { icon: "", text: &d, count: self.done.watcher.armed().map(|_| 1) },
];
tabs(ui, t, &tr!("control.title"), &options, &mut self.tab);
ui.add_space(crate::tokens::space::L);
match self.tab {
0 => self.speed(ui, t, settings, change),
1 => self.queues.show(ui, t, lists, shot, bridge, selected),
2 => self.week(ui, t, settings, change),
_ => {
let names: Vec<_> = lists.queues.iter().map(|q| (q.id, super::list::model::queue_name(lists, q.id))).collect();
self.done.picker(ui, t, &names);
}
}
});
if closed {
open = false;
}
self.open = open;
}
fn speed(&mut self, ui: &mut Ui, t: &Theme, settings: &Settings, change: &mut Option<Change>) {
let mode = settings.mode;
let speed = settings.limits_of_ref(mode).speed;
let before = change.clone();
modes::dial(ui, t, mode, speed, &mut self.mb, change);
let _ = before;
}
fn week(&mut self, ui: &mut Ui, t: &Theme, settings: &Settings, change: &mut Option<Change>) {
use eframe::egui::RichText;
ui.label(RichText::new(tr!("week.help")).font(crate::fonts::ui(crate::tokens::text::LABEL)).color(t.on_variant));
ui.add_space(crate::tokens::space::S);
if self.cells.len() != 168 {
self.cells = if settings.mode_grid.len() == 168 { settings.mode_grid.clone() } else { vec![0; 168] };
}
let fills = [t.high, t.tertiary_container, t.secondary_container, t.primary_container, t.error_container];
let mut levels = vec![Level { name: tr!("week.chosen"), fill: fills[0] }];
levels.extend(MODES.iter().enumerate().map(|(i, m)| Level { name: modes::label(*m), fill: fills[i + 1] }));
if schedule::week(ui, t, "modes", &mut self.cells, &levels, &mut self.brush) {
*change = Some(Change::Grid(self.cells.clone()));
}
}
}