Skip to content

Commit c778d4a

Browse files
committed
Re-work event loop run APIs
Overall this re-works the APIs for how an `EventLoop` is run to cover these use-cases, with varying portability caveats: 1. A portable `run()` API that consumes the `EventLoop` and runs the loop on the calling thread until the app exits. This can be supported across _all_ platforms and compared to the previous `run() -> !` API is now able to return a `Result` status on all platforms except iOS and Web. Fixes: #2709 2. A less portable `run_ondmand()` API that covers the use case in #2431 where applications need to be able to re-run a Winit application multiple times against a persistent `EventLoop`. This doesn't allow `Window` state to carry across separate runs of the loop, but does allow orthogonal re-instantiations of a gui application. Available On: Windows, Linux, MacOS Could be supported on Android if there's a use case Incompatible with iOS, Web Fixes: #2431 3. A less portable (and on MacOS, likely less-optimal) `pump_events()` API that covers the use case in #2706 and allows a Winit event loop to be embedded within an external `loop {}`. Applications call `pump_events()` once per iteration of their own external loop to dispatch all pending Winit events, without blocking the external loop. Available On: Windows, Linux, MacOS, Android Incompatible With: iOS, Web Fixes: #2706 Each method of running the loop will behave consistently in terms of how `NewEvents(Init)`, `Resumed` and `LoopDestroyed` events are dispatched (so portable application code wouldn't necessarily need to have any awareness of which method of running the loop was being used) In particular by moving away from `run() -> !` we stop calling `std::process::exit()` internally as a means to kill the process without returning which means it's possible to return an exit status and applications can return from their `main()` function normally. This also fixes Android support where an Activity runs in a thread but we can't assume to have full ownership of the process (other services could be running in separate threads). `run_return` has been removed, and the overlapping use cases that `run_return` previously partially aimed to support have been split across `run_ondemand` and `pump_events`. To help test the changes this adds: examples/window_ondemand examples/window_pump_events examples/window_pump_events_rfd The last _rfd example, tests the interaction between the `rfd` crate and using `pump_events` to embed Winit within an external event loop. Additionally all examples have generally been updated so that `main()` returns a `Result` from `run()` Fixes: #2709 Fixes: #2706 Fixes: #2431 Fixes: #2752 TODO: X11/Wayland backends TODO: ask someone to test orbital changes, made blindly TODO: split patch up
1 parent 2486f0f commit c778d4a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1071
-736
lines changed

Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ serde = { version = "1", optional = true, features = ["serde_derive"] }
6161
image = { version = "0.24.0", default-features = false, features = ["png"] }
6262
simple_logger = { version = "2.1.0", default_features = false }
6363

64+
[target.'cfg(any(target_os = "windows", target_os = "macos", target_os = "linux"))'.dev-dependencies]
65+
rfd = { version = "0.11.0" }
66+
6467
[target.'cfg(target_os = "android")'.dependencies]
6568
# Coordinate the next winit release with android-ndk-rs: https://github.com/rust-windowing/winit/issues/1995
6669
android-activity = "0.4.0"

examples/child_window.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(any(x11_platform, macos_platform, windows_platform))]
2-
fn main() {
2+
fn main() -> std::result::Result<(), impl std::error::Error> {
33
use std::collections::HashMap;
44

55
use raw_window_handle::HasRawWindowHandle;

examples/control_flow.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ enum Mode {
1919
const WAIT_TIME: time::Duration = time::Duration::from_millis(100);
2020
const POLL_SLEEP_TIME: time::Duration = time::Duration::from_millis(100);
2121

22-
fn main() {
22+
fn main() -> std::result::Result<(), impl std::error::Error> {
2323
SimpleLogger::new().init().unwrap();
2424

2525
println!("Press '1' to switch to Wait mode.");
@@ -110,5 +110,5 @@ fn main() {
110110
}
111111
_ => (),
112112
}
113-
});
113+
})
114114
}

examples/cursor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::{CursorIcon, WindowBuilder},
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -48,7 +48,7 @@ fn main() {
4848
}
4949
_ => (),
5050
}
51-
});
51+
})
5252
}
5353

5454
const CURSORS: &[CursorIcon] = &[

examples/cursor_grab.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::{CursorGrabMode, WindowBuilder},
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -66,5 +66,5 @@ fn main() {
6666
},
6767
_ => (),
6868
}
69-
});
69+
})
7070
}

