use std::cell::RefCell;
use std::rc::{Rc, Weak};
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject, NSObjectProtocol, ProtocolObject};
use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, define_class, msg_send, sel};
use objc2_app_kit::{
NSControlStateValueOff, NSControlStateValueOn, NSEventModifierFlags, NSImage, NSMenu, NSMenuDelegate, NSMenuItem, NSResponder, NSSearchToolbarItem, NSSplitView, NSSplitViewDelegate, NSSplitViewDividerStyle, NSToolbar,
NSToolbarDelegate, NSToolbarItem, NSView, NSWindow, NSWindowToolbarStyle,
};
use objc2_foundation::{NSArray, NSNotification, NSSize, NSString, NSUserDefaults};
use crate::controls::FieldDelegate;
use crate::signal::{Effect, Signal, batch};
use crate::view::{AnyView, Cx, Kind, Meta, Node, View};
define_class!(
#[unsafe(super(NSView, NSResponder, NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitSplitPane"]
#[ivars = RefCell<Weak<Meta>>]
pub(crate) struct SplitPane;
impl SplitPane {
#[unsafe(method(isFlipped))]
fn is_flipped(&self) -> bool {
true
}
#[unsafe(method(setFrameSize:))]
fn set_frame_size(&self, size: NSSize) {
crate::failure::guard("a window's chrome (set_frame_size)", (), || {
let _: () = unsafe { msg_send![super(self), setFrameSize: size] };
if crate::in_layout() {
return; }
if let Some(meta) = self.ivars().borrow().upgrade() {
meta.mark();
crate::relayout_now();
}
})
}
}
);
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitSplitDelegate"]
#[ivars = SplitInfo]
pub(crate) struct SplitDelegate;
unsafe impl NSObjectProtocol for SplitDelegate {}
unsafe impl NSSplitViewDelegate for SplitDelegate {
#[unsafe(method(splitView:shouldAdjustSizeOfSubview:))]
fn should_adjust(&self, split: &NSSplitView, view: &NSView) -> bool {
crate::failure::guard("a window's chrome (should_adjust)", true, || {
let panes = split.arrangedSubviews();
let index = panes.iter().position(|p| std::ptr::eq(&*p, view));
panes.count() < 2 || !index.is_some_and(|i| i == 0 || self.ivars().collapsible.iter().any(|(c, _)| *c == i))
})
}
#[unsafe(method(splitView:canCollapseSubview:))]
fn can_collapse(&self, split: &NSSplitView, view: &NSView) -> bool {
crate::failure::guard("a window's chrome (can_collapse)", false, || {
let index = split.arrangedSubviews().iter().position(|p| std::ptr::eq(&*p, view));
index.is_some_and(|i| self.ivars().collapsible.iter().any(|(c, _)| *c == i))
})
}
#[unsafe(method(splitViewDidResizeSubviews:))]
fn did_resize(&self, notification: &NSNotification) {
crate::failure::guard("a window's chrome (did_resize)", (), || {
let Some(split) = notification.object().and_then(|o| o.downcast::<NSSplitView>().ok()) else { return };
if let Some(f) = &*self.ivars().on_resize.borrow() {
f(&split);
}
let panes = split.arrangedSubviews();
for (i, collapsed) in self.ivars().collapsible.iter().filter(|_| !self.ivars().applying.get()) {
if let Some(pane) = (*i < panes.count()).then(|| panes.objectAtIndex(*i)) {
let size = if split.isVertical() { pane.frame().size.width } else { pane.frame().size.height };
collapsed.set_if_changed(split.isSubviewCollapsed(&pane) || pane.isHidden() || (size < 1.0 && !crate::in_layout()));
}
}
let (Some(key), false) = (&self.ivars().key, crate::in_layout()) else { return };
if let Some(first) = panes.firstObject().filter(|f| !f.isHidden())
{
let size = first.frame().size;
NSUserDefaults::standardUserDefaults().setDouble_forKey(if split.isVertical() { size.width } else { size.height }, key);
}
})
}
}
);
pub(crate) struct SplitInfo {
key: Option<Retained<NSString>>,
collapsible: Vec<(usize, Signal<bool>)>,
applying: std::cell::Cell<bool>,
on_resize: RefCell<Option<OnResize>>,
}
type OnResize = Box<dyn Fn(&NSSplitView)>;
pub struct Split {
panes: Vec<AnyView>,
position: Option<f64>,
remember: Option<String>,
stacked: bool,
collapsible: Vec<(usize, Signal<bool>)>,
trailing: Option<f64>,
}
impl Split {
pub fn collapsible(mut self, index: usize, collapsed: &Signal<bool>) -> Self {
self.collapsible.push((index, *collapsed));
self
}
pub fn trailing(mut self, points: f64) -> Self {
self.trailing = Some(points);
self
}
pub fn stacked(mut self) -> Self {
self.stacked = true;
self
}
pub fn position(mut self, points: f64) -> Self {
self.position = Some(points);
self
}
pub fn remember(mut self, name: &str) -> Self {
self.remember = Some(name.into());
self
}
}
pub(crate) fn split_key(name: &str) -> Retained<NSString> {
NSString::from_str(&format!("ferrokit split {name}"))
}
impl View for Split {
fn build(self, cx: &mut Cx) -> Node {
let mtm = cx.mtm;
let split = NSSplitView::new(mtm);
split.setVertical(!self.stacked);
split.setDividerStyle(NSSplitViewDividerStyle::Thin);
let key = self.remember.as_deref().map(split_key);
let defaults = NSUserDefaults::standardUserDefaults();
let saved = key.as_ref().filter(|k| defaults.objectForKey(k).is_some()).map(|k| defaults.doubleForKey(k));
let delegate = SplitDelegate::alloc(mtm).set_ivars(SplitInfo { key, collapsible: self.collapsible.clone(), applying: std::cell::Cell::new(false), on_resize: RefCell::default() });
let delegate: Retained<SplitDelegate> = unsafe { msg_send![super(delegate), init] };
split.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
cx.container.addSubview(&split);
let (mut panes, mut typed) = (Vec::new(), Vec::new());
for child in self.panes {
let pane = SplitPane::alloc(mtm).set_ivars(RefCell::new(Weak::new()));
let pane: Retained<SplitPane> = unsafe { msg_send![super(pane), init] };
split.addArrangedSubview(&pane);
let node = child.build_node(&mut Cx::new(Retained::into_super(pane.clone())));
panes.push((Retained::into_super(pane.clone()), node));
typed.push(pane);
}
let mut node = Node::new(Kind::Split {
split,
panes,
position: std::cell::Cell::new(saved.map(|p| (p, false)).or(self.position.map(|p| (p, true)))),
trailing: std::cell::Cell::new(self.trailing),
stacked: self.stacked,
});
node.keep(Retained::into_super(delegate.clone()));
let count = typed.len();
for (i, collapsed) in self.collapsible {
let Some(pane) = typed.get(i).cloned() else { continue };
let meta = Rc::downgrade(&node.meta);
let given = if i == 0 { self.position } else if i + 1 == count { self.trailing } else { None };
let size = std::cell::Cell::new(given.unwrap_or(220.0));
let stacked = self.stacked;
let delegate = delegate.clone();
node.own(Effect::new(move || {
let hide = collapsed.get();
if pane.isHidden() != hide {
delegate.ivars().applying.set(true);
let along = |r: objc2_foundation::NSRect| if stacked { r.size.height } else { r.size.width };
if hide && along(pane.frame()) > 20.0 {
size.set(along(pane.frame()));
}
pane.setHidden(hide);
if let Some(split) = unsafe { pane.superview() }.and_then(|s| s.downcast::<NSSplitView>().ok()) {
split.adjustSubviews();
if !hide && along(pane.frame()) < 20.0 {
if i == 0 {
split.setPosition_ofDividerAtIndex(size.get(), 0);
} else if i + 1 == count {
let total = along(split.bounds());
split.setPosition_ofDividerAtIndex(total - size.get() - split.dividerThickness(), (count - 2) as isize);
}
}
}
delegate.ivars().applying.set(false);
if let Some(split) = unsafe { pane.superview() } {
relayout_all(&split);
}
if let Some(meta) = meta.upgrade() {
meta.mark();
}
crate::view::invalidate_layout();
}
}));
}
for pane in typed {
*pane.ivars().borrow_mut() = Rc::downgrade(&node.meta);
}
node
}
}
fn relayout_all(view: &NSView) {
view.setNeedsLayout(true);
view.setNeedsDisplay(true);
for sub in view.subviews().iter() {
relayout_all(&sub);
}
}
pub fn split(panes: impl crate::view::Views) -> Split {
Split { panes: panes.into_views(), position: None, remember: None, stacked: false, collapsible: Vec::new(), trailing: None }
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitMenuTarget"]
#[ivars = MenuAction]
pub(crate) struct MenuTarget;
impl MenuTarget {
#[unsafe(method(fire:))]
fn fire(&self, _sender: Option<&AnyObject>) {
crate::models::flush();
crate::failure::entry("a menu item's action", (), || (self.ivars().action)());
}
#[unsafe(method(validateMenuItem:))]
fn validate(&self, item: &NSMenuItem) -> bool {
crate::failure::guard("a window's chrome (validate)", false, || {
if let Some(checked) = &self.ivars().checked {
item.setState(if checked() { NSControlStateValueOn } else { NSControlStateValueOff });
}
if let Some(titled) = &self.ivars().titled {
item.setTitle(&NSString::from_str(&titled()));
}
self.ivars().enabled.as_ref().is_none_or(|f| f())
})
}
}
);
pub(crate) struct MenuAction {
action: Box<dyn Fn()>,
enabled: Option<Box<dyn Fn() -> bool>>,
checked: Option<Box<dyn Fn() -> bool>>,
titled: Option<Box<dyn Fn() -> String>>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Modifier {
Command,
Shift,
Option,
Control,
}
pub struct MenuItem(Entry);
enum Entry {
Action {
title: String,
action: Box<dyn Fn()>,
enabled: Option<Box<dyn Fn() -> bool>>,
checked: Option<Box<dyn Fn() -> bool>>,
titled: Option<Box<dyn Fn() -> String>>,
key: Option<(String, NSEventModifierFlags)>,
},
Separator,
Standard { title: String, action: objc2::runtime::Sel, key: &'static str, modifiers: NSEventModifierFlags, tag: isize, font_manager: bool },
Submenu { title: String, items: Vec<MenuItem> },
Dynamic { title: String, items: Rc<dyn Fn() -> Vec<MenuItem>> },
}
impl MenuItem {
pub(crate) fn has_command_key(&self, key: &str) -> bool {
matches!(&self.0, Entry::Action { key: Some((k, flags)), .. } if k == key && *flags == NSEventModifierFlags::Command)
}
pub fn disabled(mut self, when: impl crate::IntoValue<bool>) -> Self {
if let Entry::Action { enabled, .. } = &mut self.0 {
let when = when.into_value();
*enabled = Some(Box::new(move || !when.get()));
}
self
}
pub fn checked(mut self, when: impl crate::IntoValue<bool>) -> Self {
if let Entry::Action { checked, .. } = &mut self.0 {
let when = when.into_value();
*checked = Some(Box::new(move || when.get()));
}
self
}
pub fn key(self, key: &str) -> Self {
self.key_with(key, &[])
}
pub fn key_with(mut self, key: &str, modifiers: &[Modifier]) -> Self {
let mut flags = NSEventModifierFlags::Command;
for m in modifiers {
flags |= match m {
Modifier::Command => NSEventModifierFlags::Command,
Modifier::Shift => NSEventModifierFlags::Shift,
Modifier::Option => NSEventModifierFlags::Option,
Modifier::Control => NSEventModifierFlags::Control,
};
}
if let Entry::Action { key: k, .. } = &mut self.0 {
*k = Some((key.into(), flags));
}
self
}
}
pub fn menu_item(title: impl crate::IntoValue<String>, action: impl Fn() + 'static) -> MenuItem {
let (title, titled): (String, Option<Box<dyn Fn() -> String>>) = match title.into_value() {
crate::Value::Fixed(t) => (t, None),
crate::Value::Computed(f) => (crate::signal::untrack(|| f()), Some(Box::new(move || f()))),
};
MenuItem(Entry::Action { title, action: Box::new(action), enabled: None, checked: None, titled, key: None })
}
pub fn format_menu() -> Vec<MenuItem> {
use objc2_app_kit::NSEventModifierFlags as M;
let item = |title: &str, action, key, modifiers, tag, font_manager| MenuItem(Entry::Standard { title: crate::t(title), action, key, modifiers, tag, font_manager });
let (cmd, shift, opt, ctrl) = (M::Command, M::Shift, M::Option, M::Control);
vec![
submenu(&crate::t("Font"), vec![
item("Show Fonts", sel!(orderFrontFontPanel:), "t", cmd, 0, true),
item("Bold", sel!(addFontTrait:), "b", cmd, 2, true),
item("Italic", sel!(addFontTrait:), "i", cmd, 1, true),
item("Underline", sel!(underline:), "u", cmd, 0, false),
menu_separator(),
item("Bigger", sel!(modifyFont:), "+", cmd, 3, true),
item("Smaller", sel!(modifyFont:), "-", cmd, 4, true),
menu_separator(),
item("Show Colors", sel!(orderFrontColorPanel:), "C", cmd | shift, 0, false),
menu_separator(),
item("Copy Style", sel!(copyFont:), "c", cmd | opt, 0, false),
item("Paste Style", sel!(pasteFont:), "v", cmd | opt, 0, false),
]),
submenu(&crate::t("Text"), vec![
item("Align Left", sel!(alignLeft:), "{", cmd, 0, false),
item("Center", sel!(alignCenter:), "|", cmd, 0, false),
item("Justify", sel!(alignJustified:), "", cmd, 0, false),
item("Align Right", sel!(alignRight:), "}", cmd, 0, false),
menu_separator(),
item("Show Ruler", sel!(toggleRuler:), "r", cmd | ctrl, 0, false),
item("Copy Ruler", sel!(copyRuler:), "c", cmd | ctrl, 0, false),
item("Paste Ruler", sel!(pasteRuler:), "v", cmd | ctrl, 0, false),
]),
]
}
pub fn customize_toolbar_item() -> MenuItem {
let toolbar = || {
let mtm = MainThreadMarker::new()?;
objc2_app_kit::NSApplication::sharedApplication(mtm).mainWindow()?.toolbar().filter(|t| t.allowsUserCustomization())
};
menu_item(crate::t("Customize Toolbar…"), move || {
if let Some(t) = toolbar() {
unsafe { t.runCustomizationPalette(None) };
}
})
.disabled(move || toolbar().is_none())
}
pub fn menu_separator() -> MenuItem {
MenuItem(Entry::Separator)
}
pub fn submenu(title: &str, items: Vec<MenuItem>) -> MenuItem {
MenuItem(Entry::Submenu { title: title.into(), items })
}
pub fn dynamic_submenu(title: &str, items: impl Fn() -> Vec<MenuItem> + 'static) -> MenuItem {
MenuItem(Entry::Dynamic { title: title.into(), items: Rc::new(items) })
}
pub(crate) struct DynamicItems {
items: Rc<dyn Fn() -> Vec<MenuItem>>,
targets: RefCell<Vec<Retained<NSObject>>>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitDynamicMenu"]
#[ivars = DynamicItems]
pub(crate) struct DynamicMenu;
unsafe impl NSObjectProtocol for DynamicMenu {}
unsafe impl NSMenuDelegate for DynamicMenu {
#[unsafe(method(menuNeedsUpdate:))]
fn menu_needs_update(&self, menu: &NSMenu) {
crate::failure::guard("a window's chrome (menu_needs_update)", (), || {
self.refill(menu);
})
}
}
);
impl DynamicMenu {
pub(crate) fn refill(&self, menu: &NSMenu) {
let mtm = MainThreadMarker::new().expect("main thread");
let (built, targets) = build_menu(mtm, (self.ivars().items)());
menu.removeAllItems();
for item in built.itemArray().iter() {
built.removeItem(&item);
menu.addItem(&item);
}
*self.ivars().targets.borrow_mut() = targets;
}
}
pub(crate) fn build_menu(mtm: MainThreadMarker, items: Vec<MenuItem>) -> (Retained<NSMenu>, Vec<Retained<NSObject>>) {
build_titled_menu(mtm, "", items)
}
pub(crate) fn build_titled_menu(mtm: MainThreadMarker, title: &str, items: Vec<MenuItem>) -> (Retained<NSMenu>, Vec<Retained<NSObject>>) {
let menu = NSMenu::initWithTitle(NSMenu::alloc(mtm), &NSString::from_str(title));
let mut keep = Vec::new();
for item in items {
match item.0 {
Entry::Separator => menu.addItem(&NSMenuItem::separatorItem(mtm)),
Entry::Standard { title, action, key, modifiers, tag, font_manager } => {
let ns = plain_item(mtm, &title, Some(action), key);
ns.setKeyEquivalentModifierMask(modifiers);
ns.setTag(tag);
if font_manager {
unsafe { ns.setTarget(Some(&objc2_app_kit::NSFontManager::sharedFontManager(mtm))) };
}
menu.addItem(&ns);
}
Entry::Submenu { title, items } => {
let (sub, targets) = build_titled_menu(mtm, &title, items);
let holder = plain_item(mtm, &title, None, "");
holder.setSubmenu(Some(&sub));
menu.addItem(&holder);
keep.extend(targets);
}
Entry::Dynamic { title, items } => {
let sub = NSMenu::initWithTitle(NSMenu::alloc(mtm), &NSString::from_str(&title));
let delegate = DynamicMenu::alloc(mtm).set_ivars(DynamicItems { items, targets: RefCell::default() });
let delegate: Retained<DynamicMenu> = unsafe { msg_send![super(delegate), init] };
delegate.refill(&sub);
sub.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
let holder = plain_item(mtm, &title, None, "");
holder.setSubmenu(Some(&sub));
menu.addItem(&holder);
keep.push(Retained::into_super(delegate));
}
Entry::Action { title, action, enabled, checked, titled, key } => {
let target = MenuTarget::alloc(mtm).set_ivars(MenuAction { action, enabled, checked, titled });
let target: Retained<MenuTarget> = unsafe { msg_send![super(target), init] };
let (k, flags) = key.unwrap_or((String::new(), NSEventModifierFlags::Command));
let ns = plain_item(mtm, &title, Some(sel!(fire:)), &k);
ns.setKeyEquivalentModifierMask(flags);
unsafe { ns.setTarget(Some(&target)) };
menu.addItem(&ns);
keep.push(Retained::into_super(target));
}
}
}
(menu, keep)
}
pub(crate) fn plain_item(mtm: MainThreadMarker, title: &str, action: Option<objc2::runtime::Sel>, key: &str) -> Retained<NSMenuItem> {
unsafe {
NSMenuItem::initWithTitle_action_keyEquivalent(NSMenuItem::alloc(mtm), &NSString::from_str(title), action, &NSString::from_str(key))
}
}
pub(crate) fn menu_host(view: &NSView) -> Retained<NSView> {
use objc2::Message as _;
match view.downcast_ref::<objc2_app_kit::NSScrollView>() {
Some(s) => s.documentView().unwrap_or_else(|| view.retain()),
None => view.retain(),
}
}
pub enum ToolbarItem {
Button {
id: String,
label: String,
symbol: String,
action: Box<dyn Fn()>,
disabled: Option<Box<dyn Fn() -> bool>>,
},
FlexibleSpace,
Space,
Search {
id: String,
query: Signal<String>,
},
Group {
id: String,
label: String,
buttons: Vec<ToolbarItem>,
},
Picker {
id: String,
label: String,
choices: Vec<(String, String)>,
selected: Box<dyn Fn() -> usize>,
choose: Box<dyn Fn(usize)>,
},
Optional(Box<ToolbarItem>),
}
impl ToolbarItem {
pub fn disabled(self, when: impl crate::IntoValue<bool>) -> Self {
match self {
ToolbarItem::Button { id, label, symbol, action, .. } => {
let when = when.into_value();
ToolbarItem::Button { id, label, symbol, action, disabled: Some(Box::new(move || when.get())) }
}
ToolbarItem::Optional(item) => ToolbarItem::Optional(Box::new(item.disabled(when))),
other => other,
}
}
pub fn optional(self) -> Self {
match self {
ToolbarItem::Optional(_) => self,
other => ToolbarItem::Optional(Box::new(other)),
}
}
fn id(&self) -> String {
match self {
ToolbarItem::Button { id, .. } | ToolbarItem::Search { id, .. } | ToolbarItem::Group { id, .. } | ToolbarItem::Picker { id, .. } => id.clone(),
ToolbarItem::FlexibleSpace => unsafe { objc2_app_kit::NSToolbarFlexibleSpaceItemIdentifier }.to_string(),
ToolbarItem::Space => unsafe { objc2_app_kit::NSToolbarSpaceItemIdentifier }.to_string(),
ToolbarItem::Optional(item) => item.id(),
}
}
}
pub fn toolbar_button(id: &str, label: &str, symbol: &str, action: impl Fn() + 'static) -> ToolbarItem {
ToolbarItem::Button { id: id.into(), label: label.into(), symbol: symbol.into(), action: Box::new(action), disabled: None }
}
pub fn toolbar_space() -> ToolbarItem {
ToolbarItem::FlexibleSpace
}
pub fn toolbar_fixed_space() -> ToolbarItem {
ToolbarItem::Space
}
pub fn toolbar_search(id: &str, query: &Signal<String>) -> ToolbarItem {
ToolbarItem::Search { id: id.into(), query: *query }
}
pub fn toolbar_group(id: &str, label: &str, buttons: Vec<ToolbarItem>) -> ToolbarItem {
ToolbarItem::Group { id: id.into(), label: label.into(), buttons }
}
pub fn toolbar_picker<T: Clone + PartialEq + 'static>(id: &str, label: &str, value: &Signal<T>, choices: Vec<(&str, &str, T)>) -> ToolbarItem {
let value = *value;
let values: Rc<Vec<T>> = Rc::new(choices.iter().map(|c| c.2.clone()).collect());
let v2 = values.clone();
ToolbarItem::Picker {
id: id.into(),
label: label.into(),
choices: choices.iter().map(|(l, s, _)| (l.to_string(), s.to_string())).collect(),
selected: Box::new(move || value.with(|v| values.iter().position(|c| c == v).unwrap_or(0))),
choose: Box::new(move |i| {
if let Some(v) = v2.get(i) {
value.set(v.clone());
}
}),
}
}
enum Spec {
Button { label: String, symbol: String, action: Rc<dyn Fn(isize)>, disabled: Option<Rc<dyn Fn() -> bool>> },
Search { query: Signal<String> },
Group { label: String, buttons: Vec<(String, String)>, action: Rc<dyn Fn(isize)> },
Picker { label: String, choices: Vec<(String, String)>, selected: Rc<dyn Fn() -> usize>, choose: Rc<dyn Fn(isize)> },
Standard,
}
struct ToolbarEntry {
id: String,
spec: Spec,
optional: bool,
}
impl ToolbarEntry {
fn new(item: ToolbarItem) -> ToolbarEntry {
let id = item.id();
let act = |f: Box<dyn Fn()>| -> Rc<dyn Fn(isize)> {
let f = crate::signal::with_current_owner(f);
Rc::new(move |_| f())
};
let (spec, optional) = match item {
ToolbarItem::Optional(item) => (ToolbarEntry::new(*item).spec, true),
ToolbarItem::Button { label, symbol, action, disabled, .. } => (Spec::Button { label, symbol, action: act(action), disabled: disabled.map(Rc::from) }, false),
ToolbarItem::Search { query, .. } => (Spec::Search { query }, false),
ToolbarItem::Group { label, buttons, .. } => {
let (mut shown, mut actions) = (Vec::new(), Vec::new());
for b in buttons {
if let ToolbarItem::Button { label, symbol, action, .. } = b {
shown.push((label, symbol));
actions.push(action);
}
}
let actions: Vec<Box<dyn Fn()>> = actions.into_iter().map(|a| Box::new(crate::signal::with_current_owner(a)) as Box<dyn Fn()>).collect();
let action = move |i: isize| {
if let Some(a) = usize::try_from(i).ok().and_then(|i| actions.get(i)) {
a();
}
};
(Spec::Group { label, buttons: shown, action: Rc::new(action) }, false)
}
ToolbarItem::Picker { label, choices, selected, choose, .. } => {
let choose: Rc<dyn Fn(usize)> = Rc::from(choose);
(Spec::Picker { label, choices, selected: Rc::from(selected), choose: Rc::new(move |i| choose(i.max(0) as usize)) }, false)
}
ToolbarItem::FlexibleSpace | ToolbarItem::Space => (Spec::Standard, false),
};
ToolbarEntry { id, spec, optional }
}
}
pub(crate) struct ToolbarAction {
action: Rc<dyn Fn(isize)>,
disabled: Option<Rc<dyn Fn() -> bool>>,
window: objc2::rc::Weak<NSWindow>,
}
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitToolbarTarget"]
#[ivars = ToolbarAction]
pub(crate) struct ToolbarTarget;
impl ToolbarTarget {
#[unsafe(method(fire:))]
fn fire(&self, sender: Option<&AnyObject>) {
crate::models::flush();
let index = sender.and_then(|s| s.downcast_ref::<objc2_app_kit::NSToolbarItemGroup>()).map_or(0, |g| g.selectedIndex());
let _keep = objc2::Message::retain(self);
let a = self.ivars();
crate::failure::guard("a toolbar item's action", (), || crate::commands::acting_in(a.window.load(), || batch(|| (a.action)(index))));
}
#[unsafe(method(validateToolbarItem:))]
fn validate(&self, _item: &NSToolbarItem) -> bool {
crate::failure::guard("a window's chrome (validate)", false, || {
self.enabled()
})
}
}
);
impl ToolbarTarget {
fn new(mtm: MainThreadMarker, action: Rc<dyn Fn(isize)>, disabled: Option<Rc<dyn Fn() -> bool>>, window: objc2::rc::Weak<NSWindow>) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(ToolbarAction { action, disabled, window });
unsafe { msg_send![super(this), init] }
}
fn enabled(&self) -> bool {
let a = self.ivars();
a.disabled.as_ref().is_none_or(|d| !crate::commands::acting_in(a.window.load(), || d()))
}
}
#[derive(Default)]
struct Live {
keep: Vec<Retained<NSObject>>,
effects: Vec<Effect>,
}
pub(crate) struct ToolbarState {
entries: Vec<ToolbarEntry>,
live: RefCell<std::collections::HashMap<String, Live>>,
window: objc2::rc::Weak<NSWindow>,
spacer: RefCell<Option<Retained<objc2_app_kit::NSLayoutConstraint>>>,
tracks_sidebar: std::cell::Cell<bool>,
remember: Option<String>,
shell: (bool, bool),
}
fn shell_ids(shell: (bool, bool)) -> (Vec<String>, Vec<String>) {
unsafe {
use objc2_app_kit::*;
let lead = if shell.0 { vec![NSToolbarToggleSidebarItemIdentifier.to_string(), NSToolbarSidebarTrackingSeparatorItemIdentifier.to_string()] } else { Vec::new() };
let end = if shell.1 { vec![NSToolbarInspectorTrackingSeparatorItemIdentifier.to_string(), NSToolbarFlexibleSpaceItemIdentifier.to_string(), NSToolbarToggleInspectorItemIdentifier.to_string()] } else { Vec::new() };
(lead, end)
}
}
const SIDEBAR_SPACER: &str = "ferrokit.sidebar-spacer";
define_class!(
#[unsafe(super(NSObject))]
#[thread_kind = MainThreadOnly]
#[name = "FerrokitToolbarDelegate"]
#[ivars = ToolbarState]
pub(crate) struct ToolbarDelegate;
unsafe impl NSObjectProtocol for ToolbarDelegate {}
unsafe impl NSToolbarDelegate for ToolbarDelegate {
#[unsafe(method_id(toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:))]
fn item_for(&self, _toolbar: &NSToolbar, id: &NSString, insert: bool) -> Option<Retained<NSToolbarItem>> {
crate::failure::guard("a window's chrome (item_for)", None, || {
if id.to_string() == SIDEBAR_SPACER {
Some(self.spacer_item(id))
} else {
self.make_item(&id.to_string(), insert)
}
})
}
#[unsafe(method_id(toolbarDefaultItemIdentifiers:))]
fn default_ids(&self, _toolbar: &NSToolbar) -> Retained<NSArray<NSString>> {
let saved = self.saved();
let ids = saved.unwrap_or_else(|| self.ivars().entries.iter().filter(|e| !e.optional).map(|e| e.id.clone()).collect());
self.with_spacer(ids)
}
#[unsafe(method_id(toolbarAllowedItemIdentifiers:))]
fn allowed_ids(&self, _toolbar: &NSToolbar) -> Retained<NSArray<NSString>> {
let mut ids: Vec<String> = self.ivars().entries.iter().filter(|e| !matches!(e.spec, Spec::Standard)).map(|e| e.id.clone()).collect();
ids.extend([unsafe { objc2_app_kit::NSToolbarSpaceItemIdentifier }.to_string(), unsafe { objc2_app_kit::NSToolbarFlexibleSpaceItemIdentifier }.to_string()]);
self.with_spacer(ids)
}
#[unsafe(method_id(toolbarImmovableItemIdentifiers:))]
fn immovable_ids(&self, _toolbar: &NSToolbar) -> Retained<objc2_foundation::NSSet<NSString>> {
objc2_foundation::NSSet::from_retained_slice(&[NSString::from_str(SIDEBAR_SPACER)])
}
#[unsafe(method(toolbarWillAddItem:))]
fn will_add(&self, _notification: &NSNotification) {
crate::failure::guard("a window's chrome (will_add)", (), || {
self.save_soon();
})
}
#[unsafe(method(toolbarDidRemoveItem:))]
fn did_remove(&self, notification: &NSNotification) {
crate::failure::guard("a window's chrome (did_remove)", (), || {
let item = notification.userInfo().and_then(|u| u.objectForKey(&NSString::from_str("item"))).and_then(|i| i.downcast::<NSToolbarItem>().ok());
if let Some(item) = item {
self.ivars().live.borrow_mut().remove(&item.itemIdentifier().to_string());
}
self.save_soon();
})
}
}
);
impl ToolbarDelegate {
fn with_spacer(&self, ids: Vec<String>) -> Retained<NSArray<NSString>> {
let (lead, end) = shell_ids(self.ivars().shell);
let mut ids: Vec<Retained<NSString>> = lead.iter().chain(ids.iter()).chain(end.iter()).map(|s| NSString::from_str(s)).collect();
if self.ivars().tracks_sidebar.get() {
ids.insert(0, NSString::from_str(SIDEBAR_SPACER));
}
NSArray::from_retained_slice(&ids)
}
fn key(&self) -> Option<Retained<NSString>> {
self.ivars().remember.as_ref().map(|n| NSString::from_str(&format!("ferrokit.toolbar.{n}")))
}
fn saved(&self) -> Option<Vec<String>> {
let list = NSUserDefaults::standardUserDefaults().stringArrayForKey(&*self.key()?)?;
let known = |id: &str| {
let standard = [unsafe { objc2_app_kit::NSToolbarSpaceItemIdentifier }.to_string(), unsafe { objc2_app_kit::NSToolbarFlexibleSpaceItemIdentifier }.to_string()];
standard.iter().any(|s| s == id) || self.ivars().entries.iter().any(|e| e.id == id)
};
Some(list.iter().map(|s| s.to_string()).filter(|s| known(s)).collect())
}
fn save_soon(&self) {
let Some(key) = self.key() else { return };
let Some(window) = self.ivars().window.load() else { return };
let defaults: Vec<String> = self.ivars().entries.iter().filter(|e| !e.optional).map(|e| e.id.clone()).collect();
let shell = self.ivars().shell;
let had = NSUserDefaults::standardUserDefaults().stringArrayForKey(&key).map(|l| l.iter().map(|s| s.to_string()).collect::<Vec<_>>());
let window = objc2::rc::Weak::from(&*window);
crate::spawn(
|| (),
move |()| {
let Some(toolbar) = window.load().and_then(|w| w.toolbar()) else { return };
let (lead, end) = shell_ids(shell);
let mut ids: Vec<String> = toolbar.items().iter().map(|i| i.itemIdentifier().to_string()).filter(|i| i != SIDEBAR_SPACER).collect();
if ids.starts_with(&lead) {
ids.drain(..lead.len());
}
if ids.ends_with(&end) {
ids.truncate(ids.len() - end.len());
}
if had.as_ref() == Some(&ids) || (had.is_none() && ids == defaults) {
return;
}
let list = NSArray::from_retained_slice(&ids.iter().map(|s| NSString::from_str(s)).collect::<Vec<_>>());
unsafe { NSUserDefaults::standardUserDefaults().setObject_forKey(Some(&list), &key) };
},
);
}
fn spacer_item(&self, id: &NSString) -> Retained<NSToolbarItem> {
let mtm = MainThreadMarker::from(self);
let view = NSView::new(mtm);
view.setTranslatesAutoresizingMaskIntoConstraints(false);
let before = self.ivars().spacer.borrow().as_ref().map_or(MIN_SPACE, |w| w.constant());
let width = view.widthAnchor().constraintEqualToConstant(before);
width.setActive(true);
view.heightAnchor().constraintEqualToConstant(1.0).setActive(true);
self.ivars().spacer.replace(Some(width));
let item = NSToolbarItem::initWithItemIdentifier(NSToolbarItem::alloc(mtm), id);
item.setView(Some(&view));
item.setNavigational(true);
item.setBordered(false);
objc2_app_kit::NSAccessibility::setAccessibilityElement(&*view, false);
item
}
fn make_item(&self, id: &str, insert: bool) -> Option<Retained<NSToolbarItem>> {
let mtm = MainThreadMarker::from(self);
let state = self.ivars();
let entry = state.entries.iter().find(|e| e.id == id)?;
let ns_id = NSString::from_str(id);
let mut live = Live::default();
let symbol = |name: &str, label: &str| NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(name), Some(&NSString::from_str(label)));
use objc2::Message as _;
let target = |live: &mut Live, item: &NSToolbarItem, action: &Rc<dyn Fn(isize)>, disabled: Option<Rc<dyn Fn() -> bool>>| {
let t = ToolbarTarget::new(mtm, action.clone(), disabled.clone(), state.window.clone());
unsafe {
item.setTarget(Some(&t));
item.setAction(Some(sel!(fire:)));
}
if disabled.is_some() {
let (i, t) = (item.retain(), t.clone());
live.effects.push(Effect::new(move || i.setEnabled(t.enabled())));
}
live.keep.push(Retained::into_super(t));
};
let item: Retained<NSToolbarItem> = match &entry.spec {
Spec::Button { label, symbol: name, action, disabled } => {
let ti = NSToolbarItem::initWithItemIdentifier(NSToolbarItem::alloc(mtm), &ns_id);
ti.setLabel(&NSString::from_str(label));
ti.setToolTip(Some(&NSString::from_str(label)));
ti.setImage(symbol(name, label).as_deref());
target(&mut live, &ti, action, disabled.clone());
ti
}
Spec::Search { query } => {
let si = NSSearchToolbarItem::initWithItemIdentifier(NSSearchToolbarItem::alloc(mtm), &ns_id);
if insert {
let query = *query;
let field = si.searchField();
let delegate = FieldDelegate::new(mtm, query);
unsafe { field.setDelegate(Some(ProtocolObject::from_ref(&*delegate))) };
live.effects.push(Effect::new(move || {
let v = query.get();
if field.stringValue().to_string() != v {
field.setStringValue(&NSString::from_str(&v));
}
}));
live.keep.push(Retained::into_super(delegate));
}
Retained::into_super(si)
}
Spec::Group { label, buttons, action } => {
let group = group_item(mtm, &ns_id, label, buttons, objc2_app_kit::NSToolbarItemGroupSelectionMode::Momentary);
target(&mut live, &group, action, None);
group
}
Spec::Picker { label, choices, selected, choose } => {
let group = group_item(mtm, &ns_id, label, choices, objc2_app_kit::NSToolbarItemGroupSelectionMode::SelectOne);
target(&mut live, &group, choose, None);
if let Some(g) = group.downcast_ref::<objc2_app_kit::NSToolbarItemGroup>().map(|g| g.retain()) {
let selected = selected.clone();
live.effects.push(Effect::new(move || g.setSelectedIndex(selected() as isize)));
}
group
}
Spec::Standard => return None,
};
if insert {
state.live.borrow_mut().insert(id.to_string(), live);
}
Some(item)
}
}
fn group_item(mtm: MainThreadMarker, id: &NSString, label: &str, parts: &[(String, String)], mode: objc2_app_kit::NSToolbarItemGroupSelectionMode) -> Retained<NSToolbarItem> {
let images: Vec<Retained<NSImage>> = parts
.iter()
.map(|(l, s)| NSImage::imageWithSystemSymbolName_accessibilityDescription(&NSString::from_str(s), Some(&NSString::from_str(l))).unwrap_or_default())
.collect();
let labels: Vec<Retained<NSString>> = parts.iter().map(|(l, _)| NSString::from_str(l)).collect();
let group = unsafe {
objc2_app_kit::NSToolbarItemGroup::groupWithItemIdentifier_images_selectionMode_labels_target_action(
id,
&NSArray::from_retained_slice(&images),
mode,
Some(&NSArray::from_retained_slice(&labels)),
None,
None,
mtm,
)
};
group.setLabel(&NSString::from_str(label));
for (sub, (l, _)) in group.subitems().iter().zip(parts) {
sub.setToolTip(Some(&NSString::from_str(l)));
}
Retained::into_super(group)
}
pub(crate) fn install_toolbar(window: &NSWindow, items: Vec<ToolbarItem>, remember: Option<String>, shell: (bool, bool)) -> Retained<NSObject> {
let mtm = MainThreadMarker::from(window);
let entries = items.into_iter().map(ToolbarEntry::new).collect();
let customizable = remember.is_some();
let state = ToolbarState { entries, live: RefCell::default(), window: objc2::rc::Weak::from(window), spacer: RefCell::default(), tracks_sidebar: std::cell::Cell::new(false), remember, shell };
let delegate = ToolbarDelegate::alloc(mtm).set_ivars(state);
let delegate: Retained<ToolbarDelegate> = unsafe { msg_send![super(delegate), init] };
thread_local! {
static TOOLBARS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
let n = TOOLBARS.with(|t| {
t.set(t.get() + 1);
t.get()
});
let toolbar = NSToolbar::initWithIdentifier(NSToolbar::alloc(mtm), &NSString::from_str(&format!("ferrokit.toolbar.{n}")));
toolbar.setDelegate(Some(ProtocolObject::from_ref(&*delegate)));
toolbar.setAllowsUserCustomization(customizable);
toolbar.setDisplayMode(objc2_app_kit::NSToolbarDisplayMode::IconOnly);
window.setToolbar(Some(&toolbar));
window.setToolbarStyle(NSWindowToolbarStyle::Unified);
Retained::into_super(delegate)
}
pub(crate) fn track_sidebar(window: &NSWindow, delegate: &NSObject, split: &NSSplitView) {
let info = split.delegate().filter(|d| {
let obj: &AnyObject = unsafe { &*Retained::as_ptr(d).cast::<AnyObject>() };
obj.class().name().to_str() == Ok("FerrokitSplitDelegate")
});
let (Some(toolbar), Some(delegate), Some(info)) = (window.toolbar(), delegate.downcast_ref::<ToolbarDelegate>(), info) else {
return;
};
let info: Retained<SplitDelegate> = unsafe { Retained::cast_unchecked(info) };
delegate.ivars().tracks_sidebar.set(true);
toolbar.insertItemWithItemIdentifier_atIndex(&NSString::from_str(SIDEBAR_SPACER), 0);
let Some(width) = delegate.ivars().spacer.borrow().clone() else { return };
let window = objc2::rc::Weak::from(window);
let fit = move |split: &NSSplitView| {
if let Some(window) = window.load() {
fit_title(&window, split, &width);
}
};
fit(split);
info.ivars().on_resize.replace(Some(Box::new(fit)));
}
const MIN_SPACE: f64 = 1.0;
fn fit_title(window: &NSWindow, split: &NSSplitView, width: &objc2_app_kit::NSLayoutConstraint) {
let sidebar = split.arrangedSubviews().firstObject().filter(|p| !p.isHidden()).map_or(0.0, |p| p.frame().size.width);
let Some(frame) = window.contentView().and_then(|c| unsafe { c.superview() }) else { return };
let title = window.title().to_string();
for _ in 0..3 {
if sidebar < 1.0 {
width.setConstant(MIN_SPACE);
return;
}
frame.layoutSubtreeIfNeeded();
let Some(label) = title_label(&frame, &title) else {
width.setConstant((sidebar - 80.0).max(MIN_SPACE));
return;
};
let x = label.convertRect_toView(label.bounds(), None).origin.x;
let want = (width.constant() + sidebar + 18.0 - x).max(MIN_SPACE);
if (want - width.constant()).abs() < 0.5 {
return;
}
width.setConstant(want);
}
}
fn title_label(view: &NSView, title: &str) -> Option<Retained<objc2_app_kit::NSTextField>> {
for sub in view.subviews().iter() {
if let Some(f) = sub.clone().downcast::<objc2_app_kit::NSTextField>().ok().filter(|f| f.stringValue().to_string() == title && !f.isHidden()) {
return Some(f);
}
if sub.class().name().to_str() != Ok("FerrokitRootView")
&& let Some(f) = title_label(&sub, title)
{
return Some(f);
}
}
None
}