Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,22 @@ public AccountAdvancedListItem() {
setActionButtonVisible(false);

setOnScroll(event -> {
double deltaY = event.getDeltaY();
if (deltaY == 0)
return;

Account current = account.get();
if (current == null) return;

ObservableList<Account> 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()));
});
}
Expand Down
16 changes: 9 additions & 7 deletions HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
import org.jackhuang.hmcl.upgrade.RemoteVersion;
import org.jackhuang.hmcl.upgrade.UpdateChecker;
import org.jackhuang.hmcl.upgrade.UpdateHandler;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.javafx.BindingMapping;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;

import java.util.List;
import java.util.Objects;
import java.util.stream.IntStream;

import static org.jackhuang.hmcl.setting.ConfigHolder.config;
import static org.jackhuang.hmcl.ui.FXUtils.SINE;
Expand Down Expand Up @@ -197,15 +197,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);
Expand Down
39 changes: 28 additions & 11 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public static <T, U> U requireNonNullElseGet(T value, Function<? super T, ? exte

/**
* Construct a mutable map by given key-value pairs.
*
* @param pairs entries in the new map
* @param <K> the type of keys
* @param <V> the type of values
* @param <K> the type of keys
* @param <V> the type of values
* @return the map which contains data in {@code pairs}.
*/
@SafeVarargs
Expand All @@ -60,9 +61,10 @@ public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {

/**
* Construct a mutable map by given key-value pairs.
*
* @param pairs entries in the new map
* @param <K> the type of keys
* @param <V> the type of values
* @param <K> the type of keys
* @param <V> the type of values
* @return the map which contains data in {@code pairs}.
*/
public static <K, V> Map<K, V> mapOf(Iterable<Pair<K, V>> pairs) {
Expand Down Expand Up @@ -122,9 +124,10 @@ public static <T> T ignoringException(ExceptionalSupplier<T, ?> supplier, T defa

/**
* 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 <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}.
*/
public static <V> Optional<V> tryCast(Object obj, Class<V> clazz) {
Expand Down Expand Up @@ -154,8 +157,8 @@ public static <T> List<T> removingDuplicates(List<T> list) {
/**
* 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 <T> the super type of elements in {@code a} and {@code b}
* @return the joint collection
*/
Expand All @@ -172,6 +175,16 @@ public static <T> List<T> copyList(List<T> 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) {
thread(() -> {
try {
Expand All @@ -185,6 +198,7 @@ public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long tim

/**
* Start a thread invoking {@code runnable} immediately.
*
* @param runnable code to run.
* @return the reference of the started thread
*/
Expand All @@ -194,8 +208,9 @@ public static Thread thread(Runnable runnable) {

/**
* 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) {
Expand All @@ -204,8 +219,9 @@ public static Thread thread(Runnable runnable, String name) {

/**
* 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
*/
Expand Down Expand Up @@ -258,7 +274,8 @@ public static Double toDoubleOrNull(Object string) {

/**
* 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
* @return the first non-null reference.
*/
Expand Down