Skip to content

Commit

Permalink
Add Duration helpers to time module
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jan 24, 2025
1 parent 3a07c63 commit 3d893ae
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 19 deletions.
25 changes: 25 additions & 0 deletions core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,28 @@
pub use web_time::Duration;
pub use web_time::Instant;

/// Creates a [`Duration`] representing the given amount of milliseconds.
pub fn milliseconds(milliseconds: u64) -> Duration {
Duration::from_millis(milliseconds)
}

/// Creates a [`Duration`] representing the given amount of seconds.
pub fn seconds(seconds: u64) -> Duration {
Duration::from_secs(seconds)
}

/// Creates a [`Duration`] representing the given amount of minutes.
pub fn minutes(minutes: u64) -> Duration {
seconds(minutes * 60)
}

/// Creates a [`Duration`] representing the given amount of hours.
pub fn hours(hours: u64) -> Duration {
minutes(hours * 60)
}

/// Creates a [`Duration`] representing the given amount of days.
pub fn days(days: u64) -> Duration {
hours(days * 24)
}
4 changes: 2 additions & 2 deletions examples/arc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -34,8 +35,7 @@ impl Arc {
}

fn subscription(&self) -> Subscription<Message> {
iced::time::every(std::time::Duration::from_millis(10))
.map(|_| Message::Tick)
window::frames().map(|_| Message::Tick)
}
}

Expand Down
4 changes: 2 additions & 2 deletions examples/clock/src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Clock {
}

fn subscription(&self) -> Subscription<Message> {
time::every(time::Duration::from_millis(500))
time::every(milliseconds(500))
.map(|_| Message::Tick(chrono::offset::Local::now()))
}

Expand Down
7 changes: 3 additions & 4 deletions examples/game_of_life/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -112,7 +111,7 @@ impl GameOfLife {

fn subscription(&self) -> Subscription<Message> {
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()
Expand Down Expand Up @@ -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::{
Expand All @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions examples/stopwatch/src/main.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
}
};

Expand Down
4 changes: 2 additions & 2 deletions examples/the_matrix/src/main.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -40,7 +40,7 @@ impl TheMatrix {
}

fn subscription(&self) -> Subscription<Message> {
time::every(std::time::Duration::from_millis(50)).map(Message::Tick)
time::every(milliseconds(50)).map(Message::Tick)
}
}

Expand Down
7 changes: 3 additions & 4 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
};
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/time.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Listen and react to time.
pub use crate::core::time::{Duration, Instant};
pub use crate::core::time::*;

#[allow(unused_imports)]
#[cfg_attr(
Expand Down

0 comments on commit 3d893ae

Please sign in to comment.