examples/custom_events.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::single_match)]
22

33
#[cfg(not(wasm_platform))]
4-
fn main() {
4+
fn main() -> std::result::Result<(), impl std::error::Error> {
55
use simple_logger::SimpleLogger;
66
use winit::{
77
event::{Event, WindowEvent},
@@ -46,7 +46,7 @@ fn main() {
4646
} => control_flow.set_exit(),
4747
_ => (),
4848
}
49-
});
49+
})
5050
}
5151

5252
#[cfg(wasm_platform)]

examples/drag_window.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use winit::{
99
window::{Window, WindowBuilder, WindowId},
1010
};
1111

12-
fn main() {
12+
fn main() -> std::result::Result<(), impl std::error::Error> {
1313
SimpleLogger::new().init().unwrap();
1414
let event_loop = EventLoop::new();
1515

@@ -60,7 +60,7 @@ fn main() {
6060
_ => (),
6161
},
6262
_ => (),
63-
});
63+
})
6464
}
6565

6666
fn name_windows(window_id: WindowId, switched: bool, window_1: &Window, window_2: &Window) {

examples/fullscreen.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use winit::window::{Fullscreen, WindowBuilder};
88
#[cfg(target_os = "macos")]
99
use winit::platform::macos::WindowExtMacOS;
1010

11-
fn main() {
11+
fn main() -> std::result::Result<(), impl std::error::Error> {
1212
SimpleLogger::new().init().unwrap();
1313
let event_loop = EventLoop::new();
1414

@@ -118,5 +118,5 @@ fn main() {
118118
},
119119
_ => {}
120120
}
121-
});
121+
})
122122
}

examples/handling_close.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::WindowBuilder,
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -82,5 +82,5 @@ fn main() {
8282
}
8383
_ => (),
8484
}
85-
});
85+
})
8686
}

examples/ime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use winit::{
99
window::{ImePurpose, WindowBuilder},
1010
};
1111

12-
fn main() {
12+
fn main() -> std::result::Result<(), impl std::error::Error> {
1313
SimpleLogger::new()
1414
.with_level(LevelFilter::Trace)
1515
.init()
@@ -108,5 +108,5 @@ fn main() {
108108
}
109109
_ => (),
110110
}
111-
});
111+
})
112112
}

examples/mouse_wheel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::WindowBuilder,
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -58,5 +58,5 @@ In other words, the deltas indicate the direction in which to move the content (
5858
},
5959
_ => (),
6060
}
61-
});
61+
})
6262
}

examples/multithreaded.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::single_match)]
22

33
#[cfg(not(wasm_platform))]
4-
fn main() {
4+
fn main() -> std::result::Result<(), impl std::error::Error> {
55
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
66

77
use simple_logger::SimpleLogger;

examples/multiwindow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use winit::{
99
window::Window,
1010
};
1111

12-
fn main() {
12+
fn main() -> std::result::Result<(), impl std::error::Error> {
1313
SimpleLogger::new().init().unwrap();
1414
let event_loop = EventLoop::new();
1515

examples/request_redraw.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::WindowBuilder,
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -37,5 +37,5 @@ fn main() {
3737
}
3838
_ => (),
3939
}
40-
});
40+
})
4141
}

examples/request_redraw_threaded.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(clippy::single_match)]
22

33
#[cfg(not(wasm_platform))]
4-
fn main() {
4+
fn main() -> std::result::Result<(), impl std::error::Error> {
55
use std::{thread, time};
66

77
use simple_logger::SimpleLogger;
@@ -39,7 +39,7 @@ fn main() {
3939
}
4040
_ => (),
4141
}
42-
});
42+
})
4343
}
4444

4545
#[cfg(wasm_platform)]

examples/resizable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use winit::{
88
window::WindowBuilder,
99
};
1010

11-
fn main() {
11+
fn main() -> std::result::Result<(), impl std::error::Error> {
1212
SimpleLogger::new().init().unwrap();
1313
let event_loop = EventLoop::new();
1414

@@ -46,5 +46,5 @@ fn main() {
4646
},
4747
_ => (),
4848
};
49-
});
49+
})
5050
}

