use ferrokit_core::kept::{UiCell, UiLocal};
use std::rc::Rc;
use objc2::rc::Retained;
use objc2::runtime::NSObject;
use objc2::sel;
use objc2_app_kit::{NSImage, NSPopover, NSPopoverBehavior, NSStatusBar, NSStatusItem, NSVariableStatusItemLength, NSView, NSViewController};
use objc2_foundation::{NSRectEdge, NSString};
use crate::appkit_views::ActionTarget;
use crate::chrome::MenuItem;
use crate::signal::Effect;
use crate::view::{AnyView, View};
enum Content {
None,
Menu(Vec<MenuItem>),
Panel(AnyView),
}
pub struct MenuBarExtra {
symbol: String,
label: String,
title: Option<Box<dyn Fn() -> String>>,
content: Content,
}
impl MenuBarExtra {
pub fn new(symbol: &str) -> Self {
MenuBarExtra { symbol: symbol.into(), label: String::new(), title: None, content: Content::None }
}
pub fn title(mut self, title: impl Fn() -> String + 'static) -> Self {
self.title = Some(Box::new(title));
self
}
pub fn accessibility_label(mut self, label: &str) -> Self {
self.label = label.into();
self
}
pub fn menu(mut self, items: Vec<MenuItem>) -> Self {
self.content = Content::Menu(items);
self
}
pub fn panel(mut self, view: impl View) -> Self {
self.content = Content::Panel(AnyView::new(view));
self
}
}
pub(crate) struct Installed {
pub(crate) item: Retained<NSStatusItem>,
pub(crate) popover: Option<Retained<NSPopover>>,
_keep: Vec<Retained<NSObject>>,
_effect: Option<Effect>,
_root: Option<Retained<NSView>>,
}
thread_local! {
pub(crate) static EXTRAS: UiCell<Vec<Rc<Installed>>> = const { UiCell::new(Vec::new()) };
}
pub(crate) fn install(extra: MenuBarExtra, app_name: &str) {
let mtm = crate::mtm();
let item = NSStatusBar::systemStatusBar().statusItemWithLength(NSVariableStatusItemLength);
let Some(button) = item.button(mtm) else { return };
let label = if extra.label.is_empty() { app_name.to_string() } else { extra.label };
if let Some(image) = NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(&extra.symbol), Some(&NSString::from_str(&label))) {
image.setTemplate(true);
button.setImage(Some(&image));
}
objc2_app_kit::NSAccessibility::setAccessibilityLabel(&*button, Some(&NSString::from_str(&label)));
let effect = extra.title.map(|title| {
let b = button.clone();
Effect::new(move || {
let t = title();
b.setTitle(&NSString::from_str(&if t.is_empty() { t } else { format!(" {t}") }));
})
});
let mut keep = Vec::new();
let mut popover = None;
let mut root = None;
match extra.content {
Content::None => {}
Content::Menu(items) => {
let (menu, targets) = crate::chrome::build_menu(mtm, items);
item.setMenu(Some(&menu));
keep.extend(targets);
}
Content::Panel(view) => {
let (r, size) = crate::host_view(view);
let p = NSPopover::new(mtm);
p.setBehavior(NSPopoverBehavior::Transient);
p.setAnimates(true);
let controller = NSViewController::new(mtm);
controller.setView(&r);
p.setContentViewController(Some(&controller));
p.setContentSize(size);
let (p2, b, r2) = (p.clone(), button.clone(), r.clone());
let target = ActionTarget::new(mtm, move || {
if p2.isShown() {
unsafe { p2.performClose(None) };
} else {
if let Some(size) = crate::hosted_ideal(&r2) {
p2.setContentSize(size);
}
p2.showRelativeToRect_ofView_preferredEdge(b.bounds(), &b, NSRectEdge::MinY);
}
});
unsafe {
button.setTarget(Some(&target));
button.setAction(Some(sel!(fire:)));
}
keep.push(Retained::into_super(target));
popover = Some(p);
root = Some(r);
}
}
EXTRAS.with_borrow_mut(|e| e.push(Rc::new(Installed { item, popover, _keep: keep, _effect: effect, _root: root })));
}