#![expect(unsafe_code, reason = "Objective-C messages to AppKit")]
use objc2::rc::Retained;
use objc2_app_kit::{NSView, NSWindow, NSWindowAnimationBehavior};
use objc2_quartz_core::CATransaction;
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use winit::window::Window;
pub(crate) fn ns_window(window: &dyn Window) -> Option<Retained<NSWindow>> {
let RawWindowHandle::AppKit(handle) = window.window_handle().ok()?.as_raw() else {
return None;
};
let view: &NSView = unsafe { handle.ns_view.cast().as_ref() };
view.window()
}
pub(crate) fn stage(window: &dyn Window) {
let Some(ns_window) = ns_window(window) else {
return;
};
ns_window.setAlphaValue(0.0);
let _ = window.set_cursor_hittest(false);
set_visible_without_open_animation(&ns_window, window);
}
pub(crate) fn reveal(window: &dyn Window) {
let Some(ns_window) = ns_window(window) else {
return;
};
ns_window.setAlphaValue(1.0);
let _ = window.set_cursor_hittest(true);
}
pub(crate) fn show_without_open_animation(window: &dyn Window, builds_pipelines: bool) {
let Some(ns_window) = ns_window(window) else {
window.set_visible(true);
return;
};
set_visible_without_open_animation(&ns_window, window);
if builds_pipelines {
CATransaction::flush();
}
}
fn set_visible_without_open_animation(ns_window: &NSWindow, window: &dyn Window) {
let behavior = ns_window.animationBehavior();
ns_window.setAnimationBehavior(NSWindowAnimationBehavior::None);
window.set_visible(true);
ns_window.setAnimationBehavior(behavior);
}