mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2025-01-11 21:31:29 +11:00
macos: Implement run_return (#1108)
* macos: Implement run_return * Update comments * Fix CHANGELOG.md
This commit is contained in:
parent
31110be396
commit
7b707e7d75
|
@ -1,5 +1,7 @@
|
||||||
# Unreleased
|
# Unreleased
|
||||||
|
|
||||||
|
- On macOS, implement `run_return`.
|
||||||
|
|
||||||
# 0.20.0 Alpha 3 (2019-08-14)
|
# 0.20.0 Alpha 3 (2019-08-14)
|
||||||
|
|
||||||
- On macOS, drop the run closure on exit.
|
- On macOS, drop the run closure on exit.
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::{
|
||||||
fmt::{self, Debug},
|
fmt::{self, Debug},
|
||||||
hint::unreachable_unchecked,
|
hint::unreachable_unchecked,
|
||||||
mem,
|
mem,
|
||||||
|
rc::Rc,
|
||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Mutex, MutexGuard,
|
Mutex, MutexGuard,
|
||||||
|
@ -37,13 +38,13 @@ pub trait EventHandler: Debug {
|
||||||
fn handle_user_events(&mut self, control_flow: &mut ControlFlow);
|
fn handle_user_events(&mut self, control_flow: &mut ControlFlow);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EventLoopHandler<F, T: 'static> {
|
struct EventLoopHandler<T: 'static> {
|
||||||
callback: F,
|
callback: Box<dyn FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow)>,
|
||||||
will_exit: bool,
|
will_exit: bool,
|
||||||
window_target: RootWindowTarget<T>,
|
window_target: Rc<RootWindowTarget<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T> Debug for EventLoopHandler<F, T> {
|
impl<T> Debug for EventLoopHandler<T> {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
formatter
|
formatter
|
||||||
.debug_struct("EventLoopHandler")
|
.debug_struct("EventLoopHandler")
|
||||||
|
@ -52,11 +53,7 @@ impl<F, T> Debug for EventLoopHandler<F, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, T> EventHandler for EventLoopHandler<F, T>
|
impl<T> EventHandler for EventLoopHandler<T> {
|
||||||
where
|
|
||||||
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
|
||||||
T: 'static,
|
|
||||||
{
|
|
||||||
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow) {
|
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow) {
|
||||||
(self.callback)(event.userify(), &self.window_target, control_flow);
|
(self.callback)(event.userify(), &self.window_target, control_flow);
|
||||||
self.will_exit |= *control_flow == ControlFlow::Exit;
|
self.will_exit |= *control_flow == ControlFlow::Exit;
|
||||||
|
@ -180,13 +177,20 @@ impl Handler {
|
||||||
pub enum AppState {}
|
pub enum AppState {}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn set_callback<F, T>(callback: F, window_target: RootWindowTarget<T>)
|
// This function extends lifetime of `callback` to 'static as its side effect
|
||||||
|
pub unsafe fn set_callback<F, T>(callback: F, window_target: Rc<RootWindowTarget<T>>)
|
||||||
where
|
where
|
||||||
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
F: FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
||||||
T: 'static,
|
|
||||||
{
|
{
|
||||||
*HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
|
*HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
|
||||||
callback,
|
// This transmute is always safe, in case it was reached through `run`, since our
|
||||||
|
// lifetime will be already 'static. In other cases caller should ensure that all data
|
||||||
|
// they passed to callback will actually outlive it, some apps just can't move
|
||||||
|
// everything to event loop, so this is something that they should care about.
|
||||||
|
callback: mem::transmute::<
|
||||||
|
Box<dyn FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow)>,
|
||||||
|
Box<dyn FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow)>,
|
||||||
|
>(Box::new(callback)),
|
||||||
will_exit: false,
|
will_exit: false,
|
||||||
window_target,
|
window_target,
|
||||||
}));
|
}));
|
||||||
|
@ -299,7 +303,7 @@ impl AppState {
|
||||||
}
|
}
|
||||||
HANDLER.update_start_time();
|
HANDLER.update_start_time();
|
||||||
match HANDLER.get_old_and_new_control_flow() {
|
match HANDLER.get_old_and_new_control_flow() {
|
||||||
(ControlFlow::Exit, _) | (_, ControlFlow::Exit) => unreachable!(),
|
(ControlFlow::Exit, _) | (_, ControlFlow::Exit) => (),
|
||||||
(old, new) if old == new => (),
|
(old, new) if old == new => (),
|
||||||
(_, ControlFlow::Wait) => HANDLER.waker().stop(),
|
(_, ControlFlow::Wait) => HANDLER.waker().stop(),
|
||||||
(_, ControlFlow::WaitUntil(instant)) => HANDLER.waker().start_at(instant),
|
(_, ControlFlow::WaitUntil(instant)) => HANDLER.waker().start_at(instant),
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque, marker::PhantomData, mem, os::raw::c_void, process, ptr, sync::mpsc,
|
collections::VecDeque, marker::PhantomData, mem, os::raw::c_void, process, ptr, rc::Rc,
|
||||||
|
sync::mpsc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use cocoa::{
|
use cocoa::{
|
||||||
|
@ -34,7 +35,7 @@ impl<T> Default for EventLoopWindowTarget<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventLoop<T: 'static> {
|
pub struct EventLoop<T: 'static> {
|
||||||
window_target: RootWindowTarget<T>,
|
window_target: Rc<RootWindowTarget<T>>,
|
||||||
_delegate: IdRef,
|
_delegate: IdRef,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,10 +60,10 @@ impl<T> EventLoop<T> {
|
||||||
};
|
};
|
||||||
setup_control_flow_observers();
|
setup_control_flow_observers();
|
||||||
EventLoop {
|
EventLoop {
|
||||||
window_target: RootWindowTarget {
|
window_target: Rc::new(RootWindowTarget {
|
||||||
p: Default::default(),
|
p: Default::default(),
|
||||||
_marker: PhantomData,
|
_marker: PhantomData,
|
||||||
},
|
}),
|
||||||
_delegate: delegate,
|
_delegate: delegate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -81,28 +82,28 @@ impl<T> EventLoop<T> {
|
||||||
&self.window_target
|
&self.window_target
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run<F>(self, callback: F) -> !
|
pub fn run<F>(mut self, callback: F) -> !
|
||||||
where
|
where
|
||||||
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
F: 'static + FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
||||||
|
{
|
||||||
|
self.run_return(callback);
|
||||||
|
process::exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn run_return<F>(&mut self, callback: F)
|
||||||
|
where
|
||||||
|
F: FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
||||||
{
|
{
|
||||||
unsafe {
|
unsafe {
|
||||||
let _pool = NSAutoreleasePool::new(nil);
|
let _pool = NSAutoreleasePool::new(nil);
|
||||||
let app = NSApp();
|
let app = NSApp();
|
||||||
assert_ne!(app, nil);
|
assert_ne!(app, nil);
|
||||||
AppState::set_callback(callback, self.window_target);
|
AppState::set_callback(callback, Rc::clone(&self.window_target));
|
||||||
let _: () = msg_send![app, run];
|
let _: () = msg_send![app, run];
|
||||||
AppState::exit();
|
AppState::exit();
|
||||||
process::exit(0)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_return<F>(&mut self, _callback: F)
|
|
||||||
where
|
|
||||||
F: FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
|
||||||
{
|
|
||||||
unimplemented!();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn create_proxy(&self) -> Proxy<T> {
|
pub fn create_proxy(&self) -> Proxy<T> {
|
||||||
Proxy::new(self.window_target.p.sender.clone())
|
Proxy::new(self.window_target.p.sender.clone())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue