2019-12-14 05:56:46 +01:00
|
|
|
//! Listen to external events in your application.
|
2020-11-12 02:22:22 +01:00
|
|
|
use crate::event::{self, Event};
|
|
|
|
|
use crate::Hasher;
|
2022-01-14 19:43:06 +07:00
|
|
|
|
2022-01-14 19:55:27 +07:00
|
|
|
use iced_futures::futures::{self, Future, Stream};
|
2021-11-14 11:04:20 -05:00
|
|
|
use iced_futures::BoxStream;
|
2019-12-08 08:21:26 +01:00
|
|
|
|
2022-01-14 19:43:06 +07:00
|
|
|
use std::hash::Hash;
|
|
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
/// A request to listen to external events.
|
|
|
|
|
///
|
|
|
|
|
/// Besides performing async actions on demand with [`Command`], most
|
|
|
|
|
/// applications also need to listen to external events passively.
|
|
|
|
|
///
|
|
|
|
|
/// A [`Subscription`] is normally provided to some runtime, like a [`Command`],
|
|
|
|
|
/// and it will generate events as long as the user keeps requesting it.
|
|
|
|
|
///
|
|
|
|
|
/// For instance, you can use a [`Subscription`] to listen to a WebSocket
|
|
|
|
|
/// connection, keyboard presses, mouse events, time ticks, etc.
|
|
|
|
|
///
|
2020-11-25 07:11:27 +01:00
|
|
|
/// [`Command`]: crate::Command
|
2020-11-12 02:22:22 +01:00
|
|
|
pub type Subscription<T> =
|
|
|
|
|
iced_futures::Subscription<Hasher, (Event, event::Status), T>;
|
2019-12-14 04:12:42 +01:00
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
/// A stream of runtime events.
|
|
|
|
|
///
|
|
|
|
|
/// It is the input of a [`Subscription`] in the native runtime.
|
2021-11-14 11:04:20 -05:00
|
|
|
pub type EventStream = BoxStream<(Event, event::Status)>;
|
2019-12-14 05:56:46 +01:00
|
|
|
|
2020-01-19 08:36:44 +01:00
|
|
|
/// A native [`Subscription`] tracker.
|
2020-11-12 02:22:22 +01:00
|
|
|
pub type Tracker =
|
|
|
|
|
iced_futures::subscription::Tracker<Hasher, (Event, event::Status)>;
|
2020-01-19 08:36:44 +01:00
|
|
|
|
2020-01-19 10:17:08 +01:00
|
|
|
pub use iced_futures::subscription::Recipe;
|
2019-12-14 04:12:42 +01:00
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
/// Returns a [`Subscription`] to all the runtime events.
|
|
|
|
|
///
|
2020-11-12 02:22:22 +01:00
|
|
|
/// This subscription will notify your application of any [`Event`] that was
|
|
|
|
|
/// not captured by any widget.
|
2019-12-14 04:49:13 +01:00
|
|
|
pub fn events() -> Subscription<Event> {
|
2022-01-14 19:43:06 +07:00
|
|
|
events_with(|event, status| match status {
|
|
|
|
|
event::Status::Ignored => Some(event),
|
|
|
|
|
event::Status::Captured => None,
|
2020-11-12 02:22:22 +01:00
|
|
|
})
|
2020-11-10 02:32:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns a [`Subscription`] that filters all the runtime events with the
|
|
|
|
|
/// provided function, producing messages accordingly.
|
|
|
|
|
///
|
|
|
|
|
/// This subscription will call the provided function for every [`Event`]
|
|
|
|
|
/// handled by the runtime. If the function:
|
|
|
|
|
///
|
|
|
|
|
/// - Returns `None`, the [`Event`] will be discarded.
|
|
|
|
|
/// - Returns `Some` message, the `Message` will be produced.
|
|
|
|
|
pub fn events_with<Message>(
|
2020-11-12 02:22:22 +01:00
|
|
|
f: fn(Event, event::Status) -> Option<Message>,
|
2020-11-10 02:32:57 +01:00
|
|
|
) -> Subscription<Message>
|
|
|
|
|
where
|
|
|
|
|
Message: 'static + Send,
|
|
|
|
|
{
|
2022-01-14 19:43:06 +07:00
|
|
|
Subscription::from_recipe(Runner {
|
2022-01-16 15:50:19 +07:00
|
|
|
id: f,
|
2022-01-17 15:29:41 +07:00
|
|
|
spawn: move |events| {
|
2022-01-14 19:43:06 +07:00
|
|
|
use futures::future;
|
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
|
|
|
|
events.filter_map(move |(event, status)| {
|
|
|
|
|
future::ready(f(event, status))
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 19:43:54 +07:00
|
|
|
/// Returns a [`Subscription`] that will create and asynchronously run the
|
2022-01-17 15:29:41 +07:00
|
|
|
/// given [`Stream`].
|
2022-01-14 19:43:54 +07:00
|
|
|
///
|
2022-01-17 15:29:41 +07:00
|
|
|
/// The `id` will be used to uniquely identify the [`Subscription`].
|
|
|
|
|
pub fn run<I, S, Message>(id: I, stream: S) -> Subscription<Message>
|
2022-01-14 19:43:54 +07:00
|
|
|
where
|
2022-01-16 15:50:19 +07:00
|
|
|
I: Hash + 'static,
|
2022-01-14 19:43:54 +07:00
|
|
|
S: Stream<Item = Message> + Send + 'static,
|
2022-01-16 15:50:19 +07:00
|
|
|
Message: 'static,
|
2022-01-14 19:43:54 +07:00
|
|
|
{
|
|
|
|
|
Subscription::from_recipe(Runner {
|
2022-01-16 15:50:19 +07:00
|
|
|
id,
|
2022-01-17 15:29:41 +07:00
|
|
|
spawn: move |_| stream,
|
2022-01-14 19:43:54 +07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-14 19:55:27 +07:00
|
|
|
/// Returns a [`Subscription`] that will create and asynchronously run a
|
|
|
|
|
/// [`Stream`] that will call the provided closure to produce every `Message`.
|
|
|
|
|
///
|
2022-01-17 15:29:41 +07:00
|
|
|
/// The `id` will be used to uniquely identify the [`Subscription`].
|
2022-01-16 15:50:19 +07:00
|
|
|
pub fn unfold<I, T, Fut, Message>(
|
|
|
|
|
id: I,
|
2022-01-14 19:55:27 +07:00
|
|
|
initial: T,
|
|
|
|
|
mut f: impl FnMut(T) -> Fut + Send + Sync + 'static,
|
|
|
|
|
) -> Subscription<Message>
|
|
|
|
|
where
|
2022-01-16 15:50:19 +07:00
|
|
|
I: Hash + 'static,
|
|
|
|
|
T: Send + 'static,
|
|
|
|
|
Fut: Future<Output = (Option<Message>, T)> + Send + 'static,
|
|
|
|
|
Message: 'static + Send,
|
2022-01-14 19:55:42 +07:00
|
|
|
{
|
2022-01-16 15:50:19 +07:00
|
|
|
use futures::future::{self, FutureExt};
|
2022-01-14 19:55:42 +07:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
2022-01-17 15:29:41 +07:00
|
|
|
run(
|
|
|
|
|
id,
|
2022-01-16 15:50:19 +07:00
|
|
|
futures::stream::unfold(initial, move |state| f(state).map(Some))
|
2022-01-17 15:29:41 +07:00
|
|
|
.filter_map(future::ready),
|
|
|
|
|
)
|
2022-01-14 19:55:42 +07:00
|
|
|
}
|
|
|
|
|
|
2022-01-17 15:29:41 +07:00
|
|
|
struct Runner<I, F, S, Message>
|
2022-01-14 19:43:06 +07:00
|
|
|
where
|
2022-01-17 15:29:41 +07:00
|
|
|
F: FnOnce(EventStream) -> S,
|
2022-01-14 19:43:06 +07:00
|
|
|
S: Stream<Item = Message>,
|
|
|
|
|
{
|
2022-01-16 15:50:19 +07:00
|
|
|
id: I,
|
2022-01-14 19:43:06 +07:00
|
|
|
spawn: F,
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-17 15:29:41 +07:00
|
|
|
impl<I, S, F, Message> Recipe<Hasher, (Event, event::Status)>
|
|
|
|
|
for Runner<I, F, S, Message>
|
2022-01-14 19:43:06 +07:00
|
|
|
where
|
2022-01-16 15:50:19 +07:00
|
|
|
I: Hash + 'static,
|
2022-01-17 15:29:41 +07:00
|
|
|
F: FnOnce(EventStream) -> S,
|
2022-01-14 19:43:06 +07:00
|
|
|
S: Stream<Item = Message> + Send + 'static,
|
|
|
|
|
{
|
|
|
|
|
type Output = Message;
|
|
|
|
|
|
|
|
|
|
fn hash(&self, state: &mut Hasher) {
|
2022-01-17 15:29:41 +07:00
|
|
|
std::any::TypeId::of::<I>().hash(state);
|
2022-01-16 15:50:19 +07:00
|
|
|
self.id.hash(state);
|
2022-01-14 19:43:06 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn stream(self: Box<Self>, input: EventStream) -> BoxStream<Self::Output> {
|
|
|
|
|
use futures::stream::StreamExt;
|
|
|
|
|
|
2022-01-17 15:29:41 +07:00
|
|
|
(self.spawn)(input).boxed()
|
2022-01-14 19:43:06 +07:00
|
|
|
}
|
2019-12-14 04:12:42 +01:00
|
|
|
}
|