From a6231ac593619ef3b10cec34460b7c5fff88dcdb Mon Sep 17 00:00:00 2001 From: Glavo Date: Fri, 2 Dec 2022 17:27:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F=20(#1888)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix memory leaks * Cache icon in cell * Lazy acquisition of availableCharsets --- .../org/jackhuang/hmcl/ui/Controllers.java | 15 ++++++++++---- .../hmcl/ui/download/VersionsPage.java | 20 +++++++++---------- .../hmcl/util/io/CompressingUtils.java | 9 +++++---- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java index e8d5b4137..6b13766ff 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/Controllers.java @@ -141,10 +141,16 @@ public final class Controllers { } public static void onApplicationStop() { - config().setHeight(stageHeight.get()); - config().setWidth(stageWidth.get()); - stageHeight = null; - stageWidth = null; + if (stageHeight != null) { + config().setHeight(stageHeight.get()); + stageHeight.unbind(); + stageHeight = null; + } + if (stageWidth != null) { + config().setWidth(stageWidth.get()); + stageWidth.unbind(); + stageWidth = null; + } } public static void initialize(Stage stage) { @@ -321,5 +327,6 @@ public final class Controllers { decorator = null; stage = null; scene = null; + onApplicationStop(); } } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/VersionsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/VersionsPage.java index 055dd59a6..35deea913 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/VersionsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/download/VersionsPage.java @@ -127,7 +127,8 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres btnRefresh.setGraphic(wrap(SVG.refresh(Theme.blackFillBinding(), -1, -1))); MutableObject lastCell = new MutableObject<>(); - list.setCellFactory(listView -> new RemoteVersionListCell(lastCell)); + EnumMap icons = new EnumMap<>(VersionIconType.class); + list.setCellFactory(listView -> new RemoteVersionListCell(lastCell, icons)); list.setOnMouseClicked(e -> { if (list.getSelectionModel().getSelectedIndex() < 0) @@ -218,29 +219,26 @@ public final class VersionsPage extends BorderPane implements WizardPage, Refres } private static class RemoteVersionListCell extends ListCell { - private static final EnumMap icon = new EnumMap<>(VersionIconType.class); - - private static Image getIcon(VersionIconType type) { - assert Platform.isFxApplicationThread(); - return icon.computeIfAbsent(type, iconType -> new Image(iconType.getResourceUrl(), 32, 32, false, true)); - } - final IconedTwoLineListItem content = new IconedTwoLineListItem(); final RipplerContainer ripplerContainer = new RipplerContainer(content); final StackPane pane = new StackPane(); private final MutableObject lastCell; + private final EnumMap icons; - RemoteVersionListCell(MutableObject lastCell) { + RemoteVersionListCell(MutableObject lastCell, EnumMap icons) { this.lastCell = lastCell; - } + this.icons = icons; - { pane.getStyleClass().add("md-list-cell"); StackPane.setMargin(content, new Insets(10, 16, 10, 16)); pane.getChildren().setAll(ripplerContainer); } + private Image getIcon(VersionIconType type) { + return icons.computeIfAbsent(type, iconType -> new Image(iconType.getResourceUrl(), 32, 32, false, true)); + } + @Override public void updateItem(RemoteVersion remoteVersion, boolean empty) { super.updateItem(remoteVersion, empty); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java index b14c909ec..b9de8c4c4 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/CompressingUtils.java @@ -88,7 +88,7 @@ public final class CompressingUtils { } public static Charset findSuitableEncoding(Path zipFile) throws IOException { - return findSuitableEncoding(zipFile, Charset.availableCharsets().values()); + return findSuitableEncoding(zipFile, null); } public static Charset findSuitableEncoding(Path zipFile, Collection candidates) throws IOException { @@ -98,7 +98,7 @@ public final class CompressingUtils { } public static Charset findSuitableEncoding(ZipFile zipFile) throws IOException { - return findSuitableEncoding(zipFile, Charset.availableCharsets().values()); + return findSuitableEncoding(zipFile, null); } public static Charset findSuitableEncoding(ZipFile zipFile, Collection candidates) throws IOException { @@ -106,6 +106,9 @@ public final class CompressingUtils { if (OperatingSystem.NATIVE_CHARSET != StandardCharsets.UTF_8 && testEncoding(zipFile, OperatingSystem.NATIVE_CHARSET)) return OperatingSystem.NATIVE_CHARSET; + if (candidates == null) + candidates = Charset.availableCharsets().values(); + for (Charset charset : candidates) if (charset != null && testEncoding(zipFile, charset)) return charset; @@ -156,8 +159,6 @@ public final class CompressingUtils { public FileSystem build() throws IOException { if (autoDetectEncoding) { if (!testEncoding(zip, encoding)) { - if (charsetCandidates == null) - charsetCandidates = Charset.availableCharsets().values(); encoding = findSuitableEncoding(zip, charsetCandidates); } }