better height calc, fix builtin help

This commit is contained in:
Joe Ardent 2022-10-20 15:06:27 -07:00
parent ad7b0c9d4d
commit f9590fe811
3 changed files with 33 additions and 46 deletions

View file

@ -12,5 +12,5 @@ eframe = { version = "0.19", features = ["wgpu"] }
egui = { version = "0.19", features = ["bytemuck"] } egui = { version = "0.19", features = ["bytemuck"] }
egui_extras = "0.19" egui_extras = "0.19"
naga = { version = "0.10", features = ["spv-out", "wgsl-out", "wgsl-in"] } naga = { version = "0.10", features = ["spv-out", "wgsl-out", "wgsl-in"] }
rodio = "0.16" rodio = { version = "0.16" }
wgpu = { version = "0.14", features = ["naga", "spirv"] } wgpu = { version = "0.14", features = ["naga", "spirv"] }

View file

@ -1,19 +1,19 @@
use clap::Parser; use clap::Parser;
#[derive(Debug, Parser)] #[derive(Debug, Parser)]
#[clap(author, version, about)] #[clap(author, version, about, disable_help_flag = true)]
pub struct Cli { pub struct Cli {
/// Hours to count down. /// Hours to count down.
#[clap(long, default_value_t = 0)] #[clap(long, short)]
pub hours: u64, pub hours: Option<u64>,
/// Minutes to count down. /// Minutes to count down.
#[clap(long, short, default_value_t = 0)] #[clap(long, short)]
pub minutes: u64, pub minutes: Option<u64>,
/// Seconds to count down. /// Seconds to count down.
#[clap(long, short, default_value_t = 0)] #[clap(long, short)]
pub seconds: u64, pub seconds: Option<u64>,
/// Audio file to play at the end of the countdown. /// Audio file to play at the end of the countdown.
#[clap(long, short)] #[clap(long, short)]
@ -30,4 +30,8 @@ pub struct Cli {
/// Use the Predator font. /// Use the Predator font.
#[clap(long, short)] #[clap(long, short)]
pub predator: bool, pub predator: bool,
/// Print this help.
#[clap(long, short = 'H', action = clap::ArgAction::Help)]
pub help: Option<bool>,
} }

View file

@ -7,7 +7,7 @@ use egui_extras::{Size, StripBuilder};
use crate::{cli::Cli, util::*}; use crate::{cli::Cli, util::*};
const MIN_REPAINT: Duration = Duration::from_millis(183); // one frame before 200ms const MIN_REPAINT: Duration = Duration::from_millis(200); // one frame before 200ms
const MAX_REPAINT: Duration = Duration::from_millis(500); const MAX_REPAINT: Duration = Duration::from_millis(500);
const DIGIT_FACTOR: f32 = 0.4; const DIGIT_FACTOR: f32 = 0.4;
@ -26,7 +26,6 @@ pub struct Timer {
state: TimerState, state: TimerState,
tstart: Instant, // so we can blink tstart: Instant, // so we can blink
alarm: Option<String>, alarm: Option<String>,
height: Option<f32>,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -60,20 +59,18 @@ struct ChronoState {
impl App for Timer { impl App for Timer {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
ctx.request_repaint_after(MAX_REPAINT); ctx.request_repaint_after(MAX_REPAINT);
let height = ctx.input().screen_rect().height();
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
let vsize = self.height.unwrap_or(500.0);
match self.state { match self.state {
TimerState::Unstarted => self.unstarted(ui, vsize), TimerState::Unstarted => self.unstarted(ui, height),
TimerState::Running(cs) => { TimerState::Running(cs) => {
if (Instant::now() - cs.updated) > MIN_REPAINT { let dur = Instant::now() - cs.updated;
ctx.request_repaint(); let dur = MIN_REPAINT.saturating_sub(dur);
} ctx.request_repaint_after(dur);
self.running(ui, vsize); self.running(ui, height);
} }
TimerState::Paused(_) => self.paused(ui, vsize), TimerState::Paused(_) => self.paused(ui, height),
TimerState::Finished => self.finished(ui, height),
TimerState::Finished => self.finished(ui, vsize),
} }
// check for quit key // check for quit key
if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) { if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) {
@ -87,7 +84,9 @@ impl Timer {
pub fn new(ctx: &CreationContext) -> Self { pub fn new(ctx: &CreationContext) -> Self {
let cli = Cli::parse(); let cli = Cli::parse();
let predator = cli.predator; let predator = cli.predator;
let seconds = cli.hours * 3600 + cli.minutes * 60 + cli.seconds; let seconds = cli.hours.unwrap_or(0) * 3600
+ cli.minutes.unwrap_or(0) * 60
+ cli.seconds.unwrap_or(0);
let duration = Duration::from_secs(seconds); let duration = Duration::from_secs(seconds);
let direction = if cli.count_up { let direction = if cli.count_up {
@ -116,7 +115,6 @@ impl Timer {
state: TimerState::Unstarted, state: TimerState::Unstarted,
tstart: Instant::now(), tstart: Instant::now(),
alarm: cli.alarm, alarm: cli.alarm,
height: None,
}; };
if cli.running { if cli.running {
let cs = ChronoState { let cs = ChronoState {
@ -133,11 +131,10 @@ impl Timer {
let tsize = size * 0.5; let tsize = size * 0.5;
let start = RichText::new("START") let start = RichText::new("START")
.font(FontId::monospace(tsize)) .font(FontId::monospace(tsize))
.size(tsize)
.color(Color32::WHITE) .color(Color32::WHITE)
.background_color(Color32::LIGHT_GREEN); .background_color(Color32::LIGHT_GREEN);
let height = StripBuilder::new(ui) StripBuilder::new(ui)
.size(Size::remainder()) .size(Size::remainder())
.cell_layout(Layout::centered_and_justified(egui::Direction::TopDown)) .cell_layout(Layout::centered_and_justified(egui::Direction::TopDown))
.horizontal(|mut strip| { .horizontal(|mut strip| {
@ -150,17 +147,13 @@ impl Timer {
}); });
} }
}); });
}) });
.rect
.height();
self.height = Some(height);
} }
fn running(&mut self, ui: &mut Ui, size: f32) { fn running(&mut self, ui: &mut Ui, size: f32) {
let tsize = size * TEXT_FACTOR; let tsize = size * TEXT_FACTOR;
let text = RichText::new("PAUSE") let text = RichText::new("PAUSE")
.font(FontId::monospace(tsize)) .font(FontId::monospace(tsize))
.size(tsize)
.color(Color32::GOLD); .color(Color32::GOLD);
let elapsed; let elapsed;
@ -175,7 +168,7 @@ impl Timer {
let remaining = remaining.saturating_sub(elapsed); let remaining = remaining.saturating_sub(elapsed);
let height = StripBuilder::new(ui) StripBuilder::new(ui)
.size(Size::relative(0.3333)) .size(Size::relative(0.3333))
.size(Size::remainder()) .size(Size::remainder())
.cell_layout(Layout::centered_and_justified(Direction::LeftToRight)) .cell_layout(Layout::centered_and_justified(Direction::LeftToRight))
@ -197,11 +190,7 @@ impl Timer {
let tsize = size * DIGIT_FACTOR; let tsize = size * DIGIT_FACTOR;
display_digits(&mut strip, remaining, color, tsize); display_digits(&mut strip, remaining, color, tsize);
}) });
.rect
.height();
self.height = Some(height);
if remaining.is_zero() { if remaining.is_zero() {
if let Some(alarm_file) = &self.alarm { if let Some(alarm_file) = &self.alarm {
@ -238,7 +227,7 @@ impl Timer {
} }
let remaining = elapsed; let remaining = elapsed;
let height = StripBuilder::new(ui) StripBuilder::new(ui)
.size(Size::relative(0.33333)) .size(Size::relative(0.33333))
.size(Size::remainder()) .size(Size::remainder())
.cell_layout(Layout::centered_and_justified(Direction::LeftToRight)) .cell_layout(Layout::centered_and_justified(Direction::LeftToRight))
@ -251,8 +240,7 @@ impl Timer {
pstrip.cell(|ui| { pstrip.cell(|ui| {
let resume = RichText::new("RESUME") let resume = RichText::new("RESUME")
.color(Color32::GREEN) .color(Color32::GREEN)
.font(FontId::monospace(tsize)) .font(FontId::monospace(tsize));
.size(tsize);
if ui.button(resume).clicked() { if ui.button(resume).clicked() {
is_running = true; is_running = true;
} }
@ -260,8 +248,7 @@ impl Timer {
pstrip.cell(|ui| { pstrip.cell(|ui| {
let reset = RichText::new("RESET") let reset = RichText::new("RESET")
.color(Color32::RED) .color(Color32::RED)
.font(FontId::monospace(tsize)) .font(FontId::monospace(tsize));
.size(tsize);
if ui.button(reset).clicked() { if ui.button(reset).clicked() {
self.state = TimerState::Unstarted; self.state = TimerState::Unstarted;
} }
@ -282,10 +269,7 @@ impl Timer {
CountDirection::Up => self.duration - remaining, CountDirection::Up => self.duration - remaining,
}; };
display_digits(&mut strip, remaining, color, vsize * DIGIT_FACTOR); display_digits(&mut strip, remaining, color, vsize * DIGIT_FACTOR);
}) });
.rect
.height();
self.height = Some(height);
if is_running { if is_running {
let cs = ChronoState { let cs = ChronoState {
@ -315,8 +299,7 @@ impl Timer {
let tsize = vsize * 0.3; let tsize = vsize * 0.3;
let reset = RichText::new("RESTART") let reset = RichText::new("RESTART")
.color(Color32::DARK_GREEN) .color(Color32::DARK_GREEN)
.font(FontId::monospace(tsize)) .font(FontId::monospace(tsize));
.size(tsize);
if ui.button(reset).clicked() { if ui.button(reset).clicked() {
self.state = TimerState::Unstarted; self.state = TimerState::Unstarted;
} }