From 55b70e734b3d767716f00322613670e612c7048c Mon Sep 17 00:00:00 2001 From: Taran Nathan Date: Wed, 29 Jul 2026 18:57:07 -0700 Subject: [PATCH] stichin it together? edit: i think it works! --- Cargo.toml | 5 +--- src/alarm.rs | 30 ++++++++++++--------- src/{egui.rs => gui.rs} | 58 +++++++++++++++++++++-------------------- 3 files changed, 49 insertions(+), 44 deletions(-) rename src/{egui.rs => gui.rs} (69%) diff --git a/Cargo.toml b/Cargo.toml index 9a2e35d..dacc89e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,12 +3,9 @@ name = "alarm-that-wont-quit" version = "0.1.0" edition = "2024" -[[bin]] -name = "egui" -path = "src/egui.rs" [[bin]] -name = "alarm" +name = "main" path = "src/alarm.rs" [dependencies] diff --git a/src/alarm.rs b/src/alarm.rs index 8b48b2c..c651b19 100644 --- a/src/alarm.rs +++ b/src/alarm.rs @@ -7,10 +7,13 @@ use itertools::Itertools; use tokio::time::{Instant, sleep, sleep_until}; use trayicon::{MenuBuilder, TrayIconBuilder}; -#[derive(Debug)] +mod gui; + +#[derive(Debug, Clone)] pub struct Alarm { pub time: NaiveTime, - pub days: u8, //if u use the 8th bit, i will be VERY ANGRY with u + ///if u use the 8th bit, i will be VERY ANGRY with u + pub days: u8, pub label: String, } @@ -33,6 +36,7 @@ enum TrayEvent { pub const WEEKDAYS: u8 = 0b11111000; pub const WEEKENDS: u8 = 0b00000110; +pub const ALL: u8 = 0b11111110; pub const MONDAY: u8 = 0b10000000; pub const TUESDAY: u8 = 0b01000000; @@ -44,8 +48,6 @@ pub const SUNDAY: u8 = 0b00000010; #[tokio::main] async fn main() -> Result<()> { - println!("hell oworld"); - let (tray_event_tx, tray_event_rx) = std::sync::mpsc::channel::(); let icon = include_bytes!("../favicon.ico"); @@ -79,6 +81,11 @@ async fn main() -> Result<()> { WEEKDAYS, "test2".to_string(), ), + Alarm::new( + NaiveTime::from_hms_opt(17, 24, 0).unwrap(), + ALL, + "hi".to_string(), + ), ]; std::thread::spawn(move || { @@ -93,25 +100,25 @@ async fn main() -> Result<()> { loop { let next = get_next_alarm(&alarms); wait_til_time(next.time).await; - snooze_loop(&next); + snooze_loop(&next).await?; } Ok(()) } -pub const SNOOZE_MINS: u8 = 5; +pub const SNOOZE_MINS: u8 = 1; -fn alarm_gui() -> bool { - todo!() +fn alarm_gui(alarm: &Alarm) -> Result { + Ok(gui::gui(alarm)?) } -async fn snooze_loop(alarm: &Alarm) { +async fn snooze_loop(alarm: &Alarm) -> Result<()> { loop { - let snoozed = alarm_gui(); + let snoozed = alarm_gui(alarm)?; if snoozed { sleep(Duration::from_mins(SNOOZE_MINS as u64)).await; } else { - return; + return Ok(()); } } } @@ -234,7 +241,6 @@ fn get_delta(time: NaiveTime) -> TimeDelta { async fn wait_til_time(time: NaiveTime) { let delta = get_delta(time); - dbg!(delta.num_seconds()); let until = delta.to_std().map_or_else( |_| Instant::now() - delta.abs().to_std().unwrap() + Duration::from_hours(24), diff --git a/src/egui.rs b/src/gui.rs similarity index 69% rename from src/egui.rs rename to src/gui.rs index 415365c..7277e67 100644 --- a/src/egui.rs +++ b/src/gui.rs @@ -5,31 +5,21 @@ use chrono::{NaiveTime, Timelike}; use eframe::{NativeOptions, egui}; use egui::{Color32, Key, RichText, Style, TextEdit}; -mod alarm; -use alarm::Alarm; +use crate::*; -pub const TEST_WORDS: u8 = 50; +pub const TEST_WORDS: usize = 20; -fn main() -> Result<()> { - let alarm = Alarm::new( - NaiveTime::from_hms_opt(17, 30, 0).unwrap(), - alarm::WEEKDAYS, - "test1".to_string(), - ); - - let mut snoozed: Rc> = Rc::new(RefCell::new(false)); - dbg!(snoozed.clone()); +pub fn gui(alarm: &Alarm) -> Result { + let snoozed: Rc> = Rc::new(RefCell::new(false)); let native_options = NativeOptions::default(); eframe::run_native( "Alarm that won't quit", native_options, - Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, alarm, snoozed.clone())))), + Box::new(|cc| Ok(Box::new(MyEguiApp::new(cc, alarm.clone(), snoozed.clone())))), )?; - dbg!(snoozed.clone()); - - Ok(()) + Ok(snoozed.take()) } struct MyEguiApp { @@ -48,10 +38,11 @@ impl MyEguiApp { // Use the cc.gl (a glow::Context) to create graphics shaders and buffers that you can use // for e.g. egui::PaintCallback. - // let style = Style { - // ..Default::default() - // }; - // cc.egui_ctx.set_global_style(style); + let style = Style { + ..Default::default() + }; + cc.egui_ctx.set_global_style(style); + let words: Vec = (0..TEST_WORDS) .into_iter() .map(|_| random_word::get(random_word::Lang::En).to_string()) @@ -91,8 +82,12 @@ impl eframe::App for MyEguiApp { } }); - let response = ui - .add(egui::TextEdit::singleline(&mut self.input).desired_width(f32::INFINITY)); + let response = ui.add( + egui::TextEdit::singleline(&mut self.input) + .desired_width(f32::INFINITY) + .return_key(None) + .char_limit(30), + ); if response.has_focus() { let commit = ui.input(|i| { @@ -107,21 +102,28 @@ impl eframe::App for MyEguiApp { if commit { let typed = self.input.trim().to_owned(); + if &self.words[self.current_word] == &typed { + self.typed.push(typed); - self.typed.push(typed); + self.input.clear(); - self.input.clear(); + if self.current_word < self.words.len() { + self.current_word += 1; + } + } else { + self.input.clear(); + } - if self.current_word < self.words.len() { - self.current_word += 1; + if &self.current_word >= &TEST_WORDS { + ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close); + *self.snoozed.borrow_mut() = false; } } } }); if keypressed.contains(&Key::Escape) { ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close); - let mut snoozed = *self.snoozed.borrow_mut(); - snoozed = true; + *self.snoozed.borrow_mut() = true; } }); } -- 2.47.3