Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: adapt macOS tray artwork to the menu bar appearance (#10880)

Wasim Yousef Saiddabb59f0a0fb
diff --git a/studio/src-tauri/Cargo.lock b/studio/src-tauri/Cargo.lock
index cf294d28687..6f21df7f81e 100644
--- a/studio/src-tauri/Cargo.lock
+++ b/studio/src-tauri/Cargo.lock
@@ -5734,6 +5734,8 @@ dependencies = [
"libc",
"log",
"objc2",
+ "objc2-app-kit",
+ "objc2-foundation",
"open",
"process-wrap",
"rand 0.10.2",
diff --git a/studio/src-tauri/Cargo.toml b/studio/src-tauri/Cargo.toml
index 23d92c0c061..b2eabfaa3d2 100644
--- a/studio/src-tauri/Cargo.toml
+++ b/studio/src-tauri/Cargo.toml
@@ -14,7 +14,7 @@ edition = "2021"
rust-version = "1.89"
[dependencies]
-tauri = { version = "2", features = ["tray-icon"] }
+tauri = { version = "~2.11.5", features = ["tray-icon"] }
tauri-plugin-single-instance = { version = "2", features = ["deep-link"] }
tauri-plugin-autostart = "2"
tauri-plugin-deep-link = "2"
@@ -51,6 +51,9 @@ signal-hook = "0.4"
[target.'cfg(target_os = "macos")'.dependencies]
objc2 = "0.6"
+objc2-app-kit = { version = "0.3.2", default-features = false, features = ["std", "NSAppearance", "NSButton", "NSControl", "NSResponder", "NSStatusBarButton", "NSStatusItem", "NSView"] }
+objc2-foundation = { version = "0.3.2", default-features = false, features = ["std", "NSArray", "NSDictionary", "NSKeyValueObserving", "NSObject", "NSString"] }
+
[target.'cfg(target_os = "linux")'.dependencies]
gdk = "0.18"
gdk-pixbuf = "0.18"
diff --git a/studio/src-tauri/icons/tray-icon-dark.png b/studio/src-tauri/icons/tray-icon-dark.png
new file mode 100644
index 00000000000..c89bcb6c19a
Binary files /dev/null and b/studio/src-tauri/icons/tray-icon-dark.png differ
diff --git a/studio/src-tauri/icons/tray-icon-light.png b/studio/src-tauri/icons/tray-icon-light.png
new file mode 100644
index 00000000000..9c2103f8232
Binary files /dev/null and b/studio/src-tauri/icons/tray-icon-light.png differ
diff --git a/studio/src-tauri/icons/tray-icon.png b/studio/src-tauri/icons/tray-icon.png
new file mode 100644
index 00000000000..0a77c1323c2
Binary files /dev/null and b/studio/src-tauri/icons/tray-icon.png differ
diff --git a/studio/src-tauri/icons/[email protected] b/studio/src-tauri/icons/[email protected]
new file mode 100644
index 00000000000..6199eae85c2
Binary files /dev/null and b/studio/src-tauri/icons/[email protected] differ
diff --git a/studio/src-tauri/src/macos_tray.rs b/studio/src-tauri/src/macos_tray.rs
new file mode 100644
index 00000000000..7c79e7bced4
--- /dev/null
+++ b/studio/src-tauri/src/macos_tray.rs
@@ -0,0 +1,197 @@
+use std::{
+ cell::{Cell, RefCell},
+ ffi::c_void,
+ ptr::null_mut,
+};
+
+use objc2::{
+ define_class, msg_send, rc::Retained, runtime::AnyObject, DefinedClass, MainThreadMarker,
+ MainThreadOnly,
+};
+use objc2_app_kit::{
+ NSAppearance, NSAppearanceCustomization, NSAppearanceNameAqua, NSAppearanceNameDarkAqua,
+ NSStatusBarButton,
+};
+use objc2_foundation::{
+ ns_string, NSArray, NSDictionary, NSKeyValueChangeKey, NSKeyValueObservingOptions, NSObject,
+ NSObjectNSKeyValueObserverRegistration, NSString,
+};
+use tauri::tray::TrayIcon;
+
+struct TrayAppearanceObserverIvars {
+ button: Retained<NSStatusBarButton>,
+ tray: TrayIcon,
+ last_dark: Cell<Option<bool>>,
+}
+
+define_class!(
+ #[unsafe(super = NSObject)]
+ #[thread_kind = MainThreadOnly]
+ #[ivars = TrayAppearanceObserverIvars]
+ struct TrayAppearanceObserver;
+
+ impl TrayAppearanceObserver {
+ #[unsafe(method(observeValueForKeyPath:ofObject:change:context:))]
+ fn observe_appearance(
+ &self,
+ _key_path: Option<&NSString>,
+ _object: Option<&AnyObject>,
+ _change: Option<&NSDictionary<NSKeyValueChangeKey, AnyObject>>,
+ _context: *mut c_void,
+ ) {
+ self.update_icon();
+ }
+ }
+);
+
+impl TrayAppearanceObserver {
+ fn new(
+ mtm: MainThreadMarker,
+ button: Retained<NSStatusBarButton>,
+ tray: TrayIcon,
+ ) -> Retained<Self> {
+ let observer = Self::alloc(mtm).set_ivars(TrayAppearanceObserverIvars {
+ button,
+ tray,
+ last_dark: Cell::new(None),
+ });
+ // SAFETY: The object is allocated and all ivars are initialized.
+ let observer: Retained<Self> = unsafe { msg_send![super(observer), init] };
+
+ // SAFETY: The observer retains the button and unregisters in Drop on the main thread.
+ unsafe {
+ observer
+ .ivars()
+ .button
+ .addObserver_forKeyPath_options_context(
+ &observer,
+ ns_string!("effectiveAppearance"),
+ NSKeyValueObservingOptions::Initial | NSKeyValueObservingOptions::New,
+ null_mut(),
+ );
+ }
+
+ observer
+ }
+
+ fn update_icon(&self) {
+ let is_dark = appearance_is_dark(&self.ivars().button.effectiveAppearance());
+ if self.ivars().last_dark.get() == Some(is_dark) {
+ return;
+ }
+
+ // Mark the classification before replacing the image so a synchronous duplicate KVO
+ // delivery cannot recursively attempt the same update.
+ self.ivars().last_dark.set(Some(is_dark));
+ if let Err(error) = self
+ .ivars()
+ .tray
+ .set_icon_with_as_template(Some(tray_icon(is_dark)), false)
+ {
+ self.ivars().last_dark.set(None);
+ log::warn!("Failed to update the macOS tray appearance: {error}");
+ }
+ }
+}
+
+impl Drop for TrayAppearanceObserver {
+ fn drop(&mut self) {
+ // SAFETY: This exactly balances the registration in new while both objects are alive.
+ unsafe {
+ self.ivars()
+ .button
+ .removeObserver_forKeyPath(self, ns_string!("effectiveAppearance"));
+ }
+ }
+}
+
+thread_local! {
+ // AppKit view objects are main-thread-only, so retain the observer in main-thread storage
+ // instead of Tauri State (which requires Send + Sync).
+ static TRAY_APPEARANCE_OBSERVER: RefCell<Option<Retained<TrayAppearanceObserver>>> =
+ const { RefCell::new(None) };
+}
+
+pub fn install_appearance_observer(tray: &TrayIcon) -> Result<(), String> {
+ let tray_for_observer = tray.clone();
+ let installed = tray
+ .with_inner_tray_icon(move |inner| {
+ let Some(mtm) = MainThreadMarker::new() else {
+ return false;
+ };
+ let Some(status_item) = inner.ns_status_item() else {
+ return false;
+ };
+ let Some(button) = status_item.button(mtm) else {
+ return false;
+ };
+
+ let observer = TrayAppearanceObserver::new(mtm, button, tray_for_observer);
+ TRAY_APPEARANCE_OBSERVER.with(|slot| {
+ *slot.borrow_mut() = Some(observer);
+ });
+ true
+ })
+ .map_err(|error| error.to_string())?;
+
+ installed
+ .then_some(())
+ .ok_or_else(|| "AppKit did not expose the tray status button".to_string())
+}
+
+pub fn remove_appearance_observer() {
+ if MainThreadMarker::new().is_none() {
+ log::warn!("Could not remove the macOS tray appearance observer off the main thread");
+ return;
+ }
+ TRAY_APPEARANCE_OBSERVER.with(|slot| {
+ slot.borrow_mut().take();
+ });
+}
+
+fn tray_icon(is_dark: bool) -> tauri::image::Image<'static> {
+ if is_dark {
+ // Light artwork contrasts with a dark menu-bar background.
+ tauri::include_image!("./icons/tray-icon-dark.png")
+ } else {
+ // Dark artwork contrasts with a light menu-bar background.
+ tauri::include_image!("./icons/tray-icon-light.png")
+ }
+}
+
+fn appearance_is_dark(appearance: &NSAppearance) -> bool {
+ // SAFETY: These are immutable AppKit constants available on every supported macOS version.
+ let (aqua, dark_aqua) = unsafe { (NSAppearanceNameAqua, NSAppearanceNameDarkAqua) };
+ let candidates = NSArray::from_slice(&[aqua, dark_aqua]);
+ appearance
+ .bestMatchFromAppearancesWithNames(&candidates)
+ .is_some_and(|name| *name == *dark_aqua)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn appearance_mapping_uses_contrasting_artwork() {
+ // SAFETY: These are immutable AppKit constants available on every supported macOS version.
+ let (aqua, dark_aqua) = unsafe { (NSAppearanceNameAqua, NSAppearanceNameDarkAqua) };
+ let light = NSAppearance::appearanceNamed(aqua).expect("Aqua appearance should exist");
+ let dark = NSAppearance::appearanceNamed(dark_aqua).expect("Dark Aqua should exist");
+
+ assert!(!appearance_is_dark(&light));
+ assert!(appearance_is_dark(&dark));
+
+ for (is_dark, expected_rgb) in [(false, [0, 0, 0]), (true, [255, 255, 255])] {
+ let icon = tray_icon(is_dark);
+ assert_eq!((icon.width(), icon.height()), (36, 36));
+ let visible: Vec<_> = icon
+ .rgba()
+ .chunks_exact(4)
+ .filter(|pixel| pixel[3] != 0)
+ .collect();
+ assert!(!visible.is_empty());
+ assert!(visible.iter().all(|pixel| pixel[..3] == expected_rgb));
+ }
+ }
+}
diff --git a/studio/src-tauri/src/main.rs b/studio/src-tauri/src/main.rs
index 7d96cf20e81..b1b329ba5a4 100644
--- a/studio/src-tauri/src/main.rs
+++ b/studio/src-tauri/src/main.rs
@@ -12,6 +12,8 @@ mod install_watchdog;
#[cfg(target_os = "linux")]
mod linux_webkit;
mod loopback_http;
+#[cfg(target_os = "macos")]
+mod macos_tray;
mod native_backend_lease;
mod native_clipboard;
mod native_file_dialogs;
@@ -1710,10 +1712,18 @@ fn setup_tray(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
.build()?;
app.manage(TrayServerToggle(toggle));
- TrayIconBuilder::new()
+ // macOS renders tray images at 18 points. Embed the 36 px scale for crisp Retina output;
+ // template mode lets AppKit choose the correct monochrome color for the current menu bar.
+ #[cfg(target_os = "macos")]
+ let tray_icon = tauri::include_image!("./icons/[email protected]");
+ #[cfg(not(target_os = "macos"))]
+ let tray_icon = app.default_window_icon().unwrap().clone();
+
+ let tray = TrayIconBuilder::new()
.menu(&menu)
.tooltip("Unsloth")
- .icon(app.default_window_icon().unwrap().clone())
+ .icon(tray_icon)
+ .icon_as_template(cfg!(target_os = "macos"))
.on_menu_event(move |app, event| match event.id().as_ref() {
"open" => show_main_window(app),
"toggle" => {
@@ -1734,6 +1744,14 @@ fn setup_tray(app: &tauri::App) -> Result<(), Box<dyn std::error::Error>> {
})
.build(app)?;
+ #[cfg(target_os = "macos")]
+ if let Err(error) = macos_tray::install_appearance_observer(&tray) {
+ // The template icon remains visible and adaptive if native observation is unavailable.
+ warn!("Could not install the macOS tray appearance observer: {error}");
+ }
+ #[cfg(not(target_os = "macos"))]
+ drop(tray);
+
Ok(())
}
@@ -2029,6 +2047,9 @@ fn main() {
// roughly 18s on Windows, where those first two graceful waits are
// `#[cfg(unix)]` and go straight to the force kill, but the backend spends
// its liveness, shutdown and CTRL_BREAK budgets in series instead.
+ #[cfg(target_os = "macos")]
+ macos_tray::remove_appearance_observer();
+
cleanup_child_processes(app);
}
_ => {}