use std::ffi::CStr;
use std::sync::OnceLock;
use objc2::msg_send;
use objc2::runtime::AnyClass;
use objc2::runtime::AnyObject;
use objc2::runtime::MethodImplementation;
use objc2::runtime::Sel;
use objc2::sel;
use objc2_app_kit::NSLayoutAttribute;
use objc2_app_kit::NSTitlebarAccessoryViewController;
use objc2_app_kit::NSWindow;
use objc2_app_kit::NSWindowStyleMask;
use objc2_app_kit::NSWindowTitleVisibility;
use objc2_foundation::NSInteger;
use objc2_foundation::NSString;
use super::appearance;
use super::layout::class_name;
use super::layout::find_descendant;
use super::layout::layout;
use super::registry;
use super::restore_group_height;
static HOOK_INSTALLED: OnceLock<()> = OnceLock::new();
unsafe fn as_window(this: &AnyObject) -> &NSWindow {
unsafe { &*(this as *const AnyObject as *const NSWindow) }
}
fn is_tab_bar(controller: &NSTitlebarAccessoryViewController) -> bool {
let view = controller.view();
find_descendant(&view, "NSTabBar").is_some()
|| class_name(view.as_ref()) == "NSTabBar"
|| (controller.layoutAttribute() == NSLayoutAttribute::Bottom
&& class_name(view.as_ref()) == "NSView"
&& view.subviews().is_empty())
}
pub fn update_titlebar_visibility(window: &NSWindow) {
let Some(configured_hidden) = registry::title_hidden(window) else { return };
let toolbar_enabled = registry::toolbar_enabled(window).unwrap_or(false);
let has_tabs = window.tabGroup().is_some_and(|group| group.isTabBarVisible());
let toolbar_visible = toolbar_enabled
&& (!window.styleMask().contains(NSWindowStyleMask::FullScreen) || has_tabs);
if let Some(toolbar) = window.toolbar()
&& toolbar.isVisible() != toolbar_visible
{
toolbar.setVisible(toolbar_visible);
}
window.setTitleVisibility(if configured_hidden || has_tabs {
NSWindowTitleVisibility::Hidden
} else {
NSWindowTitleVisibility::Visible
});
if registry::translucent_background(window).unwrap_or(false) {
appearance::update(window, true);
}
}
unsafe extern "C-unwind" fn add_accessory(
this: &AnyObject,
_sel: Sel,
controller: &NSTitlebarAccessoryViewController,
) {
let tabs = registry::is_registered(this) && is_tab_bar(controller);
if tabs {
controller.setLayoutAttribute(NSLayoutAttribute::Right);
let window = unsafe { as_window(this) };
window.setTitleVisibility(NSWindowTitleVisibility::Hidden);
if let Some(toolbar) = window.toolbar() {
toolbar.setVisible(true);
}
}
let superclass = AnyClass::get(c"NSWindow").unwrap();
let _: () = unsafe {
msg_send![super(this, superclass), addTitlebarAccessoryViewController: controller]
};
if tabs {
layout(unsafe { as_window(this) });
schedule_layout(this);
schedule_appearance(this);
}
}
unsafe extern "C-unwind" fn remove_accessory(this: &AnyObject, _sel: Sel, index: NSInteger) {
let superclass = AnyClass::get(c"NSWindow").unwrap();
let _: () = unsafe {
msg_send![super(this, superclass), removeTitlebarAccessoryViewControllerAtIndex: index]
};
if registry::is_registered(this) {
update_titlebar_visibility(unsafe { as_window(this) });
schedule_layout(this);
schedule_appearance(this);
}
}
unsafe extern "C-unwind" fn become_main_window(this: &AnyObject, _sel: Sel) {
let superclass = AnyClass::get(c"NSWindow").unwrap();
let _: () = unsafe { msg_send![super(this, superclass), becomeMainWindow] };
if registry::is_registered(this) {
layout(unsafe { as_window(this) });
schedule_layout(this);
schedule_appearance(this);
}
}
unsafe extern "C-unwind" fn set_title(this: &AnyObject, _sel: Sel, title: &NSString) {
let superclass = AnyClass::get(c"NSWindow").unwrap();
let _: () = unsafe { msg_send![super(this, superclass), setTitle: title] };
if registry::is_registered(this) {
update_titlebar_visibility(unsafe { as_window(this) });
}
}
unsafe extern "C-unwind" fn deferred_layout(this: &AnyObject, _sel: Sel) {
if registry::is_registered(this) {
let window = unsafe { as_window(this) };
update_titlebar_visibility(window);
layout(window);
restore_group_height(window);
if registry::translucent_background(window).unwrap_or(false) {
appearance::update(window, true);
}
if let Some(content) = window.contentView() {
content.setNeedsDisplay(true);
}
}
}
unsafe extern "C-unwind" fn deferred_appearance(this: &AnyObject, _sel: Sel) {
if let Some(translucent) = registry::translucent_background(unsafe { as_window(this) }) {
appearance::update(unsafe { as_window(this) }, translucent);
}
}
fn schedule_appearance(window: &AnyObject) {
unsafe {
let _: () = msg_send![window, performSelector: sel!(neovideUpdateTabAppearance),
withObject: std::ptr::null_mut::<AnyObject>(), afterDelay: 0.05_f64];
}
}
fn schedule_layout(window: &AnyObject) {
unsafe {
let _: () = msg_send![window, performSelector: sel!(neovideLayoutNativeTabs),
withObject: std::ptr::null_mut::<AnyObject>(), afterDelay: 0.0_f64];
}
}
pub fn refresh(window: &NSWindow) {
update_titlebar_visibility(window);
layout(window);
schedule_layout(window.as_ref());
}
pub fn install_class_hooks() {
HOOK_INSTALLED.get_or_init(|| {
let winit_class = AnyClass::get(c"WinitWindow").expect("winit window class is registered");
unsafe {
add_method(
winit_class,
sel!(addTitlebarAccessoryViewController:),
(add_accessory as unsafe extern "C-unwind" fn(_, _, _)).__imp(),
c"v@:@",
);
add_method(
winit_class,
sel!(removeTitlebarAccessoryViewControllerAtIndex:),
(remove_accessory as unsafe extern "C-unwind" fn(_, _, _)).__imp(),
c"v@:q",
);
add_method(
winit_class,
sel!(becomeMainWindow),
(become_main_window as unsafe extern "C-unwind" fn(_, _)).__imp(),
c"v@:",
);
add_method(
winit_class,
sel!(setTitle:),
(set_title as unsafe extern "C-unwind" fn(_, _, _)).__imp(),
c"v@:@",
);
add_method(
winit_class,
sel!(neovideLayoutNativeTabs),
(deferred_layout as unsafe extern "C-unwind" fn(_, _)).__imp(),
c"v@:",
);
add_method(
winit_class,
sel!(neovideUpdateTabAppearance),
(deferred_appearance as unsafe extern "C-unwind" fn(_, _)).__imp(),
c"v@:",
);
}
});
}
unsafe fn add_method(
class: &AnyClass,
selector: Sel,
implementation: objc2::runtime::Imp,
encoding: &CStr,
) {
let class_ptr = class as *const AnyClass as *mut AnyClass;
assert!(
unsafe {
objc2::ffi::class_addMethod(class_ptr, selector, implementation, encoding.as_ptr())
}
.as_bool()
);
}