Skip to content

Commit e6d0b3b

Browse files
authored
Merge pull request #2456 from iced-rs/window-id-in-event-subscriptions
Introduce `window::Id` to `Event` subscriptions
2 parents bda0156 + 6ea7846 commit e6d0b3b

25 files changed

Lines changed: 225 additions & 198 deletions

File tree

core/src/event.rs

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,10 @@ pub enum Event {
1919
Mouse(mouse::Event),
2020

2121
/// A window event
22-
Window(window::Id, window::Event),
22+
Window(window::Event),
2323

2424
/// A touch event
2525
Touch(touch::Event),
26-
27-
/// A platform specific event
28-
PlatformSpecific(PlatformSpecific),
29-
}
30-
31-
/// A platform specific event
32-
#[derive(Debug, Clone, PartialEq, Eq)]
33-
pub enum PlatformSpecific {
34-
/// A MacOS specific event
35-
MacOS(MacOS),
36-
}
37-
38-
/// Describes an event specific to MacOS
39-
#[derive(Debug, Clone, PartialEq, Eq)]
40-
pub enum MacOS {
41-
/// Triggered when the app receives an URL from the system
42-
///
43-
/// _**Note:** For this event to be triggered, the executable needs to be properly [bundled]!_
44-
///
45-
/// [bundled]: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW19
46-
ReceivedUrl(String),
4726
}
4827

4928
/// The status of an [`Event`] after being processed.