examples/theme.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::{Theme, WindowBuilder},
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -67,5 +67,5 @@ fn main() {
6767
},
6868
_ => (),
6969
}
70-
});
70+
})
7171
}

examples/timer.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use winit::{
1010
window::WindowBuilder,
1111
};
1212

13-
fn main() {
13+
fn main() -> std::result::Result<(), impl std::error::Error> {
1414
SimpleLogger::new().init().unwrap();
1515
let event_loop = EventLoop::new();
1616

@@ -38,5 +38,5 @@ fn main() {
3838
} => control_flow.set_exit(),
3939
_ => (),
4040
}
41-
});
41+
})
4242
}

examples/touchpad_gestures.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use winit::{
55
window::WindowBuilder,
66
};
77

8-
fn main() {
8+
fn main() -> std::result::Result<(), impl std::error::Error> {
99
SimpleLogger::new().init().unwrap();
1010
let event_loop = EventLoop::new();
1111

@@ -42,5 +42,5 @@ fn main() {
4242
_ => (),
4343
}
4444
}
45-
});
45+
})
4646
}

examples/transparent.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::WindowBuilder,
88
};
99

10-
fn main() {
10+
fn main() -> std::result::Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -30,5 +30,5 @@ fn main() {
3030
} => control_flow.set_exit(),
3131
_ => (),
3232
}
33-
});
33+
})
3434
}

examples/web.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use winit::{
66
window::WindowBuilder,
77
};
88

9-
pub fn main() {
9+
pub fn main() -> std::result::Result<(), impl std::error::Error> {
1010
let event_loop = EventLoop::new();
1111

1212
let window = WindowBuilder::new()
@@ -33,7 +33,7 @@ pub fn main() {
3333
}
3434
_ => (),
3535
}
36-
});
36+
})
3737
}
3838

3939
#[cfg(wasm_platform)]

examples/window.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use winit::{
77
window::WindowBuilder,
88
};
99

10-
fn main() {
10+
fn main() -> Result<(), impl std::error::Error> {
1111
SimpleLogger::new().init().unwrap();
1212
let event_loop = EventLoop::new();
1313

@@ -25,11 +25,14 @@ fn main() {
2525
Event::WindowEvent {
2626
event: WindowEvent::CloseRequested,
2727
window_id,
28-
} if window_id == window.id() => control_flow.set_exit(),
28+
} if window_id == window.id() => {
29+
log::warn!("Close button pressed");
30+
control_flow.set_exit()
31+
}
2932
Event::MainEventsCleared => {
3033
window.request_redraw();
3134
}
3235
_ => (),
3336
}
34-
});
37+
})
3538
}

examples/window_buttons.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use winit::{
1010
window::{WindowBuilder, WindowButtons},
1111
};
1212

13-
fn main() {
13+
fn main() -> std::result::Result<(), impl std::error::Error> {
1414
SimpleLogger::new().init().unwrap();
1515
let event_loop = EventLoop::new();
1616

@@ -64,5 +64,5 @@ fn main() {
6464
} if window_id == window.id() => control_flow.set_exit(),
6565
_ => (),
6666
}
67-
});
67+
})
6868
}

examples/window_debug.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use winit::{
1010
window::{Fullscreen, WindowBuilder},
1111
};
1212

13-
fn main() {
13+
fn main() -> std::result::Result<(), impl std::error::Error> {
1414
SimpleLogger::new().init().unwrap();
1515
let event_loop = EventLoop::new();
1616

@@ -128,5 +128,5 @@ fn main() {
128128
} if window_id == window.id() => control_flow.set_exit(),
129129
_ => (),
130130
}
131-
});
131+
})
132132
}

examples/window_drag_resize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use winit::{
1111

1212
const BORDER: f64 = 8.0;
1313

14-
fn main() {
14+
fn main() -> std::result::Result<(), impl std::error::Error> {
1515
SimpleLogger::new().init().unwrap();
1616
let event_loop = EventLoop::new();
1717

@@ -67,7 +67,7 @@ fn main() {
6767
_ => (),
6868
},
6969
_ => (),
70-
});
70+
})
7171
}
7272

7373
fn cursor_direction_icon(resize_direction: Option<ResizeDirection>) -> CursorIcon {

0 commit comments

Comments
 (0)