From: Taran Nathan Date: Thu, 30 Jul 2026 01:57:07 +0000 (-0700) Subject: stichin it together? edit: i think it works! X-Git-Url: https://git.taranathan.com/?a=commitdiff_plain;h=55b70e734b3d767716f00322613670e612c7048c;p=alarm-that-wont-quit.git stichin it together? edit: i think it works! --- 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/egui.rs deleted file mode 100644 index 415365c..0000000 --- a/src/egui.rs +++ /dev/null @@ -1,128 +0,0 @@ -use std::{any::Any, cell::RefCell, rc::Rc}; - -use anyhow::Result; -use chrono::{NaiveTime, Timelike}; -use eframe::{NativeOptions, egui}; -use egui::{Color32, Key, RichText, Style, TextEdit}; - -mod alarm; -use alarm::Alarm; - -pub const TEST_WORDS: u8 = 50; - -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()); - - 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())))), - )?; - - dbg!(snoozed.clone()); - - Ok(()) -} - -struct MyEguiApp { - snoozed: Rc>, - alarm: Alarm, - words: Vec, - current_word: usize, - input: String, - typed: Vec, -} - -impl MyEguiApp { - fn new(cc: &eframe::CreationContext<'_>, alarm: Alarm, snoozed: Rc>) -> Self { - // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_global_style. - // Restore app state using cc.storage (requires the "persistence" feature). - // 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 words: Vec = (0..TEST_WORDS) - .into_iter() - .map(|_| random_word::get(random_word::Lang::En).to_string()) - .collect(); - - Self { - snoozed, - alarm, - words, - current_word: 0, - input: String::new(), - typed: vec![], - } - } -} - -impl eframe::App for MyEguiApp { - fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { - let keypressed = ui.input(|i| i.keys_down.clone()); - - egui::CentralPanel::default().show(ui, |ui| { - ui.vertical_centered_justified(|ui| { - ui.heading(&self.alarm.label); - let time = chrono::Local::now(); - ui.heading(format!("{:02}:{:02}", time.hour(), time.minute())); - ui.horizontal_wrapped(|ui| { - for (i, word) in self.words.iter().enumerate() { - let color = if i < self.current_word { - Color32::LIGHT_GREEN - } else if i == self.current_word { - Color32::YELLOW - } else { - ui.visuals().text_color() - }; - - ui.label(RichText::new(format!("{word} ")).color(color)); - } - }); - - let response = ui - .add(egui::TextEdit::singleline(&mut self.input).desired_width(f32::INFINITY)); - - if response.has_focus() { - let commit = ui.input(|i| { - i.key_pressed(egui::Key::Enter) - || i.events.iter().any(|e| { - matches!( - e, - egui::Event::Text(text) if text == " " - ) - }) - }); - - if commit { - let typed = self.input.trim().to_owned(); - - self.typed.push(typed); - - self.input.clear(); - - if self.current_word < self.words.len() { - self.current_word += 1; - } - } - } - }); - if keypressed.contains(&Key::Escape) { - ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close); - let mut snoozed = *self.snoozed.borrow_mut(); - snoozed = true; - } - }); - } -} diff --git a/src/gui.rs b/src/gui.rs new file mode 100644 index 0000000..7277e67 --- /dev/null +++ b/src/gui.rs @@ -0,0 +1,130 @@ +use std::{any::Any, cell::RefCell, rc::Rc}; + +use anyhow::Result; +use chrono::{NaiveTime, Timelike}; +use eframe::{NativeOptions, egui}; +use egui::{Color32, Key, RichText, Style, TextEdit}; + +use crate::*; + +pub const TEST_WORDS: usize = 20; + +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.clone(), snoozed.clone())))), + )?; + + Ok(snoozed.take()) +} + +struct MyEguiApp { + snoozed: Rc>, + alarm: Alarm, + words: Vec, + current_word: usize, + input: String, + typed: Vec, +} + +impl MyEguiApp { + fn new(cc: &eframe::CreationContext<'_>, alarm: Alarm, snoozed: Rc>) -> Self { + // Customize egui here with cc.egui_ctx.set_fonts and cc.egui_ctx.set_global_style. + // Restore app state using cc.storage (requires the "persistence" feature). + // 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 words: Vec = (0..TEST_WORDS) + .into_iter() + .map(|_| random_word::get(random_word::Lang::En).to_string()) + .collect(); + + Self { + snoozed, + alarm, + words, + current_word: 0, + input: String::new(), + typed: vec![], + } + } +} + +impl eframe::App for MyEguiApp { + fn ui(&mut self, ui: &mut egui::Ui, frame: &mut eframe::Frame) { + let keypressed = ui.input(|i| i.keys_down.clone()); + + egui::CentralPanel::default().show(ui, |ui| { + ui.vertical_centered_justified(|ui| { + ui.heading(&self.alarm.label); + let time = chrono::Local::now(); + ui.heading(format!("{:02}:{:02}", time.hour(), time.minute())); + ui.horizontal_wrapped(|ui| { + for (i, word) in self.words.iter().enumerate() { + let color = if i < self.current_word { + Color32::LIGHT_GREEN + } else if i == self.current_word { + Color32::YELLOW + } else { + ui.visuals().text_color() + }; + + ui.label(RichText::new(format!("{word} ")).color(color)); + } + }); + + 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| { + i.key_pressed(egui::Key::Enter) + || i.events.iter().any(|e| { + matches!( + e, + egui::Event::Text(text) if text == " " + ) + }) + }); + + if commit { + let typed = self.input.trim().to_owned(); + if &self.words[self.current_word] == &typed { + self.typed.push(typed); + + self.input.clear(); + + if self.current_word < self.words.len() { + self.current_word += 1; + } + } else { + self.input.clear(); + } + + 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); + *self.snoozed.borrow_mut() = true; + } + }); + } +}