macOS“大苏尔"检测黑暗的菜单栏/系统托盘 [英] macOS "Big Sur" Detect dark menu-bar/system tray
问题描述
从macOS(10.16"Beta"/11.0)"Big Sur"开始,菜单栏和系统任务栏不再遵循桌面暗模式设置,因此很难为此主题正确设置系统任务栏图标桌面.
Starting with macOS (10.16 "Beta"/11.0) "Big Sur", the menu-bar and system tray no longer honor the Desktop dark-mode preference, making it difficult to properly theme a system tray icon for this Desktop.
以前,使用shell命令默认读取
,可以检测到暗模式:
Previously, using a shell command default read
, the dark mode could be detected:
defaults read -g AppleInterfaceStyle
# "Dark"
这对于检测Window主题仍然非常有用,但不适用于菜单栏和系统任务栏主题.
This still works great for detecting the Window theme, but it does not work for the menu-bar and system tray theme.
由于该区域似乎受墙纸亮度/白度/亮度的影响,因此我们如何检测深色系统托盘?
Since this area seems to be driven by the wallpaper brightness/whiteness/luminosity, how do we detect a dark system tray?
如何在(例如)Objective-C/C ++中检测到这一点?任何解决方案都是可以接受的,因为大多数解决方案都可以适应.
How to detect this in (e.g.) Objective-C/C++? Any solution is welcome, as most can be adapted.
问题也发布到了Apple Developer论坛: https://developer.apple.com/forums/线程/652540
Question also posted to Apple Developer forums: https://developer.apple.com/forums/thread/652540
Qt5.6具有称为 setIsMask(...)
,它允许操作系统自动处理此问题.这实际上是 NSImage :: setTemplate:Yes
Qt5.6 has a feature called setIsMask(...)
which allows the OS to handle this automatically. This is effectively an alias for NSImage::setTemplate:Yes
更多有关macOS黑暗模式"的参考:
More references to macOS "Dark Mode":
采用OpenJDK上游错误报告:
AdoptOpenJDK upstream bug report:
关键字: NSStatusBar
,菜单栏附加功能
推荐答案
我遇到了同样的问题,但我想我找到了解决方案.如 NSStatusItem
的条目),您只需观察 NSStatusItem
的 button
的 effectiveAppearance
.如果 effectiveAppearance
的名称包含 dark
,则为暗模式.否则就是灯光模式.
I had the same issue but I think I found a solution. As written in AppKit Release Notes for Big Sur (see the entry for NSStatusItem
) you can just observe NSStatusItem
's button
's effectiveAppearance
. If the name of the effectiveAppearance
contains dark
, then it's the dark mode. Otherwise it's the light mode.
我创建的示例代码显示 light
或 dark
作为 NSStatusItem
的文本标签,可在 AppDelegate.m .(对于使用Objective-C几乎灭绝的恐龙,我们感到抱歉.)您可以通过在Catalina或Big Sur上运行,从系统偏好设置"中更改暗/亮"设置或桌面图片的颜色来对其进行测试.
A sample code I created which shows light
or dark
as the text label of the NSStatusItem
is available at this GitHub repo, see in particular AppDelegate.m. (I'm sorry for being a near-extinct dinosaur for using Objective-C.) You can test it by running on Catalina or on Big Sur, changing the Dark/Light settings or the color of the desktop picture from System Preferences.
事实证明,大苏尔有时会将 effectiveAppearance
从浅色变为浅色或从暗色变为深色(从某种意义上说,尽管外观未变,但调用了KVO)t实际上已更改.)因此,建议在更改之前和之后都检查 effectiveApparance
的值,以确认该值实际上已更改.
It turns out that Big Sur sometimes changes effectiveAppearance
from light to light or from dark to dark (in the sense that KVO is invoked although the appearance hasn’t actually changed.) Therefore it’s advisable to check both the value of effectiveApparance
before and after the change to confirm the value actually changed.
这篇关于macOS“大苏尔"检测黑暗的菜单栏/系统托盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!