Skip to content

Commit

Permalink
Add a very crude luminosity screen-reader to find out what color the …
Browse files Browse the repository at this point in the history
…icon should be
  • Loading branch information
tresf committed Jul 8, 2020
1 parent 2644b22 commit 2af58e9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
16 changes: 15 additions & 1 deletion src/qz/utils/MacUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import qz.ui.component.IconCache;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -107,10 +108,23 @@ public static void registerQuitHandler(TrayManager trayManager) {
* Runs a shell command to determine if "Dark" desktop theme is enabled
* @return true if enabled, false if not
*/
public static boolean isDarkMode() {
public static boolean isDarkDesktop() {
return !ShellUtilities.execute(new String[] { "defaults", "read", "-g", "AppleInterfaceStyle" }, new String[] { "Dark" }, true, true).isEmpty();
}

public static boolean isDarkTaskbar() {
try {
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Rectangle topCorner = new Rectangle(rectangle.x, rectangle.y, 1, 1);
BufferedImage pixel = new Robot().createScreenCapture(topCorner);
Color color = new Color(pixel.getRGB(0, 0));
return (color.getRed() * 0.299) + (color.getGreen() * 0.587) + (color.getBlue() * 0.114) <= 174;
} catch(AWTException e) {
log.warn("Unable to detect dark taskbar: {}", e.getMessage());
}
return false;
}

public static int getScaleFactor() {
// Java 9+ per JDK-8172962
if (Constants.JAVA_VERSION.greaterThanOrEqualTo(Version.valueOf("9.0.0"))) {
Expand Down
12 changes: 7 additions & 5 deletions src/qz/utils/SystemUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,13 @@ public static boolean isDarkTaskbar() {

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 {
if (isWindows()) {
darkTaskbar = WindowsUtilities.isDarkTaskbar();
} else if(isMac()) {
darkTaskbar = MacUtilities.isDarkTaskbar();
} else {
// Linux doesn't differentiate; return the cached darkDesktop value
darkTaskbar = isDarkDesktop();
}
}
return darkTaskbar.booleanValue();
Expand All @@ -331,7 +333,7 @@ public static boolean isDarkDesktop(boolean recheck) {
if (darkDesktop == null || recheck) {
// Check for Dark Mode on MacOS
if (isMac()) {
darkDesktop = MacUtilities.isDarkMode();
darkDesktop = MacUtilities.isDarkDesktop();
} else if (isWindows()) {
darkDesktop = WindowsUtilities.isDarkDesktop();
} else {
Expand Down

0 comments on commit 2af58e9

Please sign in to comment.