diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java index 887e75a35..fa690b14e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/FXUtils.java @@ -51,6 +51,7 @@ import java.lang.reflect.Method; import java.net.URI; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BooleanSupplier; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -92,6 +93,11 @@ public final class FXUtils { consumer.accept(value.getValue()); } + public static void runLaterIf(BooleanSupplier condition, Runnable runnable) { + if (condition.getAsBoolean()) Platform.runLater(() -> runLaterIf(condition, runnable)); + else runnable.run(); + } + public static void limitSize(ImageView imageView, double maxWidth, double maxHeight) { imageView.setPreserveRatio(true); onChangeAndOperate(imageView.imageProperty(), image -> { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java index 8bffc8fe3..b67007ee2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/LeftPaneController.java @@ -29,6 +29,7 @@ import org.jackhuang.hmcl.game.ModpackHelper; import org.jackhuang.hmcl.mod.Modpack; import org.jackhuang.hmcl.mod.UnsupportedModpackException; import org.jackhuang.hmcl.setting.Accounts; +import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profiles; import org.jackhuang.hmcl.setting.Theme; import org.jackhuang.hmcl.task.Schedulers; @@ -86,7 +87,9 @@ public final class LeftPaneController extends AdvancedListBox { .add(launcherSettingsItem); EventBus.EVENT_BUS.channel(RefreshedVersionsEvent.class).register(event -> onRefreshedVersions((HMCLGameRepository) event.getSource())); - if (Profiles.selectedProfileProperty().get().getRepository().isLoaded()) + + Profile profile = Profiles.getSelectedProfile(); + if (profile != null && profile.getRepository().isLoaded()) onRefreshedVersions(Profiles.selectedProfileProperty().get().getRepository()); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java index be748ec98..b8658424b 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/MainPage.java @@ -87,7 +87,7 @@ public final class MainPage extends StackPane implements DecoratorPage { graphic.setTranslateX(12); btnMenu.setGraphic(graphic); - Profiles.selectedVersionProperty().addListener((o, a, version) -> { + FXUtils.onChangeAndOperate(Profiles.selectedVersionProperty(), version -> { if (version != null) { lblCurrentGame.setText(version); } else { diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java index a6afc4f86..2431781a6 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/decorator/DecoratorController.java @@ -94,6 +94,7 @@ public class DecoratorController { welcomeView = new ImageView(); welcomeView.setImage(new Image("/assets/img/welcome.png")); welcomeView.setCursor(Cursor.HAND); + FXUtils.limitSize(welcomeView, 796, 517); welcomeView.setOnMouseClicked(e -> { Timeline nowAnimation = new Timeline(); nowAnimation.getKeyFrames().addAll( diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java index 61793b23f..ab652618d 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/versions/GameAdvancedListItem.java @@ -24,30 +24,34 @@ import org.jackhuang.hmcl.event.EventBus; import org.jackhuang.hmcl.event.RefreshedVersionsEvent; import org.jackhuang.hmcl.setting.Profile; import org.jackhuang.hmcl.setting.Profiles; +import org.jackhuang.hmcl.ui.FXUtils; import org.jackhuang.hmcl.ui.WeakListenerHolder; import org.jackhuang.hmcl.ui.construct.AdvancedListItem; import java.io.File; +import java.util.Objects; import static org.jackhuang.hmcl.util.i18n.I18n.i18n; public class GameAdvancedListItem extends AdvancedListItem { public GameAdvancedListItem() { - Profiles.selectedVersionProperty().addListener((o, a, version) -> { - File iconFile = Profiles.getSelectedProfile().getRepository().getVersionIcon(version); - if (iconFile.exists()) - imageProperty().set(new Image("file:" + iconFile.getAbsolutePath())); - else - imageProperty().set(new Image("/assets/img/grass.png")); + FXUtils.onChangeAndOperate(Profiles.selectedVersionProperty(), version -> { + FXUtils.runLaterIf(() -> !Objects.nonNull(Profiles.getSelectedProfile()), () -> { + File iconFile = Profiles.getSelectedProfile().getRepository().getVersionIcon(version); + if (iconFile.exists()) + imageProperty().set(new Image("file:" + iconFile.getAbsolutePath())); + else + imageProperty().set(new Image("/assets/img/grass.png")); - if (version != null) { - titleProperty().set(version); - subtitleProperty().set(null); - } else { - titleProperty().set(i18n("version.empty")); - subtitleProperty().set(i18n("version.empty.add")); - } + if (version != null) { + titleProperty().set(version); + subtitleProperty().set(null); + } else { + titleProperty().set(i18n("version.empty")); + subtitleProperty().set(i18n("version.empty.add")); + } + }); }); } }