Skip to content

Commit 037888e

Browse files
author
IMS
authored
Dark mode (IrisShaders#17)
* Dark mode * macOS titlebar support A bit hacky, and only supports java 14+ afaik, but it works and doesn't hurt for java 8 * Simplify code a bit * Replace install with setup * Tiny note about solaris * Clean up detector
1 parent bcafffb commit 037888e

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ repositories {
2222

2323
dependencies {
2424
implementation "org.json:json:20210307"
25-
implementation "com.formdev:flatlaf:1.1.2"
25+
implementation "com.formdev:flatlaf:1.6.1"
2626

2727
implementation "net.fabricmc:fabric-installer:0.9.0"
2828
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
org.gradle.jvmargs=-Xmx1G
33

44
# Version and packaging info
5-
version=2.0.1
5+
version=2.0.2
66
maven_group=net.hypercubemc
77
archives_base_name=Iris-Installer
88

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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+
}

src/main/java/net/hypercubemc/iris_installer/Installer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package net.hypercubemc.iris_installer;
2+
import com.formdev.flatlaf.FlatDarkLaf;
23
import com.formdev.flatlaf.FlatLightLaf;
34
import net.fabricmc.installer.Main;
45
import net.fabricmc.installer.util.MetaHandler;
@@ -51,13 +52,12 @@ public static void main(String[] args) {
5152
}
5253

5354
public void start() {
54-
FlatLightLaf.install();
55-
56-
try {
57-
UIManager.setLookAndFeel(new FlatLightLaf());
58-
} catch (Exception e) {
59-
System.out.println("Failed to set UI theme!");
60-
e.printStackTrace();
55+
boolean dark = DarkModeDetector.isDarkMode();
56+
System.setProperty("apple.awt.application.appearance", "system");
57+
if (dark) {
58+
FlatDarkLaf.setup();
59+
} else {
60+
FlatLightLaf.setup();
6161
}
6262

6363
Main.LOADER_META = new MetaHandler(Reference.getMetaServerEndpoint("v2/versions/loader"));

0 commit comments

Comments
 (0)