From 3d893ae01b64eb51b2f5854949c694090118e052 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 24 Jan 2025 18:47:34 +0100 Subject: Add `Duration` helpers to `time` module --- examples/arc/src/main.rs | 4 ++-- examples/clock/src/main.rs | 4 ++-- examples/game_of_life/src/main.rs | 7 +++---- examples/stopwatch/src/main.rs | 6 ++---- examples/the_matrix/src/main.rs | 4 ++-- examples/toast/src/main.rs | 7 +++---- 6 files changed, 14 insertions(+), 18 deletions(-) (limited to 'examples') diff --git a/examples/arc/src/main.rs b/examples/arc/src/main.rs index 18873259..88544caa 100644 --- a/examples/arc/src/main.rs +++ b/examples/arc/src/main.rs @@ -4,6 +4,7 @@ use iced::mouse; use iced::widget::canvas::{ self, stroke, Cache, Canvas, Geometry, Path, Stroke, }; +use iced::window; use iced::{Element, Fill, Point, Rectangle, Renderer, Subscription, Theme}; pub fn main() -> iced::Result { @@ -34,8 +35,7 @@ impl Arc { } fn subscription(&self) -> Subscription { - iced::time::every(std::time::Duration::from_millis(10)) - .map(|_| Message::Tick) + window::frames().map(|_| Message::Tick) } } diff --git a/examples/clock/src/main.rs b/examples/clock/src/main.rs index 7d11a3b5..0810594f 100644 --- a/examples/clock/src/main.rs +++ b/examples/clock/src/main.rs @@ -1,5 +1,5 @@ use iced::mouse; -use iced::time; +use iced::time::{self, milliseconds}; use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke}; use iced::widget::{canvas, container}; use iced::{alignment, Radians}; @@ -49,7 +49,7 @@ impl Clock { } fn subscription(&self) -> Subscription { - time::every(time::Duration::from_millis(500)) + time::every(milliseconds(500)) .map(|_| Message::Tick(chrono::offset::Local::now())) } diff --git a/examples/game_of_life/src/main.rs b/examples/game_of_life/src/main.rs index 7a7224d5..1008e477 100644 --- a/examples/game_of_life/src/main.rs +++ b/examples/game_of_life/src/main.rs @@ -5,12 +5,11 @@ mod preset; use grid::Grid; use preset::Preset; -use iced::time; +use iced::time::{self, milliseconds}; use iced::widget::{ button, checkbox, column, container, pick_list, row, slider, text, }; use iced::{Center, Element, Fill, Subscription, Task, Theme}; -use std::time::Duration; pub fn main() -> iced::Result { tracing_subscriber::fmt::init(); @@ -112,7 +111,7 @@ impl GameOfLife { fn subscription(&self) -> Subscription { if self.is_playing { - time::every(Duration::from_millis(1000 / self.speed as u64)) + time::every(milliseconds(1000 / self.speed as u64)) .map(|_| Message::Tick) } else { Subscription::none() @@ -191,6 +190,7 @@ mod grid { use crate::Preset; use iced::alignment; use iced::mouse; + use iced::time::{Duration, Instant}; use iced::touch; use iced::widget::canvas; use iced::widget::canvas::{ @@ -202,7 +202,6 @@ mod grid { use rustc_hash::{FxHashMap, FxHashSet}; use std::future::Future; use std::ops::RangeInclusive; - use std::time::{Duration, Instant}; pub struct Grid { state: State, diff --git a/examples/stopwatch/src/main.rs b/examples/stopwatch/src/main.rs index 0d824d36..a814da55 100644 --- a/examples/stopwatch/src/main.rs +++ b/examples/stopwatch/src/main.rs @@ -1,10 +1,8 @@ use iced::keyboard; -use iced::time; +use iced::time::{self, milliseconds, Duration, Instant}; use iced::widget::{button, center, column, row, text}; use iced::{Center, Element, Subscription, Theme}; -use std::time::{Duration, Instant}; - pub fn main() -> iced::Result { iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view) .subscription(Stopwatch::subscription) @@ -63,7 +61,7 @@ impl Stopwatch { let tick = match self.state { State::Idle => Subscription::none(), State::Ticking { .. } => { - time::every(Duration::from_millis(10)).map(Message::Tick) + time::every(milliseconds(10)).map(Message::Tick) } }; diff --git a/examples/the_matrix/src/main.rs b/examples/the_matrix/src/main.rs index c6b20ece..315e9ee5 100644 --- a/examples/the_matrix/src/main.rs +++ b/examples/the_matrix/src/main.rs @@ -1,5 +1,5 @@ use iced::mouse; -use iced::time::{self, Instant}; +use iced::time::{self, milliseconds, Instant}; use iced::widget::canvas; use iced::{ Color, Element, Fill, Font, Point, Rectangle, Renderer, Subscription, Theme, @@ -40,7 +40,7 @@ impl TheMatrix { } fn subscription(&self) -> Subscription { - time::every(std::time::Duration::from_millis(50)).map(Message::Tick) + time::every(milliseconds(50)).map(Message::Tick) } } diff --git a/examples/toast/src/main.rs b/examples/toast/src/main.rs index a1b5886f..2ae9bfe2 100644 --- a/examples/toast/src/main.rs +++ b/examples/toast/src/main.rs @@ -162,7 +162,6 @@ impl Default for App { mod toast { use std::fmt; - use std::time::{Duration, Instant}; use iced::advanced::layout::{self, Layout}; use iced::advanced::overlay; @@ -171,6 +170,7 @@ mod toast { use iced::advanced::{Clipboard, Shell, Widget}; use iced::mouse; use iced::theme; + use iced::time::{self, Duration, Instant}; use iced::widget::{ button, column, container, horizontal_rule, horizontal_space, row, text, }; @@ -502,9 +502,8 @@ mod toast { self.instants.iter_mut().enumerate().for_each( |(index, maybe_instant)| { if let Some(instant) = maybe_instant.as_mut() { - let remaining = - Duration::from_secs(self.timeout_secs) - .saturating_sub(instant.elapsed()); + let remaining = time::seconds(self.timeout_secs) + .saturating_sub(instant.elapsed()); if remaining == Duration::ZERO { maybe_instant.take(); -- cgit