use concinnity_core::ecs::StepResult;
use crate::app::runtime::Runtime;
pub fn install_ctrlc_handler(runtime: &Runtime) {
let token = runtime.shutdown_token();
let installed = ctrlc::set_handler(move || {
tracing::info!("CTRL+C received, canceling all subsystems");
token.cancel();
});
if let Err(e) = installed {
tracing::warn!("CTRL+C handler not installed: {e}");
}
}
#[cfg(target_os = "macos")]
pub fn activate_app_macos() {
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy};
let mtm = objc2::MainThreadMarker::new()
.expect("activate_app_macos must be called from the main thread");
let ns_app = NSApplication::sharedApplication(mtm);
ns_app.setActivationPolicy(NSApplicationActivationPolicy::Regular);
ns_app.activate();
}
pub fn run_loop(runtime: &mut Runtime, pump_events: bool, mut on_tick: impl FnMut(&mut Runtime)) {
let shutdown = runtime.shutdown_token();
loop {
if shutdown.is_canceled() {
tracing::info!("Shutdown token canceled, exiting loop");
return;
}
#[cfg(target_os = "macos")]
if pump_events {
drain_cocoa_events();
}
#[cfg(not(target_os = "macos"))]
let _ = pump_events;
on_tick(runtime);
match runtime.world_step() {
StepResult::Continue => {}
StepResult::Stop | StepResult::Done => return,
}
}
}
#[cfg(target_os = "macos")]
pub(crate) fn drain_cocoa_events() {
use objc2_core_foundation::{CFRunLoop, CFRunLoopRunResult, kCFRunLoopDefaultMode};
let mode = unsafe { kCFRunLoopDefaultMode };
while CFRunLoop::run_in_mode(mode, 0.0, true) == CFRunLoopRunResult::HandledSource {}
}