version = "0.1.0"
edition = "2024"
-[[bin]]
-name = "egui"
-path = "src/egui.rs"
[[bin]]
-name = "alarm"
+name = "main"
path = "src/alarm.rs"
[dependencies]
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,
}
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;
#[tokio::main]
async fn main() -> Result<()> {
- println!("hell oworld");
-
let (tray_event_tx, tray_event_rx) = std::sync::mpsc::channel::<TrayEvent>();
let icon = include_bytes!("../favicon.ico");
WEEKDAYS,
"test2".to_string(),
),
+ Alarm::new(
+ NaiveTime::from_hms_opt(17, 24, 0).unwrap(),
+ ALL,
+ "hi".to_string(),
+ ),
];
std::thread::spawn(move || {
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<bool> {
+ 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(());
}
}
}
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),
+++ /dev/null
-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<RefCell<bool>> = 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<RefCell<bool>>,
- alarm: Alarm,
- words: Vec<String>,
- current_word: usize,
- input: String,
- typed: Vec<String>,
-}
-
-impl MyEguiApp {
- fn new(cc: &eframe::CreationContext<'_>, alarm: Alarm, snoozed: Rc<RefCell<bool>>) -> 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<String> = (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;
- }
- });
- }
-}
--- /dev/null
+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<bool> {
+ let snoozed: Rc<RefCell<bool>> = 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<RefCell<bool>>,
+ alarm: Alarm,
+ words: Vec<String>,
+ current_word: usize,
+ input: String,
+ typed: Vec<String>,
+}
+
+impl MyEguiApp {
+ fn new(cc: &eframe::CreationContext<'_>, alarm: Alarm, snoozed: Rc<RefCell<bool>>) -> 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<String> = (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;
+ }
+ });
+ }
+}