Skip to content

Commit 09211f8

Browse files
committed
event_loop: group core API in EventLoopProvider
This helps with portability and defines some top-level structure around the event loop, so in the future, backends can get an idea of what API to use. This also changes the API to be object safe by using `dyn` throughout.
1 parent e512b7d commit 09211f8

File tree

15 files changed

+292
-253
lines changed

15 files changed

+292
-253
lines changed

examples/application.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use winit::application::ApplicationHandler;
1919
use winit::dpi::{LogicalSize, PhysicalPosition, PhysicalSize};
2020
use winit::error::RequestError;
2121
use winit::event::{DeviceEvent, DeviceId, Ime, MouseButton, MouseScrollDelta, WindowEvent};
22-
use winit::event_loop::{ActiveEventLoop, EventLoop};
22+
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
2323
use winit::icon::RgbaIcon;
2424
use winit::keyboard::{Key, ModifiersState};
2525
use winit::monitor::Fullscreen;
@@ -75,7 +75,7 @@ fn main() -> Result<(), Box<dyn Error>> {
7575
});
7676
}
7777

78-
let app = Application::new(&event_loop, receiver, sender);
78+
let app = Box::new(Application::new(&event_loop, receiver, sender));
7979
Ok(event_loop.run_app(app)?)
8080
}
8181

examples/child_window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn main() -> Result<(), impl std::error::Error> {
66
use winit::application::ApplicationHandler;
77
use winit::dpi::{LogicalPosition, LogicalSize, Position};
88
use winit::event::{ElementState, KeyEvent, WindowEvent};
9-
use winit::event_loop::{ActiveEventLoop, EventLoop};
9+
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
1010
use winit::raw_window_handle::HasRawWindowHandle;
1111
use winit::window::{Window, WindowAttributes, WindowId};
1212

@@ -117,7 +117,7 @@ fn main() -> Result<(), impl std::error::Error> {
117117
}
118118

119119
let event_loop = EventLoop::new().unwrap();
120-
event_loop.run_app(Application::default())
120+
event_loop.run_app(Box::new(Application::default()))
121121
}
122122

123123
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]

examples/control_flow.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use ::tracing::{info, warn};
99
use web_time as time;
1010
use winit::application::ApplicationHandler;
1111
use winit::event::{ElementState, KeyEvent, StartCause, WindowEvent};
12-
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
12+
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProvider};
1313
use winit::keyboard::{Key, NamedKey};
1414
use winit::window::{Window, WindowAttributes, WindowId};
1515

@@ -43,7 +43,7 @@ fn main() -> Result<(), impl std::error::Error> {
4343

4444
let event_loop = EventLoop::new().unwrap();
4545

46-
event_loop.run_app(ControlFlowDemo::default())
46+
event_loop.run_app(Box::new(ControlFlowDemo::default()))
4747
}
4848

4949
#[derive(Default, Debug)]

examples/dnd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22

33
use winit::application::ApplicationHandler;
44
use winit::event::WindowEvent;
5-
use winit::event_loop::{ActiveEventLoop, EventLoop};
5+
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
66
use winit::window::{Window, WindowAttributes, WindowId};
77

88
#[path = "util/fill.rs"]
@@ -15,7 +15,7 @@ fn main() -> Result<(), Box<dyn Error>> {
1515

1616
let event_loop = EventLoop::new()?;
1717

18-
let app = Application::new();
18+
let app = Box::new(Application::new());
1919
Ok(event_loop.run_app(app)?)
2020
}
2121

examples/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::error::Error;
44

55
use winit::application::ApplicationHandler;
66
use winit::event::WindowEvent;
7-
use winit::event_loop::{ActiveEventLoop, EventLoop};
7+
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
88
#[cfg(web_platform)]
99
use winit::platform::web::WindowAttributesExtWeb;
1010
use winit::window::{Window, WindowAttributes, WindowId};
@@ -77,7 +77,7 @@ fn main() -> Result<(), Box<dyn Error>> {
7777
let event_loop = EventLoop::new()?;
7878

7979
// For alternative loop run options see `pump_events` and `run_on_demand` examples.
80-
event_loop.run_app(App::default())?;
80+
event_loop.run_app(Box::new(App::default()))?;
8181

8282
Ok(())
8383
}

