Skip to content

Commit

Permalink
Added some flesh to poc
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Aug 26, 2021
1 parent 0894e5b commit 02feee9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class AppearanceTabViewModel implements PreferenceTabViewModel {

private final DialogService dialogService;
private final PreferencesService preferences;
private final AppearancePreferences initialAppearancePreferences;
private final AppearancePreferences appearancePreferences;

private final Validator fontSizeValidator;
private final Validator customPathToThemeValidator;
Expand All @@ -51,7 +51,7 @@ public class AppearanceTabViewModel implements PreferenceTabViewModel {
public AppearanceTabViewModel(DialogService dialogService, PreferencesService preferences) {
this.dialogService = dialogService;
this.preferences = preferences;
this.initialAppearancePreferences = preferences.getAppearancePreferences();
this.appearancePreferences = preferences.getAppearancePreferences();

fontSizeValidator = new FunctionBasedValidator<>(
fontSizeProperty,
Expand All @@ -78,10 +78,10 @@ public AppearanceTabViewModel(DialogService dialogService, PreferencesService pr

@Override
public void setValues() {
fontOverrideProperty.setValue(initialAppearancePreferences.shouldOverrideDefaultFontSize());
fontSizeProperty.setValue(String.valueOf(initialAppearancePreferences.getMainFontSize()));
fontOverrideProperty.setValue(appearancePreferences.shouldOverrideDefaultFontSize());
fontSizeProperty.setValue(String.valueOf(appearancePreferences.getMainFontSize()));

Theme currentTheme = initialAppearancePreferences.getTheme();
Theme currentTheme = appearancePreferences.getTheme();
if (currentTheme.getType() == Theme.Type.LIGHT) {
themeLightProperty.setValue(true);
themeDarkProperty.setValue(false);
Expand All @@ -100,35 +100,34 @@ public void setValues() {

@Override
public void storeSettings() {
if (initialAppearancePreferences.shouldOverrideDefaultFontSize() != fontOverrideProperty.getValue()) {
if (appearancePreferences.shouldOverrideDefaultFontSize() != fontOverrideProperty.getValue()) {
restartWarnings.add(Localization.lang("Override font settings"));
}

int newFontSize = Integer.parseInt(fontSizeProperty.getValue());
if (initialAppearancePreferences.getMainFontSize() != newFontSize) {
if (appearancePreferences.getMainFontSize() != newFontSize) {
restartWarnings.add(Localization.lang("Override font size"));
}

Theme newTheme = initialAppearancePreferences.getTheme();
if (themeLightProperty.getValue() && initialAppearancePreferences.getTheme().getType() != Theme.Type.LIGHT) {
Theme newTheme = appearancePreferences.getTheme();
if (themeLightProperty.getValue() && appearancePreferences.getTheme().getType() != Theme.Type.LIGHT) {
restartWarnings.add(Localization.lang("Theme changed to light theme."));
newTheme = new Theme("", preferences);
} else if (themeDarkProperty.getValue() && initialAppearancePreferences.getTheme().getType() != Theme.Type.DARK) {
} else if (themeDarkProperty.getValue() && appearancePreferences.getTheme().getType() != Theme.Type.DARK) {
restartWarnings.add(Localization.lang("Theme changed to dark theme."));
newTheme = new Theme(EMBEDDED_DARK_THEME_CSS, preferences);
} else if (themeCustomProperty.getValue() &&
(!initialAppearancePreferences.getTheme().getCssPathString()
.equalsIgnoreCase(customPathToThemeProperty.getValue())
|| initialAppearancePreferences.getTheme().getType() != Theme.Type.CUSTOM)) {
(!appearancePreferences.getTheme().getCssPathString()
.equalsIgnoreCase(customPathToThemeProperty.getValue())
|| appearancePreferences.getTheme().getType() != Theme.Type.CUSTOM)) {
restartWarnings.add(Localization.lang("Theme changed to a custom theme:") + " "
+ customPathToThemeProperty().getValue());
newTheme = new Theme(customPathToThemeProperty.getValue(), preferences);
}

preferences.storeAppearancePreference(new AppearancePreferences(
fontOverrideProperty.getValue(),
newFontSize,
newTheme));
appearancePreferences.setShouldOverrideDefaultFontSize(fontOverrideProperty.getValue());
appearancePreferences.setMainFontSize(newFontSize);
appearancePreferences.setTheme(newTheme);

preferences.updateTheme();
}
Expand Down
35 changes: 27 additions & 8 deletions src/main/java/org/jabref/preferences/AppearancePreferences.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
package org.jabref.preferences;

import org.jabref.gui.util.Theme;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;

import org.jabref.gui.util.Theme;

public class AppearancePreferences {
private final BooleanProperty shouldOverrideDefaultFontSize;
private final IntegerProperty mainFontSize;
private final ObjectProperty<Theme> theme;

public AppearancePreferences(boolean shouldOverrideDefaultFontSize, int mainFontSize, Theme theme) {
this.shouldOverrideDefaultFontSize = new BooleanProperty(shouldOverrideDefaultFontSize);
this.mainFontSize = new IntegerProperty(mainFontSize);
this.theme = new SimpleObjectProperty(theme);
this.shouldOverrideDefaultFontSize = new SimpleBooleanProperty(shouldOverrideDefaultFontSize);
this.mainFontSize = new SimpleIntegerProperty(mainFontSize);
this.theme = new SimpleObjectProperty<>(theme);
}

public boolean shouldOverrideDefaultFontSize() {
return shouldOverrideDefaultFontSize.get();
}

public boolean setShouldOverrideDefaultFontSize(boolean newValue) {
return shouldOverrideDefaultFontSize.set(newValue);
public void setShouldOverrideDefaultFontSize(boolean newValue) {
shouldOverrideDefaultFontSize.set(newValue);
}

public BooleanProperty shouldOverrideDefaultFontSizeProperty() {
return shouldOverrideDefaultFontSize;
}

public int getMainFontSize() {
return mainFontSize.get();
}

public void setMainFontSize(int mainFontSize) {
this.mainFontSize.set(mainFontSize);
}

public IntegerProperty mainFontSizeProperty() {
return mainFontSize;
}

public Theme getTheme() {
return theme.get();
}

public void setTheme(Theme theme) {
this.theme.set(theme);
}

public ObjectProperty<Theme> themeProperty() {
return theme;
}
}
17 changes: 9 additions & 8 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import org.jabref.model.push.PushToApplicationConstants;
import org.jabref.model.strings.StringUtil;

import com.tobiasdiez.easybind.EasyBind;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -192,6 +193,7 @@ public class JabRefPreferences implements PreferencesService {
public static final String AUTO_ASSIGN_GROUP = "autoAssignGroup";
public static final String DISPLAY_GROUP_COUNT = "displayGroupCount";
public static final String EXTRA_FILE_COLUMNS = "extraFileColumns";
public static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
public static final String MAIN_FONT_SIZE = "mainFontSize";

public static final String RECENT_DATABASES = "recentDatabases";
Expand Down Expand Up @@ -416,6 +418,7 @@ public class JabRefPreferences implements PreferencesService {
private Set<CustomImporter> customImporters;
private String userName;
private AppearancePreferences appearancePreferences;

// The constructor is made private to enforce this as a singleton class:
private JabRefPreferences() {
try {
Expand Down Expand Up @@ -1990,22 +1993,20 @@ public AppearancePreferences getAppearancePreferences() {
if (appearancePreferences != null) {
return appearancePreferences;
}
String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";

appearancePreferences = new AppearancePreferences(
getBoolean(OVERRIDE_DEFAULT_FONT_SIZE),
getInt(MAIN_FONT_SIZE),
getTheme());
getTheme()
);

EasyBind.subscribe(appearancePreferences.shouldOverrideDefaultFontSizeProperty(), newValue -> putBoolean(OVERRIDE_DEFAULT_FONT_SIZE, newValue));
EasyBind.subscribe(appearancePreferences.mainFontSizeProperty(), newValue -> putInt(MAIN_FONT_SIZE, newValue));
EasyBind.subscribe(appearancePreferences.themeProperty(), newValue -> put(FX_THEME, newValue.getCssPathString()));

return appearancePreferences;
}

@Override
public void storeAppearancePreference(AppearancePreferences preferences) {
putInt(MAIN_FONT_SIZE, preferences.getMainFontSize());
put(FX_THEME, preferences.getTheme().getCssPathString());
}

//*************************************************************************************************************
// File preferences
//*************************************************************************************************************
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/preferences/PreferencesService.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,6 @@ public interface PreferencesService {

AppearancePreferences getAppearancePreferences();

void storeAppearancePreference(AppearancePreferences preferences);

//*************************************************************************************************************
// File preferences
//*************************************************************************************************************
Expand Down

0 comments on commit 02feee9

Please sign in to comment.