use gpui::{App, Window, WindowHandle};
#[cfg(target_os = "macos")]
pub fn with_ns_window<F, R>(window: &Window, f: F) -> Option<R>
where
F: FnOnce(*mut objc2::runtime::AnyObject) -> R,
{
use objc2::msg_send;
use raw_window_handle::HasWindowHandle;
if let Ok(handle) = HasWindowHandle::window_handle(window) {
if let raw_window_handle::RawWindowHandle::AppKit(appkit) = handle.as_raw() {
let ns_view = appkit.ns_view.as_ptr() as *mut objc2::runtime::AnyObject;
unsafe {
let ns_window: *mut objc2::runtime::AnyObject = msg_send![ns_view, window];
if !ns_window.is_null() {
return Some(f(ns_window));
}
}
}
}
None
}
#[cfg(target_os = "macos")]
pub fn configure_borderless_overlay<V>(window: &WindowHandle<V>, click_through: bool, cx: &mut App)
where
V: 'static,
{
use objc2::{class, msg_send};
let _ = window.update(cx, |_, window, _cx| {
let _ = with_ns_window(window, |ns_window| unsafe {
let _: () = msg_send![ns_window, setHasShadow: false];
let _: () = msg_send![ns_window, setOpaque: false];
let _: () = msg_send![ns_window, setIgnoresMouseEvents: click_through];
let clear: *mut objc2::runtime::AnyObject = msg_send![class!(NSColor), clearColor];
let _: () = msg_send![ns_window, setBackgroundColor: clear];
let _: () = msg_send![ns_window, setLevel: 3i64];
let style: u64 = msg_send![ns_window, styleMask];
let _: () = msg_send![ns_window, setStyleMask: style | 128u64];
let _: () = msg_send![ns_window, setHidesOnDeactivate: false];
});
});
}
#[cfg(not(target_os = "macos"))]
pub fn configure_borderless_overlay<V>(
_window: &WindowHandle<V>,
_click_through: bool,
_cx: &mut App,
) where
V: 'static,
{
}
#[cfg(target_os = "macos")]
pub fn configure_floating_key_panel<V>(window: &WindowHandle<V>, cx: &mut App)
where
V: 'static,
{
use objc2::{class, msg_send};
let _ = window.update(cx, |_, window, _cx| {
let _ = with_ns_window(window, |ns_window| unsafe {
let clear: *mut objc2::runtime::AnyObject = msg_send![class!(NSColor), clearColor];
let _: () = msg_send![ns_window, setBackgroundColor: clear];
let _: () = msg_send![ns_window, setLevel: 3i64];
let style: u64 = msg_send![ns_window, styleMask];
let _: () = msg_send![ns_window, setStyleMask: style | 128u64];
let _: () = msg_send![ns_window, setHidesOnDeactivate: false];
let _: () = msg_send![ns_window, makeKeyAndOrderFront: ns_window];
});
});
}
#[cfg(not(target_os = "macos"))]
pub fn configure_floating_key_panel<V>(_window: &WindowHandle<V>, _cx: &mut App)
where
V: 'static,
{
}
#[cfg(target_os = "macos")]
pub fn configure_app_as_accessory() {
use objc2_app_kit::{NSApp, NSApplicationActivationPolicy};
use objc2_foundation::MainThreadMarker;
if let Some(mtm) = MainThreadMarker::new() {
let app = NSApp(mtm);
app.setActivationPolicy(NSApplicationActivationPolicy::Accessory);
}
}
#[cfg(not(target_os = "macos"))]
pub fn configure_app_as_accessory() {}
#[cfg(target_os = "macos")]
pub fn activate_app() {
use objc2_app_kit::NSApp;
use objc2_foundation::MainThreadMarker;
if let Some(mtm) = MainThreadMarker::new() {
let app = NSApp(mtm);
app.activate();
}
}
#[cfg(not(target_os = "macos"))]
pub fn activate_app() {}
#[cfg(target_os = "macos")]
pub fn activate_app_on_main_thread() {
use objc2::{class, msg_send};
unsafe {
let app: *mut objc2::runtime::AnyObject = msg_send![class!(NSApplication), sharedApplication];
let nil: *mut objc2::runtime::AnyObject = std::ptr::null_mut();
let _: () = msg_send![
app,
performSelectorOnMainThread: objc2::sel!(activate),
withObject: nil,
waitUntilDone: false
];
}
}
#[cfg(not(target_os = "macos"))]
pub fn activate_app_on_main_thread() {}