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::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::{
|
||||
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<Message> {
|
||||
iced::time::every(std::time::Duration::from_millis(10))
|
||||
.map(|_| Message::Tick)
|
||||
window::frames().map(|_| Message::Tick)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<Message> {
|
||||
time::every(time::Duration::from_millis(500))
|
||||
time::every(milliseconds(500))
|
||||
.map(|_| Message::Tick(chrono::offset::Local::now()))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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<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()
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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<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 {
|
||||
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,8 +502,7 @@ 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)
|
||||
let remaining = time::seconds(self.timeout_secs)
|
||||
.saturating_sub(instant.elapsed());
|
||||
|
||||
if remaining == Duration::ZERO {
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue