2022-07-26 05:03:28 +02:00
|
|
|
#![cfg_attr(
|
2022-07-26 23:36:24 +02:00
|
|
|
all(not(debug_assertions), target_os = "windows"),
|
|
|
|
|
windows_subsystem = "windows"
|
2022-07-26 05:03:28 +02:00
|
|
|
)]
|
|
|
|
|
|
2022-07-26 23:36:24 +02:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
|
|
use chrono::{DateTime, FixedOffset, Local};
|
|
|
|
|
use tauri::{async_runtime::spawn, Window};
|
|
|
|
|
use tokio::time::interval;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Default, serde::Serialize)]
|
|
|
|
|
struct Timer {
|
|
|
|
|
id: Uuid,
|
|
|
|
|
started: Option<DateTime<FixedOffset>>,
|
|
|
|
|
duration: Duration,
|
|
|
|
|
elapsed: Option<Duration>,
|
|
|
|
|
message: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Timer {
|
|
|
|
|
fn new(message: &str, duration: Duration) -> Self {
|
2022-07-27 12:58:22 +02:00
|
|
|
Self {
|
2022-07-26 23:36:24 +02:00
|
|
|
id: Uuid::new_v4(),
|
2022-07-27 12:58:22 +02:00
|
|
|
duration,
|
2022-07-26 23:36:24 +02:00
|
|
|
message: message.to_string(),
|
|
|
|
|
..Default::default()
|
2022-07-27 12:58:22 +02:00
|
|
|
}
|
2022-07-26 23:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
fn complete(&self) -> bool {
|
|
|
|
|
self.elapsed.map_or(false, |e| e >= self.duration)
|
2022-07-26 23:36:24 +02:00
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
async fn run(&mut self, window: Window) {
|
2022-07-26 23:36:24 +02:00
|
|
|
self.started = Some(Local::now().into());
|
|
|
|
|
let mut elapsed = Duration::from_secs(0);
|
|
|
|
|
self.elapsed = Some(elapsed);
|
|
|
|
|
let mut last_checked = Instant::now();
|
|
|
|
|
|
|
|
|
|
let mut interval = interval(Duration::from_secs(1) / 60);
|
|
|
|
|
loop {
|
|
|
|
|
interval.tick().await;
|
|
|
|
|
let now = Instant::now();
|
|
|
|
|
let duration = now - last_checked;
|
|
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
elapsed += duration;
|
2022-07-26 23:36:24 +02:00
|
|
|
self.elapsed = Some(elapsed);
|
|
|
|
|
|
|
|
|
|
if self.complete() {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 12:58:22 +02:00
|
|
|
if window.emit("timer-tick", self.clone()).is_err() {
|
2022-07-26 23:36:24 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
last_checked = now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window
|
|
|
|
|
.emit("timer-done", self.clone())
|
|
|
|
|
.expect("Our window went away?");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-26 05:03:28 +02:00
|
|
|
#[tauri::command]
|
2022-07-26 23:36:24 +02:00
|
|
|
fn start_timer(window: Window, duration: Duration, message: &str) -> Uuid {
|
|
|
|
|
let mut timer = Timer::new(message, duration);
|
2022-07-27 12:58:22 +02:00
|
|
|
let timer_id = timer.id;
|
2022-07-26 23:36:24 +02:00
|
|
|
|
|
|
|
|
spawn(async move {
|
|
|
|
|
timer.run(window).await;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
timer_id
|
2022-07-26 05:03:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn main() {
|
2022-07-26 23:36:24 +02:00
|
|
|
tauri::Builder::default()
|
|
|
|
|
.invoke_handler(tauri::generate_handler![start_timer])
|
|
|
|
|
.run(tauri::generate_context!())
|
|
|
|
|
.expect("error while running tauri application");
|
2022-07-26 05:03:28 +02:00
|
|
|
}
|