diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/MemoryStatusBar.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MemoryStatusBar.java new file mode 100644 index 0000000000..00e6a6118f --- /dev/null +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MemoryStatusBar.java @@ -0,0 +1,228 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2026 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.ui; + +import javafx.application.Platform; +import javafx.beans.property.*; +import javafx.geometry.Insets; +import javafx.scene.Node; +import javafx.scene.control.Control; +import javafx.scene.control.SkinBase; +import javafx.scene.layout.StackPane; +import javafx.scene.shape.Rectangle; +import org.jackhuang.hmcl.util.FXThread; +import org.jackhuang.hmcl.util.MathUtils; +import org.jackhuang.hmcl.util.platform.SystemInfo; +import org.jackhuang.hmcl.util.platform.hardware.PhysicalMemoryStatus; + +import java.lang.ref.WeakReference; + +/// @author Glavo +public final class MemoryStatusBar extends Control { + + private static final String DEFAULT_STYLE_CLASS = "memory-status-bar"; + + public MemoryStatusBar() { + this.getStyleClass().add(DEFAULT_STYLE_CLASS); + + setPrefWidth(200); + } + + private final ReadOnlyObjectProperty memoryStatus = UpdateMemoryStatus.memoryStatusProperty(); + + /// The memory status of the system. + /// + /// The property will be automatically updated. + /// + /// The property has no bean, because it may share between multiple MemoryStatusBar instances. + public ReadOnlyObjectProperty memoryStatusProperty() { + return memoryStatus; + } + + public PhysicalMemoryStatus getMemoryStatus() { + return memoryStatus.get(); + } + + private final DoubleProperty memoryAllocated = new SimpleDoubleProperty(); + + public DoubleProperty memoryAllocatedProperty() { + return memoryAllocated; + } + + public double getMemoryAllocated() { + return memoryAllocated.get(); + } + + public void setMemoryAllocated(double memoryAllocated) { + this.memoryAllocated.set(memoryAllocated); + } + + @Override + protected SkinBase createDefaultSkin() { + return new Skin(this); + } + + private static final class Skin extends SkinBase { + private static final int HEIGHT = 4; + + private final StackPane track; + private final Rectangle memoryUsed; + private final Rectangle memoryAllocate; + + private double contentWidth = -1; + + Skin(MemoryStatusBar control) { + super(control); + + track = new StackPane(); + track.getStyleClass().add("track"); + track.setPadding(new Insets(HEIGHT, 0, 0, 0)); + + memoryUsed = new Rectangle(); + memoryUsed.setManaged(false); + memoryUsed.setArcWidth(HEIGHT); + memoryUsed.setArcHeight(HEIGHT); + memoryUsed.getStyleClass().add("memory-used"); + + memoryAllocate = new Rectangle(); + memoryAllocate.setManaged(false); + memoryAllocate.setArcWidth(HEIGHT); + memoryAllocate.setArcHeight(HEIGHT); + memoryAllocate.getStyleClass().add("memory-allocate"); + + this.getChildren().setAll(track, memoryAllocate, memoryUsed); + + registerInvalidationListener(control.memoryStatusProperty(), it -> updateBar()); + registerInvalidationListener(control.memoryAllocatedProperty(), it -> updateBar()); + } + + private void updateBar() { + if (contentWidth <= 0) { + // not yet layout + return; + } + + PhysicalMemoryStatus status = getSkinnable().getMemoryStatus(); + double total = status.getTotal(); + double used = status.getUsed(); + double allocated = getSkinnable().getMemoryAllocated(); + + if (total <= 0) { + memoryUsed.setWidth(contentWidth); + memoryAllocate.setWidth(contentWidth); + return; + } + + double usedWidth = MathUtils.clamp(contentWidth * (used / total), 0, contentWidth); + double allocatedWidth = MathUtils.clamp(contentWidth * (allocated / total) + usedWidth, 0, contentWidth); + + memoryUsed.setWidth(usedWidth); + memoryAllocate.setWidth(allocatedWidth); + } + + @Override + protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) { + track.resizeRelocate(contentX, contentY, contentWidth, contentHeight); + memoryUsed.relocate(contentX, contentY); + memoryAllocate.relocate(contentX, contentY); + + this.contentWidth = contentWidth; + + memoryUsed.setHeight(contentHeight); + memoryAllocate.setHeight(contentHeight); + + updateBar(); + } + + @Override + public double computeBaselineOffset(double topInset, double rightInset, double bottomInset, double leftInset) { + return Node.BASELINE_OFFSET_SAME_AS_HEIGHT; + } + + @Override + protected double computePrefWidth(double height, double topInset, double rightInset, double bottomInset, double leftInset) { + return leftInset + track.prefWidth(height) + rightInset; + } + + @Override + protected double computePrefHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) { + return topInset + HEIGHT + bottomInset; + } + + @Override + protected double computeMaxHeight(double width, double topInset, double rightInset, double bottomInset, double leftInset) { + return computePrefHeight(width, topInset, rightInset, bottomInset, leftInset); + } + } + + private static final class UpdateMemoryStatus extends Thread { + + private static final int UPDATE_INTERVAL = 3000; + + @FXThread + private static WeakReference> memoryStatusPropertyCache; + + @FXThread + static ReadOnlyObjectProperty memoryStatusProperty() { + if (memoryStatusPropertyCache != null) { + var property = memoryStatusPropertyCache.get(); + if (property != null) { + return property; + } + } + + ObjectProperty property = new SimpleObjectProperty<>(PhysicalMemoryStatus.INVALID); + memoryStatusPropertyCache = new WeakReference<>(property); + new UpdateMemoryStatus(memoryStatusPropertyCache).start(); + return property; + } + + private final WeakReference> memoryStatusPropertyRef; + + UpdateMemoryStatus(WeakReference> memoryStatusPropertyRef) { + this.memoryStatusPropertyRef = memoryStatusPropertyRef; + + setName("UpdateMemoryStatus"); + setDaemon(true); + setPriority(Thread.MIN_PRIORITY); + } + + @Override + public void run() { + while (true) { + PhysicalMemoryStatus status = SystemInfo.getPhysicalMemoryStatus(); + + var memoryStatusProperty = memoryStatusPropertyRef.get(); + if (memoryStatusProperty == null) + return; + + if (Controllers.isStopped()) + return; + + Platform.runLater(() -> memoryStatusProperty.set(status)); + + try { + //noinspection BusyWait + Thread.sleep(UPDATE_INTERVAL); + } catch (InterruptedException e) { + return; + } + } + } + } +} diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java index ad6ab14187..0d5e4757ae 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/VersionSettingsPage.java @@ -18,7 +18,6 @@ package org.jackhuang.hmcl.ui.versions; import com.jfoenix.controls.*; -import javafx.application.Platform; import javafx.beans.InvalidationListener; import javafx.beans.binding.Bindings; import javafx.beans.property.*; @@ -43,6 +42,7 @@ import org.jackhuang.hmcl.java.JavaRuntime; import org.jackhuang.hmcl.setting.*; import org.jackhuang.hmcl.ui.Controllers; import org.jackhuang.hmcl.ui.FXUtils; +import org.jackhuang.hmcl.ui.MemoryStatusBar; import org.jackhuang.hmcl.ui.WeakListenerHolder; import org.jackhuang.hmcl.ui.construct.*; import org.jackhuang.hmcl.ui.decorator.DecoratorPage; @@ -57,7 +57,6 @@ import org.jackhuang.hmcl.util.platform.SystemInfo; import org.jackhuang.hmcl.util.platform.hardware.PhysicalMemoryStatus; import org.jackhuang.hmcl.util.versioning.GameVersionNumber; -import java.lang.ref.WeakReference; import java.util.*; import java.util.concurrent.atomic.AtomicBoolean; @@ -115,8 +114,6 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag private final IntegerProperty maxMemory = new SimpleIntegerProperty(); private final BooleanProperty modpack = new SimpleBooleanProperty(); - private final ReadOnlyObjectProperty memoryStatus = UpdateMemoryStatus.memoryStatusProperty(); - public VersionSettingsPage(boolean globalSetting) { ScrollPane scrollPane = new ScrollPane(); scrollPane.setFitToHeight(true); @@ -296,30 +293,14 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag lowerBoundPane.getChildren().setAll(label, slider, txtMaxMemory, new Label(i18n("settings.memory.unit.mib"))); } - StackPane progressBarPane = new StackPane(); - progressBarPane.setAlignment(Pos.CENTER_LEFT); - VBox.setMargin(progressBarPane, new Insets(8, 0, 0, 16)); - { - progressBarPane.setMinHeight(4); - progressBarPane.getStyleClass().add("memory-total"); - - StackPane usedMemory = new StackPane(); - usedMemory.getStyleClass().add("memory-used"); - usedMemory.maxWidthProperty().bind(Bindings.createDoubleBinding(() -> - progressBarPane.getWidth() * - (memoryStatus.get().getUsed() * 1.0 / memoryStatus.get().getTotal()), progressBarPane.widthProperty(), - memoryStatus)); - StackPane allocateMemory = new StackPane(); - allocateMemory.getStyleClass().add("memory-allocate"); - allocateMemory.maxWidthProperty().bind(Bindings.createDoubleBinding(() -> - progressBarPane.getWidth() * - Math.min(1.0, - (double) (HMCLGameRepository.getAllocatedMemory(maxMemory.get() * 1024L * 1024L, memoryStatus.get().getAvailable(), chkAutoAllocate.isSelected()) - + memoryStatus.get().getUsed()) / memoryStatus.get().getTotal()), progressBarPane.widthProperty(), - maxMemory, memoryStatus, chkAutoAllocate.selectedProperty())); - - progressBarPane.getChildren().setAll(allocateMemory, usedMemory); - } + MemoryStatusBar memoryStatusBar = new MemoryStatusBar(); + VBox.setMargin(memoryStatusBar, new Insets(8, 0, 0, 16)); + memoryStatusBar.memoryAllocatedProperty().bind(Bindings.createDoubleBinding(() -> + (double) HMCLGameRepository.getAllocatedMemory( + maxMemory.get() * 1024L * 1024L, + memoryStatusBar.getMemoryStatus().getAvailable(), + chkAutoAllocate.isSelected()), + memoryStatusBar.memoryStatusProperty(), maxMemory, chkAutoAllocate.selectedProperty())); BorderPane digitalPane = new BorderPane(); VBox.setMargin(digitalPane, new Insets(0, 0, 0, 16)); @@ -327,26 +308,29 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag Label lblPhysicalMemory = new Label(); lblPhysicalMemory.getStyleClass().add("memory-label"); digitalPane.setLeft(lblPhysicalMemory); - lblPhysicalMemory.textProperty().bind(Bindings.createStringBinding(() -> - i18n("settings.memory.used_per_total", - GIGABYTES.convertFromBytes(memoryStatus.get().getUsed()), - GIGABYTES.convertFromBytes(memoryStatus.get().getTotal())), memoryStatus)); + lblPhysicalMemory.textProperty().bind(Bindings.createStringBinding(() -> { + PhysicalMemoryStatus memoryStatus = memoryStatusBar.getMemoryStatus(); + return i18n("settings.memory.used_per_total", + GIGABYTES.convertFromBytes(memoryStatus.getUsed()), + GIGABYTES.convertFromBytes(memoryStatus.getTotal())); + }, memoryStatusBar.memoryStatusProperty())); Label lblAllocateMemory = new Label(); lblAllocateMemory.textProperty().bind(Bindings.createStringBinding(() -> { + PhysicalMemoryStatus memoryStatus = memoryStatusBar.getMemoryStatus(); long maxMemory = Lang.parseInt(this.maxMemory.get(), 0) * 1024L * 1024L; - return i18n(memoryStatus.get().hasAvailable() && maxMemory > memoryStatus.get().getAvailable() + return i18n(memoryStatus.hasAvailable() && maxMemory > memoryStatus.getAvailable() ? (chkAutoAllocate.isSelected() ? "settings.memory.allocate.auto.exceeded" : "settings.memory.allocate.manual.exceeded") : (chkAutoAllocate.isSelected() ? "settings.memory.allocate.auto" : "settings.memory.allocate.manual"), GIGABYTES.convertFromBytes(maxMemory), - GIGABYTES.convertFromBytes(HMCLGameRepository.getAllocatedMemory(maxMemory, memoryStatus.get().getAvailable(), chkAutoAllocate.isSelected())), - GIGABYTES.convertFromBytes(memoryStatus.get().getAvailable())); - }, memoryStatus, maxMemory, chkAutoAllocate.selectedProperty())); + GIGABYTES.convertFromBytes(HMCLGameRepository.getAllocatedMemory(maxMemory, memoryStatus.getAvailable(), chkAutoAllocate.isSelected())), + GIGABYTES.convertFromBytes(memoryStatus.getAvailable())); + }, memoryStatusBar.memoryStatusProperty(), maxMemory, chkAutoAllocate.selectedProperty())); lblAllocateMemory.getStyleClass().add("memory-label"); digitalPane.setRight(lblAllocateMemory); } - maxMemoryPane.getChildren().setAll(title, chkAutoAllocate, lowerBoundPane, progressBarPane, digitalPane); + maxMemoryPane.getChildren().setAll(title, chkAutoAllocate, lowerBoundPane, memoryStatusBar, digitalPane); } launcherVisibilityPane = new LineSelectButton<>(); @@ -472,7 +456,7 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag componentList.disableProperty().bind(enableSpecificSettings.not()); } - + private void fireWorkingDirChanged() { FXUtils.runInFX(() -> fireEvent(new VersionPage.WorkingDirChangedEvent())); } @@ -776,57 +760,4 @@ public final class VersionSettingsPage extends StackPane implements DecoratorPag return state.getReadOnlyProperty(); } - private static final class UpdateMemoryStatus extends Thread { - - @FXThread - private static WeakReference> memoryStatusPropertyCache; - - @FXThread - static ReadOnlyObjectProperty memoryStatusProperty() { - if (memoryStatusPropertyCache != null) { - var property = memoryStatusPropertyCache.get(); - if (property != null) { - return property; - } - } - - ObjectProperty property = new SimpleObjectProperty<>(PhysicalMemoryStatus.INVALID); - memoryStatusPropertyCache = new WeakReference<>(property); - new UpdateMemoryStatus(memoryStatusPropertyCache).start(); - return property; - } - - private final WeakReference> memoryStatusPropertyRef; - - UpdateMemoryStatus(WeakReference> memoryStatusPropertyRef) { - this.memoryStatusPropertyRef = memoryStatusPropertyRef; - - setName("UpdateMemoryStatus"); - setDaemon(true); - setPriority(Thread.MIN_PRIORITY); - } - - @Override - public void run() { - while (true) { - PhysicalMemoryStatus status = SystemInfo.getPhysicalMemoryStatus(); - - var memoryStatusProperty = memoryStatusPropertyRef.get(); - if (memoryStatusProperty == null) - return; - - if (Controllers.isStopped()) - return; - - Platform.runLater(() -> memoryStatusProperty.set(status)); - - try { - //noinspection BusyWait - Thread.sleep(1000); - } catch (InterruptedException e) { - return; - } - } - } - } } diff --git a/HMCL/src/main/resources/assets/css/root.css b/HMCL/src/main/resources/assets/css/root.css index 5b035f97f6..19630eacbc 100644 --- a/HMCL/src/main/resources/assets/css/root.css +++ b/HMCL/src/main/resources/assets/css/root.css @@ -113,28 +113,26 @@ .memory-label { } -.memory-used { - -fx-background-color: -monet-primary; +.memory-status-bar { + -fx-opacity: 1; } -.memory-used:disabled { +.memory-status-bar:disabled { -fx-opacity: 0.4; } -.memory-allocate { - -fx-background-color: -monet-primary-transparent-50; +.memory-status-bar .track { + -fx-background-color: -monet-secondary-container; + -fx-background-radius: 2; + -fx-background-insets: 0; } -.memory-allocate:disabled { - -fx-opacity: 0.4; +.memory-status-bar .memory-used { + -fx-fill: -monet-primary; } -.memory-total { - -fx-background-color: -monet-surface-container; -} - -.memory-total:disabled { - -fx-opacity: 0.4; +.memory-status-bar .memory-allocate { + -fx-fill: -monet-primary-transparent-50; } .update-label {