Merge pull request #2421 from skygrango/wasm/update-clock-example

Fix `clock` example doesn't get the correct local time under unix system
This commit is contained in:
Héctor Ramón 2024-05-18 11:44:12 +02:00 committed by GitHub
commit d993b53e09
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 14 deletions

View file

@ -8,6 +8,5 @@ publish = false
[dependencies] [dependencies]
iced.workspace = true iced.workspace = true
iced.features = ["canvas", "tokio", "debug"] iced.features = ["canvas", "tokio", "debug"]
chrono = "0.4"
time = { version = "0.3", features = ["local-offset"] }
tracing-subscriber = "0.3" tracing-subscriber = "0.3"

View file

@ -1,5 +1,6 @@
use iced::alignment; use iced::alignment;
use iced::mouse; use iced::mouse;
use iced::time;
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
use iced::widget::{canvas, container}; use iced::widget::{canvas, container};
use iced::{ use iced::{
@ -18,13 +19,13 @@ pub fn main() -> iced::Result {
} }
struct Clock { struct Clock {
now: time::OffsetDateTime, now: chrono::DateTime<chrono::Local>,
clock: Cache, clock: Cache,
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
enum Message { enum Message {
Tick(time::OffsetDateTime), Tick(chrono::DateTime<chrono::Local>),
} }
impl Clock { impl Clock {
@ -54,16 +55,12 @@ impl Clock {
} }
fn subscription(&self) -> Subscription<Message> { fn subscription(&self) -> Subscription<Message> {
iced::time::every(std::time::Duration::from_millis(500)).map(|_| { time::every(time::Duration::from_millis(500))
Message::Tick( .map(|_| Message::Tick(chrono::offset::Local::now()))
time::OffsetDateTime::now_local()
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
)
})
} }
fn theme(&self) -> Theme { fn theme(&self) -> Theme {
Theme::ALL[(self.now.unix_timestamp() as usize / 10) % Theme::ALL.len()] Theme::ALL[(self.now.timestamp() as usize / 10) % Theme::ALL.len()]
.clone() .clone()
} }
} }
@ -71,8 +68,7 @@ impl Clock {
impl Default for Clock { impl Default for Clock {
fn default() -> Self { fn default() -> Self {
Self { Self {
now: time::OffsetDateTime::now_local() now: chrono::offset::Local::now(),
.unwrap_or_else(|_| time::OffsetDateTime::now_utc()),
clock: Cache::default(), clock: Cache::default(),
} }
} }
@ -89,6 +85,8 @@ impl<Message> canvas::Program<Message> for Clock {
bounds: Rectangle, bounds: Rectangle,
_cursor: mouse::Cursor, _cursor: mouse::Cursor,
) -> Vec<Geometry> { ) -> Vec<Geometry> {
use chrono::Timelike;
let clock = self.clock.draw(renderer, bounds.size(), |frame| { let clock = self.clock.draw(renderer, bounds.size(), |frame| {
let palette = theme.extended_palette(); let palette = theme.extended_palette();
@ -169,7 +167,7 @@ impl<Message> canvas::Program<Message> for Clock {
} }
} }
fn hand_rotation(n: u8, total: u8) -> Degrees { fn hand_rotation(n: u32, total: u32) -> Degrees {
let turns = n as f32 / total as f32; let turns = n as f32 / total as f32;
Degrees(360.0 * turns) Degrees(360.0 * turns)