Add real-time clock to home automation demo (#8390)

This commit is contained in:
Tasuku Suzuki 2025-05-08 19:19:03 +09:00 committed by GitHub
parent 6ba3b8586e
commit 0051c95b1b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 46 additions and 4 deletions

View file

@ -20,6 +20,7 @@ name = "home_automation_lib"
[dependencies]
slint = { path = "../../../api/rs/slint" }
chrono = "0.4"
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }

View file

@ -1,11 +1,14 @@
// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT
use chrono::{Datelike, Local, Timelike};
use slint::{Timer, TimerMode};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
slint::slint! {
export { AppWindow } from "../ui/demo.slint";
export { Api, AppWindow } from "../ui/demo.slint";
}
#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
@ -15,5 +18,27 @@ pub fn main() {
#[cfg(all(debug_assertions, target_arch = "wasm32"))]
console_error_panic_hook::set_once();
AppWindow::new().expect("AppWindow::new() failed").run().expect("AppWindow::run() failed");
let app = AppWindow::new().expect("AppWindow::new() failed");
let app_weak = app.as_weak();
let timer = Timer::default();
timer.start(TimerMode::Repeated, std::time::Duration::from_millis(1000), move || {
if let Some(app) = app_weak.upgrade() {
let api = app.global::<Api>();
let now = Local::now();
let mut date = Date::default();
date.year = now.year() as i32;
date.month = now.month() as i32;
date.day = now.day() as i32;
api.set_current_date(date);
let mut time = Time::default();
time.hour = now.hour() as i32;
time.minute = now.minute() as i32;
time.second = now.second() as i32;
api.set_current_time(time);
}
});
app.run().expect("AppWindow::run() failed");
}