修复滚动切换游戏实例/账户功能 (#4466)

This commit is contained in:
Glavo
2025-09-13 14:16:24 +08:00
committed by GitHub
parent 356960cc82
commit 60ff34a587
3 changed files with 48 additions and 22 deletions

View File

@@ -78,15 +78,22 @@ public class AccountAdvancedListItem extends AdvancedListItem {
setActionButtonVisible(false); setActionButtonVisible(false);
setOnScroll(event -> { setOnScroll(event -> {
double deltaY = event.getDeltaY();
if (deltaY == 0)
return;
Account current = account.get(); Account current = account.get();
if (current == null) return; if (current == null) return;
ObservableList<Account> accounts = Accounts.getAccounts(); ObservableList<Account> accounts = Accounts.getAccounts();
int currentIndex = accounts.indexOf(account.get()); int currentIndex = accounts.indexOf(current);
if (event.getDeltaY() > 0) { // up if (currentIndex < 0) return;
if (deltaY > 0) // up
currentIndex--; currentIndex--;
} else { // down else // down
currentIndex++; currentIndex++;
}
Accounts.setSelectedAccount(accounts.get((currentIndex + accounts.size()) % accounts.size())); Accounts.setSelectedAccount(accounts.get((currentIndex + accounts.size()) % accounts.size()));
}); });
} }

View File