examples/x11_embed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::error::Error;
55
fn main() -> Result<(), Box<dyn Error>> {
66
use winit::application::ApplicationHandler;
77
use winit::event::WindowEvent;
8-
use winit::event_loop::{ActiveEventLoop, EventLoop};
8+
use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
99
use winit::platform::x11::WindowAttributesExtX11;
1010
use winit::window::{Window, WindowAttributes, WindowId};
1111

@@ -59,7 +59,7 @@ fn main() -> Result<(), Box<dyn Error>> {
5959
tracing_subscriber::fmt::init();
6060
let event_loop = EventLoop::new()?;
6161

62-
Ok(event_loop.run_app(XEmbedDemo { parent_window_id, window: None })?)
62+
Ok(event_loop.run_app(Box::new(XEmbedDemo { parent_window_id, window: None }))?)
6363
}
6464

6565
#[cfg(not(x11_platform))]

src/application.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ use crate::window::WindowId;
88

99
/// The handler of application-level events.
1010
///
11-
/// See [the top-level docs] for example usage, and [`EventLoop::run_app`] for an overview of when
12-
/// events are delivered.
11+
/// See [the top-level docs] for example usage, and [`EventLoopProvider::run_app`] for an overview
12+
/// of when events are delivered.
1313
///
1414
/// This is [dropped] when the event loop is shut down. Note that this only works if you're passing
15-
/// the entire state to [`EventLoop::run_app`] (passing `&mut app` won't work).
15+
/// the entire state to [`EventLoopProvider::run_app`] (passing `&mut app` won't work).
1616
///
1717
/// [the top-level docs]: crate
18-
/// [`EventLoop::run_app`]: crate::event_loop::EventLoop::run_app
18+
/// [`EventLoopProvider::run_app`]: crate::event_loop::EventLoopProvider::run_app
1919
/// [dropped]: std::ops::Drop
2020
pub trait ApplicationHandler {
2121
/// Emitted when new events arrive from the OS to be processed.
@@ -136,7 +136,7 @@ pub trait ApplicationHandler {
136136
/// use std::time::Duration;
137137
///
138138
/// use winit::application::ApplicationHandler;
139-
/// use winit::event_loop::{ActiveEventLoop, EventLoop};
139+
/// use winit::event_loop::{ActiveEventLoop, EventLoop, EventLoopProvider};
140140
///
141141
/// struct MyApp {
142142
/// receiver: mpsc::Receiver<u64>,
@@ -189,7 +189,7 @@ pub trait ApplicationHandler {
189189
/// }
190190
/// });
191191
///
192-
/// event_loop.run_app(MyApp { receiver })?;
192+
/// event_loop.run_app(Box::new(MyApp { receiver }))?;
193193
///
194194
/// background_thread.join().unwrap();
195195
///
@@ -210,9 +210,9 @@ pub trait ApplicationHandler {
210210

211211
/// Emitted when the OS sends an event to a device.
212212
///
213-
/// For this to be called, it must be enabled with [`EventLoop::listen_device_events`].
213+
/// For this to be called, it must be enabled with [`EventLoopProvider::listen_device_events`].
214214
///
215-
/// [`EventLoop::listen_device_events`]: crate::event_loop::EventLoop::listen_device_events
215+
/// [`EventLoopProvider::listen_device_events`]: crate::event_loop::EventLoopProvider::listen_device_events
216216
fn device_event(
217217
&mut self,
218218
event_loop: &dyn ActiveEventLoop,

src/changelog/unreleased.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ changelog entry.
195195
- Renamed "super" key to "meta", to match the naming in the W3C specification.
196196
`NamedKey::Super` still exists, but it's non-functional and deprecated, `NamedKey::Meta` should be used instead.
197197
- Move `IconExtWindows` into `WinIcon`.
198+
- `run_app_on_demand`/`pump_app_events` now accept `&mut dyn ApplicationHandler` instead of generic.
199+
- Moved common `EventLoop` methods like `run_app` into `EventLoopProvider` trait.
200+
- Moved `event_loop::EventLoop` into `platform::event_loop::EventLoop` keeping the old re-export in place.
201+
- `EventLoopProvider::run_app` now takes `Box<dyn ApplicationHandler` instead of owned generic.
198202

199203
### Removed
200204

0 commit comments

Comments
 (0)