2019-12-14 05:56:46 +01:00
|
|
|
//! Listen to external events in your application.
|
2019-12-10 03:43:00 +01:00
|
|
|
use crate::{Event, Hasher};
|
2020-01-20 04:47:36 +01:00
|
|
|
use iced_futures::futures::stream::BoxStream;
|
2019-12-08 08:21:26 +01:00
|
|
|
|
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.
|
|
|
|
|
///
|
|
|
|
|
/// [`Command`]: ../struct.Command.html
|
|
|
|
|
/// [`Subscription`]: struct.Subscription.html
|
2020-01-19 10:17:08 +01:00
|
|
|
pub type Subscription<T> = iced_futures::Subscription<Hasher, Event, 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.
|
|
|
|
|
///
|
|
|
|
|
/// [`Subscription`]: type.Subscription.html
|
|
|
|
|
pub type EventStream = BoxStream<'static, Event>;
|
|
|
|
|
|
2020-01-19 08:36:44 +01:00
|
|
|
/// A native [`Subscription`] tracker.
|
|
|
|
|
///
|
|
|
|
|
/// [`Subscription`]: type.Subscription.html
|
2020-01-19 10:17:08 +01:00
|
|
|
pub type Tracker = iced_futures::subscription::Tracker<Hasher, Event>;
|
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 04:49:13 +01:00
|
|
|
mod events;
|
2019-12-14 04:12:42 +01:00
|
|
|
|
2019-12-14 04:49:13 +01:00
|
|
|
use events::Events;
|
2019-12-14 04:12:42 +01:00
|
|
|
|
2019-12-14 05:56:46 +01:00
|
|
|
/// Returns a [`Subscription`] to all the runtime events.
|
|
|
|
|
///
|
|
|
|
|
/// This subscription will notify your application of any [`Event`] handled by
|
|
|
|
|
/// the runtime.
|
|
|
|
|
///
|
|
|
|
|
/// [`Subscription`]: type.Subscription.html
|
|
|
|
|
/// [`Event`]: ../enum.Event.html
|
2019-12-14 04:49:13 +01:00
|
|
|
pub fn events() -> Subscription<Event> {
|
2020-11-10 02:32:57 +01:00
|
|
|
Subscription::from_recipe(Events { f: Some })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
/// [`Subscription`]: type.Subscription.html
|
|
|
|
|
/// [`Event`]: ../enum.Event.html
|
|
|
|
|
pub fn events_with<Message>(
|
|
|
|
|
f: fn(Event) -> Option<Message>,
|
|
|
|
|
) -> Subscription<Message>
|
|
|
|
|
where
|
|
|
|
|
Message: 'static + Send,
|
|
|
|
|
{
|
|
|
|
|
Subscription::from_recipe(Events { f })
|
2019-12-14 04:12:42 +01:00
|
|
|
}
|