2019-05-02 09:03:30 +10:00
|
|
|
use std::{
|
2019-06-22 01:33:15 +10:00
|
|
|
collections::VecDeque,
|
|
|
|
fmt::{self, Debug},
|
|
|
|
hint::unreachable_unchecked,
|
|
|
|
mem,
|
2019-08-23 19:30:53 +10:00
|
|
|
rc::Rc,
|
2019-06-22 01:33:15 +10:00
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Mutex, MutexGuard,
|
|
|
|
},
|
|
|
|
time::Instant,
|
2019-05-02 09:03:30 +10:00
|
|
|
};
|
|
|
|
|
2019-12-30 09:16:12 +11:00
|
|
|
use cocoa::{
|
|
|
|
appkit::NSApp,
|
|
|
|
base::nil,
|
|
|
|
foundation::{NSAutoreleasePool, NSString},
|
|
|
|
};
|
2019-05-02 09:03:30 +10:00
|
|
|
|
2019-06-18 04:27:00 +10:00
|
|
|
use crate::{
|
2019-12-29 07:36:06 +11:00
|
|
|
event::{Event, StartCause},
|
2019-05-02 09:03:30 +10:00
|
|
|
event_loop::{ControlFlow, EventLoopWindowTarget as RootWindowTarget},
|
2019-06-22 01:33:15 +10:00
|
|
|
platform_impl::platform::{observer::EventLoopWaker, util::Never},
|
2019-05-02 09:03:30 +10:00
|
|
|
window::WindowId,
|
|
|
|
};
|
2019-12-19 20:10:47 +11:00
|
|
|
use objc::runtime::Object;
|
2019-05-02 09:03:30 +10:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref HANDLER: Handler = Default::default();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Event<Never> {
|
|
|
|
fn userify<T: 'static>(self) -> Event<T> {
|
|
|
|
self.map_nonuser_event()
|
|
|
|
// `Never` can't be constructed, so the `UserEvent` variant can't
|
|
|
|
// be present here.
|
|
|
|
.unwrap_or_else(|_| unsafe { unreachable_unchecked() })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EventHandler: Debug {
|
|
|
|
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow);
|
|
|
|
fn handle_user_events(&mut self, control_flow: &mut ControlFlow);
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:30:53 +10:00
|
|
|
struct EventLoopHandler<T: 'static> {
|
|
|
|
callback: Box<dyn FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow)>,
|
2019-05-02 09:03:30 +10:00
|
|
|
will_exit: bool,
|
2019-08-23 19:30:53 +10:00
|
|
|
window_target: Rc<RootWindowTarget<T>>,
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
|
|
|
|
2019-08-23 19:30:53 +10:00
|
|
|
impl<T> Debug for EventLoopHandler<T> {
|
2019-06-18 04:27:00 +10:00
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-06-22 01:33:15 +10:00
|
|
|
formatter
|
|
|
|
.debug_struct("EventLoopHandler")
|
2019-05-02 09:03:30 +10:00
|
|
|
.field("window_target", &self.window_target)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 19:30:53 +10:00
|
|
|
impl<T> EventHandler for EventLoopHandler<T> {
|
2019-05-02 09:03:30 +10:00
|
|
|
fn handle_nonuser_event(&mut self, event: Event<Never>, control_flow: &mut ControlFlow) {
|
2019-06-22 01:33:15 +10:00
|
|
|
(self.callback)(event.userify(), &self.window_target, control_flow);
|
2019-05-02 09:03:30 +10:00
|
|
|
self.will_exit |= *control_flow == ControlFlow::Exit;
|
|
|
|
if self.will_exit {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_user_events(&mut self, control_flow: &mut ControlFlow) {
|
|
|
|
let mut will_exit = self.will_exit;
|
|
|
|
for event in self.window_target.p.receiver.try_iter() {
|
2019-06-22 01:33:15 +10:00
|
|
|
(self.callback)(Event::UserEvent(event), &self.window_target, control_flow);
|
2019-05-02 09:03:30 +10:00
|
|
|
will_exit |= *control_flow == ControlFlow::Exit;
|
|
|
|
if will_exit {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.will_exit = will_exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Handler {
|
|
|
|
ready: AtomicBool,
|
|
|
|
in_callback: AtomicBool,
|
|
|
|
control_flow: Mutex<ControlFlow>,
|
|
|
|
control_flow_prev: Mutex<ControlFlow>,
|
|
|
|
start_time: Mutex<Option<Instant>>,
|
|
|
|
callback: Mutex<Option<Box<dyn EventHandler>>>,
|
|
|
|
pending_events: Mutex<VecDeque<Event<Never>>>,
|
|
|
|
pending_redraw: Mutex<Vec<WindowId>>,
|
|
|
|
waker: Mutex<EventLoopWaker>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for Handler {}
|
|
|
|
unsafe impl Sync for Handler {}
|
|
|
|
|
|
|
|
impl Handler {
|
|
|
|
fn events<'a>(&'a self) -> MutexGuard<'a, VecDeque<Event<Never>>> {
|
|
|
|
self.pending_events.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn redraw<'a>(&'a self) -> MutexGuard<'a, Vec<WindowId>> {
|
|
|
|
self.pending_redraw.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn waker<'a>(&'a self) -> MutexGuard<'a, EventLoopWaker> {
|
|
|
|
self.waker.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_ready(&self) -> bool {
|
|
|
|
self.ready.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_ready(&self) {
|
|
|
|
self.ready.store(true, Ordering::Release);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_exit(&self) -> bool {
|
|
|
|
*self.control_flow.lock().unwrap() == ControlFlow::Exit
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_control_flow_and_update_prev(&self) -> ControlFlow {
|
|
|
|
let control_flow = self.control_flow.lock().unwrap();
|
|
|
|
*self.control_flow_prev.lock().unwrap() = *control_flow;
|
|
|
|
*control_flow
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_old_and_new_control_flow(&self) -> (ControlFlow, ControlFlow) {
|
|
|
|
let old = *self.control_flow_prev.lock().unwrap();
|
|
|
|
let new = *self.control_flow.lock().unwrap();
|
|
|
|
(old, new)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_start_time(&self) -> Option<Instant> {
|
|
|
|
*self.start_time.lock().unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_start_time(&self) {
|
|
|
|
*self.start_time.lock().unwrap() = Some(Instant::now());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn take_events(&self) -> VecDeque<Event<Never>> {
|
|
|
|
mem::replace(&mut *self.events(), Default::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn should_redraw(&self) -> Vec<WindowId> {
|
|
|
|
mem::replace(&mut *self.redraw(), Default::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_in_callback(&self) -> bool {
|
|
|
|
self.in_callback.load(Ordering::Acquire)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_in_callback(&self, in_callback: bool) {
|
|
|
|
self.in_callback.store(in_callback, Ordering::Release);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_nonuser_event(&self, event: Event<Never>) {
|
|
|
|
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
|
2019-06-22 01:33:15 +10:00
|
|
|
callback.handle_nonuser_event(event, &mut *self.control_flow.lock().unwrap());
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_user_events(&self) {
|
|
|
|
if let Some(ref mut callback) = *self.callback.lock().unwrap() {
|
2019-06-22 01:33:15 +10:00
|
|
|
callback.handle_user_events(&mut *self.control_flow.lock().unwrap());
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum AppState {}
|
|
|
|
|
|
|
|
impl AppState {
|
2019-08-23 19:30:53 +10:00
|
|
|
// 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>>)
|
2019-05-02 09:03:30 +10:00
|
|
|
where
|
2019-08-23 19:30:53 +10:00
|
|
|
F: FnMut(Event<T>, &RootWindowTarget<T>, &mut ControlFlow),
|
2019-05-02 09:03:30 +10:00
|
|
|
{
|
|
|
|
*HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler {
|
2019-08-23 19:30:53 +10:00
|
|
|
// 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)),
|
2019-05-02 09:03:30 +10:00
|
|
|
will_exit: false,
|
|
|
|
window_target,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn exit() {
|
|
|
|
HANDLER.set_in_callback(true);
|
|
|
|
HANDLER.handle_nonuser_event(Event::LoopDestroyed);
|
|
|
|
HANDLER.set_in_callback(false);
|
2019-07-24 06:44:07 +10:00
|
|
|
HANDLER.callback.lock().unwrap().take();
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn launched() {
|
|
|
|
HANDLER.set_ready();
|
|
|
|
HANDLER.waker().start();
|
|
|
|
HANDLER.set_in_callback(true);
|
|
|
|
HANDLER.handle_nonuser_event(Event::NewEvents(StartCause::Init));
|
|
|
|
HANDLER.set_in_callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn wakeup() {
|
2019-06-22 01:33:15 +10:00
|
|
|
if !HANDLER.is_ready() {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-02 09:03:30 +10:00
|
|
|
let start = HANDLER.get_start_time().unwrap();
|
|
|
|
let cause = match HANDLER.get_control_flow_and_update_prev() {
|
|
|
|
ControlFlow::Poll => StartCause::Poll,
|
2019-06-25 02:14:55 +10:00
|
|
|
ControlFlow::Wait => StartCause::WaitCancelled {
|
|
|
|
start,
|
|
|
|
requested_resume: None,
|
2019-05-02 09:03:30 +10:00
|
|
|
},
|
|
|
|
ControlFlow::WaitUntil(requested_resume) => {
|
|
|
|
if Instant::now() >= requested_resume {
|
|
|
|
StartCause::ResumeTimeReached {
|
|
|
|
start,
|
|
|
|
requested_resume,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
StartCause::WaitCancelled {
|
|
|
|
start,
|
|
|
|
requested_resume: Some(requested_resume),
|
|
|
|
}
|
|
|
|
}
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
2019-06-22 01:33:15 +10:00
|
|
|
ControlFlow::Exit => StartCause::Poll, //panic!("unexpected `ControlFlow::Exit`"),
|
2019-05-02 09:03:30 +10:00
|
|
|
};
|
|
|
|
HANDLER.set_in_callback(true);
|
|
|
|
HANDLER.handle_nonuser_event(Event::NewEvents(cause));
|
|
|
|
HANDLER.set_in_callback(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is called from multiple threads at present
|
|
|
|
pub fn queue_redraw(window_id: WindowId) {
|
|
|
|
let mut pending_redraw = HANDLER.redraw();
|
|
|
|
if !pending_redraw.contains(&window_id) {
|
|
|
|
pending_redraw.push(window_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn queue_event(event: Event<Never>) {
|
|
|
|
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
|
|
|
|
panic!("Event queued from different thread: {:#?}", event);
|
|
|
|
}
|
|
|
|
HANDLER.events().push_back(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn queue_events(mut events: VecDeque<Event<Never>>) {
|
|
|
|
if !unsafe { msg_send![class!(NSThread), isMainThread] } {
|
|
|
|
panic!("Events queued from different thread: {:#?}", events);
|
|
|
|
}
|
|
|
|
HANDLER.events().append(&mut events);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn cleared() {
|
2019-06-22 01:33:15 +10:00
|
|
|
if !HANDLER.is_ready() {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-02 09:03:30 +10:00
|
|
|
if !HANDLER.get_in_callback() {
|
|
|
|
HANDLER.set_in_callback(true);
|
|
|
|
HANDLER.handle_user_events();
|
|
|
|
for event in HANDLER.take_events() {
|
|
|
|
HANDLER.handle_nonuser_event(event);
|
|
|
|
}
|
2019-10-28 02:09:54 +11:00
|
|
|
HANDLER.handle_nonuser_event(Event::MainEventsCleared);
|
2019-05-02 09:03:30 +10:00
|
|
|
for window_id in HANDLER.should_redraw() {
|
2019-10-28 02:09:54 +11:00
|
|
|
HANDLER.handle_nonuser_event(Event::RedrawRequested(window_id));
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
2019-10-28 02:09:54 +11:00
|
|
|
HANDLER.handle_nonuser_event(Event::RedrawEventsCleared);
|
2019-05-02 09:03:30 +10:00
|
|
|
HANDLER.set_in_callback(false);
|
|
|
|
}
|
|
|
|
if HANDLER.should_exit() {
|
2019-12-19 20:10:47 +11:00
|
|
|
unsafe {
|
|
|
|
let _: () = msg_send![NSApp(), stop: nil];
|
|
|
|
|
2019-12-30 09:16:12 +11:00
|
|
|
let pool = NSAutoreleasePool::new(nil);
|
|
|
|
|
2019-12-19 20:10:47 +11:00
|
|
|
let windows: *const Object = msg_send![NSApp(), windows];
|
|
|
|
let window: *const Object = msg_send![windows, objectAtIndex:0];
|
|
|
|
assert_ne!(window, nil);
|
|
|
|
|
|
|
|
let title: *const Object = msg_send![window, title];
|
|
|
|
assert_ne!(title, nil);
|
|
|
|
let postfix = NSString::alloc(nil).init_str("*");
|
|
|
|
let some_unique_title: *const Object =
|
|
|
|
msg_send![title, stringByAppendingString: postfix];
|
|
|
|
assert_ne!(some_unique_title, nil);
|
|
|
|
|
|
|
|
// To stop event loop immediately, we need to send some UI event here.
|
|
|
|
let _: () = msg_send![window, setTitle: some_unique_title];
|
|
|
|
// And restore it.
|
|
|
|
let _: () = msg_send![window, setTitle: title];
|
2019-12-30 09:16:12 +11:00
|
|
|
|
|
|
|
pool.drain();
|
2019-12-19 20:10:47 +11:00
|
|
|
};
|
2019-05-02 09:03:30 +10:00
|
|
|
}
|
|
|
|
HANDLER.update_start_time();
|
|
|
|
match HANDLER.get_old_and_new_control_flow() {
|
2019-08-23 19:30:53 +10:00
|
|
|
(ControlFlow::Exit, _) | (_, ControlFlow::Exit) => (),
|
2019-05-02 09:03:30 +10:00
|
|
|
(old, new) if old == new => (),
|
|
|
|
(_, ControlFlow::Wait) => HANDLER.waker().stop(),
|
|
|
|
(_, ControlFlow::WaitUntil(instant)) => HANDLER.waker().start_at(instant),
|
|
|
|
(_, ControlFlow::Poll) => HANDLER.waker().start(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|