Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace EventLoopWindowTarget with ActiveEventLoop #3299

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Unreleased` header.

# Unreleased

- **Breaking:** Removed unnecessary generic parameter `T` from `EventLoopWindowTarget`.
- **Breaking:** Removed `EventLoopWindowTarget<T>`, and replaced it with `ActiveEventLoop<'app>`, which allows Winit to clearly define a boundary between what is possible when the application is not running, and when it is.
- Added `MaybeActiveEventLoop<'app>` trait to help be generic over `EventLoop<T>` and `ActiveEventLoop<'app>`.
- Added `ActiveEventLoopExtWayland` and `ActiveEventLoopExtX11` to mimic the similar traits on the `EventLoop`.
- On Windows, macOS, X11, Wayland and Web, implement setting images as cursors. See the `custom_cursors.rs` example.
- **Breaking:** Remove `Window::set_cursor_icon`
- Add `WindowBuilder::with_cursor` and `Window::set_cursor` which takes a `CursorIcon` or `CustomCursor`
Expand Down
10 changes: 5 additions & 5 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ fn main() -> Result<(), impl std::error::Error> {
use winit::{
dpi::{LogicalPosition, LogicalSize, Position},
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{EventLoop, EventLoopWindowTarget},
event_loop::{ActiveEventLoop, EventLoop},
raw_window_handle::HasRawWindowHandle,
window::{Window, WindowBuilder, WindowId},
};

fn spawn_child_window(
parent: &Window,
event_loop: &EventLoopWindowTarget,
event_loop: ActiveEventLoop<'_>,
windows: &mut HashMap<WindowId, Window>,
) {
let parent = parent.raw_window_handle().unwrap();
Expand Down Expand Up @@ -53,12 +53,12 @@ fn main() -> Result<(), impl std::error::Error> {

println!("parent window: {parent_window:?})");

event_loop.run(move |event: Event<()>, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, window_id } = event {
match event {
WindowEvent::CloseRequested => {
windows.clear();
elwt.exit();
event_loop.exit();
}
WindowEvent::CursorEntered { device_id: _ } => {
// On x11, println when the cursor entered in a window even if the child window is created
Expand All @@ -75,7 +75,7 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => {
spawn_child_window(&parent_window, elwt, &mut windows);
spawn_child_window(&parent_window, event_loop, &mut windows);
}
WindowEvent::RedrawRequested => {
if let Some(window) = windows.get(&window_id) {
Expand Down
10 changes: 5 additions & 5 deletions examples/control_flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn main() -> Result<(), impl std::error::Error> {
let mut wait_cancelled = false;
let mut close_requested = false;

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
use winit::event::StartCause;
println!("{event:?}");
match event {
Expand Down Expand Up @@ -104,22 +104,22 @@ fn main() -> Result<(), impl std::error::Error> {
}

match mode {
Mode::Wait => elwt.set_control_flow(ControlFlow::Wait),
Mode::Wait => event_loop.set_control_flow(ControlFlow::Wait),
Mode::WaitUntil => {
if !wait_cancelled {
elwt.set_control_flow(ControlFlow::WaitUntil(
event_loop.set_control_flow(ControlFlow::WaitUntil(
time::Instant::now() + WAIT_TIME,
));
}
}
Mode::Poll => {
thread::sleep(POLL_SLEEP_TIME);
elwt.set_control_flow(ControlFlow::Poll);
event_loop.set_control_flow(ControlFlow::Poll);
}
};

if close_requested {
elwt.exit();
event_loop.exit();
}
}
_ => (),
Expand Down
4 changes: 2 additions & 2 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ fn main() -> Result<(), impl std::error::Error> {

let mut cursor_idx = 0;

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::KeyboardInput {
Expand All @@ -42,7 +42,7 @@ fn main() -> Result<(), impl std::error::Error> {
fill::fill_window(&window);
}
WindowEvent::CloseRequested => {
elwt.exit();
event_loop.exit();
}
_ => (),
}
Expand Down
6 changes: 3 additions & 3 deletions examples/cursor_grab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn main() -> Result<(), impl std::error::Error> {

let mut modifiers = ModifiersState::default();

event_loop.run(move |event, elwt| match event {
event_loop.run(move |event, event_loop| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
Expand All @@ -36,7 +36,7 @@ fn main() -> Result<(), impl std::error::Error> {
} => {
let result = match key {
Key::Named(NamedKey::Escape) => {
elwt.exit();
event_loop.exit();
Ok(())
}
Key::Character(ch) => match ch.to_lowercase().as_str() {
Expand Down
17 changes: 8 additions & 9 deletions examples/custom_cursors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use simple_logger::SimpleLogger;
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::{EventLoop, EventLoopWindowTarget},
event_loop::EventLoop,
keyboard::Key,
window::{CursorIcon, CustomCursor, WindowBuilder},
};
Expand All @@ -18,14 +18,13 @@ use {
#[cfg(web_platform)]
static COUNTER: AtomicU64 = AtomicU64::new(0);

fn decode_cursor(bytes: &[u8], window_target: &EventLoopWindowTarget) -> CustomCursor {
fn decode_cursor(bytes: &[u8], event_loop: &EventLoop<()>) -> CustomCursor {
let img = image::load_from_memory(bytes).unwrap().to_rgba8();
let samples = img.into_flat_samples();
let (_, w, h) = samples.extents();
let (w, h) = (w as u16, h as u16);
let builder = CustomCursor::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap();

builder.build(window_target)
builder.build(event_loop)
}

#[cfg(not(web_platform))]
Expand Down Expand Up @@ -58,7 +57,7 @@ fn main() -> Result<(), impl std::error::Error> {
decode_cursor(include_bytes!("data/cross2.png"), &event_loop),
];

event_loop.run(move |event, _elwt| match event {
event_loop.run(move |event, _event_loop| match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::KeyboardInput {
event:
Expand Down Expand Up @@ -95,7 +94,7 @@ fn main() -> Result<(), impl std::error::Error> {
64,
64,
)
.build(_elwt),
.build(_event_loop),
);
}
#[cfg(web_platform)]
Expand All @@ -115,11 +114,11 @@ fn main() -> Result<(), impl std::error::Error> {
64,
64,
)
.build(_elwt),
.build(_event_loop),
],
)
.unwrap()
.build(_elwt),
.build(_event_loop),
);
}
_ => {}
Expand All @@ -130,7 +129,7 @@ fn main() -> Result<(), impl std::error::Error> {
}
WindowEvent::CloseRequested => {
#[cfg(not(web_platform))]
_elwt.exit();
_event_loop.exit();
}
_ => (),
},
Expand Down
4 changes: 2 additions & 2 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ fn main() -> Result<(), impl std::error::Error> {
}
});

event_loop.run(move |event, elwt| match event {
event_loop.run(move |event, event_loop| match event {
Event::UserEvent(event) => println!("user event: {event:?}"),
Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} => elwt.exit(),
} => event_loop.exit(),
Event::WindowEvent {
event: WindowEvent::RedrawRequested,
..
Expand Down
4 changes: 2 additions & 2 deletions examples/drag_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ fn main() -> Result<(), impl std::error::Error> {
let mut entered_id = window_2.id();
let mut cursor_location = None;

event_loop.run(move |event, elwt| match event {
event_loop.run(move |event, event_loop| match event {
Event::NewEvents(StartCause::Init) => {
eprintln!("Switch which window is to be dragged by pressing \"x\".")
}
Event::WindowEvent { event, window_id } => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::CursorMoved { position, .. } => cursor_location = Some(position),
WindowEvent::MouseInput { state, button, .. } => {
let window = if (window_id == window_1.id() && switched)
Expand Down
6 changes: 3 additions & 3 deletions examples/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn main() -> Result<(), impl std::error::Error> {
.unwrap();

let mut deadline = time::Instant::now() + time::Duration::from_secs(3);
event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
match event {
Event::NewEvents(StartCause::ResumeTimeReached { .. }) => {
// Timeout reached; focus the window.
Expand All @@ -36,7 +36,7 @@ fn main() -> Result<(), impl std::error::Error> {
window.focus_window();
}
Event::WindowEvent { event, window_id } if window_id == window.id() => match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::RedrawRequested => {
// Notify the windowing system that we'll be presenting to the window.
window.pre_present_notify();
Expand All @@ -51,6 +51,6 @@ fn main() -> Result<(), impl std::error::Error> {
_ => (),
}

elwt.set_control_flow(winit::event_loop::ControlFlow::WaitUntil(deadline));
event_loop.set_control_flow(winit::event_loop::ControlFlow::WaitUntil(deadline));
})
}
14 changes: 8 additions & 6 deletions examples/fullscreen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ fn main() -> Result<(), impl std::error::Error> {
println!("- I\tToggle mIn size limit");
println!("- A\tToggle mAx size limit");

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::KeyboardInput {
event:
KeyEvent {
Expand All @@ -65,7 +65,7 @@ fn main() -> Result<(), impl std::error::Error> {
},
..
} => match key {
Key::Named(NamedKey::Escape) => elwt.exit(),
Key::Named(NamedKey::Escape) => event_loop.exit(),
// WARNING: Consider using `key_without_modifers()` if available on your platform.
// See the `key_binding` example
Key::Character(ch) => match ch.to_lowercase().as_str() {
Expand All @@ -88,12 +88,14 @@ fn main() -> Result<(), impl std::error::Error> {
}
"s" => {
monitor_index += 1;
if let Some(mon) = elwt.available_monitors().nth(monitor_index) {
if let Some(mon) = event_loop.available_monitors().nth(monitor_index) {
monitor = mon;
} else {
monitor_index = 0;
monitor =
elwt.available_monitors().next().expect("no monitor found!");
monitor = event_loop
.available_monitors()
.next()
.expect("no monitor found!");
}
println!("Monitor: {:?}", monitor.name());

Expand Down
4 changes: 2 additions & 2 deletions examples/handling_close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() -> Result<(), impl std::error::Error> {

let mut close_requested = false;

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => {
Expand Down Expand Up @@ -64,7 +64,7 @@ fn main() -> Result<(), impl std::error::Error> {
// event loop (i.e. if it's a multi-window application), you need to
// drop the window. That closes it, and results in `Destroyed` being
// sent.
elwt.exit();
event_loop.exit();
}
}
Key::Character("n") => {
Expand Down
4 changes: 2 additions & 2 deletions examples/ime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ fn main() -> Result<(), impl std::error::Error> {
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
let mut ime_pos = PhysicalPosition::new(0.0, 0.0);

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::CursorMoved { position, .. } => {
cursor_position = position;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/key_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ fn main() -> Result<(), impl std::error::Error> {

let mut modifiers = ModifiersState::default();

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::ModifiersChanged(new) => {
modifiers = new.state();
}
Expand Down
4 changes: 2 additions & 2 deletions examples/mouse_wheel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ In both cases the example window should move like the content of a scroll area i
In other words, the deltas indicate the direction in which to move the content (in this case the window)."
);

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => elwt.exit(),
WindowEvent::CloseRequested => event_loop.exit(),
WindowEvent::MouseWheel { delta, .. } => match delta {
winit::event::MouseScrollDelta::LineDelta(x, y) => {
println!("mouse wheel Line Delta: ({x},{y})");
Expand Down
4 changes: 2 additions & 2 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ fn main() -> Result<(), impl std::error::Error> {
}
});
}
event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if window_senders.is_empty() {
elwt.exit()
event_loop.exit()
}
match event {
Event::WindowEvent { event, window_id } => match event {
Expand Down
8 changes: 4 additions & 4 deletions examples/multiwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn main() -> Result<(), impl std::error::Error> {

println!("Press N to open a new window.");

event_loop.run(move |event, elwt| {
event_loop.run(move |event, event_loop| {
if let Event::WindowEvent { event, window_id } = event {
match event {
WindowEvent::CloseRequested => {
Expand All @@ -36,17 +36,17 @@ fn main() -> Result<(), impl std::error::Error> {
windows.remove(&window_id);

if windows.is_empty() {
elwt.exit();
event_loop.exit();
}
}
WindowEvent::KeyboardInput {
event,
is_synthetic: false,
..
} if event.state == ElementState::Pressed => match event.logical_key {
Key::Named(NamedKey::Escape) => elwt.exit(),
Key::Named(NamedKey::Escape) => event_loop.exit(),
Key::Character(c) if c == "n" || c == "N" => {
let window = Window::new(elwt).unwrap();
let window = Window::new(event_loop).unwrap();
println!("Opened a new window: {:?}", window.id());
windows.insert(window.id(), window);
}
Expand Down
Loading