mitsuami-appkit 2.5.2

AppKit (macOS) backend for mitsuami
Documentation
//! The tray icon on macOS: a status item in the menu bar
//! (`NSStatusBar.systemStatusBar`), its image a template that AppKit
//! tints for the bar's light, dark and highlighted looks, and its menu
//! built as context menus are. A click opens the menu, as every status
//! item's does. Its autosave name is the app's id, so macOS keeps where
//! the user dragged it.

use std::cell::RefCell;
use std::rc::Rc;

use mitsuami_core::AppIcon;
use mitsuami_core::services::TrayData;
use objc2::rc::Retained;
use objc2::{MainThreadMarker, sel};
use objc2_app_kit::{
    NSAccessibility, NSApplication, NSImage, NSMenuItem, NSStatusBar, NSStatusItem, NSVariableStatusItemLength,
};
use objc2_foundation::{NSCopying, NSSize, NSString};

use crate::backend::{AppKitHandle, app_icon_image};
use crate::classes::ClosureTarget;
use crate::services::{ItemTarget, context_menu};

/// How tall a status item's image is, in points: the menu bar's.
const IMAGE_HEIGHT: f64 = 18.0;

/// What a chosen item reports to: the last `activate` the core gave.
type Activate = Rc<RefCell<Option<Rc<dyn Fn(u32)>>>>;

pub(crate) struct Tray {
    mtm: MainThreadMarker,
    item: Option<Retained<NSStatusItem>>,
    /// Its menu's items' target, which they hold weakly.
    target: Retained<ClosureTarget>,
    activate: Activate,
    shown: bool,
    /// The icon the status item shows and what it was made from, so a
    /// change to the tooltip or menu doesn't decode it again.
    image: Option<(Option<AppIcon>, Option<Retained<NSImage>>)>,
}

impl Tray {
    pub(crate) fn new(mtm: MainThreadMarker, backend: AppKitHandle) -> Tray {
        let activate: Activate = Rc::default();
        let target = ClosureTarget::new(mtm, {
            let activate = activate.clone();
            move |sender| {
                let chosen = activate.borrow().clone();
                if let (Some(item), Some(activate)) = (sender.downcast_ref::<NSMenuItem>(), chosen) {
                    // A status item's menu leaves the app inactive.
                    backend.activate_on_show();
                    activate(item.tag() as u32);
                }
            }
        });
        Tray { mtm, item: None, target, activate, shown: false, image: None }
    }

    pub(crate) fn set(
        &mut self,
        tray: Option<&TrayData>,
        app_id: Option<&str>,
        activate: Rc<dyn Fn(u32)>,
        shown: Rc<dyn Fn(bool)>,
    ) {
        *self.activate.borrow_mut() = Some(activate);
        let Some(tray) = tray else {
            if let Some(item) = self.item.take() {
                NSStatusBar::systemStatusBar().removeStatusItem(&item);
            }
            self.image = None;
            self.report(false, &shown);
            return;
        };
        let item = self.item.get_or_insert_with(|| {
            let item = NSStatusBar::systemStatusBar().statusItemWithLength(NSVariableStatusItemLength);
            if let Some(id) = app_id {
                item.setAutosaveName(Some(&NSString::from_str(id)));
            }
            item
        });
        if let Some(button) = item.button(self.mtm) {
            // Without an icon of its own it shows the app's, which the app
            // may have changed: copying that one decodes nothing.
            if tray.icon.is_none() || self.image.as_ref().is_none_or(|(icon, _)| *icon != tray.icon) {
                let image = status_image(self.mtm, tray.icon.as_ref());
                button.setImage(image.as_deref());
                self.image = Some((tray.icon.clone(), image));
            }
            let tooltip = (!tray.tooltip.is_empty()).then(|| NSString::from_str(&tray.tooltip));
            button.setToolTip(tooltip.as_deref());
            button.setAccessibilityLabel(Some(&NSString::from_str(&tray.title)));
        }
        let target = ItemTarget { object: &self.target, action: sel!(fire:) };
        item.setMenu(Some(&context_menu(self.mtm, &tray.menu, target)));
        self.report(true, &shown);
    }

    fn report(&mut self, showing: bool, shown: &Rc<dyn Fn(bool)>) {
        if self.shown != showing {
            self.shown = showing;
            shown(showing);
        }
    }
}

/// The status item's image, sized to the menu bar: the app's icon, in
/// colour, when it gives none, or none macOS can show (a theme name alone,
/// a file that won't load), rather than an empty slot in the menu bar.
fn status_image(mtm: MainThreadMarker, icon: Option<&AppIcon>) -> Option<Retained<NSImage>> {
    let image = match icon.and_then(app_icon_image) {
        Some(image) => {
            image.setTemplate(true);
            image
        }
        // A copy: the app's icon is one shared image, and resizing it
        // would shrink it everywhere else it's drawn (alerts, About).
        None => NSApplication::sharedApplication(mtm).applicationIconImage()?.copy(),
    };
    let size = image.size();
    let width = if size.height > 0.0 { size.width * IMAGE_HEIGHT / size.height } else { IMAGE_HEIGHT };
    image.setSize(NSSize::new(width, IMAGE_HEIGHT));
    Some(image)
}

impl Drop for Tray {
    fn drop(&mut self) {
        if let Some(item) = self.item.take() {
            NSStatusBar::systemStatusBar().removeStatusItem(&item);
        }
    }
}