iced_futures/
event.rs

1//! Listen to runtime events.
2use crate::MaybeSend;
3use crate::core::event::{self, Event};
4use crate::core::window;
5use crate::subscription::{self, Subscription};
6
7/// Returns a [`Subscription`] to all the ignored runtime events.
8///
9/// This subscription will notify your application of any [`Event`] that was
10/// not captured by any widget.
11pub fn listen() -> Subscription<Event> {
12    listen_with(|event, status, _window| match status {
13        event::Status::Ignored => Some(event),
14        event::Status::Captured => None,
15    })
16}
17
18/// Creates a [`Subscription`] that listens and filters all the runtime events
19/// with the provided function, producing messages accordingly.
20///
21/// This subscription will call the provided function for every [`Event`]
22/// handled by the runtime. If the function:
23///
24/// - Returns `None`, the [`Event`] will be discarded.
25/// - Returns `Some` message, the `Message` will be produced.
26pub fn listen_with<Message>(
27    f: fn(Event, event::Status, window::Id) -> Option<Message>,
28) -> Subscription<Message>
29where
30    Message: 'static + MaybeSend,
31{
32    #[derive(Hash)]
33    struct EventsWith;
34
35    subscription::filter_map((EventsWith, f), move |event| match event {
36        subscription::Event::Interaction {
37            event: Event::Window(window::Event::RedrawRequested(_)),
38            ..
39        }
40        | subscription::Event::SystemThemeChanged(_)
41        | subscription::Event::PlatformSpecific(_) => None,
42        subscription::Event::Interaction {
43            window,
44            event,
45            status,
46        } => f(event, status, window),
47    })
48}
49
50/// Creates a [`Subscription`] that produces a message for every runtime event,
51/// including the redraw request events.
52///
53/// **Warning:** This [`Subscription`], if unfiltered, may produce messages in
54/// an infinite loop.
55pub fn listen_raw<Message>(
56    f: fn(Event, event::Status, window::Id) -> Option<Message>,
57) -> Subscription<Message>
58where
59    Message: 'static + MaybeSend,
60{
61    #[derive(Hash)]
62    struct RawEvents;
63
64    subscription::filter_map((RawEvents, f), move |event| match event {
65        subscription::Event::Interaction {
66            window,
67            event,
68            status,
69        } => f(event, status, window),
70        subscription::Event::SystemThemeChanged(_)
71        | subscription::Event::PlatformSpecific(_) => None,
72    })
73}
74
75/// Creates a [`Subscription`] that notifies of custom application URL
76/// received from the system.
77///
78/// _**Note:** Currently, it only triggers on macOS and the executable needs to be properly [bundled]!_
79///
80/// [bundled]: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW19
81pub fn listen_url() -> Subscription<String> {
82    #[derive(Hash)]
83    struct ListenUrl;
84
85    subscription::filter_map(ListenUrl, move |event| match event {
86        subscription::Event::PlatformSpecific(
87            subscription::PlatformSpecific::MacOS(
88                subscription::MacOS::ReceivedUrl(url),
89            ),
90        ) => Some(url),
91        _ => None,
92    })
93}