Skip to content

Commit abe4ef2

Browse files
committed
修复滚动切换游戏实例/账户功能 (#4466)
1 parent f62c12f commit abe4ef2

3 files changed

Lines changed: 53 additions & 22 deletions

File tree

HMCL/src/main/java/org/jackhuang/hmcl/ui/account/AccountAdvancedListItem.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,22 @@ public AccountAdvancedListItem() {
7878
setActionButtonVisible(false);
7979

8080
setOnScroll(event -> {
81+
double deltaY = event.getDeltaY();
82+
if (deltaY == 0)
83+
return;
84+
8185
Account current = account.get();
8286
if (current == null) return;
87+
8388
ObservableList<Account> accounts = Accounts.getAccounts();
84-
int currentIndex = accounts.indexOf(account.get());
85-
if (event.getDeltaY() > 0) { // up
89+
int currentIndex = accounts.indexOf(current);
90+
if (currentIndex < 0) return;
91+
92+
if (deltaY > 0) // up
8693
currentIndex--;
87-
} else { // down
94+
else // down
8895
currentIndex++;
89-
}
96+
9097
Accounts.setSelectedAccount(accounts.get((currentIndex + accounts.size()) % accounts.size()));
9198
});
9299
}

HMCL/src/main/java/org/jackhuang/hmcl/ui/main/MainPage.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@
6262
import org.jackhuang.hmcl.upgrade.RemoteVersion;
6363
import org.jackhuang.hmcl.upgrade.UpdateChecker;
6464
import org.jackhuang.hmcl.upgrade.UpdateHandler;
65+
import org.jackhuang.hmcl.util.Holder;
66+
import org.jackhuang.hmcl.util.Lang;
67+
import org.jackhuang.hmcl.util.StringUtils;
68+
import org.jackhuang.hmcl.util.TaskCancellationAction;
6569
import org.jackhuang.hmcl.util.javafx.BindingMapping;
6670
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
6771

6872
import java.util.List;
6973
import java.util.Objects;
70-
import java.util.stream.IntStream;
74+
import java.util.concurrent.CancellationException;
75+
import java.util.function.Consumer;
7176

7277
import static org.jackhuang.hmcl.setting.ConfigHolder.config;
7378
import static org.jackhuang.hmcl.ui.FXUtils.SINE;
@@ -197,15 +202,17 @@ public final class MainPage extends StackPane implements DecoratorPage {
197202
launchPane.setMaxWidth(230);
198203
launchPane.setMaxHeight(55);
199204
launchPane.setOnScroll(event -> {
200-
int index = IntStream.range(0, versions.size())
201-
.filter(i -> versions.get(i).getId().equals(getCurrentGame()))
202-
.findFirst().orElse(-1);
205+
double deltaY = event.getDeltaY();
206+
if (deltaY == 0)
207+
return;
208+
209+
String currentId = getCurrentGame();
210+
int index = Lang.indexWhere(versions, instance -> instance.getId().equals(currentId));
203211
if (index < 0) return;
204-
if (event.getDeltaY() > 0) {
212+
if (deltaY > 0) // up
205213
index--;
206-
} else {
214+
else // down
207215
index++;
208-
}
209216
profile.setSelectedVersion(versions.get((index + versions.size()) % versions.size()).getId());
210217
});
211218
StackPane.setAlignment(launchPane, Pos.BOTTOM_RIGHT);

HMCLCore/src/main/java/org/jackhuang/hmcl/util/Lang.java

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ public static <T, U> U requireNonNullElseGet(T value, Function<? super T, ? exte
4848

4949
/**
5050
* Construct a mutable map by given key-value pairs.
51+
*
5152
* @param pairs entries in the new map
52-
* @param <K> the type of keys
53-
* @param <V> the type of values
53+
* @param <K> the type of keys
54+
* @param <V> the type of values
5455
* @return the map which contains data in {@code pairs}.
5556
*/
5657
@SafeVarargs
@@ -60,9 +61,10 @@ public static <K, V> Map<K, V> mapOf(Pair<K, V>... pairs) {
6061

6162
/**
6263
* Construct a mutable map by given key-value pairs.
64+
*
6365
* @param pairs entries in the new map
64-
* @param <K> the type of keys
65-
* @param <V> the type of values
66+
* @param <K> the type of keys
67+
* @param <V> the type of values
6668
* @return the map which contains data in {@code pairs}.
6769
*/
6870
public static <K, V> Map<K, V> mapOf(Iterable<Pair<K, V>> pairs) {
@@ -122,9 +124,10 @@ public static <T> T ignoringException(ExceptionalSupplier<T, ?> supplier, T defa
122124

123125
/**
124126
* Cast {@code obj} to V dynamically.
125-
* @param obj the object reference to be cast.
127+
*
128+
* @param obj the object reference to be cast.
126129
* @param clazz the class reference of {@code V}.
127-
* @param <V> the type that {@code obj} is being cast to.
130+
* @param <V> the type that {@code obj} is being cast to.
128131
* @return {@code obj} in the type of {@code V}.
129132
*/
130133
public static <V> Optional<V> tryCast(Object obj, Class<V> clazz) {
@@ -154,8 +157,8 @@ public static <T> List<T> removingDuplicates(List<T> list) {
154157
/**
155158
* Join two collections into one list.
156159
*
157-
* @param a one collection, to be joined.
158-
* @param b another collection to be joined.
160+
* @param a one collection, to be joined.
161+
* @param b another collection to be joined.
159162
* @param <T> the super type of elements in {@code a} and {@code b}
160163
* @return the joint collection
161164
*/
@@ -172,6 +175,16 @@ public static <T> List<T> copyList(List<T> list) {
172175
return list == null ? null : list.isEmpty() ? null : new ArrayList<>(list);
173176
}
174177

178+
public static <T> int indexWhere(List<T> list, Predicate<T> predicate) {
179+
int idx = 0;
180+
for (T value : list) {
181+
if (predicate.test(value))
182+
return idx;
183+
idx++;
184+
}
185+
return -1;
186+
}
187+
175188
public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long timeout, boolean isDaemon) {
176189
thread(() -> {
177190
try {
@@ -185,6 +198,7 @@ public static void executeDelayed(Runnable runnable, TimeUnit timeUnit, long tim
185198

186199
/**
187200
* Start a thread invoking {@code runnable} immediately.
201+
*
188202
* @param runnable code to run.
189203
* @return the reference of the started thread
190204
*/
@@ -194,8 +208,9 @@ public static Thread thread(Runnable runnable) {
194208

195209
/**
196210
* Start a thread invoking {@code runnable} immediately.
211+
*
197212
* @param runnable code to run
198-
* @param name the name of thread
213+
* @param name the name of thread
199214
* @return the reference of the started thread
200215
*/
201216
public static Thread thread(Runnable runnable, String name) {
@@ -204,8 +219,9 @@ public static Thread thread(Runnable runnable, String name) {
204219

205220
/**
206221
* Start a thread invoking {@code runnable} immediately.
222+
*
207223
* @param runnable code to run
208-
* @param name the name of thread
224+
* @param name the name of thread
209225
* @param isDaemon true if thread will be terminated when only daemon threads are running.
210226
* @return the reference of the started thread
211227
*/
@@ -258,7 +274,8 @@ public static Double toDoubleOrNull(Object string) {
258274

259275
/**
260276
* Find the first non-null reference in given list.
261-
* @param t nullable references list.
277+
*
278+
* @param t nullable references list.
262279
* @param <T> the type of nullable references
263280
* @return the first non-null reference.
264281
*/

0 commit comments

Comments
 (0)