Add exit code to ControlFlow::Exit (#2100)

* Add exit code to control flow and impl on linux

* Fix examples to have an exit code

* Fix doc examples to use an exit code

* Improve documentation wording on the exit code

* Add exit code example

* Add exit code on windows

* Change i32 as exit code to u8

This avoids nasty surprises with negative numbers on some unix-alikes
due to two's complement.

* Fix android usages of ControlFlow::Exit

* Fix ios usages of ControlFlow::Exit

* Fix web usages of ControlFlow::Exit

* Add macos exit code

* Add changelog note

* Document exit code on display server disconnection

* Revert "Change i32 as exit code to u8"

This reverts commit f88fba0253b45de6a2ac0c3cbcf01f50503c9396.

* Change Exit to ExitWithCode and make an Exit const

* Revert "Add exit code example"

This reverts commit fbd3d03de9c2d7516c7a63da489c99f498b710df.

* Revert "Fix doc examples to use an exit code"

This reverts commit daabcdf9ef9e16acad715c094ae442529e39fcbc.

* Revert "Fix examples to have an exit code"

This reverts commit 0df486896b8d106acf65ba83c45cc88d60d228e1.

* Fix unix-alike to use ExitWithCode instead of Exit

* Fix windows to use ExitWithCode rather than Exit

* Silence warning about non-uppercase Exit const

* Refactor exit code handling

* Fix macos Exit usage and recover original semantic

* Fix ios to use ExitWithCode instead of Exit

* Update documentation to reflect ExitWithCode

* Fix web to use ExitWithCode when needed, not Exit

* Fix android to use ExitWithCode, not Exit

* Apply documenation nits

* Apply even more documentation nits

* Move change in CHANGELOG.md under "Unreleased"

* Try to use OS error code as exit code on wayland
This commit is contained in:
multisn8 2022-01-11 01:23:20 +01:00 committed by GitHub
parent 2a2abc4843
commit a52f755ce8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 140 additions and 92 deletions

View file

@ -188,11 +188,11 @@ impl<T: 'static> EventLoop<T> {
where
F: 'static + FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
self.run_return(event_handler);
::std::process::exit(0);
let exit_code = self.run_return(event_handler);
::std::process::exit(exit_code);
}
pub fn run_return<F>(&mut self, mut event_handler: F)
pub fn run_return<F>(&mut self, mut event_handler: F) -> i32
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
@ -209,13 +209,13 @@ impl<T: 'static> EventLoop<T> {
let runner = &self.window_target.p.runner_shared;
unsafe {
let exit_code = unsafe {
let mut msg = mem::zeroed();
runner.poll();
'main: loop {
if 0 == winuser::GetMessageW(&mut msg, ptr::null_mut(), 0, 0) {
break 'main;
break 'main 0;
}
winuser::TranslateMessage(&msg);
winuser::DispatchMessageW(&msg);
@ -225,16 +225,20 @@ impl<T: 'static> EventLoop<T> {
panic::resume_unwind(payload);
}
if runner.control_flow() == ControlFlow::Exit && !runner.handling_events() {
break 'main;
if let ControlFlow::ExitWithCode(code) = runner.control_flow() {
if !runner.handling_events() {
break 'main code;
}
}
}
}
};
unsafe {
runner.loop_destroyed();
}
runner.reset_runner();
exit_code
}
pub fn create_proxy(&self) -> EventLoopProxy<T> {
@ -763,7 +767,7 @@ unsafe fn process_control_flow<T: 'static>(runner: &EventLoopRunner<T>) {
Box::into_raw(WaitUntilInstantBox::new(until)) as LPARAM,
);
}
ControlFlow::Exit => (),
ControlFlow::ExitWithCode(_) => (),
}
}