use crate::WindowOptions;
use objc2::runtime::AnyObject;
use objc2::{define_class, msg_send, sel, MainThreadOnly};
use objc2_app_kit::{NSWindow, NSWindowDelegate, NSWindowTitleVisibility};
use objc2_foundation::{NSObjectProtocol, NSSize};
use super::options::native_window_style;
define_class!(
#[unsafe(super = NSWindow)]
#[thread_kind = MainThreadOnly]
pub(super) struct DesktopWindow;
impl DesktopWindow {
#[unsafe(method(canBecomeKeyWindow))]
fn can_become_key_window(&self) -> bool {
true
}
#[unsafe(method(canBecomeMainWindow))]
fn can_become_main_window(&self) -> bool {
true
}
#[unsafe(method(performClose:))]
fn perform_close(&self, sender: Option<&AnyObject>) {
if self
.styleMask()
.contains(objc2_app_kit::NSWindowStyleMask::Titled)
{
unsafe {
let _: () = msg_send![super(self), performClose: sender];
}
return;
}
if let Some(delegate) = self.delegate() {
if delegate.respondsToSelector(sel!(windowShouldClose:))
&& !delegate.windowShouldClose(self)
{
return;
}
}
self.close();
}
}
);
pub(super) fn apply_window_options(window: &NSWindow, options: &WindowOptions) {
let style = native_window_style(options);
if style.transparent_titlebar {
window.setTitlebarAppearsTransparent(true);
window.setTitleVisibility(NSWindowTitleVisibility::Hidden);
}
if let (Some(width), Some(height)) = (options.min_width, options.min_height) {
window.setContentMinSize(NSSize::new(f64::from(width), f64::from(height)));
}
if let (Some(width), Some(height)) = (options.max_width, options.max_height) {
window.setContentMaxSize(NSSize::new(f64::from(width), f64::from(height)));
}
if options.always_on_top {
unsafe {
let _: () = objc2::msg_send![window, setLevel: 3_i64];
};
}
}