use objc2::rc::Retained;
use objc2_app_kit::{NSView, NSWindowButton};
use winit::window::Window;
use crate::Rect;
const ASSUMED: Rect = Rect {
x: 0.0,
y: 0.0,
w: 78.0,
h: 28.0,
};
pub fn native_controls(window: &Window) -> Rect {
measure(window).unwrap_or(ASSUMED)
}
fn measure(window: &Window) -> Option<Rect> {
use winit::raw_window_handle::{HasWindowHandle, RawWindowHandle};
let RawWindowHandle::AppKit(h) = window.window_handle().ok()?.as_raw() else {
return None;
};
let view = unsafe { Retained::retain(h.ns_view.as_ptr().cast::<NSView>()) }?;
let ns_window = view.window()?;
let frame = ns_window.frame();
let titlebar = frame.size.height - ns_window.contentLayoutRect().size.height;
if titlebar <= 0.0 {
return None;
}
let in_window = |b: NSWindowButton| {
let button = ns_window.standardWindowButton(b)?;
let superview = unsafe { button.superview() }?;
Some(superview.convertRect_toView(button.frame(), None))
};
let close = in_window(NSWindowButton::CloseButton)?;
let zoom = in_window(NSWindowButton::ZoomButton)?;
let leading = close.origin.x;
let trailing = zoom.origin.x + zoom.size.width;
if leading < 0.0 || trailing <= leading {
return None;
}
Some(Rect::new(
0.0,
0.0,
(trailing + leading) as f32,
titlebar as f32,
))
}