From 511fcf29f690f42cc8f6d79678e4923ee12784f1 Mon Sep 17 00:00:00 2001 From: Elie Gambache Date: Tue, 3 Feb 2026 09:17:13 +0200 Subject: [PATCH] Fix macOS isMenuBarInDarkMode returning true in light mode - Dispatch tray_is_menu_dark() to main thread for correct AppKit access - Use NSApp.effectiveAppearance as fallback when no tray context exists instead of assuming dark mode Fixes #351 --- maclib/tray.swift | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/maclib/tray.swift b/maclib/tray.swift index 7666c17f..ac11fa6c 100644 --- a/maclib/tray.swift +++ b/maclib/tray.swift @@ -303,10 +303,18 @@ public func tray_set_theme_callback(_ cb: @escaping ThemeCallback) { @_cdecl("tray_is_menu_dark") public func tray_is_menu_dark() -> Int32 { + if !Thread.isMainThread { + return DispatchQueue.main.sync { tray_is_menu_dark() } + } let ctx = trayInstance.flatMap { contexts[$0] } ?? contexts.values.first - guard let button = ctx?.statusItem.button else { return 1 } - guard let name = button.effectiveAppearance.bestMatch(from: [NSAppearance.Name.darkAqua, NSAppearance.Name.aqua]) else { - return 1 // assume dark if unknown + let appearance: NSAppearance + if let button = ctx?.statusItem.button { + appearance = button.effectiveAppearance + } else { + appearance = NSApp.effectiveAppearance + } + guard let name = appearance.bestMatch(from: [.darkAqua, .aqua]) else { + return 1 } return name == .darkAqua ? 1 : 0 }