fix #1562: Do not save stage size when window is minimized (#1979)

This commit is contained in:
Glavo 2023-01-07 21:38:36 +08:00 committed by GitHub
parent 15a93a9dc0
commit 5dc1d14f73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,7 +19,10 @@ package org.jackhuang.hmcl.ui;
import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXDialogLayout; import com.jfoenix.controls.JFXDialogLayout;
import javafx.beans.InvalidationListener;
import javafx.beans.WeakInvalidationListener;
import javafx.beans.property.DoubleProperty; import javafx.beans.property.DoubleProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Node; import javafx.scene.Node;
import javafx.scene.Scene; import javafx.scene.Scene;
@ -61,12 +64,12 @@ import java.io.File;
import java.util.List; import java.util.List;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.*;
import static org.jackhuang.hmcl.setting.ConfigHolder.globalConfig;
import static org.jackhuang.hmcl.ui.FXUtils.newImage; import static org.jackhuang.hmcl.ui.FXUtils.newImage;
import static org.jackhuang.hmcl.util.i18n.I18n.i18n; import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
public final class Controllers { public final class Controllers {
private static InvalidationListener stageSizeChangeListener;
private static DoubleProperty stageWidth = new SimpleDoubleProperty(); private static DoubleProperty stageWidth = new SimpleDoubleProperty();
private static DoubleProperty stageHeight = new SimpleDoubleProperty(); private static DoubleProperty stageHeight = new SimpleDoubleProperty();
@ -142,14 +145,13 @@ public final class Controllers {
} }
public static void onApplicationStop() { public static void onApplicationStop() {
stageSizeChangeListener = null;
if (stageHeight != null) { if (stageHeight != null) {
config().setHeight(stageHeight.get()); config().setHeight(stageHeight.get());
stageHeight.unbind();
stageHeight = null; stageHeight = null;
} }
if (stageWidth != null) { if (stageWidth != null) {
config().setWidth(stageWidth.get()); config().setWidth(stageWidth.get());
stageWidth.unbind();
stageWidth = null; stageWidth = null;
} }
} }
@ -159,10 +161,28 @@ public final class Controllers {
Controllers.stage = stage; Controllers.stage = stage;
stage.setHeight(config().getHeight()); stageSizeChangeListener = o -> {
stageHeight.bind(stage.heightProperty()); ReadOnlyDoubleProperty sourceProperty = (ReadOnlyDoubleProperty) o;
stage.setWidth(config().getWidth()); DoubleProperty targetProperty = "width".equals(sourceProperty.getName()) ? stageWidth : stageHeight;
stageWidth.bind(stage.widthProperty());
if (targetProperty != null
&& Controllers.stage != null
&& !Controllers.stage.isIconified()) {
targetProperty.set(sourceProperty.get());
}
};
WeakInvalidationListener weakListener = new WeakInvalidationListener(stageSizeChangeListener);
double initHeight = config().getHeight();
double initWidth = config().getWidth();
stage.setHeight(initHeight);
stage.setWidth(initWidth);
stageHeight.set(initHeight);
stageWidth.set(initWidth);
stage.heightProperty().addListener(weakListener);
stage.widthProperty().addListener(weakListener);
stage.setOnCloseRequest(e -> Launcher.stopApplication()); stage.setOnCloseRequest(e -> Launcher.stopApplication());