use super::runtime_mailbox::{RuntimeMailbox, RuntimeStatus};
use objc2::rc::Retained;
use objc2::runtime::{AnyClass, AnyObject, Bool, Imp, Sel};
use objc2::sel;
use objc2_app_kit::{NSApplication, NSModalPanelRunLoopMode};
use objc2_foundation::{MainThreadMarker, NSRunLoop, NSRunLoopCommonModes, NSTimer};
use std::cell::{Cell, RefCell};
use std::ffi::c_char;
use std::marker::PhantomData;
use std::rc::Rc;
use std::sync::Arc;
struct Active {
mailbox: Arc<RuntimeMailbox>,
timer: Option<Retained<NSTimer>>,
pending: bool,
replied: bool,
}
impl Active {
fn begin(&mut self) -> bool {
if self.pending {
return false;
}
self.pending = true;
self.mailbox.request_shutdown();
true
}
fn completion(&mut self) -> Option<RuntimeStatus> {
if !self.pending || self.replied {
return None;
}
let status = self.mailbox.status();
if matches!(status, RuntimeStatus::Stopped { .. }) {
self.replied = true;
Some(status)
} else {
None
}
}
}
thread_local! {
static ACTIVE: RefCell<Option<Active>> = const { RefCell::new(None) };
static INSTALLED: Cell<bool> = const { Cell::new(false) };
}
#[link(name = "objc")]
extern "C" {
fn class_addMethod(
class: *const AnyClass,
selector: Sel,
imp: Imp,
types: *const c_char,
) -> Bool;
}
unsafe extern "C" fn should_terminate(receiver: &AnyObject, _: Sel, _: &NSApplication) -> usize {
ACTIVE.with(|slot| {
let mut slot = slot.borrow_mut();
let Some(active) = slot.as_mut() else {
return 1;
}; if !active.begin() {
return 2; }
let timer = unsafe {
NSTimer::timerWithTimeInterval_target_selector_userInfo_repeats(
0.01,
receiver,
sel!(systemlessPollRuntimeTermination:),
None,
true,
)
};
let run_loop = unsafe { NSRunLoop::mainRunLoop() };
unsafe {
run_loop.addTimer_forMode(&timer, NSModalPanelRunLoopMode);
run_loop.addTimer_forMode(&timer, NSRunLoopCommonModes);
}
active.timer = Some(timer);
2
})
}
unsafe extern "C" fn poll_termination(_: &AnyObject, _: Sel, _: &NSTimer) {
let finished = ACTIVE.with(|slot| {
let mut slot = slot.borrow_mut();
let Some(active) = slot.as_mut() else {
return false;
};
let Some(RuntimeStatus::Stopped { error, .. }) = active.completion() else {
return false;
};
if let Some(error) = error {
eprintln!("[SYSTEMLESS] Runtime stopped during native Quit: {error}");
}
if let Some(timer) = active.timer.take() {
unsafe { timer.invalidate() };
}
true
});
if finished {
let mtm = MainThreadMarker::new().expect("termination timer runs on main thread");
unsafe { NSApplication::sharedApplication(mtm).replyToApplicationShouldTerminate(true) };
}
}
pub(super) struct NativeTermination(PhantomData<Rc<()>>);
impl NativeTermination {
pub fn install() -> Result<Self, String> {
let mtm = MainThreadMarker::new().ok_or("native termination requires main thread")?;
if ACTIVE.with(|slot| slot.borrow().is_some()) {
return Err("native termination already has an active runtime".into());
}
if !INSTALLED.with(Cell::get) {
let app = NSApplication::sharedApplication(mtm);
let delegate = unsafe { app.delegate() }.ok_or("AppKit has no event-loop delegate")?;
let object = unsafe { &*Retained::as_ptr(&delegate).cast::<AnyObject>() };
let class = object.class();
let terminate = sel!(applicationShouldTerminate:);
let poll = sel!(systemlessPollRuntimeTermination:);
if class.instance_method(terminate).is_some() || class.instance_method(poll).is_some() {
return Err("AppKit delegate already implements a termination hook".into());
}
let installed = unsafe {
class_addMethod(
class,
poll,
std::mem::transmute::<unsafe extern "C" fn(&AnyObject, Sel, &NSTimer), Imp>(
poll_termination,
),
b"v@:@\0".as_ptr().cast(),
)
.as_bool()
&& class_addMethod(
class,
terminate,
std::mem::transmute::<
unsafe extern "C" fn(&AnyObject, Sel, &NSApplication) -> usize,
Imp,
>(should_terminate),
b"Q@:@\0".as_ptr().cast(),
)
.as_bool()
};
if !installed {
return Err("could not install AppKit termination callbacks".into());
}
INSTALLED.with(|installed| installed.set(true));
}
Ok(Self(PhantomData))
}
pub fn activate(&self, mailbox: Arc<RuntimeMailbox>) {
activate(mailbox);
}
}
pub(super) fn activate(mailbox: Arc<RuntimeMailbox>) {
ACTIVE.with(|slot| {
let mut slot = slot.borrow_mut();
assert!(!slot.as_ref().is_some_and(|active| active.pending));
*slot = Some(Active {
mailbox,
timer: None,
pending: false,
replied: false,
});
});
}
pub(super) fn pending() -> bool {
ACTIVE.with(|slot| slot.borrow().as_ref().is_some_and(|active| active.pending))
}
impl Drop for NativeTermination {
fn drop(&mut self) {
let active = ACTIVE.with(|slot| slot.borrow_mut().take());
if let Some(mut active) = active {
if let Some(timer) = active.timer.take() {
unsafe { timer.invalidate() };
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn native_quit_waits_for_terminal_status_and_replies_once() {
let mailbox = Arc::new(RuntimeMailbox::new(|| {}));
let mut active = Active {
mailbox: mailbox.clone(),
timer: None,
pending: false,
replied: false,
};
assert!(active.completion().is_none());
assert!(active.begin());
assert!(mailbox.shutdown_requested());
assert!(!active.begin());
assert!(active.completion().is_none());
mailbox.set_status(RuntimeStatus::Ready);
assert!(active.completion().is_none());
let stopped = RuntimeStatus::Stopped {
error: Some("save error".into()),
instructions: 42,
};
mailbox.set_status(stopped.clone());
assert_eq!(active.completion(), Some(stopped.clone()));
assert!(active.completion().is_none());
assert_eq!(mailbox.poll().status, stopped);
}
}