1+ package net .hypercubemc .iris_installer ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .IOException ;
5+ import java .io .InputStreamReader ;
6+ import java .util .regex .Pattern ;
7+
8+ // Based off HanSolo's Detector, with added support for GNOME, and removed support for macOS accent colors for Java 8 compatibility. Original: https://gist.github.com/HanSolo/7cf10b86efff8ca2845bf5ec2dd0fe1d
9+ public class DarkModeDetector {
10+ public enum OperatingSystem {WINDOWS , MACOS , LINUX , SOLARIS , NONE }
11+
12+ private static final String REGQUERY_UTIL = "reg query " ;
13+ private static final String REGDWORD_TOKEN = "REG_DWORD" ;
14+ private static final String DARK_THEME_CMD = REGQUERY_UTIL + "\" HKEY_CURRENT_USER\\ Software\\ Microsoft\\ Windows\\ CurrentVersion\\ Themes\\ Personalize\" " + " /v AppsUseLightTheme" ;
15+
16+ public static boolean isDarkMode () {
17+ switch (getOperatingSystem ()) {
18+ case WINDOWS :
19+ return isWindowsDarkMode ();
20+ case MACOS :
21+ return isMacOsDarkMode ();
22+ case LINUX :
23+ return isGnome () && isGnomeDarkMode ();
24+ case SOLARIS : // Solaris is screwed so we'll just return false.
25+ default :
26+ return false ;
27+ }
28+ }
29+
30+ public static boolean isMacOsDarkMode () {
31+ boolean isDarkMode = false ;
32+ String line = query ("defaults read -g AppleInterfaceStyle" );
33+ if (line .equals ("Dark" )) {
34+ isDarkMode = true ;
35+ }
36+ return isDarkMode ;
37+ }
38+
39+ public static boolean isWindowsDarkMode () {
40+ try {
41+ String result = query (DARK_THEME_CMD );
42+ int p = result .indexOf (REGDWORD_TOKEN );
43+
44+ if (p == -1 ) {
45+ return false ;
46+ }
47+
48+ // 1 == Light Mode, 0 == Dark Mode
49+ String temp = result .substring (p + REGDWORD_TOKEN .length ()).trim ();
50+ return ((Integer .parseInt (temp .substring ("0x" .length ()), 16 ))) == 0 ;
51+ } catch (Exception e ) {
52+ return false ;
53+ }
54+ }
55+
56+ public static boolean isGnomeDarkMode () {
57+ // This is based off jSystemThemeDetector's code.
58+ final Pattern darkThemeNamePattern = Pattern .compile (".*dark.*" , Pattern .CASE_INSENSITIVE );
59+ return darkThemeNamePattern .matcher (query ("gsettings get org.gnome.desktop.interface gtk-theme" )).matches ();
60+ }
61+
62+ public static OperatingSystem getOperatingSystem () {
63+ String os = System .getProperty ("os.name" ).toLowerCase ();
64+ if (os .contains ("win" )) {
65+ return OperatingSystem .WINDOWS ;
66+ } else if (os .contains ("mac" )) {
67+ return OperatingSystem .MACOS ;
68+ } else if (os .contains ("nix" ) || os .contains ("nux" )) {
69+ return OperatingSystem .LINUX ;
70+ } else if (os .contains ("sunos" )) {
71+ return OperatingSystem .SOLARIS ;
72+ } else {
73+ return OperatingSystem .NONE ;
74+ }
75+ }
76+
77+ // The following GNOME + query methods are based off jSystemThemeDetector's code.
78+ public static boolean isGnome () {
79+ return getOperatingSystem () == OperatingSystem .LINUX && (
80+ queryResultContains ("echo $XDG_CURRENT_DESKTOP" , "gnome" ) ||
81+ queryResultContains ("echo $XDG_DATA_DIRS | grep -Eo 'gnome'" , "gnome" ) ||
82+ queryResultContains ("ps -e | grep -E -i \" gnome\" " , "gnome" )
83+ );
84+ }
85+
86+ private static boolean queryResultContains (String cmd , String subResult ) {
87+ return query (cmd ).toLowerCase ().contains (subResult );
88+ }
89+
90+ private static String query (String cmd ) {
91+ try {
92+ Process process = Runtime .getRuntime ().exec (cmd );
93+ StringBuilder stringBuilder = new StringBuilder ();
94+ try (BufferedReader reader = new BufferedReader (new InputStreamReader (process .getInputStream ()))) {
95+ String actualReadLine ;
96+ while ((actualReadLine = reader .readLine ()) != null ) {
97+ if (stringBuilder .length () != 0 )
98+ stringBuilder .append ('\n' );
99+ stringBuilder .append (actualReadLine );
100+ }
101+ }
102+ return stringBuilder .toString ();
103+ } catch (IOException e ) {
104+ System .out .println ("Exception caught while querying the OS:" );
105+ e .printStackTrace ();
106+ return "" ;
107+ }
108+ }
109+ }
0 commit comments