From 60ff34a587c7feae45463ebacd155d803212c6d1 Mon Sep 17 00:00:00 2001 From: Glavo Date: Sat, 13 Sep 2025 14:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=BB=9A=E5=8A=A8=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=B8=B8=E6=88=8F=E5=AE=9E=E4=BE=8B/=E8=B4=A6?= =?UTF-8?q?=E6=88=B7=E5=8A=9F=E8=83=BD=20(#4466)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/account/AccountAdvancedListItem.java | 15 +++++-- .../org/jackhuang/hmcl/ui/main/MainPage.java | 16 ++++---- .../java/org/jackhuang/hmcl/util/Lang.java | 39 +++++++++++++------ 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java index 8ea02ad83..811af1a32 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java @@ -78,15 +78,22 @@ public class AccountAdvancedListItem extends AdvancedListItem { setActionButtonVisible(false); setOnScroll(event -> { + double deltaY = event.getDeltaY(); + if (deltaY == 0) + return; + Account current = account.get(); if (current == null) return; + ObservableList accounts = Accounts.getAccounts(); - int currentIndex = accounts.indexOf(account.get()); - if (event.getDeltaY() > 0) { // up + int currentIndex = accounts.indexOf(current); + if (currentIndex < 0) return; + + if (deltaY > 0) // up currentIndex--; - } else { // down + else // down currentIndex++; - } + Accounts.setSelectedAccount(accounts.get((currentIndex + accounts.size()) % accounts.size())); }); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java index ae5633634..1fde8095e 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java @@ -69,6 +69,7 @@ import org.jackhuang.hmcl.upgrade.RemoteVersion; import org.jackhuang.hmcl.upgrade.UpdateChecker; import org.jackhuang.hmcl.upgrade.UpdateHandler; import org.jackhuang.hmcl.util.Holder; +import org.jackhuang.hmcl.util.Lang; import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.TaskCancellationAction; import org.jackhuang.hmcl.util.javafx.BindingMapping; @@ -79,7 +80,6 @@ import java.util.List; import java.util.Objects; import java.util.concurrent.CancellationException; import java.util.function.Consumer; -import java.util.stream.IntStream; import static org.jackhuang.hmcl.download.RemoteVersion.Type.RELEASE; import static org.jackhuang.hmcl.setting.ConfigHolder.config; @@ -209,15 +209,17 @@ public final class MainPage extends StackPane implements DecoratorPage { launchPane.setMaxWidth(230); launchPane.setMaxHeight(55); launchPane.setOnScroll(event -> { - int index = IntStream.range(0, versions.size()) - .filter(i -> versions.get(i).getId().equals(getCurrentGame())) - .findFirst().orElse(-1); + double deltaY = event.getDeltaY(); + if (deltaY == 0) + return; + + String currentId = getCurrentGame(); + int index = Lang.indexWhere(versions, instance -> instance.getId().equals(currentId)); if (index < 0) return; - if (event.getDeltaY() > 0) { + if (deltaY > 0) // up index--; - } else { + else // down index++; - } profile.setSelectedVersion(versions.get((index + versions.size()) % versions.size()).getId()); }); StackPane.setAlignment(launchPane, Pos.BOTTOM_RIGHT); diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java index 85001ba8c..3fabc93ba 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java @@ -48,9 +48,10 @@ public final class Lang { /** * Construct a mutable map by given key-value pairs. + * * @param pairs entries in the new map - * @param the type of keys - * @param the type of values + * @param the type of keys + * @param the type of values * @return the map which contains data in {@code pairs}. */ @SafeVarargs @@ -60,9 +61,10 @@ public final class Lang { /** * Construct a mutable map by given key-value pairs. + * * @param pairs entries in the new map - * @param the type of keys - * @param the type of values + * @param the type of keys + * @param the type of values * @return the map which contains data in {@code pairs}. */ public static Map mapOf(Iterable> pairs) { @@ -122,9 +124,10 @@ public final class Lang { /** * Cast {@code obj} to V dynamically. - * @param obj the object reference to be cast. + * + * @param obj the object reference to be cast. * @param clazz the class reference of {@code V}. - * @param the type that {@code obj} is being cast to. + * @param the type that {@code obj} is being cast to. * @return {@code obj} in the type of {@code V}. */ public static Optional tryCast(Object obj, Class clazz) { @@ -154,8 +157,8 @@ public final class Lang { /** * Join two collections into one list. * - * @param a one collection, to be joined. - * @param b another collection to be joined. + * @param a one collection, to be joined. + * @param b another collection to be joined. * @param the super type of elements in {@code a} and {@code b} * @return the joint collection */ @@ -172,6 +175,16 @@ public final class Lang { return list == null ? null : list.isEmpty() ? null : new ArrayList<>(list); } + public static int indexWhere(List list, Predicate predicate) { + int idx = 0; + for (T value : list) { + if (predicate.test(value)) + return idx; + idx++; + } + return -1; + } + public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long timeout, boolean isDaemon) { thread(() -> { try { @@ -185,6 +198,7 @@ public final class Lang { /** * Start a thread invoking {@code runnable} immediately. + * * @param runnable code to run. * @return the reference of the started thread */ @@ -194,8 +208,9 @@ public final class Lang { /** * Start a thread invoking {@code runnable} immediately. + * * @param runnable code to run - * @param name the name of thread + * @param name the name of thread * @return the reference of the started thread */ public static Thread thread(Runnable runnable, String name) { @@ -204,8 +219,9 @@ public final class Lang { /** * Start a thread invoking {@code runnable} immediately. + * * @param runnable code to run - * @param name the name of thread + * @param name the name of thread * @param isDaemon true if thread will be terminated when only daemon threads are running. * @return the reference of the started thread */ @@ -258,7 +274,8 @@ public final class Lang { /** * Find the first non-null reference in given list. - * @param t nullable references list. + * + * @param t nullable references list. * @param the type of nullable references * @return the first non-null reference. */