move some shit around
This commit is contained in:
parent
628a8e73d6
commit
4ccc2e4738
6 changed files with 72 additions and 62 deletions
12
src/lib.rs
12
src/lib.rs
|
@ -1,5 +1,13 @@
|
||||||
pub const AIRHORN: &[u8] = include_bytes!("../airhorn_alarm.mp3");
|
use std::time::Duration;
|
||||||
pub const PREDATOR_FONT: &[u8] = include_bytes!("../Predator.ttf");
|
|
||||||
|
pub const AIRHORN: &[u8] = include_bytes!("../resources/airhorn_alarm.mp3");
|
||||||
|
pub const PREDATOR_FONT: &[u8] = include_bytes!("../resources/Predator.ttf");
|
||||||
|
|
||||||
|
pub(crate) const MIN_REPAINT: Duration = Duration::from_millis(100);
|
||||||
|
pub(crate) const MAX_REPAINT: Duration = Duration::from_millis(250);
|
||||||
|
|
||||||
|
pub(crate) const DIGIT_FACTOR: f32 = 0.4;
|
||||||
|
pub(crate) const TEXT_FACTOR: f32 = 0.2;
|
||||||
|
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
pub mod timer;
|
pub mod timer;
|
||||||
|
|
28
src/timer/eframe_app.rs
Normal file
28
src/timer/eframe_app.rs
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
use super::{state::*, Timer};
|
||||||
|
use crate::{MAX_REPAINT, MIN_REPAINT};
|
||||||
|
|
||||||
|
impl eframe::App for Timer {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
||||||
|
ctx.request_repaint_after(MAX_REPAINT);
|
||||||
|
let height = ctx.input().screen_rect().height();
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
match self.state {
|
||||||
|
TimerState::Unstarted => self.unstarted(ui, height),
|
||||||
|
TimerState::Running(cs) => {
|
||||||
|
let dur = Instant::now() - cs.updated;
|
||||||
|
let dur = MIN_REPAINT.saturating_sub(dur);
|
||||||
|
ctx.request_repaint_after(dur);
|
||||||
|
self.running(ui, height, cs);
|
||||||
|
}
|
||||||
|
TimerState::Paused(cs) => self.paused(ui, height, cs),
|
||||||
|
TimerState::Finished => self.finished(ui, height),
|
||||||
|
}
|
||||||
|
// check for quit key
|
||||||
|
if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) {
|
||||||
|
frame.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,14 @@
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use eframe::{App, CreationContext};
|
|
||||||
use egui::{Color32, Direction, FontId, Layout, RichText, Ui};
|
use egui::{Color32, Direction, FontId, Layout, RichText, Ui};
|
||||||
use egui_extras::{Size, StripBuilder};
|
use egui_extras::{Size, StripBuilder};
|
||||||
|
|
||||||
use crate::{cli::Cli, util::*, AIRHORN, PREDATOR_FONT};
|
use crate::{cli::Cli, util::*, AIRHORN, DIGIT_FACTOR, PREDATOR_FONT, TEXT_FACTOR};
|
||||||
|
|
||||||
const MIN_REPAINT: Duration = Duration::from_millis(100);
|
mod state;
|
||||||
const MAX_REPAINT: Duration = Duration::from_millis(250);
|
use state::{ChronoState, TimerState};
|
||||||
|
mod eframe_app;
|
||||||
const DIGIT_FACTOR: f32 = 0.4;
|
|
||||||
const TEXT_FACTOR: f32 = 0.2;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum CountDirection {
|
pub enum CountDirection {
|
||||||
|
@ -28,60 +25,8 @@ pub struct Timer {
|
||||||
alarm: Option<Vec<u8>>,
|
alarm: Option<Vec<u8>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
enum TimerState {
|
|
||||||
Unstarted,
|
|
||||||
Paused(ChronoState),
|
|
||||||
Running(ChronoState),
|
|
||||||
Finished,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for TimerState {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
matches!(
|
|
||||||
(self, other),
|
|
||||||
(Self::Paused(_), Self::Paused(_))
|
|
||||||
| (Self::Running(_), Self::Running(_))
|
|
||||||
| (Self::Unstarted, Self::Unstarted)
|
|
||||||
| (Self::Finished, Self::Finished)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Eq for TimerState {}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
struct ChronoState {
|
|
||||||
updated: Instant,
|
|
||||||
remaining: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl App for Timer {
|
|
||||||
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
|
|
||||||
ctx.request_repaint_after(MAX_REPAINT);
|
|
||||||
let height = ctx.input().screen_rect().height();
|
|
||||||
egui::CentralPanel::default().show(ctx, |ui| {
|
|
||||||
match self.state {
|
|
||||||
TimerState::Unstarted => self.unstarted(ui, height),
|
|
||||||
TimerState::Running(cs) => {
|
|
||||||
let dur = Instant::now() - cs.updated;
|
|
||||||
let dur = MIN_REPAINT.saturating_sub(dur);
|
|
||||||
ctx.request_repaint_after(dur);
|
|
||||||
self.running(ui, height, cs);
|
|
||||||
}
|
|
||||||
TimerState::Paused(cs) => self.paused(ui, height, cs),
|
|
||||||
TimerState::Finished => self.finished(ui, height),
|
|
||||||
}
|
|
||||||
// check for quit key
|
|
||||||
if ui.input().key_pressed(egui::Key::Q) || ui.input().key_pressed(egui::Key::Escape) {
|
|
||||||
frame.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Timer {
|
impl Timer {
|
||||||
pub fn new(ctx: &CreationContext) -> Self {
|
pub fn new(ctx: &eframe::CreationContext) -> Self {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
let predator = cli.predator;
|
let predator = cli.predator;
|
||||||
let seconds = cli.hours.unwrap_or(0) * 3600
|
let seconds = cli.hours.unwrap_or(0) * 3600
|
29
src/timer/state.rs
Normal file
29
src/timer/state.rs
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub(crate) enum TimerState {
|
||||||
|
Unstarted,
|
||||||
|
Paused(ChronoState),
|
||||||
|
Running(ChronoState),
|
||||||
|
Finished,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialEq for TimerState {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
matches!(
|
||||||
|
(self, other),
|
||||||
|
(Self::Paused(_), Self::Paused(_))
|
||||||
|
| (Self::Running(_), Self::Running(_))
|
||||||
|
| (Self::Unstarted, Self::Unstarted)
|
||||||
|
| (Self::Finished, Self::Finished)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for TimerState {}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub(crate) struct ChronoState {
|
||||||
|
pub updated: Instant,
|
||||||
|
pub remaining: Duration,
|
||||||
|
}
|
Loading…
Reference in a new issue