From f8b1e2d0a3cbe8a4a37bbabc756e757b58a5eabe Mon Sep 17 00:00:00 2001 From: Tobias Diez Date: Mon, 29 Oct 2018 18:02:53 +0100 Subject: [PATCH] Fix highlight color of selected text and progress bar (#4420) In the progress, also a few NPEs in the ThemeLoader were fixed. --- src/main/java/org/jabref/JabRefGUI.java | 2 +- src/main/java/org/jabref/gui/Base.css | 15 +++ .../jabref/gui/customjfx/CustomJFXPanel.java | 2 +- .../gui/preferences/AppearancePrefsTab.java | 15 ++- .../java/org/jabref/gui/util/BaseDialog.java | 2 +- .../java/org/jabref/gui/util/ThemeLoader.java | 91 +++++++++---------- .../jabref/preferences/JabRefPreferences.java | 2 +- .../jabref/styletester/StyleTesterMain.java | 2 +- 8 files changed, 70 insertions(+), 61 deletions(-) diff --git a/src/main/java/org/jabref/JabRefGUI.java b/src/main/java/org/jabref/JabRefGUI.java index ac996a79899..57214d9ef9f 100644 --- a/src/main/java/org/jabref/JabRefGUI.java +++ b/src/main/java/org/jabref/JabRefGUI.java @@ -150,7 +150,7 @@ private void openWindow(Stage mainStage) { root.getChildren().add(JabRefGUI.mainFrame); Scene scene = new Scene(root, 800, 800); - Globals.getThemeLoader().installBaseCss(scene, Globals.prefs); + Globals.getThemeLoader().installCss(scene, Globals.prefs); mainStage.setTitle(JabRefFrame.FRAME_TITLE); mainStage.getIcons().addAll(IconTheme.getLogoSetFX()); mainStage.setScene(scene); diff --git a/src/main/java/org/jabref/gui/Base.css b/src/main/java/org/jabref/gui/Base.css index ed4bbd29f96..349b7acc6d7 100644 --- a/src/main/java/org/jabref/gui/Base.css +++ b/src/main/java/org/jabref/gui/Base.css @@ -242,6 +242,7 @@ /* * The base css file defining the style that is valid for every pane and dialog. */ + .hyperlink { -fx-padding: 0; -fx-underline: false; @@ -630,8 +631,10 @@ } .text-input:focused { + -fx-highlight-fill: derive(-jr-accent, 20%); -fx-background-color: -jr-accent, -fx-control-inner-background; -fx-background-insets: 0, 2; + -fx-highlight-text-fill: -fx-text-inner-color; } .text-area { @@ -941,3 +944,15 @@ We want to have a look that matches our icons in the tool-bar */ -fx-text-fill: -fx-light-text-color; -fx-padding: -1ex -0.5ex -1ex -0.5ex; } + +.progress-bar > .bar { + -fx-background-color: -jr-theme; +} + +.progress-bar:indeterminate > .bar { + -fx-background-color: -jr-theme; +} + +.progress-bar > .track { + -fx-background-color: -jr-accent; +} diff --git a/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java b/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java index 5cdafa593ce..e21f8ce805e 100644 --- a/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java +++ b/src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java @@ -13,7 +13,7 @@ public class CustomJFXPanel { public static JFXPanel wrap(Scene scene) { JFXPanel container = new JFXPanel(); - Globals.getThemeLoader().installBaseCss(scene, Globals.prefs); + Globals.getThemeLoader().installCss(scene, Globals.prefs); DefaultTaskExecutor.runInJavaFXThread(() -> container.setScene(scene)); return container; } diff --git a/src/main/java/org/jabref/gui/preferences/AppearancePrefsTab.java b/src/main/java/org/jabref/gui/preferences/AppearancePrefsTab.java index 185b7ca38c4..917b88b3ea4 100644 --- a/src/main/java/org/jabref/gui/preferences/AppearancePrefsTab.java +++ b/src/main/java/org/jabref/gui/preferences/AppearancePrefsTab.java @@ -13,14 +13,13 @@ import org.jabref.gui.DialogService; import org.jabref.gui.util.ControlHelper; +import org.jabref.gui.util.ThemeLoader; import org.jabref.logic.l10n.Localization; import org.jabref.model.strings.StringUtil; import org.jabref.preferences.JabRefPreferences; class AppearancePrefsTab extends Pane implements PrefsTab { - public static final String BASE_CSS = "Base.css"; - public static final String DARK_CSS = "Dark.css"; private final JabRefPreferences prefs; private final CheckBox fontTweaksLAF; private final TextField fontSize; @@ -55,9 +54,9 @@ public AppearancePrefsTab(DialogService dialogService, JabRefPreferences prefs) darkTheme.setToggleGroup(themeGroup); String cssFileName = prefs.get(JabRefPreferences.FX_THEME); - if (StringUtil.isBlank(cssFileName) || BASE_CSS.equals(cssFileName)) { + if (StringUtil.isBlank(cssFileName) || ThemeLoader.MAIN_CSS.equalsIgnoreCase(cssFileName)) { lightTheme.setSelected(true); - } else if (DARK_CSS.equals(cssFileName)) { + } else if (ThemeLoader.DARK_CSS.equals(cssFileName)) { darkTheme.setSelected(true); } @@ -89,11 +88,11 @@ public void storeSettings() { boolean isThemeChanged = false; - if (lightTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(BASE_CSS)) { - prefs.put(JabRefPreferences.FX_THEME, BASE_CSS); + if (lightTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(ThemeLoader.MAIN_CSS)) { + prefs.put(JabRefPreferences.FX_THEME, ThemeLoader.MAIN_CSS); isThemeChanged = true; - } else if (darkTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(DARK_CSS)) { - prefs.put(JabRefPreferences.FX_THEME, DARK_CSS); + } else if (darkTheme.isSelected() && !prefs.get(JabRefPreferences.FX_THEME).equals(ThemeLoader.DARK_CSS)) { + prefs.put(JabRefPreferences.FX_THEME, ThemeLoader.DARK_CSS); isThemeChanged = true; } diff --git a/src/main/java/org/jabref/gui/util/BaseDialog.java b/src/main/java/org/jabref/gui/util/BaseDialog.java index 41dda14d551..59f6e034e3b 100644 --- a/src/main/java/org/jabref/gui/util/BaseDialog.java +++ b/src/main/java/org/jabref/gui/util/BaseDialog.java @@ -21,7 +21,7 @@ protected BaseDialog() { setDialogIcon(IconTheme.getJabRefImageFX()); setResizable(true); - Globals.getThemeLoader().installBaseCss(getDialogPane().getScene(), Globals.prefs); + Globals.getThemeLoader().installCss(getDialogPane().getScene(), Globals.prefs); } private void setDialogIcon(Image image) { diff --git a/src/main/java/org/jabref/gui/util/ThemeLoader.java b/src/main/java/org/jabref/gui/util/ThemeLoader.java index 5f152bfe36d..b2230505578 100644 --- a/src/main/java/org/jabref/gui/util/ThemeLoader.java +++ b/src/main/java/org/jabref/gui/util/ThemeLoader.java @@ -1,13 +1,15 @@ package org.jabref.gui.util; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Objects; +import java.util.Optional; -import javafx.scene.Parent; import javafx.scene.Scene; import org.jabref.gui.JabRefFrame; @@ -34,29 +36,40 @@ */ public class ThemeLoader { - public static final String DEFAULT_MAIN_CSS = "Base.css"; - private static final String DEFAULT_PATH_MAIN_CSS = JabRefFrame.class.getResource(DEFAULT_MAIN_CSS).toExternalForm(); + public static final String MAIN_CSS = "Base.css"; + public static final String DARK_CSS = "Dark.css"; + private static final Logger LOGGER = LoggerFactory.getLogger(ThemeLoader.class); - private String cssToLoad = System.getProperty("jabref.theme.css"); + private final Optional additionalCssToLoad; private final FileUpdateMonitor fileUpdateMonitor; public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRefPreferences) { this.fileUpdateMonitor = Objects.requireNonNull(fileUpdateMonitor); - if (!StringUtil.isNullOrEmpty(cssToLoad)) { - LOGGER.info("using css from system " + cssToLoad); - return; - } - - // otherwise load css from preference - String cssFileName = jabRefPreferences.get(JabRefPreferences.FX_THEME); - if (cssFileName != null) { + String cssVmArgument = System.getProperty("jabref.theme.css"); + String cssPreferences = jabRefPreferences.get(JabRefPreferences.FX_THEME); + if (StringUtil.isNotBlank(cssVmArgument)) { + // First priority: VM argument + LOGGER.info("Using css from VM option: " + cssVmArgument); + URL cssVmUrl = null; try { - cssToLoad = JabRefFrame.class.getResource(cssFileName).toExternalForm(); - LOGGER.info("using css " + cssToLoad); - } catch (Exception e) { - LOGGER.warn("can't get css file path of " + cssFileName); + cssVmUrl = Paths.get(cssVmArgument).toUri().toURL(); + } catch (MalformedURLException e) { + LOGGER.warn("Cannot load css " + cssVmArgument, e); + } + additionalCssToLoad = Optional.ofNullable(cssVmUrl); + } else if (StringUtil.isNotBlank(cssPreferences) && !MAIN_CSS.equalsIgnoreCase(cssPreferences)) { + // Otherwise load css from preference + URL cssResource = JabRefFrame.class.getResource(cssPreferences); + if (cssResource != null) { + LOGGER.debug("Using css " + cssResource); + additionalCssToLoad = Optional.of(cssResource); + } else { + additionalCssToLoad = Optional.empty(); + LOGGER.warn("Cannot load css " + cssPreferences); } + } else { + additionalCssToLoad = Optional.empty(); } } @@ -65,50 +78,32 @@ public ThemeLoader(FileUpdateMonitor fileUpdateMonitor, JabRefPreferences jabRef * Installs the base css file as a stylesheet in the given scene. Changes in the css file lead to a redraw of the * scene using the new css file. */ - public void installBaseCss(Scene scene, JabRefPreferences preferences) { - if (!StringUtil.isNullOrEmpty(cssToLoad)) { - addAndWatchForChanges(scene, cssToLoad, 0); - } else { - LOGGER.warn("using the last default css " + DEFAULT_PATH_MAIN_CSS); - addAndWatchForChanges(scene, DEFAULT_PATH_MAIN_CSS, 0); - } + public void installCss(Scene scene, JabRefPreferences preferences) { + addAndWatchForChanges(scene, JabRefFrame.class.getResource(MAIN_CSS), 0); + additionalCssToLoad.ifPresent(file -> addAndWatchForChanges(scene, file, 1)); preferences.getFontSize().ifPresent(size -> scene.getRoot().setStyle("-fx-font-size: " + size + "pt;")); } - private void addAndWatchForChanges(Scene scene, String cssUrl, int index) { - // avoid repeat add - if (scene.getStylesheets().contains(cssUrl)) { - return; - } - - scene.getStylesheets().add(index, cssUrl); + private void addAndWatchForChanges(Scene scene, URL cssFile, int index) { + scene.getStylesheets().add(index, cssFile.toExternalForm()); try { - // If -Djabref.theme.css is defined and the resources are not part of a .jar bundle, - // we watch the file for changes and turn on live reloading - if (!cssUrl.startsWith("jar:")) { - Path cssFile = Paths.get(new URL(cssUrl).toURI()); - LOGGER.info("Enabling live reloading of " + cssFile); - fileUpdateMonitor.addListenerForFile(cssFile, () -> { + // If the file is an ordinary file (i.e. not a resource part of a .jar bundle), we watch it for changes and turn on live reloading + Path cssPath = Paths.get(cssFile.toURI()); + if (Files.isRegularFile(cssPath)) { + LOGGER.info("Enabling live reloading of " + cssPath); + fileUpdateMonitor.addListenerForFile(cssPath, () -> { LOGGER.info("Reload css file " + cssFile); DefaultTaskExecutor.runInJavaFXThread(() -> { - scene.getStylesheets().remove(cssUrl); - scene.getStylesheets().add(index, cssUrl); + scene.getStylesheets().remove(cssFile.toExternalForm()); + scene.getStylesheets().add(index, cssFile.toExternalForm()); } ); }); } - } catch (URISyntaxException | IOException e) { - LOGGER.error("Could not watch css file for changes " + cssUrl, e); + } catch (IOException | URISyntaxException e) { + LOGGER.error("Could not watch css file for changes " + cssFile, e); } } - - /** - * @deprecated you should never need to add css to a control, add it to the scene containing the control - */ - @Deprecated - public void installBaseCss(Parent control) { - control.getStylesheets().add(0, DEFAULT_PATH_MAIN_CSS); - } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index a61c1762f99..aaf9b4f24aa 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -784,7 +784,7 @@ private JabRefPreferences() { + "__NEWLINE__

"); // set default theme - defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.DEFAULT_MAIN_CSS); + defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.MAIN_CSS); setLanguageDependentDefaultValues(); } diff --git a/src/main/java/org/jabref/styletester/StyleTesterMain.java b/src/main/java/org/jabref/styletester/StyleTesterMain.java index 68a7331b989..13fa1ffcf7c 100644 --- a/src/main/java/org/jabref/styletester/StyleTesterMain.java +++ b/src/main/java/org/jabref/styletester/StyleTesterMain.java @@ -30,7 +30,7 @@ public void start(Stage stage) throws JabRefException { JabRefExecutorService.INSTANCE.executeInterruptableTask(fileUpdateMonitor, "FileUpdateMonitor"); Scene scene = new Scene(view.getContent()); - new ThemeLoader(fileUpdateMonitor, JabRefPreferences.getInstance()).installBaseCss(scene, JabRefPreferences.getInstance()); + new ThemeLoader(fileUpdateMonitor, JabRefPreferences.getInstance()).installCss(scene, JabRefPreferences.getInstance()); stage.setScene(scene); stage.show(); }