2019-05-01 17:03:30 -06:00
|
|
|
use std::any::Any;
|
2024-02-28 04:33:47 +01:00
|
|
|
use std::cell::Cell;
|
2021-03-08 19:56:39 +01:00
|
|
|
use std::os::raw::c_void;
|
2024-02-28 04:33:47 +01:00
|
|
|
use std::panic::{catch_unwind, resume_unwind, RefUnwindSafe, UnwindSafe};
|
2023-04-11 12:50:52 +01:00
|
|
|
use std::ptr;
|
2021-03-08 19:56:39 +01:00
|
|
|
use std::rc::{Rc, Weak};
|
2024-06-24 13:04:55 +03:00
|
|
|
use std::sync::atomic::{AtomicBool, Ordering as AtomicOrdering};
|
2024-11-13 15:29:05 +03:00
|
|
|
use std::sync::Arc;
|
2023-07-23 23:27:38 +01:00
|
|
|
use std::time::{Duration, Instant};
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2022-09-08 21:03:25 +02:00
|
|
|
use core_foundation::base::{CFIndex, CFRelease};
|
|
|
|
|
use core_foundation::runloop::{
|
|
|
|
|
kCFRunLoopCommonModes, CFRunLoopAddSource, CFRunLoopGetMain, CFRunLoopSourceContext,
|
|
|
|
|
CFRunLoopSourceCreate, CFRunLoopSourceRef, CFRunLoopSourceSignal, CFRunLoopWakeUp,
|
|
|
|
|
};
|
2024-05-27 14:49:22 +02:00
|
|
|
use objc2::rc::{autoreleasepool, Retained};
|
2024-08-05 20:51:38 +02:00
|
|
|
use objc2::{msg_send_id, sel, ClassType};
|
2024-08-11 23:14:18 +02:00
|
|
|
use objc2_app_kit::{
|
|
|
|
|
NSApplication, NSApplicationActivationPolicy, NSApplicationDidFinishLaunchingNotification,
|
|
|
|
|
NSApplicationWillTerminateNotification, NSWindow,
|
|
|
|
|
};
|
|
|
|
|
use objc2_foundation::{MainThreadMarker, NSNotificationCenter, NSObject, NSObjectProtocol};
|
2024-11-13 15:29:05 +03:00
|
|
|
use rwh_06::HasDisplayHandle;
|
2023-12-23 23:07:55 +01:00
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
use super::super::notification_center::create_observer;
|
2022-09-08 16:45:29 +02:00
|
|
|
use super::app::WinitApplication;
|
2024-08-11 23:14:18 +02:00
|
|
|
use super::app_state::AppState;
|
2024-06-24 13:26:49 +02:00
|
|
|
use super::cursor::CustomCursor;
|
2023-12-23 23:07:55 +01:00
|
|
|
use super::event::dummy_event;
|
2024-08-06 21:02:53 +03:00
|
|
|
use super::monitor;
|
2024-01-14 03:37:53 +01:00
|
|
|
use super::observer::setup_control_flow_observers;
|
2024-05-20 20:27:36 +04:00
|
|
|
use crate::application::ApplicationHandler;
|
2024-09-06 17:20:11 +03:00
|
|
|
use crate::error::{EventLoopError, RequestError};
|
2024-08-06 21:02:53 +03:00
|
|
|
use crate::event_loop::{
|
|
|
|
|
ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents,
|
2024-11-12 10:56:20 +03:00
|
|
|
EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider,
|
2024-11-13 15:29:05 +03:00
|
|
|
OwnedDisplayHandle as CoreOwnedDisplayHandle,
|
2024-08-06 21:02:53 +03:00
|
|
|
};
|
|
|
|
|
use crate::monitor::MonitorHandle as RootMonitorHandle;
|
2023-06-18 12:10:09 +01:00
|
|
|
use crate::platform::macos::ActivationPolicy;
|
|
|
|
|
use crate::platform::pump_events::PumpStatus;
|
2024-08-06 21:02:53 +03:00
|
|
|
use crate::platform_impl::Window;
|
2024-08-23 23:40:27 +03:00
|
|
|
use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource, Theme};
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2021-03-08 19:56:39 +01:00
|
|
|
#[derive(Default)]
|
|
|
|
|
pub struct PanicInfo {
|
|
|
|
|
inner: Cell<Option<Box<dyn Any + Send + 'static>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// WARNING:
|
|
|
|
|
// As long as this struct is used through its `impl`, it is UnwindSafe.
|
|
|
|
|
// (If `get_mut` is called on `inner`, unwind safety may get broken.)
|
|
|
|
|
impl UnwindSafe for PanicInfo {}
|
|
|
|
|
impl RefUnwindSafe for PanicInfo {}
|
|
|
|
|
impl PanicInfo {
|
|
|
|
|
pub fn is_panicking(&self) -> bool {
|
|
|
|
|
let inner = self.inner.take();
|
|
|
|
|
let result = inner.is_some();
|
|
|
|
|
self.inner.set(inner);
|
|
|
|
|
result
|
|
|
|
|
}
|
2024-04-26 19:11:44 +04:00
|
|
|
|
2024-04-27 21:29:11 +08:00
|
|
|
/// Overwrites the current state if the current state is not panicking
|
2021-03-08 19:56:39 +01:00
|
|
|
pub fn set_panic(&self, p: Box<dyn Any + Send + 'static>) {
|
|
|
|
|
if !self.is_panicking() {
|
|
|
|
|
self.inner.set(Some(p));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-04-26 19:11:44 +04:00
|
|
|
|
2021-03-08 19:56:39 +01:00
|
|
|
pub fn take(&self) -> Option<Box<dyn Any + Send + 'static>> {
|
|
|
|
|
self.inner.take()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-27 17:04:39 +02:00
|
|
|
#[derive(Debug)]
|
2024-01-31 17:29:59 +04:00
|
|
|
pub struct ActiveEventLoop {
|
2024-08-11 23:14:18 +02:00
|
|
|
pub(super) app_state: Rc<AppState>,
|
2024-01-14 05:19:23 +01:00
|
|
|
pub(super) mtm: MainThreadMarker,
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
|
2024-01-31 17:29:59 +04:00
|
|
|
impl ActiveEventLoop {
|
2024-08-06 21:02:53 +03:00
|
|
|
pub(crate) fn hide_application(&self) {
|
|
|
|
|
NSApplication::sharedApplication(self.mtm).hide(None)
|
2024-02-28 04:33:47 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
pub(crate) fn hide_other_applications(&self) {
|
|
|
|
|
NSApplication::sharedApplication(self.mtm).hideOtherApplications(None)
|
2024-05-06 16:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
pub(crate) fn set_allows_automatic_window_tabbing(&self, enabled: bool) {
|
|
|
|
|
NSWindow::setAllowsAutomaticWindowTabbing(enabled, self.mtm)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn allows_automatic_window_tabbing(&self) -> bool {
|
|
|
|
|
NSWindow::allowsAutomaticWindowTabbing(self.mtm)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl RootActiveEventLoop for ActiveEventLoop {
|
2024-11-12 10:56:20 +03:00
|
|
|
fn create_proxy(&self) -> CoreEventLoopProxy {
|
|
|
|
|
CoreEventLoopProxy::new(self.app_state.event_loop_proxy().clone())
|
2024-08-06 21:02:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_window(
|
|
|
|
|
&self,
|
|
|
|
|
window_attributes: crate::window::WindowAttributes,
|
2024-09-06 17:20:11 +03:00
|
|
|
) -> Result<Box<dyn crate::window::Window>, RequestError> {
|
2024-08-23 23:40:27 +03:00
|
|
|
Ok(Box::new(Window::new(self, window_attributes)?))
|
2024-08-06 21:02:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn create_custom_cursor(
|
2024-08-06 18:57:03 +02:00
|
|
|
&self,
|
|
|
|
|
source: CustomCursorSource,
|
2024-09-06 17:20:11 +03:00
|
|
|
) -> Result<RootCustomCursor, RequestError> {
|
2024-08-06 18:57:03 +02:00
|
|
|
Ok(RootCustomCursor { inner: CustomCursor::new(source.inner)? })
|
2024-02-03 07:27:17 +04:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn available_monitors(&self) -> Box<dyn Iterator<Item = RootMonitorHandle>> {
|
|
|
|
|
Box::new(monitor::available_monitors().into_iter().map(|inner| RootMonitorHandle { inner }))
|
2020-07-04 15:46:41 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn primary_monitor(&self) -> Option<crate::monitor::MonitorHandle> {
|
2020-09-07 20:20:47 +03:00
|
|
|
let monitor = monitor::primary_monitor();
|
2024-08-06 21:02:53 +03:00
|
|
|
Some(RootMonitorHandle { inner: monitor })
|
2020-07-04 15:46:41 -04:00
|
|
|
}
|
2022-07-21 22:22:36 +03:00
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn listen_device_events(&self, _allowed: DeviceEvents) {}
|
2023-09-01 23:14:16 +02:00
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn system_theme(&self) -> Option<Theme> {
|
2024-08-05 20:51:38 +02:00
|
|
|
let app = NSApplication::sharedApplication(self.mtm);
|
|
|
|
|
|
|
|
|
|
if app.respondsToSelector(sel!(effectiveAppearance)) {
|
|
|
|
|
Some(super::window_delegate::appearance_to_theme(&app.effectiveAppearance()))
|
|
|
|
|
} else {
|
|
|
|
|
Some(Theme::Light)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn set_control_flow(&self, control_flow: ControlFlow) {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_control_flow(control_flow)
|
2023-09-07 08:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn control_flow(&self) -> ControlFlow {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.control_flow()
|
2023-09-07 08:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn exit(&self) {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.exit()
|
2023-09-07 08:25:04 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn exiting(&self) -> bool {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.exiting()
|
2023-09-07 08:25:04 +02:00
|
|
|
}
|
2024-01-15 11:58:11 -08:00
|
|
|
|
2024-11-13 15:29:05 +03:00
|
|
|
fn owned_display_handle(&self) -> CoreOwnedDisplayHandle {
|
|
|
|
|
CoreOwnedDisplayHandle::new(Arc::new(OwnedDisplayHandle))
|
2021-12-01 12:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle {
|
|
|
|
|
self
|
2023-07-13 15:55:51 +00:00
|
|
|
}
|
2024-08-06 21:02:53 +03:00
|
|
|
}
|
2023-07-13 15:55:51 +00:00
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
impl rwh_06::HasDisplayHandle for ActiveEventLoop {
|
|
|
|
|
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
|
|
|
|
|
let raw = rwh_06::RawDisplayHandle::AppKit(rwh_06::AppKitDisplayHandle::new());
|
|
|
|
|
unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) }
|
2023-07-13 15:55:51 +00:00
|
|
|
}
|
2021-12-01 12:20:56 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
pub struct EventLoop {
|
2023-08-27 17:04:39 +02:00
|
|
|
/// Store a reference to the application for convenience.
|
2023-08-30 15:19:30 +02:00
|
|
|
///
|
2024-02-19 11:58:44 +07:00
|
|
|
/// We intentionally don't store `WinitApplication` since we want to have
|
|
|
|
|
/// the possibility of swapping that out at some point.
|
2024-05-27 14:49:22 +02:00
|
|
|
app: Retained<NSApplication>,
|
2024-08-11 23:14:18 +02:00
|
|
|
app_state: Rc<AppState>,
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
window_target: ActiveEventLoop,
|
2021-03-08 19:56:39 +01:00
|
|
|
panic_info: Rc<PanicInfo>,
|
2024-08-11 23:14:18 +02:00
|
|
|
|
|
|
|
|
// Since macOS 10.11, we no longer need to remove the observers before they are deallocated;
|
|
|
|
|
// the system instead cleans it up next time it would have posted a notification to it.
|
|
|
|
|
//
|
|
|
|
|
// Though we do still need to keep the observers around to prevent them from being deallocated.
|
|
|
|
|
_did_finish_launching_observer: Retained<NSObject>,
|
|
|
|
|
_will_terminate_observer: Retained<NSObject>,
|
2017-06-09 22:13:30 +10:00
|
|
|
}
|
|
|
|
|
|
2022-06-10 13:43:33 +03:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2022-02-16 22:09:03 +01:00
|
|
|
pub(crate) struct PlatformSpecificEventLoopAttributes {
|
2024-09-16 06:49:18 -07:00
|
|
|
pub(crate) activation_policy: Option<ActivationPolicy>,
|
2022-02-16 22:09:03 +01:00
|
|
|
pub(crate) default_menu: bool,
|
2022-11-23 14:42:46 +02:00
|
|
|
pub(crate) activate_ignoring_other_apps: bool,
|
2022-02-16 22:09:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Default for PlatformSpecificEventLoopAttributes {
|
|
|
|
|
fn default() -> Self {
|
2024-09-16 06:49:18 -07:00
|
|
|
Self { activation_policy: None, default_menu: true, activate_ignoring_other_apps: true }
|
2022-02-16 22:09:03 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
impl EventLoop {
|
2023-08-13 23:20:09 +04:00
|
|
|
pub(crate) fn new(
|
|
|
|
|
attributes: &PlatformSpecificEventLoopAttributes,
|
|
|
|
|
) -> Result<Self, EventLoopError> {
|
2023-08-14 21:19:57 +02:00
|
|
|
let mtm = MainThreadMarker::new()
|
2023-08-30 15:19:30 +02:00
|
|
|
.expect("on macOS, `EventLoop` must be created on the main thread!");
|
2017-06-09 22:13:30 +10:00
|
|
|
|
2024-05-27 14:49:22 +02:00
|
|
|
let app: Retained<NSApplication> =
|
2022-09-08 16:45:29 +02:00
|
|
|
unsafe { msg_send_id![WinitApplication::class(), sharedApplication] };
|
|
|
|
|
|
2023-08-30 15:19:30 +02:00
|
|
|
if !app.is_kind_of::<WinitApplication>() {
|
|
|
|
|
panic!(
|
|
|
|
|
"`winit` requires control over the principal class. You must create the event \
|
|
|
|
|
loop before other parts of your application initialize NSApplication"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 16:45:29 +02:00
|
|
|
let activation_policy = match attributes.activation_policy {
|
2024-09-16 06:49:18 -07:00
|
|
|
None => None,
|
|
|
|
|
Some(ActivationPolicy::Regular) => Some(NSApplicationActivationPolicy::Regular),
|
|
|
|
|
Some(ActivationPolicy::Accessory) => Some(NSApplicationActivationPolicy::Accessory),
|
|
|
|
|
Some(ActivationPolicy::Prohibited) => Some(NSApplicationActivationPolicy::Prohibited),
|
2019-05-01 17:03:30 -06:00
|
|
|
};
|
2024-06-24 13:04:55 +03:00
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
let app_state = AppState::setup_global(
|
2023-12-23 23:07:55 +01:00
|
|
|
mtm,
|
2022-11-23 14:42:46 +02:00
|
|
|
activation_policy,
|
|
|
|
|
attributes.default_menu,
|
|
|
|
|
attributes.activate_ignoring_other_apps,
|
|
|
|
|
);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
let center = unsafe { NSNotificationCenter::defaultCenter() };
|
|
|
|
|
|
|
|
|
|
let weak_app_state = Rc::downgrade(&app_state);
|
|
|
|
|
let _did_finish_launching_observer = create_observer(
|
|
|
|
|
¢er,
|
|
|
|
|
// `applicationDidFinishLaunching:`
|
|
|
|
|
unsafe { NSApplicationDidFinishLaunchingNotification },
|
|
|
|
|
move |notification| {
|
|
|
|
|
if let Some(app_state) = weak_app_state.upgrade() {
|
|
|
|
|
app_state.did_finish_launching(notification);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let weak_app_state = Rc::downgrade(&app_state);
|
|
|
|
|
let _will_terminate_observer = create_observer(
|
|
|
|
|
¢er,
|
|
|
|
|
// `applicationWillTerminate:`
|
|
|
|
|
unsafe { NSApplicationWillTerminateNotification },
|
|
|
|
|
move |notification| {
|
|
|
|
|
if let Some(app_state) = weak_app_state.upgrade() {
|
|
|
|
|
app_state.will_terminate(notification);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2022-09-08 16:45:29 +02:00
|
|
|
|
2021-03-08 19:56:39 +01:00
|
|
|
let panic_info: Rc<PanicInfo> = Default::default();
|
2024-06-06 12:32:02 +02:00
|
|
|
setup_control_flow_observers(mtm, Rc::downgrade(&panic_info));
|
2023-08-14 21:19:57 +02:00
|
|
|
|
2023-08-13 23:20:09 +04:00
|
|
|
Ok(EventLoop {
|
2023-08-27 17:04:39 +02:00
|
|
|
app,
|
2024-08-11 23:14:18 +02:00
|
|
|
app_state: app_state.clone(),
|
|
|
|
|
window_target: ActiveEventLoop { app_state, mtm },
|
2021-03-08 19:56:39 +01:00
|
|
|
panic_info,
|
2024-08-11 23:14:18 +02:00
|
|
|
_did_finish_launching_observer,
|
|
|
|
|
_will_terminate_observer,
|
2023-08-13 23:20:09 +04:00
|
|
|
})
|
2017-06-09 22:13:30 +10:00
|
|
|
}
|
2017-02-05 12:51:09 +11:00
|
|
|
|
2024-08-06 21:02:53 +03:00
|
|
|
pub fn window_target(&self) -> &dyn RootActiveEventLoop {
|
2019-05-01 17:03:30 -06:00
|
|
|
&self.window_target
|
2017-02-05 12:51:09 +11:00
|
|
|
}
|
|
|
|
|
|
2024-07-11 15:38:09 +02:00
|
|
|
pub fn run_app<A: ApplicationHandler>(mut self, app: A) -> Result<(), EventLoopError> {
|
2024-05-20 20:27:36 +04:00
|
|
|
self.run_app_on_demand(app)
|
2019-08-23 12:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
2023-06-18 12:10:09 +01:00
|
|
|
// NB: we don't base this on `pump_events` because for `MacOs` we can't support
|
|
|
|
|
// `pump_events` elegantly (we just ask to run the loop for a "short" amount of
|
|
|
|
|
// time and so a layered implementation would end up using a lot of CPU due to
|
|
|
|
|
// redundant wake ups.
|
2024-06-24 13:04:55 +03:00
|
|
|
pub fn run_app_on_demand<A: ApplicationHandler>(
|
2024-05-20 20:27:36 +04:00
|
|
|
&mut self,
|
2024-07-11 15:38:09 +02:00
|
|
|
mut app: A,
|
2024-05-20 20:27:36 +04:00
|
|
|
) -> Result<(), EventLoopError> {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.clear_exit();
|
|
|
|
|
self.app_state.set_event_handler(&mut app, || {
|
2024-02-28 04:33:47 +01:00
|
|
|
autoreleasepool(|_| {
|
2023-07-23 23:27:38 +01:00
|
|
|
// clear / normalize pump_events state
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_wait_timeout(None);
|
|
|
|
|
self.app_state.set_stop_before_wait(false);
|
|
|
|
|
self.app_state.set_stop_after_wait(false);
|
|
|
|
|
self.app_state.set_stop_on_redraw(false);
|
|
|
|
|
|
|
|
|
|
if self.app_state.is_launched() {
|
|
|
|
|
debug_assert!(!self.app_state.is_running());
|
|
|
|
|
self.app_state.set_is_running(true);
|
|
|
|
|
self.app_state.dispatch_init_events();
|
2023-06-18 12:10:09 +01:00
|
|
|
}
|
2024-02-28 04:33:47 +01:00
|
|
|
|
|
|
|
|
// SAFETY: We do not run the application re-entrantly
|
2023-08-27 17:04:39 +02:00
|
|
|
unsafe { self.app.run() };
|
2023-06-18 12:10:09 +01:00
|
|
|
|
|
|
|
|
// While the app is running it's possible that we catch a panic
|
|
|
|
|
// to avoid unwinding across an objective-c ffi boundary, which
|
2023-12-23 23:07:55 +01:00
|
|
|
// will lead to us stopping the `NSApplication` and saving the
|
2023-06-18 12:10:09 +01:00
|
|
|
// `PanicInfo` so that we can resume the unwind at a controlled,
|
|
|
|
|
// safe point in time.
|
|
|
|
|
if let Some(panic) = self.panic_info.take() {
|
|
|
|
|
resume_unwind(panic);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.internal_exit()
|
2024-02-28 04:33:47 +01:00
|
|
|
})
|
2021-05-27 17:38:41 +02:00
|
|
|
});
|
2022-01-11 01:23:20 +01:00
|
|
|
|
2023-09-07 08:25:04 +02:00
|
|
|
Ok(())
|
2023-06-18 12:10:09 +01:00
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
pub fn pump_app_events<A: ApplicationHandler>(
|
2024-05-20 20:27:36 +04:00
|
|
|
&mut self,
|
|
|
|
|
timeout: Option<Duration>,
|
2024-07-11 15:38:09 +02:00
|
|
|
mut app: A,
|
2024-05-20 20:27:36 +04:00
|
|
|
) -> PumpStatus {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_event_handler(&mut app, || {
|
2024-02-28 04:33:47 +01:00
|
|
|
autoreleasepool(|_| {
|
2023-12-23 23:07:55 +01:00
|
|
|
// As a special case, if the application hasn't been launched yet then we at least
|
2023-06-18 12:10:09 +01:00
|
|
|
// run the loop until it has fully launched.
|
2024-08-11 23:14:18 +02:00
|
|
|
if !self.app_state.is_launched() {
|
|
|
|
|
debug_assert!(!self.app_state.is_running());
|
2023-06-18 12:10:09 +01:00
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_stop_on_launch();
|
2024-02-28 04:33:47 +01:00
|
|
|
// SAFETY: We do not run the application re-entrantly
|
|
|
|
|
unsafe { self.app.run() };
|
2023-06-18 12:10:09 +01:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
// Note: we dispatch `NewEvents(Init)` + `Resumed` events after the application
|
|
|
|
|
// has launched
|
2024-08-11 23:14:18 +02:00
|
|
|
} else if !self.app_state.is_running() {
|
2023-12-23 23:07:55 +01:00
|
|
|
// Even though the application may have been launched, it's possible we aren't
|
2023-06-18 12:10:09 +01:00
|
|
|
// running if the `EventLoop` was run before and has since
|
|
|
|
|
// exited. This indicates that we just starting to re-run
|
|
|
|
|
// the same `EventLoop` again.
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_is_running(true);
|
|
|
|
|
self.app_state.dispatch_init_events();
|
2023-06-18 12:10:09 +01:00
|
|
|
} else {
|
2023-12-23 23:07:55 +01:00
|
|
|
// Only run for as long as the given `Duration` allows so we don't block the
|
|
|
|
|
// external loop.
|
2023-07-23 23:27:38 +01:00
|
|
|
match timeout {
|
|
|
|
|
Some(Duration::ZERO) => {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_wait_timeout(None);
|
|
|
|
|
self.app_state.set_stop_before_wait(true);
|
2023-07-23 23:27:38 +01:00
|
|
|
},
|
|
|
|
|
Some(duration) => {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_stop_before_wait(false);
|
2023-07-23 23:27:38 +01:00
|
|
|
let timeout = Instant::now() + duration;
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_wait_timeout(Some(timeout));
|
|
|
|
|
self.app_state.set_stop_after_wait(true);
|
2023-07-23 23:27:38 +01:00
|
|
|
},
|
|
|
|
|
None => {
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_wait_timeout(None);
|
|
|
|
|
self.app_state.set_stop_before_wait(false);
|
|
|
|
|
self.app_state.set_stop_after_wait(true);
|
2023-07-23 23:27:38 +01:00
|
|
|
},
|
|
|
|
|
}
|
2024-08-11 23:14:18 +02:00
|
|
|
self.app_state.set_stop_on_redraw(true);
|
2024-02-28 04:33:47 +01:00
|
|
|
// SAFETY: We do not run the application re-entrantly
|
|
|
|
|
unsafe { self.app.run() };
|
2023-06-18 12:10:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// While the app is running it's possible that we catch a panic
|
|
|
|
|
// to avoid unwinding across an objective-c ffi boundary, which
|
2023-12-23 23:07:55 +01:00
|
|
|
// will lead to us stopping the application and saving the
|
2023-06-18 12:10:09 +01:00
|
|
|
// `PanicInfo` so that we can resume the unwind at a controlled,
|
|
|
|
|
// safe point in time.
|
|
|
|
|
if let Some(panic) = self.panic_info.take() {
|
|
|
|
|
resume_unwind(panic);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-11 23:14:18 +02:00
|
|
|
if self.app_state.exiting() {
|
|
|
|
|
self.app_state.internal_exit();
|
2023-09-07 08:25:04 +02:00
|
|
|
PumpStatus::Exit(0)
|
2023-06-18 12:10:09 +01:00
|
|
|
} else {
|
|
|
|
|
PumpStatus::Continue
|
|
|
|
|
}
|
2024-02-28 04:33:47 +01:00
|
|
|
})
|
2023-06-18 12:10:09 +01:00
|
|
|
})
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
}
|
2017-02-04 00:51:38 +11:00
|
|
|
|
2024-01-15 11:58:11 -08:00
|
|
|
pub(crate) struct OwnedDisplayHandle;
|
|
|
|
|
|
2024-11-13 15:29:05 +03:00
|
|
|
impl HasDisplayHandle for OwnedDisplayHandle {
|
|
|
|
|
fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
|
|
|
|
|
let raw = rwh_06::RawDisplayHandle::AppKit(rwh_06::AppKitDisplayHandle::new());
|
|
|
|
|
unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) }
|
2024-01-15 11:58:11 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 03:37:53 +01:00
|
|
|
pub(super) fn stop_app_immediately(app: &NSApplication) {
|
|
|
|
|
autoreleasepool(|_| {
|
|
|
|
|
app.stop(None);
|
|
|
|
|
// To stop event loop immediately, we need to post some event here.
|
|
|
|
|
// See: https://stackoverflow.com/questions/48041279/stopping-the-nsapplication-main-event-loop/48064752#48064752
|
|
|
|
|
app.postEvent_atStart(&dummy_event().unwrap(), true);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 19:56:39 +01:00
|
|
|
/// Catches panics that happen inside `f` and when a panic
|
|
|
|
|
/// happens, stops the `sharedApplication`
|
|
|
|
|
#[inline]
|
|
|
|
|
pub fn stop_app_on_panic<F: FnOnce() -> R + UnwindSafe, R>(
|
2023-12-23 23:07:55 +01:00
|
|
|
mtm: MainThreadMarker,
|
2021-03-08 19:56:39 +01:00
|
|
|
panic_info: Weak<PanicInfo>,
|
|
|
|
|
f: F,
|
|
|
|
|
) -> Option<R> {
|
|
|
|
|
match catch_unwind(f) {
|
|
|
|
|
Ok(r) => Some(r),
|
|
|
|
|
Err(e) => {
|
|
|
|
|
// It's important that we set the panic before requesting a `stop`
|
|
|
|
|
// because some callback are still called during the `stop` message
|
|
|
|
|
// and we need to know in those callbacks if the application is currently
|
|
|
|
|
// panicking
|
|
|
|
|
{
|
|
|
|
|
let panic_info = panic_info.upgrade().unwrap();
|
|
|
|
|
panic_info.set_panic(e);
|
|
|
|
|
}
|
2023-12-23 23:07:55 +01:00
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
2024-01-14 03:37:53 +01:00
|
|
|
stop_app_immediately(&app);
|
2021-03-08 19:56:39 +01:00
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 10:56:20 +03:00
|
|
|
#[derive(Debug)]
|
2024-06-24 13:04:55 +03:00
|
|
|
pub struct EventLoopProxy {
|
2024-11-12 10:56:20 +03:00
|
|
|
pub(crate) wake_up: AtomicBool,
|
2019-05-01 17:03:30 -06:00
|
|
|
source: CFRunLoopSourceRef,
|
|
|
|
|
}
|
2017-02-04 00:51:38 +11:00
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
unsafe impl Send for EventLoopProxy {}
|
|
|
|
|
unsafe impl Sync for EventLoopProxy {}
|
2017-02-04 00:51:38 +11:00
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
impl Drop for EventLoopProxy {
|
2020-04-20 23:48:42 +02:00
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
CFRelease(self.source as _);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-24 13:04:55 +03:00
|
|
|
impl EventLoopProxy {
|
2024-11-12 10:56:20 +03:00
|
|
|
pub(crate) fn new() -> Self {
|
2019-05-01 17:03:30 -06:00
|
|
|
unsafe {
|
2019-07-13 01:05:07 +02:00
|
|
|
// just wake up the eventloop
|
2022-09-08 21:03:25 +02:00
|
|
|
extern "C" fn event_loop_proxy_handler(_: *const c_void) {}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
|
|
|
|
// adding a Source to the main CFRunLoop lets us wake it up and
|
|
|
|
|
// process user events through the normal OS EventLoop mechanisms.
|
|
|
|
|
let rl = CFRunLoopGetMain();
|
2022-09-08 21:03:25 +02:00
|
|
|
let mut context = CFRunLoopSourceContext {
|
|
|
|
|
version: 0,
|
|
|
|
|
info: ptr::null_mut(),
|
|
|
|
|
retain: None,
|
|
|
|
|
release: None,
|
|
|
|
|
copyDescription: None,
|
|
|
|
|
equal: None,
|
|
|
|
|
hash: None,
|
|
|
|
|
schedule: None,
|
|
|
|
|
cancel: None,
|
|
|
|
|
perform: event_loop_proxy_handler,
|
|
|
|
|
};
|
2024-06-15 15:26:26 +03:00
|
|
|
let source = CFRunLoopSourceCreate(ptr::null_mut(), CFIndex::MAX - 1, &mut context);
|
2019-05-01 17:03:30 -06:00
|
|
|
CFRunLoopAddSource(rl, source, kCFRunLoopCommonModes);
|
|
|
|
|
CFRunLoopWakeUp(rl);
|
|
|
|
|
|
2024-11-12 10:56:20 +03:00
|
|
|
EventLoopProxy { wake_up: AtomicBool::new(false), source }
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-12 10:56:20 +03:00
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
|
2024-11-12 10:56:20 +03:00
|
|
|
impl EventLoopProxyProvider for EventLoopProxy {
|
|
|
|
|
fn wake_up(&self) {
|
|
|
|
|
self.wake_up.store(true, AtomicOrdering::Relaxed);
|
2017-05-31 15:00:49 +10:00
|
|
|
unsafe {
|
2024-11-12 10:56:20 +03:00
|
|
|
// Let the main thread know there's a new event.
|
2019-05-01 17:03:30 -06:00
|
|
|
CFRunLoopSourceSignal(self.source);
|
|
|
|
|
let rl = CFRunLoopGetMain();
|
|
|
|
|
CFRunLoopWakeUp(rl);
|
2017-05-31 15:00:49 +10:00
|
|
|
}
|
|
|
|
|
}
|
2017-02-03 23:05:57 +11:00
|
|
|
}
|