From 981902409c71a9e9eb04a6190ae52a95433e5ae5 Mon Sep 17 00:00:00 2001 From: Martin Beyer Date: Thu, 26 Aug 2021 10:33:31 +0200 Subject: [PATCH] calculate new position on display configuration change --- .../cryptomator/common/settings/Settings.java | 14 ++++-- .../common/settings/SettingsJsonAdapter.java | 4 +- .../ui/mainwindow/ResizeController.java | 47 ++++++++++++------- 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/cryptomator/common/settings/Settings.java b/src/main/java/org/cryptomator/common/settings/Settings.java index 5d2cd7c22..ac15dbe0e 100644 --- a/src/main/java/org/cryptomator/common/settings/Settings.java +++ b/src/main/java/org/cryptomator/common/settings/Settings.java @@ -8,9 +8,6 @@ ******************************************************************************/ package org.cryptomator.common.settings; -import org.apache.commons.lang3.SystemUtils; -import org.cryptomator.common.Environment; - import javafx.beans.Observable; import javafx.beans.property.BooleanProperty; import javafx.beans.property.IntegerProperty; @@ -23,6 +20,9 @@ import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.NodeOrientation; +import org.apache.commons.lang3.SystemUtils; +import org.cryptomator.common.Environment; + import java.util.function.Consumer; public class Settings { @@ -43,6 +43,8 @@ public class Settings { public static final NodeOrientation DEFAULT_USER_INTERFACE_ORIENTATION = NodeOrientation.LEFT_TO_RIGHT; public static final String DEFAULT_LICENSE_KEY = ""; public static final boolean DEFAULT_SHOW_MINIMIZE_BUTTON = false; + public static final String DEFAULT_DISPLAY_CONFIGURATION = ""; + private final ObservableList directories = FXCollections.observableArrayList(VaultSettings::observables); private final BooleanProperty askedForUpdateCheck = new SimpleBooleanProperty(DEFAULT_ASKED_FOR_UPDATE_CHECK); @@ -63,6 +65,7 @@ public class Settings { private final IntegerProperty windowYPosition = new SimpleIntegerProperty(); private final IntegerProperty windowWidth = new SimpleIntegerProperty(); private final IntegerProperty windowHeight = new SimpleIntegerProperty(); + private final ObjectProperty displayConfiguration = new SimpleObjectProperty<>(DEFAULT_DISPLAY_CONFIGURATION); private Consumer saveCmd; @@ -92,6 +95,7 @@ public class Settings { windowYPosition.addListener(this::somethingChanged); windowWidth.addListener(this::somethingChanged); windowHeight.addListener(this::somethingChanged); + displayConfiguration.addListener(this::somethingChanged); } void setSaveCmd(Consumer saveCmd) { @@ -183,4 +187,8 @@ public class Settings { public IntegerProperty windowHeightProperty() { return windowHeight; } + + public ObjectProperty displayConfigurationProperty() { + return displayConfiguration; + } } diff --git a/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java b/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java index f390f0b84..ee040071a 100644 --- a/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java +++ b/src/main/java/org/cryptomator/common/settings/SettingsJsonAdapter.java @@ -9,13 +9,13 @@ import com.google.gson.TypeAdapter; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; import com.google.gson.stream.JsonWriter; +import javafx.geometry.NodeOrientation; import org.cryptomator.common.Environment; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; import javax.inject.Singleton; -import javafx.geometry.NodeOrientation; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -56,6 +56,7 @@ public class SettingsJsonAdapter extends TypeAdapter { out.name("windowYPosition").value((value.windowYPositionProperty().get())); out.name("windowWidth").value((value.windowWidthProperty().get())); out.name("windowHeight").value((value.windowHeightProperty().get())); + out.name("displayConfiguration").value((value.displayConfigurationProperty().get())); out.endObject(); } @@ -95,6 +96,7 @@ public class SettingsJsonAdapter extends TypeAdapter { case "windowYPosition" -> settings.windowYPositionProperty().set(in.nextInt()); case "windowWidth" -> settings.windowWidthProperty().set(in.nextInt()); case "windowHeight"-> settings.windowHeightProperty().set(in.nextInt()); + case "displayConfiguration" -> settings.displayConfigurationProperty().set(in.nextString()); default -> { LOG.warn("Unsupported vault setting found in JSON: " + name); diff --git a/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java b/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java index 20c072895..cd943cb19 100644 --- a/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java +++ b/src/main/java/org/cryptomator/ui/mainwindow/ResizeController.java @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.inject.Inject; +import java.awt.*; @MainWindow public class ResizeController implements FxController { @@ -63,24 +64,36 @@ public class ResizeController implements FxController { lResizer.setOnMouseDragged(this::resizeLeft); window.setHeight(settings.windowHeightProperty().get()); - //TODO: remove comments - //window.setHeight(settings.windowHeightProperty().get() > window.getMaxHeight() ? window.getMaxHeight() * 0.95 : settings.windowHeightProperty().get()); - window.setWidth(settings.windowWidthProperty().get()); - //window.setWidth(settings.windowWidthProperty().get() > window.getMaxWidth() ? window.getMaxWidth() * 0.95 : settings.windowWidthProperty().get()); - - //TODO: define illegalPosition - boolean illegalPosition = false; - if (illegalPosition) { - // if the position is illegal, then the window appears on the main screen in the middle of the window. - window.setY((window.getMaxHeight() - window.getHeight()) / 2); - window.setX((window.getMaxWidth() - window.getWidth()) / 2); + if (checkForChangedDisplayConfiguration()) { + //If the position is illegal, then the window appears on the main screen in the middle of the window. + Dimension size = Toolkit.getDefaultToolkit().getScreenSize(); + window.setX((size.getWidth() - window.getWidth()) / 2); + window.setY((size.getHeight() - window.getHeight()) / 2); } else { window.setX(settings.windowXPositionProperty().get()); window.setY(settings.windowYPositionProperty().get()); } + savePositionalSettings(); + } + private boolean checkForChangedDisplayConfiguration(){ + String currentDisplayConfiguration = getMonitorSizes(); + boolean changedConfiguration = !settings.displayConfigurationProperty().get().equals(currentDisplayConfiguration); + if (changedConfiguration) settings.displayConfigurationProperty().setValue(currentDisplayConfiguration); + return changedConfiguration; + } + + private String getMonitorSizes() { + GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); + GraphicsDevice[] gs = ge.getScreenDevices(); + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < gs.length; i++) { + DisplayMode dm = gs[i].getDisplayMode(); + sb.append("screenId: " + i + ", " + dm.getWidth() + "x" + dm.getHeight() + "; "); + } + return sb.toString(); } private void startResize(MouseEvent evt) { @@ -93,25 +106,25 @@ public class ResizeController implements FxController { private void resizeTopLeft(MouseEvent evt) { resizeTop(evt); resizeLeft(evt); - saveSettings(); + savePositionalSettings(); } private void resizeTopRight(MouseEvent evt) { resizeTop(evt); resizeRight(evt); - saveSettings(); + savePositionalSettings(); } private void resizeBottomLeft(MouseEvent evt) { resizeBottom(evt); resizeLeft(evt); - saveSettings(); + savePositionalSettings(); } private void resizeBottomRight(MouseEvent evt) { resizeBottom(evt); resizeRight(evt); - saveSettings(); + savePositionalSettings(); } private void resizeTop(MouseEvent evt) { @@ -148,7 +161,7 @@ public class ResizeController implements FxController { } } - private void saveSettings() { + private void savePositionalSettings() { settings.windowHeightProperty().setValue(window.getHeight()); settings.windowWidthProperty().setValue(window.getWidth()); settings.windowYPositionProperty().setValue(window.getY()); @@ -160,7 +173,7 @@ public class ResizeController implements FxController { } public boolean isShowResizingArrows() { - //If in fullscreen resizing should not be possible; + //If in fullscreen resizing is not be possible; return !window.isFullScreen(); }