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

Make global font size customizable #4178

Merged
merged 5 commits into from
Jul 12, 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/gui/customjfx/CustomJFXPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.jabref.gui.util.DefaultTaskExecutor;

/**
* Remove as soon as possible
* TODO: Remove as soon as possible
*/
public class CustomJFXPanel {

Expand Down
89 changes: 44 additions & 45 deletions src/main/java/org/jabref/gui/preftabs/AppearancePrefsTab.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package org.jabref.gui.preftabs;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridBagLayout;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

import org.jabref.gui.DialogService;
import org.jabref.gui.GUIGlobals;
import org.jabref.gui.customjfx.CustomJFXPanel;
import org.jabref.gui.util.ControlHelper;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.JabRefPreferences;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -24,8 +29,9 @@ class AppearancePrefsTab extends JPanel implements PrefsTab {

private final JabRefPreferences prefs;

private final Font usedFont = GUIGlobals.currentFont;
private final JCheckBox fxFontTweaksLAF;
private final CheckBox fontTweaksLAF;
private final TextField fontSize;
private final CheckBox overrideFonts;

private final DialogService dialogService;

Expand All @@ -39,53 +45,46 @@ public AppearancePrefsTab(DialogService dialogService, JabRefPreferences prefs)
this.prefs = prefs;
setLayout(new BorderLayout());

FormLayout layout = new FormLayout("1dlu, 8dlu, left:pref, 4dlu, fill:pref, 4dlu, fill:60dlu, 4dlu, fill:pref", "");
DefaultFormBuilder builder = new DefaultFormBuilder(layout);

fxFontTweaksLAF = new JCheckBox(Localization.lang("Tweak font rendering for entry editor on Linux"));
// Only list L&F which are available

// only the default L&F shows the OSX specific first drop-down menu

builder.append(fxFontTweaksLAF);
builder.nextLine();

builder.leadingColumnOffset(2);

JPanel upper = new JPanel();
JPanel sort = new JPanel();
JPanel namesp = new JPanel();
JPanel iconCol = new JPanel();
GridBagLayout gbl = new GridBagLayout();
upper.setLayout(gbl);
sort.setLayout(gbl);
namesp.setLayout(gbl);
iconCol.setLayout(gbl);

JPanel pan = builder.getPanel();
pan.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(pan, BorderLayout.CENTER);
overrideFonts = new CheckBox(Localization.lang("Override default font settings"));
fontSize = new TextField();
fontSize.setTextFormatter(ControlHelper.getIntegerTextFormatter());
Label fontSizeLabel = new Label(Localization.lang("Font size:"));
HBox fontSizeContainer = new HBox(fontSizeLabel, fontSize);
VBox.setMargin(fontSizeContainer, new Insets(0, 0, 0, 35));
fontSizeContainer.disableProperty().bind(overrideFonts.selectedProperty().not());
fontTweaksLAF = new CheckBox(Localization.lang("Tweak font rendering for entry editor on Linux"));

VBox container = new VBox();
container.getChildren().addAll(overrideFonts, fontSizeContainer, fontTweaksLAF);

JFXPanel panel = CustomJFXPanel.wrap(new Scene(container));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(panel, BorderLayout.CENTER);
}

@Override
public void setValues() {
fxFontTweaksLAF.setSelected(prefs.getBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK));
fontTweaksLAF.setSelected(prefs.getBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK));
overrideFonts.setSelected(prefs.getBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE));
fontSize.setText(String.valueOf(prefs.getInt(JabRefPreferences.MAIN_FONT_SIZE)));
}

@Override
public void storeSettings() {
boolean isRestartRequired = false;

// Java FX font rendering tweak
final boolean oldFxTweakValue = prefs.getBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK);
prefs.putBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK, fxFontTweaksLAF.isSelected());
isRestartRequired |= oldFxTweakValue != fxFontTweaksLAF.isSelected();

prefs.put(JabRefPreferences.FONT_FAMILY, usedFont.getFamily());
prefs.putInt(JabRefPreferences.FONT_STYLE, usedFont.getStyle());
prefs.putInt(JabRefPreferences.FONT_SIZE, usedFont.getSize());
GUIGlobals.currentFont = usedFont;

