matryer / xbar

Put the output from any script or program into your macOS Menu Bar (the BitBar reboot)
https://xbarapp.com
MIT License
17.49k stars 643 forks source link

Support for NSStatusItem button `effectiveAppearance` property #870

Open dnicolson opened 1 year ago

dnicolson commented 1 year ago

It's possible to detect Dark Mode with the XBARDarkMode environment variable, but it doesn't seem possible to determine when an NSStatusItem has a light or dark tint. Even when appearance is set to light, a dark wallpaper will trigger white status item colors.

In Swift one can use the following:

statusItem.button!.effectiveAppearance.name.rawValue

Which outputs NSAppearanceNameVibrantLight, NSAppearanceNameVibrantDark, or possibly others.

BartmanEH commented 11 months ago

I stumbled across this after researching the same issue and coming up with nothing. It looks like Java has the same issue and they eventually worked a property into Java 17 with a backport to Java 11 forthcoming: https://stackoverflow.com/questions/62685948/macos-big-sur-detect-dark-menu-bar-system-tray

xbar hasn't had a release in almost 2 years now and there are several outstanding Pull Requests too. I'm not holding hope that something will be added to xbar anytime soon.

BartmanEH commented 5 months ago

A good demonstration of the issue is here on Stack Exchange.

I've made progress on this issue with a workaround. Working through the solution offered here, if I issue the following command in terminal: /usr/sbin/screencapture -R 27,20,1,1 -t png /tmp/x.png; results=$(/opt/homebrew/bin/convert /tmp/x.png sparse-color:); if echo "$results" | grep -q '0,0,srgba(255,255,255,1)'; then echo "true"; else echo "false"; fi; rm /tmp/x.png it returns true when the Apple Logo Icon is white and false otherwise. It's dependent on a homebrew install of ImageMagick.

I've integrated this logic test into my xbar script to adjust the menubar text color accordingly and it works perfectly. Now when the menu bar text is white, so is my xbar script echo output in the menu bar. Similarly, when the menu bar text is something other than white, my xbar script menu bar item turns black.