Skip to content

Commit

Permalink
Add wokraround for theme detector issue (JabRef#10777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siedlerchr authored Jan 13, 2024
1 parent 46ece6f commit aa41185
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/main/java/org/jabref/gui/theme/ThemeManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ 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<>());
private final OsThemeDetector detector = OsThemeDetector.getDetector();

public ThemeManager(WorkspacePreferences workspacePreferences,
FileUpdateMonitor fileUpdateMonitor,
Expand All @@ -71,13 +72,20 @@ public ThemeManager(WorkspacePreferences workspacePreferences,
EasyBind.subscribe(workspacePreferences.themeSyncOsProperty(), theme -> updateThemeSettings());
EasyBind.subscribe(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), should -> updateFontSettings());
EasyBind.subscribe(workspacePreferences.mainFontSizeProperty(), size -> updateFontSettings());
detector.registerListener(isDark -> updateThemeSettings());

try {
detector = OsThemeDetector.getDetector();
detector.registerListener(isDark -> updateThemeSettings());
} catch (Exception ex) {
LOGGER.error("Could not initialize Theme detector!", ex);
workspacePreferences.setThemeSyncOs(false);
}
}

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

if (workspacePreferences.themeSyncOsProperty().getValue()) {
if (workspacePreferences.themeSyncOsProperty().getValue() && detector != null) {
if (detector.isDark()) {
newTheme = Theme.dark();
} else {
Expand Down Expand Up @@ -162,10 +170,10 @@ private void updateBaseCss() {
getMainWindowScene().ifPresent(scene -> {
List<String> stylesheets = scene.getStylesheets();
if (!stylesheets.isEmpty()) {
stylesheets.remove(0);
stylesheets.removeFirst();
}

stylesheets.add(0, baseStyleSheet.getSceneStylesheet().toExternalForm());
stylesheets.addFirst(baseStyleSheet.getSceneStylesheet().toExternalForm());
});
}

Expand Down

0 comments on commit aa41185

Please sign in to comment.