use objc2::rc::Retained;
use objc2::{define_class, msg_send, MainThreadMarker, MainThreadOnly};
use objc2_app_kit::{
NSBackingStoreType, NSScreen, NSWindow, NSWindowOcclusionState, NSWindowStyleMask,
};
use objc2_foundation::NSRect;
define_class!(
#[unsafe(super = NSWindow)]
#[thread_kind = MainThreadOnly]
#[name = "VsHeadlessWindow"]
pub(super) struct HeadlessWindow;
impl HeadlessWindow {
#[unsafe(method(isVisible))]
fn is_visible(&self) -> bool {
true
}
#[unsafe(method(occlusionState))]
fn occlusion_state(&self) -> NSWindowOcclusionState {
NSWindowOcclusionState::Visible
}
#[unsafe(method(isKeyWindow))]
fn is_key_window(&self) -> bool {
true
}
#[unsafe(method(canBecomeKeyWindow))]
fn can_become_key_window(&self) -> bool {
true
}
#[unsafe(method(screen))]
fn screen(&self) -> *mut NSScreen {
NSScreen::mainScreen(MainThreadMarker::from(self))
.map_or(std::ptr::null_mut(), |s| Retained::as_ptr(&s).cast_mut())
}
}
);
impl HeadlessWindow {
pub(super) fn host(
mtm: MainThreadMarker,
frame: NSRect,
style: NSWindowStyleMask,
backing: NSBackingStoreType,
) -> Retained<NSWindow> {
let this: Retained<Self> = unsafe {
msg_send![
Self::alloc(mtm),
initWithContentRect: frame,
styleMask: style,
backing: backing,
defer: false,
]
};
this.into_super()
}
}