Skip to content

Commit

Permalink
Fix window decorations (#11440)
Browse files Browse the repository at this point in the history
* Fix window decorations

* Enable debug log

* Check all screens

* Debug output screens

* Always save new window position

* Save window state after fixing it

* Fix casing

* Update tinylog.properties

* CHANGELOG.md

---------

Co-authored-by: Oliver Kopp <kopp.dev@gmail.com>
  • Loading branch information
calixtus and koppor authored Aug 2, 2024
1 parent 64699ab commit 60282b4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where the 'Check for updates' preference was not saved. [#11485](https://github.com/JabRef/jabref/pull/11485)
- We fixed an issue where an exception was thrown after changing "show preview as a tab" in the preferences. [#11515](https://github.com/JabRef/jabref/pull/11515)
- We fixed an issue where a new unsaved library was not marked with an asterisk [#11519](https://github.com/JabRef/jabref/pull/11519)
- We fixed an issue where JabRef starts without window decorations. [#11440](https://github.com/JabRef/jabref/pull/11440)

### Removed

Expand Down
60 changes: 31 additions & 29 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.stage.Screen;
Expand Down Expand Up @@ -66,7 +67,6 @@ public class JabRefGUI extends Application {

private static RemoteListenerServerManager remoteListenerServerManager;

private boolean correctedWindowPos = false;
private Stage mainStage;

public static void setup(List<UiCommand> uiCommands,
Expand Down Expand Up @@ -184,22 +184,29 @@ private void openWindow() {

mainStage.setMinHeight(330);
mainStage.setMinWidth(580);
mainStage.setFullScreen(guiPreferences.isWindowFullscreen());
mainStage.setMaximized(guiPreferences.isWindowMaximised());
if ((Screen.getScreens().size() == 1) && isWindowPositionOutOfBounds()) {
// corrects the Window, if it is outside the mainscreen
LOGGER.debug("The Jabref window is outside the main screen");
mainStage.setX(0);
mainStage.setY(0);
mainStage.setWidth(1024);
mainStage.setHeight(768);
correctedWindowPos = true;
// maximized target state is stored, because "saveWindowState" saves x and y only if not maximized
boolean windowMaximised = guiPreferences.isWindowMaximised();

LOGGER.debug("Screens: {}", Screen.getScreens());
debugLogWindowState(mainStage);

if (isWindowPositionOutOfBounds()) {
LOGGER.debug("The JabRef window is outside of screen bounds. Position and size will be corrected. Main screen will be used.");
Rectangle2D bounds = Screen.getPrimary().getBounds();
mainStage.setX(bounds.getMinX());
mainStage.setY(bounds.getMinY());
mainStage.setHeight(Math.min(bounds.getHeight(), 786.0));
mainStage.setWidth(Math.min(bounds.getWidth(), 1024.0));
saveWindowState();
} else {
LOGGER.debug("The JabRef window is inside screen bounds.");
mainStage.setX(guiPreferences.getPositionX());
mainStage.setY(guiPreferences.getPositionY());
mainStage.setWidth(guiPreferences.getSizeX());
mainStage.setHeight(guiPreferences.getSizeY());
}
// after calling "saveWindowState" the maximized state can be set
mainStage.setMaximized(windowMaximised);
debugLogWindowState(mainStage);

Scene scene = new Scene(JabRefGUI.mainFrame);
Expand Down Expand Up @@ -239,26 +246,20 @@ public void onCloseRequest(WindowEvent event) {
}

public void onHiding(WindowEvent event) {
if (!correctedWindowPos) {
// saves the window position only if its not corrected -> the window will rest at the old Position,
// if the external Screen is connected again.
saveWindowState();
}

saveWindowState();
preferencesService.flush();

// Goodbye!
Platform.exit();
}

private void saveWindowState() {
GuiPreferences preferences = preferencesService.getGuiPreferences();
preferences.setPositionX(mainStage.getX());
preferences.setPositionY(mainStage.getY());
preferences.setSizeX(mainStage.getWidth());
preferences.setSizeY(mainStage.getHeight());
if (!mainStage.isMaximized()) {
preferences.setPositionX(mainStage.getX());
preferences.setPositionY(mainStage.getY());
preferences.setSizeX(mainStage.getWidth());
preferences.setSizeY(mainStage.getHeight());
}
preferences.setWindowMaximised(mainStage.isMaximized());
preferences.setWindowFullScreen(mainStage.isFullScreen());
debugLogWindowState(mainStage);
}

Expand All @@ -281,13 +282,14 @@ private void debugLogWindowState(Stage mainStage) {

/**
* Tests if the window coordinates are out of the mainscreen
*
* @return outbounds
*/
private boolean isWindowPositionOutOfBounds() {
return !Screen.getPrimary().getBounds().contains(
preferencesService.getGuiPreferences().getPositionX(),
preferencesService.getGuiPreferences().getPositionY());
// The upper right corner is checked as there are most probably the window controls.
GuiPreferences guiPreferences = preferencesService.getGuiPreferences();
double rightX = guiPreferences.getPositionX() + guiPreferences.getSizeX();
double topY = guiPreferences.getPositionY();
return Screen.getScreens().stream().noneMatch((screen -> screen.getBounds().contains(
rightX, topY)));
}

// Background tasks
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/org/jabref/preferences/GuiPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class GuiPreferences {
private final DoubleProperty sizeY;

private final BooleanProperty windowMaximised;
private final BooleanProperty windowFullScreen;

// the last libraries that were open when jabref closes and should be reopened on startup
private final ObservableList<Path> lastFilesOpened;
Expand All @@ -39,7 +38,6 @@ public GuiPreferences(double positionX,
double sizeX,
double sizeY,
boolean windowMaximised,
boolean windowFullScreen,
List<Path> lastFilesOpened,
Path lastFocusedFile,
FileHistory fileHistory,
Expand All @@ -50,7 +48,6 @@ public GuiPreferences(double positionX,
this.sizeX = new SimpleDoubleProperty(sizeX);
this.sizeY = new SimpleDoubleProperty(sizeY);
this.windowMaximised = new SimpleBooleanProperty(windowMaximised);
this.windowFullScreen = new SimpleBooleanProperty(windowFullScreen);
this.lastFilesOpened = FXCollections.observableArrayList(lastFilesOpened);
this.lastFocusedFile = new SimpleObjectProperty<>(lastFocusedFile);
this.lastSelectedIdBasedFetcher = new SimpleStringProperty(lastSelectedIdBasedFetcher);
Expand Down Expand Up @@ -118,18 +115,6 @@ public void setWindowMaximised(boolean windowMaximised) {
this.windowMaximised.set(windowMaximised);
}

public BooleanProperty windowFullScreenProperty() {
return windowFullScreen;
}

public void setWindowFullScreen(boolean windowFullScreen) {
this.windowFullScreen.set(windowFullScreen);
}

public boolean isWindowFullscreen() {
return windowFullScreen.get();
}

public ObservableList<Path> getLastFilesOpened() {
return lastFilesOpened;
}
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public class JabRefPreferences implements PreferencesService {
public static final String ENTRY_EDITOR_PREVIEW_DIVIDER_POS = "entryEditorPreviewDividerPos";
public static final String AUTO_RESIZE_MODE = "autoResizeMode";
public static final String WINDOW_MAXIMISED = "windowMaximised";
public static final String WINDOW_FULLSCREEN = "windowFullscreen";

public static final String REFORMAT_FILE_ON_SAVE_AND_EXPORT = "reformatFileOnSaveAndExport";
public static final String EXPORT_IN_ORIGINAL_ORDER = "exportInOriginalOrder";
Expand Down Expand Up @@ -620,7 +619,6 @@ private JabRefPreferences() {
defaults.put(SIZE_X, 1024);
defaults.put(SIZE_Y, 768);
defaults.put(WINDOW_MAXIMISED, Boolean.TRUE);
defaults.put(WINDOW_FULLSCREEN, Boolean.FALSE);
defaults.put(AUTO_RESIZE_MODE, Boolean.FALSE); // By default disable "Fit table horizontally on the screen"
defaults.put(ENTRY_EDITOR_HEIGHT, 0.65);
defaults.put(ENTRY_EDITOR_PREVIEW_DIVIDER_POS, 0.5);
Expand Down Expand Up @@ -2617,7 +2615,6 @@ public GuiPreferences getGuiPreferences() {
getDouble(SIZE_X),
getDouble(SIZE_Y),
getBoolean(WINDOW_MAXIMISED),
getBoolean(WINDOW_FULLSCREEN),
getStringList(LAST_EDITED).stream()
.map(Path::of)
.collect(Collectors.toList()),
Expand All @@ -2631,7 +2628,6 @@ public GuiPreferences getGuiPreferences() {
EasyBind.listen(guiPreferences.sizeXProperty(), (obs, oldValue, newValue) -> putDouble(SIZE_X, newValue.doubleValue()));
EasyBind.listen(guiPreferences.sizeYProperty(), (obs, oldValue, newValue) -> putDouble(SIZE_Y, newValue.doubleValue()));
EasyBind.listen(guiPreferences.windowMaximisedProperty(), (obs, oldValue, newValue) -> putBoolean(WINDOW_MAXIMISED, newValue));
EasyBind.listen(guiPreferences.windowFullScreenProperty(), (obs, oldValue, newValue) -> putBoolean(WINDOW_FULLSCREEN, newValue));
guiPreferences.getLastFilesOpened().addListener((ListChangeListener<Path>) change -> {
if (change.getList().isEmpty()) {
prefs.remove(LAST_EDITED);
Expand Down

0 comments on commit 60282b4

Please sign in to comment.