Skip to content

Fix highlight color of selected text and progress bar #4420

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

Merged
merged 2 commits into from
Oct 29, 2018
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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/gui/Base.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/customjfx/CustomJFXPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/org/jabref/gui/preferences/AppearancePrefsTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/util/BaseDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
91 changes: 43 additions & 48 deletions src/main/java/org/jabref/gui/util/ThemeLoader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<URL> 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();
}
}

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ private JabRefPreferences() {
+ "</dd>__NEWLINE__<p></p></font>");

// set default theme
defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.DEFAULT_MAIN_CSS);
defaults.put(JabRefPreferences.FX_THEME, ThemeLoader.MAIN_CSS);
setLanguageDependentDefaultValues();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/styletester/StyleTesterMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down