@@ -69,6 +69,7 @@ import org.jackhuang.hmcl.upgrade.RemoteVersion;
import org.jackhuang.hmcl.upgrade.UpdateChecker; import org.jackhuang.hmcl.upgrade.UpdateChecker;
import org.jackhuang.hmcl.upgrade.UpdateHandler; import org.jackhuang.hmcl.upgrade.UpdateHandler;
import org.jackhuang.hmcl.util.Holder; import org.jackhuang.hmcl.util.Holder;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.StringUtils; import org.jackhuang.hmcl.util.StringUtils;
import org.jackhuang.hmcl.util.TaskCancellationAction; import org.jackhuang.hmcl.util.TaskCancellationAction;
import org.jackhuang.hmcl.util.javafx.BindingMapping; import org.jackhuang.hmcl.util.javafx.BindingMapping;
@@ -79,7 +80,6 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.IntStream;
import static org.jackhuang.hmcl.download.RemoteVersion.Type.RELEASE; import static org.jackhuang.hmcl.download.RemoteVersion.Type.RELEASE;
import static org.jackhuang.hmcl.setting.ConfigHolder.config; import static org.jackhuang.hmcl.setting.ConfigHolder.config;
@@ -209,15 +209,17 @@ public final class MainPage extends StackPane implements DecoratorPage {
launchPane.setMaxWidth(230); launchPane.setMaxWidth(230);
launchPane.setMaxHeight(55); launchPane.setMaxHeight(55);
launchPane.setOnScroll(event -> { launchPane.setOnScroll(event -> {
int index = IntStream.range(0, versions.size()) double deltaY = event.getDeltaY();
.filter(i -> versions.get(i).getId().equals(getCurrentGame())) if (deltaY == 0)
.findFirst().orElse(-1); return;
String currentId = getCurrentGame();
int index = Lang.indexWhere(versions, instance -> instance.getId().equals(currentId));
if (index < 0) return; if (index < 0) return;
if (event.getDeltaY() > 0) { if (deltaY > 0) // up
index--; index--;
} else { else // down
index++; index++;
}
profile.setSelectedVersion(versions.get((index + versions.size()) % versions.size()).getId()); profile.setSelectedVersion(versions.get((index + versions.size()) % versions.size()).getId());
}); });
StackPane.setAlignment(launchPane, Pos.BOTTOM_RIGHT); StackPane.setAlignment(launchPane, Pos.BOTTOM_RIGHT);

View File

@@ -48,9 +48,10 @@ public final class Lang {
/** /**
* Construct a mutable map by given key-value pairs. * Construct a mutable map by given key-value pairs.
*
* @param pairs entries in the new map * @param pairs entries in the new map
* @param <K> the type of keys * @param <K> the type of keys
* @param <V> the type of values * @param <V> the type of values
* @return the map which contains data in {@code pairs}. * @return the map which contains data in {@code pairs}.
*/ */
@SafeVarargs @SafeVarargs
@@ -60,9 +61,10 @@ public final class Lang {
/** /**
* Construct a mutable map by given key-value pairs. * Construct a mutable map by given key-value pairs.
*
* @param pairs entries in the new map * @param pairs entries in the new map
* @param <K> the type of keys * @param <K> the type of keys
* @param <V> the type of values * @param <V> the type of values
* @return the map which contains data in {@code pairs}. * @return the map which contains data in {@code pairs}.
*/ */
public static <K, V> Map<K, V> mapOf(Iterable<Pair<K, V>> pairs) { public static <K, V> Map<K, V> mapOf(Iterable<Pair<K, V>> pairs) {
@@ -122,9 +124,10 @@ public final class Lang {
/** /**
* Cast {@code obj} to V dynamically. * 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 clazz the class reference of {@code V}.
* @param <V> the type that {@code obj} is being cast to. * @param <V> the type that {@code obj} is being cast to.
* @return {@code obj} in the type of {@code V}. * @return {@code obj} in the type of {@code V}.
*/ */
public static <V> Optional<V> tryCast(Object obj, Class<V> clazz) { public static <V> Optional<V> tryCast(Object obj, Class<V> clazz) {
@@ -154,8 +157,8 @@ public final class Lang {
/** /**
* Join two collections into one list. * Join two collections into one list.
* *
* @param a one collection, to be joined. * @param a one collection, to be joined.
* @param b another collection to be joined. * @param b another collection to be joined.
* @param <T> the super type of elements in {@code a} and {@code b} * @param <T> the super type of elements in {@code a} and {@code b}
* @return the joint collection * @return the joint collection
*/ */
@@ -172,6 +175,16 @@ public final class Lang {
return list == null ? null : list.isEmpty() ? null : new ArrayList<>(list); return list == null ? null : list.isEmpty() ? null : new ArrayList<>(list);
} }
public static <T> int indexWhere(List<T> list, Predicate<T> 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) { public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long timeout, boolean isDaemon) {
thread(() -> { thread(() -> {
try { try {
@@ -185,6 +198,7 @@ public final class Lang {
/** /**
* Start a thread invoking {@code runnable} immediately. * Start a thread invoking {@code runnable} immediately.
*
* @param runnable code to run. * @param runnable code to run.
* @return the reference of the started thread * @return the reference of the started thread
*/ */
@@ -194,8 +208,9 @@ public final class Lang {
/** /**
* Start a thread invoking {@code runnable} immediately. * Start a thread invoking {@code runnable} immediately.
*
* @param runnable code to run * @param runnable code to run
* @param name the name of thread * @param name the name of thread
* @return the reference of the started thread * @return the reference of the started thread
*/ */
public static Thread thread(Runnable runnable, String name) { public static Thread thread(Runnable runnable, String name) {
@@ -204,8 +219,9 @@ public final class Lang {
/** /**
* Start a thread invoking {@code runnable} immediately. * Start a thread invoking {@code runnable} immediately.
*
* @param runnable code to run * @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. * @param isDaemon true if thread will be terminated when only daemon threads are running.
* @return the reference of the started thread * @return the reference of the started thread
*/ */
@@ -258,7 +274,8 @@ public final class Lang {
/** /**
* Find the first non-null reference in given list. * Find the first non-null reference in given list.
* @param t nullable references list. *
* @param t nullable references list.
* @param <T> the type of nullable references * @param <T> the type of nullable references
* @return the first non-null reference. * @return the first non-null reference.
*/ */