Skip to content

Commit

Permalink
Merge pull request #33 from Koitharu/feature/gnome_fix
Browse files Browse the repository at this point in the history
Improve dark mode detection in Gnome
  • Loading branch information
Dansoftowner authored Sep 28, 2023
2 parents fcc9bd8 + 9a3824c commit 61d9025
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/java/com/jthemedetecor/GnomeThemeDetector.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ class GnomeThemeDetector extends OsThemeDetector {

private static final Logger logger = LoggerFactory.getLogger(GnomeThemeDetector.class);

private static final String MONITORING_CMD = "gsettings monitor org.gnome.desktop.interface gtk-theme";
private static final String GET_CMD = "gsettings get org.gnome.desktop.interface gtk-theme";
private static final String MONITORING_CMD = "gsettings monitor org.gnome.desktop.interface";
private static final String[] GET_CMD = new String[]{
"gsettings get org.gnome.desktop.interface gtk-theme",
"gsettings get org.gnome.desktop.interface color-scheme"
};

private final Set<Consumer<Boolean>> listeners = new ConcurrentHashSet<>();
private final Pattern darkThemeNamePattern = Pattern.compile(".*dark.*", Pattern.CASE_INSENSITIVE);
Expand All @@ -50,11 +53,13 @@ class GnomeThemeDetector extends OsThemeDetector {
public boolean isDark() {
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(GET_CMD);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String readLine = reader.readLine();
if (readLine != null) {
return isDarkTheme(readLine);
for (String cmd : GET_CMD) {
Process process = runtime.exec(cmd);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String readLine = reader.readLine();
if (readLine != null && isDarkTheme(readLine)) {
return true;
}
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -98,6 +103,7 @@ public synchronized void removeListener(@Nullable Consumer<Boolean> darkThemeLis
private static final class DetectorThread extends Thread {

private final GnomeThemeDetector detector;
private final Pattern outputPattern = Pattern.compile("(gtk-theme|color-scheme).*", Pattern.CASE_INSENSITIVE);
private boolean lastValue;

DetectorThread(@NotNull GnomeThemeDetector detector) {
Expand All @@ -117,6 +123,9 @@ public void run() {
while (!this.isInterrupted()) {
//Expected input = gtk-theme: '$GtkThemeName'
String readLine = reader.readLine();
if (!outputPattern.matcher(readLine).matches()) {
continue;
}
String[] keyValue = readLine.split("\\s");
String value = keyValue[1];
boolean currentDetection = detector.isDarkTheme(value);
Expand Down

0 comments on commit 61d9025

Please sign in to comment.