Make some basic timer functionality
18
src-tauri/Cargo.lock
generated
@@ -263,6 +263,19 @@ version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cocoa"
|
||||
version = "0.24.0"
|
||||
@@ -3130,6 +3143,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd6469f4314d5f1ffec476e05f17cc9a78bc7a27a6a857842170bdf8d6f98d2f"
|
||||
dependencies = [
|
||||
"getrandom 0.2.7",
|
||||
"rand 0.8.5",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3707,8 +3722,11 @@ checksum = "d2d7d3948613f75c98fd9328cfdcc45acc4d360655289d0a7d4ec931392200a3"
|
||||
name = "ziemniak"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tauri",
|
||||
"tauri-build",
|
||||
"tokio",
|
||||
"uuid 1.1.2",
|
||||
]
|
||||
|
||||
@@ -15,9 +15,12 @@ rust-version = "1.57"
|
||||
tauri-build = { version = "1.0.4", features = [] }
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
tauri = { version = "1.0.5", features = ["api-all"] }
|
||||
chrono = { version = "0.4.19", default-features = false, features = [ "std", "clock", "serde" ] }
|
||||
tokio = { version = "1.19", features = [ "rt", "rt-multi-thread", "sync", "fs", "io-util", "time" ] }
|
||||
uuid = { version = "1.1.2", features = [ "v4", "fast-rng", "serde" ] }
|
||||
|
||||
[features]
|
||||
# by default Tauri runs in production mode
|
||||
|
||||
|
Before Width: | Height: | Size: 3.4 KiB After Width: | Height: | Size: 3.1 KiB |
|
Before Width: | Height: | Size: 6.8 KiB After Width: | Height: | Size: 6.3 KiB |
|
Before Width: | Height: | Size: 974 B After Width: | Height: | Size: 824 B |
|
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.4 KiB |
|
Before Width: | Height: | Size: 3.9 KiB After Width: | Height: | Size: 3.6 KiB |
|
Before Width: | Height: | Size: 7.6 KiB After Width: | Height: | Size: 7.1 KiB |
|
Before Width: | Height: | Size: 903 B After Width: | Height: | Size: 782 B |
|
Before Width: | Height: | Size: 8.4 KiB After Width: | Height: | Size: 7.8 KiB |
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.1 KiB |
|
Before Width: | Height: | Size: 2.0 KiB After Width: | Height: | Size: 1.7 KiB |
|
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.1 KiB |
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 85 KiB After Width: | Height: | Size: 4.2 KiB |
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
@@ -1,16 +1,84 @@
|
||||
#![cfg_attr(
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
all(not(debug_assertions), target_os = "windows"),
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
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 {
|
||||
return Self {
|
||||
id: Uuid::new_v4(),
|
||||
duration: duration,
|
||||
message: message.to_string(),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
fn complete(self: &Self) -> bool {
|
||||
return self.elapsed.map_or(false, |e| e >= self.duration);
|
||||
}
|
||||
|
||||
async fn run(self: &mut Self, window: Window) {
|
||||
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;
|
||||
|
||||
elapsed = elapsed + duration;
|
||||
self.elapsed = Some(elapsed);
|
||||
|
||||
if self.complete() {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Err(_) = window.emit("timer-tick", self.clone()) {
|
||||
break;
|
||||
}
|
||||
last_checked = now;
|
||||
}
|
||||
|
||||
window
|
||||
.emit("timer-done", self.clone())
|
||||
.expect("Our window went away?");
|
||||
}
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}!", name)
|
||||
fn start_timer(window: Window, duration: Duration, message: &str) -> Uuid {
|
||||
let mut timer = Timer::new(message, duration);
|
||||
let timer_id = timer.id.clone();
|
||||
|
||||
spawn(async move {
|
||||
timer.run(window).await;
|
||||
});
|
||||
|
||||
timer_id
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![start_timer])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
},
|
||||
"bundle": {
|
||||
"active": true,
|
||||
"category": "DeveloperTool",
|
||||
"copyright": "",
|
||||
"category": "Lifestyle",
|
||||
"copyright": "2022 ModZero",
|
||||
"deb": {
|
||||
"depends": []
|
||||
},
|
||||
@@ -29,8 +29,8 @@
|
||||
"icons/icon.icns",
|
||||
"icons/icon.ico"
|
||||
],
|
||||
"identifier": "com.tauri.dev",
|
||||
"longDescription": "",
|
||||
"identifier": "xyz.modzero.ziemniak",
|
||||
"longDescription": "A bit more flexible interval timer",
|
||||
"macOS": {
|
||||
"entitlements": null,
|
||||
"exceptionDomain": "",
|
||||
|
||||