use std::ffi::c_void;
use std::os::raw::c_ulong;
use anyhow::Result;
use objc2::msg_send;
use objc2::runtime::AnyObject;
unsafe extern "C" {
fn CGWindowLevelForKey(key: i32) -> i32;
}
const UNDERLAY_COLLECTION_BEHAVIOR: c_ulong = 1 << 0 | 1 << 4 | 1 << 6;
pub(super) unsafe fn set_underlay(ns_window: *mut c_void) -> Result<()> {
let ns_window = ns_window as *mut AnyObject;
let level = unsafe { CGWindowLevelForKey(2) } - 1;
let () = msg_send![ns_window, setLevel: level];
let behavior: c_ulong = msg_send![ns_window, collectionBehavior];
let () = msg_send![ns_window, setCollectionBehavior: behavior | UNDERLAY_COLLECTION_BEHAVIOR];
Ok(())
}
pub(super) unsafe fn unset_underlay(ns_window: *mut c_void) -> Result<()> {
let ns_window = ns_window as *mut AnyObject;
let level = unsafe { CGWindowLevelForKey(4) };
let () = msg_send![ns_window, setLevel: level];
let behavior: c_ulong = msg_send![ns_window, collectionBehavior];
let () = msg_send![ns_window, setCollectionBehavior: behavior & !UNDERLAY_COLLECTION_BEHAVIOR];
Ok(())
}