Add Duration helpers to time module
This commit is contained in:
parent
3a07c631ad
commit
3d893ae01b
8 changed files with 40 additions and 19 deletions
|
|
@ -2,3 +2,28 @@
|
||||||
|
|
||||||
pub use web_time::Duration;
|
pub use web_time::Duration;
|
||||||
pub use web_time::Instant;
|
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,6 +4,7 @@ use iced::mouse;
|
||||||
use iced::widget::canvas::{
|
use iced::widget::canvas::{
|
||||||
self, stroke, Cache, Canvas, Geometry, Path, Stroke,
|
self, stroke, Cache, Canvas, Geometry, Path, Stroke,
|
||||||
};
|
};
|
||||||
|
use iced::window;
|
||||||
use iced::{Element, Fill, Point, Rectangle, Renderer, Subscription, Theme};
|
use iced::{Element, Fill, Point, Rectangle, Renderer, Subscription, Theme};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -34,8 +35,7 @@ impl Arc {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
iced::time::every(std::time::Duration::from_millis(10))
|
window::frames().map(|_| Message::Tick)
|
||||||
.map(|_| Message::Tick)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use iced::mouse;
|
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::{stroke, Cache, Geometry, LineCap, Path, Stroke};
|
||||||
use iced::widget::{canvas, container};
|
use iced::widget::{canvas, container};
|
||||||
use iced::{alignment, Radians};
|
use iced::{alignment, Radians};
|
||||||
|
|
@ -49,7 +49,7 @@ impl Clock {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
time::every(time::Duration::from_millis(500))
|
time::every(milliseconds(500))
|
||||||
.map(|_| Message::Tick(chrono::offset::Local::now()))
|
.map(|_| Message::Tick(chrono::offset::Local::now()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,11 @@ mod preset;
|
||||||
use grid::Grid;
|
use grid::Grid;
|
||||||
use preset::Preset;
|
use preset::Preset;
|
||||||
|
|
||||||
use iced::time;
|
use iced::time::{self, milliseconds};
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, checkbox, column, container, pick_list, row, slider, text,
|
button, checkbox, column, container, pick_list, row, slider, text,
|
||||||
};
|
};
|
||||||
use iced::{Center, Element, Fill, Subscription, Task, Theme};
|
use iced::{Center, Element, Fill, Subscription, Task, Theme};
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
|
|
@ -112,7 +111,7 @@ impl GameOfLife {
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
if self.is_playing {
|
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)
|
.map(|_| Message::Tick)
|
||||||
} else {
|
} else {
|
||||||
Subscription::none()
|
Subscription::none()
|
||||||
|
|
@ -191,6 +190,7 @@ mod grid {
|
||||||
use crate::Preset;
|
use crate::Preset;
|
||||||
use iced::alignment;
|
use iced::alignment;
|
||||||
use iced::mouse;
|
use iced::mouse;
|
||||||
|
use iced::time::{Duration, Instant};
|
||||||
use iced::touch;
|
use iced::touch;
|
||||||
use iced::widget::canvas;
|
use iced::widget::canvas;
|
||||||
use iced::widget::canvas::{
|
use iced::widget::canvas::{
|
||||||
|
|
@ -202,7 +202,6 @@ mod grid {
|
||||||
use rustc_hash::{FxHashMap, FxHashSet};
|
use rustc_hash::{FxHashMap, FxHashSet};
|
||||||
use std::future::Future;
|
use std::future::Future;
|
||||||
use std::ops::RangeInclusive;
|
use std::ops::RangeInclusive;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
pub struct Grid {
|
pub struct Grid {
|
||||||
state: State,
|
state: State,
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
use iced::keyboard;
|
use iced::keyboard;
|
||||||
use iced::time;
|
use iced::time::{self, milliseconds, Duration, Instant};
|
||||||
use iced::widget::{button, center, column, row, text};
|
use iced::widget::{button, center, column, row, text};
|
||||||
use iced::{Center, Element, Subscription, Theme};
|
use iced::{Center, Element, Subscription, Theme};
|
||||||
|
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
|
iced::application("Stopwatch - Iced", Stopwatch::update, Stopwatch::view)
|
||||||
.subscription(Stopwatch::subscription)
|
.subscription(Stopwatch::subscription)
|
||||||
|
|
@ -63,7 +61,7 @@ impl Stopwatch {
|
||||||
let tick = match self.state {
|
let tick = match self.state {
|
||||||
State::Idle => Subscription::none(),
|
State::Idle => Subscription::none(),
|
||||||
State::Ticking { .. } => {
|
State::Ticking { .. } => {
|
||||||
time::every(Duration::from_millis(10)).map(Message::Tick)
|
time::every(milliseconds(10)).map(Message::Tick)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use iced::mouse;
|
use iced::mouse;
|
||||||
use iced::time::{self, Instant};
|
use iced::time::{self, milliseconds, Instant};
|
||||||
use iced::widget::canvas;
|
use iced::widget::canvas;
|
||||||
use iced::{
|
use iced::{
|
||||||
Color, Element, Fill, Font, Point, Rectangle, Renderer, Subscription, Theme,
|
Color, Element, Fill, Font, Point, Rectangle, Renderer, Subscription, Theme,
|
||||||
|
|
@ -40,7 +40,7 @@ impl TheMatrix {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
time::every(std::time::Duration::from_millis(50)).map(Message::Tick)
|
time::every(milliseconds(50)).map(Message::Tick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -162,7 +162,6 @@ impl Default for App {
|
||||||
|
|
||||||
mod toast {
|
mod toast {
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::time::{Duration, Instant};
|
|
||||||
|
|
||||||
use iced::advanced::layout::{self, Layout};
|
use iced::advanced::layout::{self, Layout};
|
||||||
use iced::advanced::overlay;
|
use iced::advanced::overlay;
|
||||||
|
|
@ -171,6 +170,7 @@ mod toast {
|
||||||
use iced::advanced::{Clipboard, Shell, Widget};
|
use iced::advanced::{Clipboard, Shell, Widget};
|
||||||
use iced::mouse;
|
use iced::mouse;
|
||||||
use iced::theme;
|
use iced::theme;
|
||||||
|
use iced::time::{self, Duration, Instant};
|
||||||
use iced::widget::{
|
use iced::widget::{
|
||||||
button, column, container, horizontal_rule, horizontal_space, row, text,
|
button, column, container, horizontal_rule, horizontal_space, row, text,
|
||||||
};
|
};
|
||||||
|
|
@ -502,9 +502,8 @@ mod toast {
|
||||||
self.instants.iter_mut().enumerate().for_each(
|
self.instants.iter_mut().enumerate().for_each(
|
||||||
|(index, maybe_instant)| {
|
|(index, maybe_instant)| {
|
||||||
if let Some(instant) = maybe_instant.as_mut() {
|
if let Some(instant) = maybe_instant.as_mut() {
|
||||||
let remaining =
|
let remaining = time::seconds(self.timeout_secs)
|
||||||
Duration::from_secs(self.timeout_secs)
|
.saturating_sub(instant.elapsed());
|
||||||
.saturating_sub(instant.elapsed());
|
|
||||||
|
|
||||||
if remaining == Duration::ZERO {
|
if remaining == Duration::ZERO {
|
||||||
maybe_instant.take();
|
maybe_instant.take();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
//! Listen and react to time.
|
//! Listen and react to time.
|
||||||
pub use crate::core::time::{Duration, Instant};
|
pub use crate::core::time::*;
|
||||||
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue