Skip to content
Open
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 @@ -20,16 +20,18 @@
import com.jfoenix.controls.JFXPopup;
import javafx.beans.InvalidationListener;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.beans.property.ListProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.css.PseudoClass;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.*;
import javafx.scene.input.*;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import org.jackhuang.hmcl.ui.FXUtils;
import org.jackhuang.hmcl.ui.SVG;
import org.jackhuang.hmcl.util.javafx.MappedObservableList;
Expand All @@ -51,6 +53,8 @@ public final class LineSelectButton<T> extends LineButton {
public LineSelectButton() {
this.getStyleClass().add(DEFAULT_STYLE_CLASS);

this.addEventHandler(KeyEvent.KEY_PRESSED, this::handleKeyEvent);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这有作用吗?我测试下来没感觉到作用。

Copy link
Contributor

@3gf8jv4dv 3gf8jv4dv Feb 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我在 Windows 测试是有作用的,目前的效果和 #5636 (comment) 的预期行为一致。


InvalidationListener updateTrailingText = observable -> {
T value = getValue();
if (value != null) {
Expand Down Expand Up @@ -81,6 +85,8 @@ public void fire() {
PopupMenu popupMenu = new PopupMenu();
this.popup = new JFXPopup(popupMenu);

popupMenu.addEventHandler(KeyEvent.KEY_PRESSED, this::handleKeyEvent);

ripplerContainer.addEventFilter(ScrollEvent.ANY, ignored -> popup.hide());

Bindings.bindContent(popupMenu.getContent(), MappedObservableList.create(itemsProperty(), item -> {
Expand Down Expand Up @@ -213,4 +219,32 @@ public ObservableList<T> getItems() {
return items.get();
}

private void handleKeyEvent(KeyEvent event) {
ObservableList<T> list = getItems();
if (list == null || list.isEmpty()) return;
int index = list.indexOf(getValue());

var code = event.getCode();

if (code == KeyCode.UP) {
if (index > 0) {
setValue(list.get(index - 1));
} else {
setValue(list.get(list.size() - 1));
}
event.consume();
} else if (code == KeyCode.DOWN) {
if (index < list.size() - 1) {
setValue(list.get(index + 1));
} else {
setValue(list.get(0));
}
event.consume();
} else if (code == KeyCode.ENTER || code == KeyCode.ESCAPE) {
if (popup != null && popup.isShowing()) {
popup.hide();
event.consume();
}
}
}
}