summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/src/time.rs25
-rw-r--r--examples/arc/src/main.rs4
-rw-r--r--examples/clock/src/main.rs4
-rw-r--r--examples/game_of_life/src/main.rs7
-rw-r--r--examples/stopwatch/src/main.rs6
-rw-r--r--examples/the_matrix/src/main.rs4
-rw-r--r--examples/toast/src/main.rs7
-rw-r--r--futures/src/backend/native/tokio.rs60
-rw-r--r--futures/src/subscription.rs26
-rw-r--r--src/lib.rs4
-rw-r--r--src/time.rs2
11 files changed, 88 insertions, 61 deletions
diff --git a/core/src/time.rs b/core/src/time.rs
index dcfe4e41..c6e30444 100644
--- a/core/src/time.rs
+++ b/core/src/time.rs
@@ -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)
+}
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<Message> {
- 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<Message> {
- 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<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,
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<Message> {
- 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();
diff --git a/futures/src/backend/native/tokio.rs b/futures/src/backend/native/tokio.rs
index 9dc3593d..e0be83a6 100644
--- a/futures/src/backend/native/tokio.rs
+++ b/futures/src/backend/native/tokio.rs
@@ -22,40 +22,25 @@ impl crate::Executor for Executor {
pub mod time {
//! Listen and react to time.
- use crate::subscription::{self, Hasher, Subscription};
+ use crate::core::time::{Duration, Instant};
+ use crate::stream;
+ use crate::subscription::Subscription;
+ use crate::MaybeSend;
+
+ use futures::SinkExt;
+ use std::future::Future;
/// Returns a [`Subscription`] that produces messages at a set interval.
///
/// The first message is produced after a `duration`, and then continues to
/// produce more messages every `duration` after that.
- pub fn every(
- duration: std::time::Duration,
- ) -> Subscription<std::time::Instant> {
- subscription::from_recipe(Every(duration))
- }
-
- #[derive(Debug)]
- struct Every(std::time::Duration);
-
- impl subscription::Recipe for Every {
- type Output = std::time::Instant;
-
- fn hash(&self, state: &mut Hasher) {
- use std::hash::Hash;
-
- std::any::TypeId::of::<Self>().hash(state);
- self.0.hash(state);
- }
-
- fn stream(
- self: Box<Self>,
- _input: subscription::EventStream,
- ) -> futures::stream::BoxStream<'static, Self::Output> {
+ pub fn every(duration: Duration) -> Subscription<Instant> {
+ Subscription::run_with(duration, |duration| {
use futures::stream::StreamExt;
- let start = tokio::time::Instant::now() + self.0;
+ let start = tokio::time::Instant::now() + *duration;
- let mut interval = tokio::time::interval_at(start, self.0);
+ let mut interval = tokio::time::interval_at(start, *duration);
interval.set_missed_tick_behavior(
tokio::time::MissedTickBehavior::Skip,
);
@@ -67,6 +52,27 @@ pub mod time {
};
stream.map(tokio::time::Instant::into_std).boxed()
- }
+ })
+ }
+
+ /// Returns a [`Subscription`] that runs the given async function at a
+ /// set interval; producing the result of the function as output.
+ pub fn repeat<F, T>(f: fn() -> F, interval: Duration) -> Subscription<T>
+ where
+ F: Future<Output = T> + MaybeSend + 'static,
+ T: MaybeSend + 'static,
+ {
+ Subscription::run_with((f, interval), |(f, interval)| {
+ let f = *f;
+ let interval = *interval;
+
+ stream::channel(1, move |mut output| async move {
+ loop {
+ let _ = output.send(f().await).await;
+
+ tokio::time::sleep(interval).await;
+ }
+ })
+ })
}
}
diff --git a/futures/src/subscription.rs b/futures/src/subscription.rs
index eaea1a1f..82cba9a1 100644
--- a/futures/src/subscription.rs
+++ b/futures/src/subscription.rs
@@ -202,8 +202,8 @@ impl<T> Subscription<T> {
T: 'static,
{
from_recipe(Runner {
- id: builder,
- spawn: move |_| builder(),
+ data: builder,
+ spawn: |builder, _| builder(),
})
}
@@ -211,15 +211,15 @@ impl<T> Subscription<T> {
/// given [`Stream`].
///
/// The `id` will be used to uniquely identify the [`Subscription`].
- pub fn run_with_id<I, S>(id: I, stream: S) -> Subscription<T>
+ pub fn run_with<D, S>(data: D, builder: fn(&D) -> S) -> Self
where
- I: Hash + 'static,
+ D: Hash + 'static,
S: Stream<Item = T> + MaybeSend + 'static,
T: 'static,
{
from_recipe(Runner {
- id,
- spawn: move |_| stream,
+ data: (data, builder),
+ spawn: |(data, builder), _| builder(data),
})
}
@@ -423,8 +423,8 @@ where
T: 'static + MaybeSend,
{
from_recipe(Runner {
- id,
- spawn: |events| {
+ data: id,
+ spawn: |_, events| {
use futures::future;
use futures::stream::StreamExt;
@@ -435,27 +435,27 @@ where
struct Runner<I, F, S, T>
where
- F: FnOnce(EventStream) -> S,
+ F: FnOnce(&I, EventStream) -> S,
S: Stream<Item = T>,
{
- id: I,
+ data: I,
spawn: F,
}
impl<I, F, S, T> Recipe for Runner<I, F, S, T>
where
I: Hash + 'static,
- F: FnOnce(EventStream) -> S,
+ F: FnOnce(&I, EventStream) -> S,
S: Stream<Item = T> + MaybeSend + 'static,
{
type Output = T;
fn hash(&self, state: &mut Hasher) {
std::any::TypeId::of::<I>().hash(state);
- self.id.hash(state);
+ self.data.hash(state);
}
fn stream(self: Box<Self>, input: EventStream) -> BoxStream<Self::Output> {
- crate::boxed_stream((self.spawn)(input))
+ crate::boxed_stream((self.spawn)(&self.data, input))
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 427e789c..939943a9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -365,9 +365,9 @@
//!
//! As with tasks, some modules expose convenient functions that build a [`Subscription`] for you—like
//! [`time::every`] which can be used to listen to time, or [`keyboard::on_key_press`] which will notify you
-//! of any key presses. But you can also create your own with [`Subscription::run`] and [`run_with_id`].
+//! of any key presses. But you can also create your own with [`Subscription::run`] and [`run_with`].
//!
-//! [`run_with_id`]: Subscription::run_with_id
+//! [`run_with`]: Subscription::run_with
//!
//! ## Scaling Applications
//! The `update`, `view`, and `Message` triplet composes very nicely.
diff --git a/src/time.rs b/src/time.rs
index 26d31c0a..98a800ac 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -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(