prefs.putBoolean(JabRefPreferences.FX_FONT_RENDERING_TWEAK, fontTweaksLAF.isSelected());

final boolean oldOverrideDefaultFontSize = prefs.getBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE);
final int oldFontSize = prefs.getInt(JabRefPreferences.MAIN_FONT_SIZE);
prefs.putBoolean(JabRefPreferences.OVERRIDE_DEFAULT_FONT_SIZE, overrideFonts.isSelected());
int newFontSize = Integer.parseInt(fontSize.getText());
prefs.putInt(JabRefPreferences.MAIN_FONT_SIZE, newFontSize);

boolean isRestartRequired =
oldFxTweakValue != fontTweaksLAF.isSelected()
|| oldOverrideDefaultFontSize != overrideFonts.isSelected()
|| oldFontSize != newFontSize;
if (isRestartRequired) {
dialogService.showWarningDialogAndWait(Localization.lang("Settings"),
Localization.lang("Some appearance settings you changed require to restart JabRef to come into effect."));
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/jabref/gui/util/ControlHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui.util;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import javax.swing.JComponent;

Expand All @@ -11,6 +12,7 @@
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextFormatter;

public class ControlHelper {

Expand Down Expand Up @@ -39,4 +41,20 @@ public static boolean childIsFocused(Parent node) {
}
});
}

/**
* Returns a text formatter that restricts input to integers
*/
public static TextFormatter<String> getIntegerTextFormatter() {
UnaryOperator<TextFormatter.Change> filter = change -> {
String text = change.getText();

if (text.matches("[0-9]*")) {
return change;
}

return null;
};
return new TextFormatter<>(filter);
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/util/ThemeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.scene.Parent;
import javafx.scene.Scene;

import org.jabref.Globals;
import org.jabref.gui.JabRefFrame;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.FileUpdateMonitor;
Expand Down Expand Up @@ -58,6 +59,8 @@ public void installBaseCss(Scene scene) {
addAndWatchForChanges(scene, cssUrl, 1);
}
}

Globals.prefs.getFontSize().ifPresent(size -> scene.getRoot().setStyle("-fx-font-size: " + size + "pt;"));
}

private void addAndWatchForChanges(Scene scene, String cssUrl, int index) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public class JabRefPreferences implements PreferencesService {
public static final String ICON_ENABLED_COLOR = "iconEnabledColor";
public static final String ICON_DISABLED_COLOR = "iconDisabledColor";
public static final String FONT_SIZE = "fontSize";
public static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
public static final String MAIN_FONT_SIZE = "mainFontSize";
public static final String FONT_STYLE = "fontStyle";
public static final String RECENT_DATABASES = "recentDatabases";
public static final String RENAME_ON_MOVE_FILE_TO_FILE_DIR = "renameOnMoveFileToFileDir";
Expand Down Expand Up @@ -688,6 +690,9 @@ private JabRefPreferences() {
defaults.put(LAST_USED_EXPORT, "");
defaults.put(SIDE_PANE_WIDTH, 0.15);

defaults.put(MAIN_FONT_SIZE, 9);
defaults.put(OVERRIDE_DEFAULT_FONT_SIZE, false);

defaults.put(IMPORT_INSPECTION_DIALOG_WIDTH, 650);
defaults.put(IMPORT_INSPECTION_DIALOG_HEIGHT, 650);
defaults.put(SHOW_FILE_LINKS_UPGRADE_WARNING, Boolean.TRUE);
Expand Down Expand Up @@ -1910,4 +1915,11 @@ public String getPreviewStyle() {
return get(PREVIEW_STYLE);
}

public Optional<Integer> getFontSize() {
if (getBoolean(OVERRIDE_DEFAULT_FONT_SIZE)) {
return Optional.of(getInt(MAIN_FONT_SIZE));
} else {
return Optional.empty();
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2251,3 +2251,5 @@ View\ change\ log=View change log
View\ event\ log=View event log
Website=Website
Write\ XMP-metadata\ to\ PDFs=Write XMP-metadata to PDFs
Font\ size\:=Font size\:
Override\ default\ font\ settings=Override default font settings