Skip to content

Commit

Permalink
Live reloading when switching themes (#7336)
Browse files Browse the repository at this point in the history
Co-authored-by: Carl Christian Snethlage <[email protected]>
  • Loading branch information
docrjp and calixtus authored Jan 3, 2022
1 parent 617c5a1 commit 222d214
Show file tree
Hide file tree
Showing 60 changed files with 1,228 additions and 733 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
### Changed

- We integrated the external file types dialog directly inside the preferences. [#8341](https://github.com/JabRef/jabref/pull/8341)
- We enabled switching themes in JabRef without the need to restart JabRef. [#7335](https://github.com/JabRef/jabref/pull/7335)

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/jabref/gui/DefaultInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import javax.swing.undo.UndoManager;

import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.journals.JournalAbbreviationRepository;
import org.jabref.logic.protectedterms.ProtectedTermsLoader;
Expand Down Expand Up @@ -39,6 +40,8 @@ private static Object createDependency(Class<?> clazz) {
return Globals.journalAbbreviationRepository;
} else if (clazz == StateManager.class) {
return Globals.stateManager;
} else if (clazz == ThemeManager.class) {
return Globals.getThemeManager();
} else if (clazz == FileUpdateMonitor.class) {
return Globals.getFileUpdateMonitor();
} else if (clazz == ProtectedTermsLoader.class) {
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/org/jabref/gui/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import org.jabref.architecture.AllowedToUseAwt;
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.util.DefaultFileUpdateMonitor;
import org.jabref.gui.util.DefaultTaskExecutor;
Expand Down Expand Up @@ -76,9 +77,8 @@ public class Globals {
public static BibEntryTypesManager entryTypesManager = new BibEntryTypesManager();

private static ClipBoardManager clipBoardManager = null;

// Key binding preferences
private static KeyBindingRepository keyBindingRepository;
private static ThemeManager themeManager;

private static DefaultFileUpdateMonitor fileUpdateMonitor;
private static TelemetryClient telemetryClient;
Expand All @@ -101,6 +101,16 @@ public static synchronized ClipBoardManager getClipboardManager() {
return clipBoardManager;
}

public static synchronized ThemeManager getThemeManager() {
if (themeManager == null) {
themeManager = new ThemeManager(
prefs.getAppearancePreferences(),
getFileUpdateMonitor(),
Runnable::run);
}
return themeManager;
}

// Background tasks
public static void startBackgroundTasks() {
Globals.fileUpdateMonitor = new DefaultFileUpdateMonitor();
Expand Down Expand Up @@ -142,7 +152,9 @@ public static FileUpdateMonitor getFileUpdateMonitor() {

public static void shutdownThreadPools() {
TASK_EXECUTOR.shutdown();
fileUpdateMonitor.shutdown();
if (fileUpdateMonitor != null) {
fileUpdateMonitor.shutdown();
}
JabRefExecutorService.INSTANCE.shutdownEverything();
}

Expand Down
28 changes: 14 additions & 14 deletions src/main/java/org/jabref/gui/JabRefDialogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
import javafx.util.Duration;

import org.jabref.gui.icon.IconTheme;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.DirectoryDialogConfiguration;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.gui.util.ZipFileChooser;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.PreferencesService;

import com.jfoenix.controls.JFXSnackbar;
import com.jfoenix.controls.JFXSnackbar.SnackbarEvent;
Expand All @@ -68,20 +68,20 @@ public class JabRefDialogService implements DialogService {

private static final Duration TOAST_MESSAGE_DISPLAY_TIME = Duration.millis(3000);
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefDialogService.class);
private static PreferencesService preferences;

private final Window mainWindow;
private final JFXSnackbar statusLine;
private final ThemeManager themeManager;

public JabRefDialogService(Window mainWindow, Pane mainPane, PreferencesService preferences) {
public JabRefDialogService(Window mainWindow, Pane mainPane, ThemeManager themeManager) {
this.mainWindow = mainWindow;
this.themeManager = themeManager;
this.statusLine = new JFXSnackbar(mainPane);
JabRefDialogService.preferences = preferences;
}

private FXDialog createDialog(AlertType type, String title, String content) {
FXDialog alert = new FXDialog(type, title, true);
preferences.getTheme().installCss(alert.getDialogPane().getScene());
themeManager.installCss(alert.getDialogPane().getScene());
alert.setHeaderText(null);
alert.setContentText(content);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
Expand Down Expand Up @@ -115,7 +115,7 @@ protected Node createDetailsButton() {

// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
preferences.getTheme().installCss(alert.getDialogPane().getScene());
themeManager.installCss(alert.getDialogPane().getScene());
alert.setHeaderText(null);
alert.setContentText(content);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
Expand All @@ -140,7 +140,7 @@ public <T> Optional<T> showChoiceDialogAndWait(String title, String content, Str
choiceDialog.setTitle(title);
choiceDialog.setContentText(content);
choiceDialog.initOwner(mainWindow);
preferences.getTheme().installCss(choiceDialog.getDialogPane().getScene());
themeManager.installCss(choiceDialog.getDialogPane().getScene());
return choiceDialog.showAndWait();
}

Expand All @@ -150,7 +150,7 @@ public Optional<String> showInputDialogAndWait(String title, String content) {
inputDialog.setHeaderText(title);
inputDialog.setContentText(content);
inputDialog.initOwner(mainWindow);
preferences.getTheme().installCss(inputDialog.getDialogPane().getScene());
themeManager.installCss(inputDialog.getDialogPane().getScene());
return inputDialog.showAndWait();
}

Expand All @@ -160,7 +160,7 @@ public Optional<String> showInputDialogWithDefaultAndWait(String title, String c
inputDialog.setHeaderText(title);
inputDialog.setContentText(content);
inputDialog.initOwner(mainWindow);
preferences.getTheme().installCss(inputDialog.getDialogPane().getScene());
themeManager.installCss(inputDialog.getDialogPane().getScene());
return inputDialog.showAndWait();
}

Expand Down Expand Up @@ -188,7 +188,7 @@ public void showErrorDialogAndWait(String message, Throwable exception) {
exceptionDialog.getDialogPane().setMaxWidth(mainWindow.getWidth() / 2);
exceptionDialog.setHeaderText(message);
exceptionDialog.initOwner(mainWindow);
preferences.getTheme().installCss(exceptionDialog.getDialogPane().getScene());
themeManager.installCss(exceptionDialog.getDialogPane().getScene());
exceptionDialog.showAndWait();
}

Expand All @@ -198,7 +198,7 @@ public void showErrorDialogAndWait(String title, String content, Throwable excep
exceptionDialog.setHeaderText(title);
exceptionDialog.setContentText(content);
exceptionDialog.initOwner(mainWindow);
preferences.getTheme().installCss(exceptionDialog.getDialogPane().getScene());
themeManager.installCss(exceptionDialog.getDialogPane().getScene());
exceptionDialog.showAndWait();
}

Expand Down Expand Up @@ -268,7 +268,7 @@ public Optional<ButtonType> showCustomDialogAndWait(String title, DialogPane con
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setResizable(true);
alert.initOwner(mainWindow);
preferences.getTheme().installCss(alert.getDialogPane().getScene());
themeManager.installCss(alert.getDialogPane().getScene());
return alert.showAndWait();
}

Expand Down Expand Up @@ -296,7 +296,7 @@ public <V> void showProgressDialog(String title, String content, Task<V> task) {
task.cancel();
progressDialog.close();
});
preferences.getTheme().installCss(progressDialog.getDialogPane().getScene());
themeManager.installCss(progressDialog.getDialogPane().getScene());
progressDialog.initOwner(mainWindow);
progressDialog.show();
}
Expand All @@ -321,7 +321,7 @@ public <V> Optional<ButtonType> showBackgroundProgressDialogAndWait(String title
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.setResizable(true);
alert.initOwner(mainWindow);
preferences.getTheme().installCss(alert.getDialogPane().getScene());
themeManager.installCss(alert.getDialogPane().getScene());

stateManager.getAnyTaskRunning().addListener((observable, oldValue, newValue) -> {
if (!newValue) {
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
import org.jabref.gui.slr.StartNewStudyAction;
import org.jabref.gui.specialfields.SpecialFieldMenuItemFactory;
import org.jabref.gui.texparser.ParseLatexAction;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.UndoRedoAction;
import org.jabref.gui.util.BackgroundTask;
Expand Down Expand Up @@ -169,6 +170,7 @@ public class JabRefFrame extends BorderPane {

private final Stage mainStage;
private final StateManager stateManager;
private final ThemeManager themeManager;
private final CountingUndoManager undoManager;
private final PushToApplicationsManager pushToApplicationsManager;
private final DialogService dialogService;
Expand All @@ -183,8 +185,9 @@ public class JabRefFrame extends BorderPane {

public JabRefFrame(Stage mainStage) {
this.mainStage = mainStage;
this.dialogService = new JabRefDialogService(mainStage, this, prefs);
this.stateManager = Globals.stateManager;
this.themeManager = Globals.getThemeManager();
this.dialogService = new JabRefDialogService(mainStage, this, themeManager);
this.pushToApplicationsManager = new PushToApplicationsManager(dialogService, stateManager, prefs);
this.undoManager = Globals.undoManager;
this.globalSearchBar = new GlobalSearchBar(this, stateManager, prefs, undoManager);
Expand Down Expand Up @@ -494,7 +497,7 @@ private Node createToolbar() {

new HBox(
factory.createIconButton(StandardActions.NEW_LIBRARY, new NewDatabaseAction(this, prefs)),
factory.createIconButton(StandardActions.OPEN_LIBRARY, new OpenDatabaseAction(this, prefs, dialogService, stateManager)),
factory.createIconButton(StandardActions.OPEN_LIBRARY, new OpenDatabaseAction(this, prefs, dialogService, stateManager, themeManager)),
factory.createIconButton(StandardActions.SAVE_LIBRARY, new SaveAction(SaveAction.SaveMethod.SAVE, this, prefs, stateManager))),

leftSpacer,
Expand Down Expand Up @@ -841,8 +844,8 @@ private MenuBar createMenu() {
factory.createMenuItem(StandardActions.SEND_AS_EMAIL, new SendAsEMailAction(dialogService, this.prefs, stateManager)),
pushToApplicationMenuItem,
new SeparatorMenuItem(),
factory.createMenuItem(StandardActions.START_NEW_STUDY, new StartNewStudyAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager)),
factory.createMenuItem(StandardActions.SEARCH_FOR_EXISTING_STUDY, new ExistingStudySearchAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager)),
factory.createMenuItem(StandardActions.START_NEW_STUDY, new StartNewStudyAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager, themeManager)),
factory.createMenuItem(StandardActions.SEARCH_FOR_EXISTING_STUDY, new ExistingStudySearchAction(this, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, prefs, stateManager, themeManager)),

new SeparatorMenuItem(),

Expand Down Expand Up @@ -1104,7 +1107,7 @@ private void trackOpenNewDatabase(LibraryTab libraryTab) {
public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) {
Objects.requireNonNull(databaseContext);

LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, databaseContext, ExternalFileTypes.getInstance());
LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, ExternalFileTypes.getInstance());
addTab(libraryTab, raisePanel);
return libraryTab;
}
Expand Down Expand Up @@ -1260,7 +1263,7 @@ public void closeCurrentTab() {
}

public OpenDatabaseAction getOpenDatabaseAction() {
return new OpenDatabaseAction(this, prefs, dialogService, stateManager);
return new OpenDatabaseAction(this, prefs, dialogService, stateManager, themeManager);
}

public PushToApplicationsManager getPushToApplicationsManager() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private void openWindow(Stage mainStage) {
root.getChildren().add(JabRefGUI.mainFrame);

Scene scene = new Scene(root, 800, 800);
preferencesService.getTheme().installCss(scene);
Globals.getThemeManager().installCss(scene);

// Handle TextEditor key bindings
scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> TextInputKeyBindings.call(scene, event));
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
Expand Down Expand Up @@ -82,6 +83,7 @@ public class LibraryTab extends Tab {
private final DialogService dialogService;
private final PreferencesService preferencesService;
private final StateManager stateManager;
private final ThemeManager themeManager;
private final BooleanProperty changedProperty = new SimpleBooleanProperty(false);
private final BooleanProperty nonUndoableChangeProperty = new SimpleBooleanProperty(false);
private BibDatabaseContext bibDatabaseContext;
Expand Down Expand Up @@ -111,6 +113,7 @@ public class LibraryTab extends Tab {
public LibraryTab(JabRefFrame frame,
PreferencesService preferencesService,
StateManager stateManager,
ThemeManager themeManager,
BibDatabaseContext bibDatabaseContext,
ExternalFileTypes externalFileTypes) {
this.frame = Objects.requireNonNull(frame);
Expand All @@ -120,6 +123,7 @@ public LibraryTab(JabRefFrame frame,
this.dialogService = frame.getDialogService();
this.preferencesService = Objects.requireNonNull(preferencesService);
this.stateManager = Objects.requireNonNull(stateManager);
this.themeManager = Objects.requireNonNull(themeManager);

bibDatabaseContext.getDatabase().registerListener(this);
bibDatabaseContext.getMetaData().registerListener(this);
Expand Down Expand Up @@ -733,7 +737,13 @@ public FileAnnotationCache getAnnotationCache() {

public void resetChangeMonitorAndChangePane() {
changeMonitor.ifPresent(DatabaseChangeMonitor::unregister);
changeMonitor = Optional.of(new DatabaseChangeMonitor(bibDatabaseContext, Globals.getFileUpdateMonitor(), Globals.TASK_EXECUTOR, preferencesService, stateManager));
changeMonitor = Optional.of(new DatabaseChangeMonitor(bibDatabaseContext,
Globals.getFileUpdateMonitor(),
Globals.TASK_EXECUTOR,
dialogService,
preferencesService,
stateManager,
themeManager));

changePane = new DatabaseChangePane(splitPane, bibDatabaseContext, changeMonitor.get());

Expand Down Expand Up @@ -779,11 +789,11 @@ public void resetChangedProperties() {
}

public static class Factory {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, Path file, BackgroundTask<ParserResult> dataLoadingTask) {
public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService preferencesService, StateManager stateManager, ThemeManager themeManager, Path file, BackgroundTask<ParserResult> dataLoadingTask) {
BibDatabaseContext context = new BibDatabaseContext();
context.setDatabasePath(file);

LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, context, ExternalFileTypes.getInstance());
LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, ExternalFileTypes.getInstance());
newTab.setDataLoadingTask(dataLoadingTask);

dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jabref/gui/auximport/FromAuxDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.auxparser.AuxParser;
Expand Down Expand Up @@ -42,6 +43,7 @@ public class FromAuxDialog extends BaseDialog<Void> {

@Inject private PreferencesService preferences;
@Inject private DialogService dialogService;
@Inject private ThemeManager themeManager;

public FromAuxDialog(JabRefFrame frame) {
libraryTab = frame.getCurrentLibraryTab();
Expand All @@ -61,6 +63,8 @@ public FromAuxDialog(JabRefFrame frame) {
}
return null;
});

themeManager.updateFontStyle(getDialogPane().getScene());
}

@FXML
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/org/jabref/gui/collab/ChangeScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.util.Collections;
import java.util.List;

import org.jabref.gui.DialogService;
import org.jabref.gui.StateManager;
import org.jabref.gui.theme.ThemeManager;
import org.jabref.logic.bibtex.comparator.BibDatabaseDiff;
import org.jabref.logic.bibtex.comparator.BibEntryDiff;
import org.jabref.logic.bibtex.comparator.BibStringDiff;
Expand All @@ -24,13 +26,21 @@ public class ChangeScanner {

private static final Logger LOGGER = LoggerFactory.getLogger(ChangeScanner.class);
private final BibDatabaseContext database;
private final DialogService dialogService;
private final PreferencesService preferencesService;
private final StateManager stateManager;
private final ThemeManager themeManager;

public ChangeScanner(BibDatabaseContext database, PreferencesService preferencesService, StateManager stateManager) {
public ChangeScanner(BibDatabaseContext database,
DialogService dialogService,
PreferencesService preferencesService,
StateManager stateManager,
ThemeManager themeManager) {
this.database = database;
this.dialogService = dialogService;
this.preferencesService = preferencesService;
this.stateManager = stateManager;
this.themeManager = themeManager;
}

public List<DatabaseChangeViewModel> scanForChanges() {
Expand Down Expand Up @@ -82,7 +92,7 @@ private DatabaseChangeViewModel createBibStringDiff(BibStringDiff diff) {

private DatabaseChangeViewModel createBibEntryDiff(BibEntryDiff diff) {
if (diff.getOriginalEntry() == null) {
return new EntryAddChangeViewModel(diff.getNewEntry(), preferencesService, stateManager);
return new EntryAddChangeViewModel(diff.getNewEntry(), preferencesService, dialogService, stateManager, themeManager);
}

if (diff.getNewEntry() == null) {
Expand Down
Loading

0 comments on commit 222d214

Please sign in to comment.