Skip to content

Commit

Permalink
Merge branch 'main' into floating-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
LoayGhreeb authored Aug 4, 2024
2 parents c62809f + 0052881 commit 9f6c846
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 86 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Fixed

- 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 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 with colors in the search bar when dark theme is enabled. [#11569](https://github.com/JabRef/jabref/issues/11569)
- 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
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pluginManagement {
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
}

rootProject.name = "JabRef"
84 changes: 56 additions & 28 deletions src/main/java/org/jabref/gui/JabRefGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void initialize() {
Injector.setModelOrService(DialogService.class, dialogService);

JabRefGUI.clipBoardManager = new ClipBoardManager();
Injector.setModelOrService(TaskExecutor.class, taskExecutor);
Injector.setModelOrService(ClipBoardManager.class, clipBoardManager);
}

private void setupProxy() {
Expand Down Expand Up @@ -181,29 +181,33 @@ private void openWindow() {
LOGGER.debug("Initializing frame");

GuiPreferences guiPreferences = preferencesService.getGuiPreferences();
LOGGER.debug("Reading from prefs: isMaximized {}", guiPreferences.isWindowMaximised());

mainStage.setMinHeight(330);
mainStage.setMinWidth(580);
mainStage.setMinHeight(330);

// 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 {
if (isWindowPositionInBounds()) {
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());
LOGGER.debug("NOT saving window positions");
} else {
LOGGER.info("The JabRef window is outside of screen bounds. Position and size will be corrected to 1024x768. Primary screen will be used.");
Rectangle2D bounds = Screen.getPrimary().getBounds();
mainStage.setX(bounds.getMinX());
mainStage.setY(bounds.getMinY());
mainStage.setWidth(Math.min(bounds.getWidth(), 1024.0));
mainStage.setHeight(Math.min(bounds.getHeight(), 786.0));
LOGGER.debug("Saving window positions");
saveWindowState();
}
// after calling "saveWindowState" the maximized state can be set
mainStage.setMaximized(windowMaximised);
Expand Down Expand Up @@ -264,32 +268,56 @@ private void saveWindowState() {
}

/**
* outprints the Data from the Screen (only in debug mode)
* prints the data from the screen (only in debug mode)
*
* @param mainStage JabRefs stage
* @param mainStage JabRef's stage
*/
private void debugLogWindowState(Stage mainStage) {
if (LOGGER.isDebugEnabled()) {
String debugLogString = "SCREEN DATA:" +
"mainStage.WINDOW_MAXIMISED: " + mainStage.isMaximized() + "\n" +
"mainStage.POS_X: " + mainStage.getX() + "\n" +
"mainStage.POS_Y: " + mainStage.getY() + "\n" +
"mainStage.SIZE_X: " + mainStage.getWidth() + "\n" +
"mainStages.SIZE_Y: " + mainStage.getHeight() + "\n";
LOGGER.debug(debugLogString);
}
LOGGER.debug("""
screen data:
mainStage.WINDOW_MAXIMISED: {}
mainStage.POS_X: {}
mainStage.POS_Y: {}
mainStage.SIZE_X: {}
mainStage.SIZE_Y: {}
""",
mainStage.isMaximized(), mainStage.getX(), mainStage.getY(), mainStage.getWidth(), mainStage.getHeight());
}

/**
* Tests if the window coordinates are out of the mainscreen
* Tests if the window coordinates are inside any screen
*/
private boolean isWindowPositionOutOfBounds() {
// The upper right corner is checked as there are most probably the window controls.
private boolean isWindowPositionInBounds() {
GuiPreferences guiPreferences = preferencesService.getGuiPreferences();
double rightX = guiPreferences.getPositionX() + guiPreferences.getSizeX();

if (LOGGER.isDebugEnabled()) {
Screen.getScreens().forEach(screen -> LOGGER.debug("Screen bounds: {}", screen.getBounds()));
}

return lowerLeftIsInBounds(guiPreferences) && upperRightIsInBounds(guiPreferences);
}

private boolean lowerLeftIsInBounds(GuiPreferences guiPreferences) {
// Windows/PowerToys somehow removes 10 pixels to the left; they are re-added
double leftX = guiPreferences.getPositionX() + 10.0;
double bottomY = guiPreferences.getPositionY() + guiPreferences.getSizeY();
LOGGER.debug("left x: {}, bottom y: {}", leftX, bottomY);

boolean inBounds = Screen.getScreens().stream().anyMatch((screen -> screen.getBounds().contains(leftX, bottomY)));
LOGGER.debug("lower left corner is in bounds: {}", inBounds);
return inBounds;
}

private boolean upperRightIsInBounds(GuiPreferences guiPreferences) {
// The upper right corner is checked as there are most probably the window controls.
// Windows/PowerToys somehow adds 10 pixels to the right and top of the screen, they are removed
double rightX = guiPreferences.getPositionX() + guiPreferences.getSizeX() - 10.0;
double topY = guiPreferences.getPositionY();
return Screen.getScreens().stream().noneMatch((screen -> screen.getBounds().contains(
rightX, topY)));
LOGGER.debug("right x: {}, top y: {}", rightX, topY);

boolean inBounds = Screen.getScreens().stream().anyMatch((screen -> screen.getBounds().contains(rightX, topY)));
LOGGER.debug("upper right corner is in bounds: {}", inBounds);
return inBounds;
}

// Background tasks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package org.jabref.logic.importer.fileformat;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PushbackInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -55,6 +52,8 @@
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import org.apache.commons.io.ByteOrderMark;
import org.apache.commons.io.input.BOMInputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -431,7 +430,7 @@ private Object unmarshallRoot(BufferedReader reader) throws XMLStreamException,
@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
Objects.requireNonNull(reader);
throw new UnsupportedOperationException("CitaviXmlImporter does not support importDatabase(BufferedReader reader)."
throw new UnsupportedOperationException("CitaviXmlImporter does not support importDatabase(BufferedReader reader). "
+ "Instead use importDatabase(Path filePath, Charset defaultEncoding).");
}

Expand All @@ -447,39 +446,25 @@ public List<BibEntry> parseEntries(InputStream inputStream) {
}

private BufferedReader getReaderFromZip(Path filePath) throws IOException {
ZipInputStream zis = new ZipInputStream(new FileInputStream(filePath.toFile()));
ZipEntry zipEntry = zis.getNextEntry();

Path newFile = Files.createTempFile("citavicontent", ".xml");
newFile.toFile().deleteOnExit();

while (zipEntry != null) {
Files.copy(zis, newFile, StandardCopyOption.REPLACE_EXISTING);

zipEntry = zis.getNextEntry();
}

zis.closeEntry();

InputStream stream = Files.newInputStream(newFile, StandardOpenOption.READ);

// check and delete the utf-8 BOM bytes
InputStream newStream = checkForUtf8BOMAndDiscardIfAny(stream);

// clean up the temp file
Files.delete(newFile);

return new BufferedReader(new InputStreamReader(newStream, StandardCharsets.UTF_8));
}

private static InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {
PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
byte[] bom = new byte[3];
if (pushbackInputStream.read(bom) != -1) {
if (!((bom[0] == (byte) 0xEF) && (bom[1] == (byte) 0xBB) && (bom[2] == (byte) 0xBF))) {
pushbackInputStream.unread(bom);
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(filePath))) {
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
Files.copy(zis, newFile, StandardCopyOption.REPLACE_EXISTING);
zipEntry = zis.getNextEntry();
}
}
return pushbackInputStream;

// Citavi XML files sometimes contains BOM markers. We just discard them.
// Solution inspired by https://stackoverflow.com/a/37445972/873282
return new BufferedReader(
new InputStreamReader(
new BOMInputStream(
Files.newInputStream(newFile, StandardOpenOption.READ),
false,
ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE)));
}

private String clean(String input) {
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/jabref/preferences/GuiPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ public class GuiPreferences {

private final BooleanProperty windowMaximised;

private final DoubleProperty sidePaneWidth;

// the last libraries that were open when jabref closes and should be reopened on startup
private final ObservableList<Path> lastFilesOpened;

private final ObjectProperty<Path> lastFocusedFile;

// observable list last files opened in the file menu
private final FileHistory fileHistory;

private final StringProperty lastSelectedIdBasedFetcher;
private final DoubleProperty sidePaneWidth;

public GuiPreferences(double positionX,
double positionY,
Expand Down
43 changes: 24 additions & 19 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,11 @@ public class JabRefPreferences implements PreferencesService {
public static final String XMP_PRIVACY_FILTERS = "xmpPrivacyFilters";
public static final String USE_XMP_PRIVACY_FILTER = "useXmpPrivacyFilter";
public static final String DEFAULT_SHOW_SOURCE = "defaultShowSource";
// Window sizes
public static final String SIZE_Y = "mainWindowSizeY";
public static final String SIZE_X = "mainWindowSizeX";
public static final String POS_Y = "mainWindowPosY";
public static final String POS_X = "mainWindowPosX";

public static final String MAIN_WINDOW_POS_X = "mainWindowPosX";
public static final String MAIN_WINDOW_POS_Y = "mainWindowPosY";
public static final String MAIN_WINDOW_WIDTH = "mainWindowSizeX";
public static final String MAIN_WINDOW_HEIGHT = "mainWindowSizeY";

public static final String LAST_EDITED = "lastEdited";
public static final String OPEN_LAST_EDITED = "openLastEdited";
Expand Down Expand Up @@ -616,12 +616,16 @@ private JabRefPreferences() {
.getSslDirectory()
.resolve("truststore.jks").toString());

defaults.put(POS_X, 0);
defaults.put(POS_Y, 0);
defaults.put(SIZE_X, 1024);
defaults.put(SIZE_Y, 768);
defaults.put(MAIN_WINDOW_POS_X, 0);
defaults.put(MAIN_WINDOW_POS_Y, 0);
defaults.put(MAIN_WINDOW_WIDTH, 1024);
defaults.put(MAIN_WINDOW_HEIGHT, 768);

defaults.put(WINDOW_MAXIMISED, Boolean.TRUE);
defaults.put(AUTO_RESIZE_MODE, Boolean.FALSE); // By default disable "Fit table horizontally on the screen"

// By default disable "Fit table horizontally on the screen"
defaults.put(AUTO_RESIZE_MODE, Boolean.FALSE);

defaults.put(ENTRY_EDITOR_HEIGHT, 0.65);
defaults.put(ENTRY_EDITOR_PREVIEW_DIVIDER_POS, 0.5);
defaults.put(NAMES_AS_IS, Boolean.FALSE); // "Show names unchanged"
Expand Down Expand Up @@ -2620,10 +2624,10 @@ public GuiPreferences getGuiPreferences() {
}

guiPreferences = new GuiPreferences(
getDouble(POS_X),
getDouble(POS_Y),
getDouble(SIZE_X),
getDouble(SIZE_Y),
getDouble(MAIN_WINDOW_POS_X),
getDouble(MAIN_WINDOW_POS_Y),
getDouble(MAIN_WINDOW_WIDTH),
getDouble(MAIN_WINDOW_HEIGHT),
getBoolean(WINDOW_MAXIMISED),
getStringList(LAST_EDITED).stream()
.map(Path::of)
Expand All @@ -2633,11 +2637,13 @@ public GuiPreferences getGuiPreferences() {
get(ID_ENTRY_GENERATOR),
getDouble(SIDE_PANE_WIDTH));

EasyBind.listen(guiPreferences.positionXProperty(), (obs, oldValue, newValue) -> putDouble(POS_X, newValue.doubleValue()));
EasyBind.listen(guiPreferences.positionYProperty(), (obs, oldValue, newValue) -> putDouble(POS_Y, newValue.doubleValue()));
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.positionXProperty(), (obs, oldValue, newValue) -> putDouble(MAIN_WINDOW_POS_X, newValue.doubleValue()));
EasyBind.listen(guiPreferences.positionYProperty(), (obs, oldValue, newValue) -> putDouble(MAIN_WINDOW_POS_Y, newValue.doubleValue()));
EasyBind.listen(guiPreferences.sizeXProperty(), (obs, oldValue, newValue) -> putDouble(MAIN_WINDOW_WIDTH, newValue.doubleValue()));
EasyBind.listen(guiPreferences.sizeYProperty(), (obs, oldValue, newValue) -> putDouble(MAIN_WINDOW_HEIGHT, newValue.doubleValue()));
EasyBind.listen(guiPreferences.windowMaximisedProperty(), (obs, oldValue, newValue) -> putBoolean(WINDOW_MAXIMISED, newValue));
EasyBind.listen(guiPreferences.sidePaneWidthProperty(), (obs, oldValue, newValue) -> putDouble(SIDE_PANE_WIDTH, newValue.doubleValue()));

guiPreferences.getLastFilesOpened().addListener((ListChangeListener<Path>) change -> {
if (change.getList().isEmpty()) {
prefs.remove(LAST_EDITED);
Expand All @@ -2657,7 +2663,6 @@ public GuiPreferences getGuiPreferences() {
});
guiPreferences.getFileHistory().addListener((InvalidationListener) change -> storeFileHistory(guiPreferences.getFileHistory()));
EasyBind.listen(guiPreferences.lastSelectedIdBasedFetcherProperty(), (obs, oldValue, newValue) -> put(ID_ENTRY_GENERATOR, newValue));
EasyBind.listen(guiPreferences.sidePaneWidthProperty(), (obs, oldValue, newValue) -> putDouble(SIDE_PANE_WIDTH, newValue.doubleValue()));

return guiPreferences;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/tinylog.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ exception = strip: jdk.internal
#[email protected] = debug

[email protected] = debug

#[email protected] = debug
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public static void testImportEntries(Importer importer, String fileName, String
if (parserResult.isInvalid()) {
throw new ImportException(parserResult.getErrorMessage());
}
List<BibEntry> entries = parserResult.getDatabase()
.getEntries();
List<BibEntry> entries = parserResult.getDatabase().getEntries();
BibEntryAssert.assertEquals(ImporterTestEngine.class, fileName.replaceAll(fileType, ".bib"), entries);
}

Expand Down

0 comments on commit 9f6c846

Please sign in to comment.