diff --git a/src/qz/common/TrayManager.java b/src/qz/common/TrayManager.java index 37ed278fe..fc83b0459 100644 --- a/src/qz/common/TrayManager.java +++ b/src/qz/common/TrayManager.java @@ -17,7 +17,6 @@ import org.slf4j.LoggerFactory; import qz.auth.Certificate; import qz.auth.RequestState; -import qz.installer.WindowsInstaller; import qz.installer.shortcut.ShortcutCreator; import qz.ui.*; import qz.ui.component.IconCache; @@ -112,7 +111,7 @@ public TrayManager(boolean isHeadless) { // OS-specific tray icon handling if (SystemTray.isSupported()) { - iconCache.fixTrayIcons(SystemUtilities.isDarkMode()); + iconCache.fixTrayIcons(SystemUtilities.isDarkTaskbar()); } // Iterates over all images denoted by IconCache.getTypes() and caches them @@ -152,13 +151,16 @@ public TrayManager(boolean isHeadless) { // Detect theme changes new Thread(() -> { - boolean darkMode = SystemUtilities.isDarkMode(); + boolean darkDesktopMode = SystemUtilities.isDarkDesktop(); + boolean darkTaskbarMode = SystemUtilities.isDarkTaskbar(); while(true) { try { Thread.sleep(1000); - if (darkMode != SystemUtilities.isDarkMode(true)) { - darkMode = SystemUtilities.isDarkMode(); - iconCache.fixTrayIcons(darkMode); + if (darkDesktopMode != SystemUtilities.isDarkDesktop(true) || + darkTaskbarMode != SystemUtilities.isDarkTaskbar(true)) { + darkDesktopMode = SystemUtilities.isDarkDesktop(); + darkTaskbarMode = SystemUtilities.isDarkTaskbar(); + iconCache.fixTrayIcons(darkTaskbarMode); refreshIcon(); SwingUtilities.invokeLater(() -> { SystemUtilities.setSystemLookAndFeel(); diff --git a/src/qz/ui/component/IconCache.java b/src/qz/ui/component/IconCache.java index ed0b64f3e..795442626 100644 --- a/src/qz/ui/component/IconCache.java +++ b/src/qz/ui/component/IconCache.java @@ -256,7 +256,7 @@ public void setBgColor(Icon i, Color bgColor) { * - macOS masked icons * - macOS 10.14+ dark mode support */ - public void fixTrayIcons(boolean darkMode) { + public void fixTrayIcons(boolean darkTaskbar) { // Fix the tray icon to look proper on Ubuntu if (SystemUtilities.isUbuntu()) { UbuntuUtilities.fixTrayIcons(this); @@ -269,9 +269,9 @@ public void fixTrayIcons(boolean darkMode) { BufferedImage clone = clone(images.get(id)); // Even on lite mode desktops, white tray icons were the norm until Windows 10 update 1903 if (SystemUtilities.isWindows() && SystemUtilities.getOSVersion().lessThan(Version.valueOf("10.0.1903"))) { - darkMode = true; + darkTaskbar = true; } - if (darkMode) { + if (darkTaskbar) { clone = ColorUtilities.invert(clone); } images.put(id.replaceAll("mask", "default"), clone); diff --git a/src/qz/utils/SystemUtilities.java b/src/qz/utils/SystemUtilities.java index c55cdfb37..0a8b96129 100644 --- a/src/qz/utils/SystemUtilities.java +++ b/src/qz/utils/SystemUtilities.java @@ -11,7 +11,6 @@ package qz.utils; import com.github.zafarkhaja.semver.Version; -import com.sun.jna.platform.win32.Advapi32Util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import qz.common.Constants; @@ -39,7 +38,8 @@ public class SystemUtilities { private static final String OS_NAME = System.getProperty("os.name").toLowerCase(Locale.ENGLISH); private static final Logger log = LoggerFactory.getLogger(TrayManager.class); - private static Boolean darkMode; + private static Boolean darkDesktop; + private static Boolean darkTaskbar; private static String uname; private static String linuxRelease; private static String classProtocol; @@ -283,27 +283,43 @@ public static String getUname() { return uname; } - public static boolean isDarkMode() { - return isDarkMode(false); + public static boolean isDarkTaskbar() { + return isDarkTaskbar(false); } - public static boolean isDarkMode(boolean recheck) { - if (darkMode == null || recheck) { + public static boolean isDarkTaskbar(boolean recheck) { + if(darkTaskbar == null || recheck) { + if (!isWindows()) { + // Mac and Linux don't differentiate; return the cached darkDesktop value + darkTaskbar = isDarkDesktop(); + } else { + darkTaskbar = WindowsUtilities.isDarkTaskbar(); + } + } + return darkTaskbar.booleanValue(); + } + + public static boolean isDarkDesktop() { + return isDarkDesktop(false); + } + + public static boolean isDarkDesktop(boolean recheck) { + if (darkDesktop == null || recheck) { // Check for Dark Mode on MacOS if (isMac()) { - darkMode = MacUtilities.isDarkMode(); + darkDesktop = MacUtilities.isDarkMode(); } else if (isWindows()) { - darkMode = WindowsUtilities.isDarkMode(); + darkDesktop = WindowsUtilities.isDarkDesktop(); } else { - darkMode = UbuntuUtilities.isDarkMode(); + darkDesktop = UbuntuUtilities.isDarkMode(); } } - return darkMode.booleanValue(); + return darkDesktop.booleanValue(); } public static void adjustThemeColors() { - Constants.WARNING_COLOR = isDarkMode() ? Constants.WARNING_COLOR_DARK : Constants.WARNING_COLOR_LITE; - Constants.TRUSTED_COLOR = isDarkMode() ? Constants.TRUSTED_COLOR_DARK : Constants.TRUSTED_COLOR_LITE; + Constants.WARNING_COLOR = isDarkDesktop() ? Constants.WARNING_COLOR_DARK : Constants.WARNING_COLOR_LITE; + Constants.TRUSTED_COLOR = isDarkDesktop() ? Constants.TRUSTED_COLOR_DARK : Constants.TRUSTED_COLOR_LITE; } public static boolean prefersMaskTrayIcon() { @@ -324,7 +340,7 @@ public static boolean setSystemLookAndFeel() { if(!isMac() && (isUnix() && UbuntuUtilities.isDarkMode())) { darkulaThemeNeeded = false; } - if(isDarkMode() && darkulaThemeNeeded) { + if(isDarkDesktop() && darkulaThemeNeeded) { UIManager.setLookAndFeel("com.bulenkov.darcula.DarculaLaf"); } else { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); diff --git a/src/qz/utils/WindowsUtilities.java b/src/qz/utils/WindowsUtilities.java index 99890b4a6..0372dd6bd 100644 --- a/src/qz/utils/WindowsUtilities.java +++ b/src/qz/utils/WindowsUtilities.java @@ -22,23 +22,29 @@ public class WindowsUtilities { protected static final Logger log = LoggerFactory.getLogger(WindowsUtilities.class); + private static String THEME_REG_KEY = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; private static final String AUTHENTICATED_USERS_SID = "S-1-5-11"; - public static boolean isDarkMode() { - String key = "Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"; - + public static boolean isDarkDesktop() { // 0 = Dark Theme. -1/1 = Light Theme Integer regVal; - if((regVal = getRegInt(HKEY_CURRENT_USER, key, "SystemUsesLightTheme")) != null) { - // Prefer system theme - return regVal == 0; - } else if((regVal = getRegInt(HKEY_CURRENT_USER, key, "AppsUseLightTheme")) != null) { + if((regVal = getRegInt(HKEY_CURRENT_USER, THEME_REG_KEY, "AppsUseLightTheme")) != null) { // Fallback on apps theme return regVal == 0; } return false; } + public static boolean isDarkTaskbar() { + // -1/0 = Dark Theme. 1 = Light Theme + Integer regVal; + if((regVal = getRegInt(HKEY_CURRENT_USER, THEME_REG_KEY, "SystemUsesLightTheme")) != null) { + // Prefer system theme + return regVal == 0; + } + return true; + } + public static int getScaleFactor() { if (Constants.JAVA_VERSION.lessThan(Version.valueOf("9.0.0"))) { WinDef.HDC hdc = GDI32.INSTANCE.CreateCompatibleDC(null);