examples/events/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,8 @@ impl Events {
3737
Command::none()
3838
}
3939
Message::EventOccurred(event) => {
40-
if let Event::Window(id, window::Event::CloseRequested) = event
41-
{
42-
window::close(id)
40+
if let Event::Window(window::Event::CloseRequested) = event {
41+
window::close(window::Id::MAIN)
4342
} else {
4443
Command::none()
4544
}

examples/integration/src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use iced_wgpu::{wgpu, Engine, Renderer};
99
use iced_winit::conversion;
1010
use iced_winit::core::mouse;
1111
use iced_winit::core::renderer;
12-
use iced_winit::core::window;
1312
use iced_winit::core::{Color, Font, Pixels, Size, Theme};
1413
use iced_winit::futures;
1514
use iced_winit::runtime::program;
@@ -317,7 +316,6 @@ pub fn main() -> Result<(), winit::error::EventLoopError> {
317316

318317
// Map window event to iced event
319318
if let Some(event) = iced_winit::conversion::window_event(
320-
window::Id::MAIN,
321319
event,
322320
window.scale_factor(),
323321
*modifiers,

examples/loading_spinners/src/circular.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ where
275275
) -> event::Status {
276276
let state = tree.state.downcast_mut::<State>();
277277

278-
if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
278+
if let Event::Window(window::Event::RedrawRequested(now)) = event {
279279
state.animation = state.animation.timed_transition(
280280
self.cycle_duration,
281281
self.rotation_duration,

examples/loading_spinners/src/linear.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ where
189189
) -> event::Status {
190190
let state = tree.state.downcast_mut::<State>();
191191

192-
if let Event::Window(_, window::Event::RedrawRequested(now)) = event {
192+
if let Event::Window(window::Event::RedrawRequested(now)) = event {
193193
*state = state.timed_transition(self.cycle_duration, now);
194194

195195
shell.request_redraw(RedrawRequest::NextFrame);

examples/multi_window/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,16 +145,18 @@ impl multi_window::Application for Example {
145145
}
146146

147147
fn subscription(&self) -> Subscription<Self::Message> {
148-
event::listen_with(|event, _| {
149-
if let iced::Event::Window(id, window_event) = event {
148+
event::listen_with(|event, _, window| {
149+
if let iced::Event::Window(window_event) = event {
150150
match window_event {
151151
window::Event::CloseRequested => {
152-
Some(Message::CloseWindow(id))
152+
Some(Message::CloseWindow(window))
153153
}
154154
window::Event::Opened { position, .. } => {
155-
Some(Message::WindowOpened(id, position))
155+
Some(Message::WindowOpened(window, position))
156+
}
157+
window::Event::Closed => {
158+
Some(Message::WindowClosed(window))
156159
}
157-
window::Event::Closed => Some(Message::WindowClosed(id)),
158160
_ => None,
159161
}
160162
} else {

examples/toast/src/main.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,7 @@ mod toast {
499499
clipboard: &mut dyn Clipboard,
500500
shell: &mut Shell<'_, Message>,
501501
) -> event::Status {
502-
if let Event::Window(_, window::Event::RedrawRequested(now)) =
503-
&event
504-
{
502+
if let Event::Window(window::Event::RedrawRequested(now)) = &event {
505503
let mut next_redraw: Option<window::RedrawRequest> = None;
506504

507505
self.instants.iter_mut().enumerate().for_each(

examples/url_handler/src/main.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use iced::event::{self, Event};
1+
use iced::event;
22
use iced::widget::{center, text};
33
use iced::{Element, Subscription};
44

@@ -15,27 +15,20 @@ struct App {
1515

1616
#[derive(Debug, Clone)]
1717
enum Message {
18-
EventOccurred(Event),
18+
UrlReceived(String),
1919
}
2020

2121
impl App {
2222
fn update(&mut self, message: Message) {
2323
match message {
24-
Message::EventOccurred(event) => {
25-
if let Event::PlatformSpecific(
26-
event::PlatformSpecific::MacOS(event::MacOS::ReceivedUrl(
27-
url,
28-
)),
29-
) = event
30-
{
31-
self.url = Some(url);
32-
}
24+
Message::UrlReceived(url) => {
25+
self.url = Some(url);
3326
}
3427
}
3528
}
3629

3730
fn subscription(&self) -> Subscription<Message> {
38-
event::listen().map(Message::EventOccurred)
31+
event::listen_url().map(Message::UrlReceived)
3932
}
4033

4134
fn view(&self) -> Element<Message> {

examples/visible_bounds/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ impl Example {
145145
}
146146

147147
fn subscription(&self) -> Subscription<Message> {
148-
event::listen_with(|event, _| match event {
148+
event::listen_with(|event, _status, _window| match event {
149149
Event::Mouse(mouse::Event::CursorMoved { position }) => {
150150
Some(Message::MouseMoved(position))
151151
}
152-
Event::Window(_, window::Event::Resized { .. }) => {
152+
Event::Window(window::Event::Resized { .. }) => {
153153
Some(Message::WindowResized)
154154
}
155155
_ => None,

futures/src/event.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::MaybeSend;
99
/// This subscription will notify your application of any [`Event`] that was
1010
/// not captured by any widget.
1111
pub fn listen() -> Subscription<Event> {
12-
listen_with(|event, status| match status {
12+
listen_with(|event, status, _window| match status {
1313
event::Status::Ignored => Some(event),
1414
event::Status::Captured => None,
1515
})
@@ -24,21 +24,26 @@ pub fn listen() -> Subscription<Event> {
2424
/// - Returns `None`, the [`Event`] will be discarded.
2525
/// - Returns `Some` message, the `Message` will be produced.
2626
pub fn listen_with<Message>(
27-
f: fn(Event, event::Status) -> Option<Message>,
27+
f: fn(Event, event::Status, window::Id) -> Option<Message>,
2828
) -> Subscription<Message>
2929
where
3030
Message: 'static + MaybeSend,
3131
{
3232
#[derive(Hash)]
3333
struct EventsWith;
3434

35-
subscription::filter_map(
36-
(EventsWith, f),
37-
move |event, status| match event {
38-
Event::Window(_, window::Event::RedrawRequested(_)) => None,
39-
_ => f(event, status),
40-
},
41-
)
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::PlatformSpecific(_) => None,
41+
subscription::Event::Interaction {
42+
window,
43+
event,
44+
status,
45+
} => f(event, status, window),
46+
})
4247
}
4348

4449
/// Creates a [`Subscription`] that produces a message for every runtime event,
@@ -47,13 +52,40 @@ where
4752
/// **Warning:** This [`Subscription`], if unfiltered, may produce messages in
4853
/// an infinite loop.
4954
pub fn listen_raw<Message>(
50-
f: fn(Event, event::Status) -> Option<Message>,
55+
f: fn(Event, event::Status, window::Id) -> Option<Message>,
5156
) -> Subscription<Message>
5257
where
5358
Message: 'static + MaybeSend,
5459
{
5560
#[derive(Hash)]
5661
struct RawEvents;
5762

58-
subscription::filter_map((RawEvents, f), f)
63+
subscription::filter_map((RawEvents, f), move |event| match event {
64+
subscription::Event::Interaction {
65+
window,
66+
event,
67+
status,
68+
} => f(event, status, window),
69+
subscription::Event::PlatformSpecific(_) => None,
70+
})
71+
}
72+
73+
/// Creates a [`Subscription`] that notifies of custom application URL
74+
/// received from the system.
75+
///
76+
/// _**Note:** Currently, it only triggers on macOS and the executable needs to be properly [bundled]!_
77+
///
78+
/// [bundled]: https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW19
79+
pub fn listen_url() -> Subscription<String> {
80+
#[derive(Hash)]
81+
struct ListenUrl;
82+
83+
subscription::filter_map(ListenUrl, move |event| match event {
84+
subscription::Event::PlatformSpecific(
85+
subscription::PlatformSpecific::MacOS(
86+
subscription::MacOS::ReceivedUrl(url),
87+
),
88+
) => Some(url),
89+
_ => None,
90+
})
5991
}

0 commit comments

Comments
 (0)