2024-02-14 20:33:07 +00:00
|
|
|
//! A simple little clock that updates the time every few milliseconds.
|
|
|
|
//!
|
|
|
|
|
2024-09-07 13:44:25 +00:00
|
|
|
use async_std::task::sleep;
|
2022-12-30 00:53:43 +00:00
|
|
|
use dioxus::prelude::*;
|
2024-09-03 15:16:37 +00:00
|
|
|
use web_time::Instant;
|
2022-12-30 00:53:43 +00:00
|
|
|
|
2024-07-25 21:58:00 +00:00
|
|
|
const STYLE: &str = asset!("./examples/assets/clock.css");
|
2024-07-23 17:29:37 +00:00
|
|
|
|
2024-01-05 03:52:49 +00:00
|
|
|
fn main() {
|
2024-09-03 15:16:37 +00:00
|
|
|
launch(app);
|
2022-12-30 00:53:43 +00:00
|
|
|
}
|
|
|
|
|
2024-01-14 04:51:37 +00:00
|
|
|
fn app() -> Element {
|
2024-02-14 20:33:07 +00:00
|
|
|
let mut millis = use_signal(|| 0);
|
2023-08-05 00:23:57 +00:00
|
|
|
|
2024-02-01 00:33:53 +00:00
|
|
|
use_future(move || async move {
|
2024-02-14 20:33:07 +00:00
|
|
|
// Save our initial timea
|
2024-09-03 15:16:37 +00:00
|
|
|
let start = Instant::now();
|
2024-02-14 20:33:07 +00:00
|
|
|
|
2023-08-05 00:25:40 +00:00
|
|
|
loop {
|
2024-09-03 15:16:37 +00:00
|
|
|
sleep(std::time::Duration::from_millis(27)).await;
|
2024-02-14 20:33:07 +00:00
|
|
|
|
|
|
|
// Update the time, using a more precise approach of getting the duration since we started the timer
|
|
|
|
millis.set(start.elapsed().as_millis() as i64);
|
2023-08-05 00:25:40 +00:00
|
|
|
}
|
|
|
|
});
|
2023-08-05 00:23:57 +00:00
|
|
|
|
2024-02-14 20:33:07 +00:00
|
|
|
// Format the time as a string
|
|
|
|
// This is rather cheap so it's fine to leave it in the render function
|
|
|
|
let time = format!(
|
|
|
|
"{:02}:{:02}:{:03}",
|
|
|
|
millis() / 1000 / 60 % 60,
|
|
|
|
millis() / 1000 % 60,
|
|
|
|
millis() % 1000
|
|
|
|
);
|
2024-01-24 20:21:14 +00:00
|
|
|
|
2024-01-16 19:18:46 +00:00
|
|
|
rsx! {
|
2024-07-25 21:58:00 +00:00
|
|
|
head::Link { rel: "stylesheet", href: STYLE }
|
2024-02-14 20:33:07 +00:00
|
|
|
div { id: "app",
|
|
|
|
div { id: "title", "Carpe diem 🎉" }
|
|
|
|
div { id: "clock-display", "{time}" }
|
|
|
|
}
|
2024-01-14 05:12:21 +00:00
|
|
|
}
|
2022-12-30 00:53:43 +00:00
|
|
|
}
|