Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace jthemedetector with new platform prefs #11079

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@ dependencies {
}

implementation 'org.controlsfx:controlsfx:11.2.0'
implementation 'com.github.Dansoftowner:jSystemThemeDetector:3.8'

implementation 'org.jsoup:jsoup:1.17.2'
implementation 'com.konghq:unirest-java:3.14.5'
Expand Down
1 change: 0 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@
requires org.antlr.antlr4.runtime;
requires org.libreoffice.uno;
requires de.saxsys.mvvmfx.validation;
requires com.jthemedetector;
requires dd.plist;
requires mslinks;
requires org.yaml.snakeyaml;
Expand Down
68 changes: 34 additions & 34 deletions src/main/java/org/jabref/gui/theme/ThemeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.function.Consumer;

import javafx.application.ColorScheme;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;

import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.model.util.FileUpdateListener;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.WorkspacePreferences;

import com.jthemedetecor.OsThemeDetector;
import com.tobiasdiez.easybind.EasyBind;
import com.google.common.annotations.VisibleForTesting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -48,8 +49,6 @@ public class ThemeManager {
private final StyleSheet baseStyleSheet;
private Theme theme;

private OsThemeDetector detector;

private Scene mainWindowScene;
private final Set<WebEngine> webEngines = Collections.newSetFromMap(new WeakHashMap<>());

Expand All @@ -68,25 +67,19 @@ public ThemeManager(WorkspacePreferences workspacePreferences,
addStylesheetToWatchlist(this.baseStyleSheet, this::baseCssLiveUpdate);
baseCssLiveUpdate();

EasyBind.subscribe(workspacePreferences.themeProperty(), theme -> updateThemeSettings());
EasyBind.subscribe(workspacePreferences.themeSyncOsProperty(), theme -> updateThemeSettings());
EasyBind.subscribe(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), should -> updateFontSettings());
EasyBind.subscribe(workspacePreferences.mainFontSizeProperty(), size -> updateFontSettings());

try {
detector = OsThemeDetector.getDetector();
detector.registerListener(isDark -> updateThemeSettings());
} catch (Exception ex) {
LOGGER.error("Could not initialize Theme detector!", ex);
workspacePreferences.setThemeSyncOs(false);
}
BindingsHelper.subscribeFuture(workspacePreferences.themeProperty(), theme -> updateThemeSettings());
BindingsHelper.subscribeFuture(workspacePreferences.themeSyncOsProperty(), theme -> updateThemeSettings());
BindingsHelper.subscribeFuture(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), should -> updateFontSettings());
BindingsHelper.subscribeFuture(workspacePreferences.mainFontSizeProperty(), size -> updateFontSettings());
BindingsHelper.subscribeFuture(Platform.getPreferences().colorSchemeProperty(), colorScheme -> updateThemeSettings());
updateThemeSettings();
}

private void updateThemeSettings() {
Theme newTheme = Objects.requireNonNull(workspacePreferences.getTheme());

if (workspacePreferences.themeSyncOsProperty().getValue() && detector != null) {
if (detector.isDark()) {
if (workspacePreferences.themeSyncOsProperty().getValue()) {
if (Platform.getPreferences().getColorScheme() == ColorScheme.DARK) {
newTheme = Theme.dark();
} else {
newTheme = Theme.light();
Expand All @@ -109,7 +102,7 @@ private void updateThemeSettings() {
}

private void updateFontSettings() {
DefaultTaskExecutor.runInJavaFXThread(() -> updateRunner.accept(() -> getMainWindowScene().ifPresent(this::updateFontStyle)));
DefaultTaskExecutor.runInJavaFXThread(() -> updateRunner.accept(() -> updateFontStyle(mainWindowScene)));
}

private void removeStylesheetFromWatchList(StyleSheet styleSheet) {
Expand Down Expand Up @@ -167,18 +160,24 @@ private void additionalCssLiveUpdate() {
}

private void updateBaseCss() {
getMainWindowScene().ifPresent(scene -> {
List<String> stylesheets = scene.getStylesheets();
if (!stylesheets.isEmpty()) {
stylesheets.removeFirst();
}
if (mainWindowScene == null) {
return;
}

stylesheets.addFirst(baseStyleSheet.getSceneStylesheet().toExternalForm());
});
List<String> stylesheets = mainWindowScene.getStylesheets();
if (!stylesheets.isEmpty()) {
stylesheets.removeFirst();
}

stylesheets.addFirst(baseStyleSheet.getSceneStylesheet().toExternalForm());
}

private void updateAdditionalCss() {
getMainWindowScene().ifPresent(scene -> scene.getStylesheets().setAll(List.of(
if (mainWindowScene == null) {
return;
}

mainWindowScene.getStylesheets().setAll(List.of(
baseStyleSheet.getSceneStylesheet().toExternalForm(),
theme.getAdditionalStylesheet().map(styleSheet -> {
URL stylesheetUrl = styleSheet.getSceneStylesheet();
Expand All @@ -189,7 +188,7 @@ private void updateAdditionalCss() {
}
})
.orElse("")
)));
));
}

/**
Expand Down Expand Up @@ -229,6 +228,10 @@ public void installCss(WebEngine webEngine) {
* @param scene is the scene, the font size should be applied to
*/
public void updateFontStyle(Scene scene) {
if (scene == null) {
return;
}

if (workspacePreferences.shouldOverrideDefaultFontSize()) {
scene.getRoot().setStyle("-fx-font-size: " + workspacePreferences.getMainFontSize() + "pt;");
} else {
Expand All @@ -239,11 +242,8 @@ public void updateFontStyle(Scene scene) {
/**
* @return the currently active theme
*/
public Theme getActiveTheme() {
@VisibleForTesting
Theme getActiveTheme() {
return this.theme;
}

public Optional<Scene> getMainWindowScene() {
return Optional.ofNullable(mainWindowScene);
}
}
Loading