Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

版本列表添加搜索 #3235

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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 @@ -17,15 +17,21 @@
*/
package org.jackhuang.hmcl.ui.versions;

import com.jfoenix.controls.JFXTextField;
import javafx.animation.PauseTransition;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.util.Duration;
import org.jackhuang.hmcl.game.HMCLGameRepository;
import org.jackhuang.hmcl.setting.Profile;
import org.jackhuang.hmcl.setting.Profiles;
Expand All @@ -40,6 +46,8 @@

import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

import static org.jackhuang.hmcl.ui.FXUtils.runInFX;
Expand All @@ -54,6 +62,8 @@ public class GameListPage extends DecoratorAnimatedPage implements DecoratorPage
private final ObjectProperty<Profile> selectedProfile;

private ToggleGroup toggleGroup;
private final TextField searchField;
private final GameList gameList;

public GameListPage() {
profileListItems = MappedObservableList.create(profilesProperty(), profile -> {
Expand All @@ -63,7 +73,36 @@ public GameListPage() {
});
selectedProfile = createSelectedItemPropertyFor(profileListItems, Profile.class);

GameList gameList = new GameList();
gameList = new GameList();

HBox searchBar = new HBox();
searchBar.setPadding(new Insets(12, 10, 0, 10));
searchField = new JFXTextField();
searchField.setPromptText(i18n("search.hint.regex"));
HBox.setHgrow(searchField, Priority.ALWAYS);
PauseTransition pause = new PauseTransition(Duration.millis(100));
pause.setOnFinished(e -> gameList.filter(searchField.getText()));
searchField.textProperty().addListener((obs, oldText, newText) -> {
pause.setRate(1);
pause.playFromStart();
});
searchBar.getChildren().setAll(searchField);

VBox centerBox = new VBox();
if (gameList.getItems().isEmpty() && searchField.getText().isEmpty()) {
setCenter(gameList);
} else {
centerBox.getChildren().setAll(searchBar, gameList);
setCenter(centerBox);
}
gameList.itemsProperty().addListener((obs, oldItems, newItems) -> {
if (newItems.isEmpty() && searchField.getText().isEmpty()) {
setCenter(gameList);
} else {
centerBox.getChildren().setAll(searchBar, gameList);
setCenter(centerBox);
}
});

{
ScrollPane pane = new ScrollPane();
Expand Down Expand Up @@ -113,8 +152,6 @@ public GameListPage() {
FXUtils.setLimitHeight(bottomLeftCornerList, 40 * 4 + 12 * 2);
setLeft(pane, bottomLeftCornerList);
}

setCenter(gameList);
}

public ObjectProperty<Profile> selectedProfileProperty() {
Expand Down Expand Up @@ -143,12 +180,17 @@ public ReadOnlyObjectProperty<State> stateProperty() {
}

private class GameList extends ListPageBase<GameListItem> {
private final ObjectProperty<String> filter = new SimpleObjectProperty<>("");
private final ObservableList<GameListItem> originalItems = FXCollections.observableArrayList();

public GameList() {
super();

Profiles.registerVersionsListener(this::loadVersions);

setOnFailedAction(e -> Controllers.navigate(Controllers.getDownloadPage()));

filter.addListener((obs, oldFilter, newFilter) -> applyFilter(newFilter));
}

private void loadVersions(Profile profile) {
Expand All @@ -164,10 +206,20 @@ private void loadVersions(Profile profile) {
List<GameListItem> children = repository.getDisplayVersions()
.map(version -> new GameListItem(toggleGroup, profile, version.getId()))
.collect(Collectors.toList());

originalItems.setAll(children);

itemsProperty().setAll(children);
children.forEach(GameListItem::checkSelection);

if (!center.getChildren().isEmpty()) {
searchField.clear();
}

if (children.isEmpty()) {
if (!center.getChildren().isEmpty()) {
setCenter(gameList);
}
setFailedReason(i18n("version.empty.hint"));
}

Expand All @@ -192,6 +244,37 @@ public void refreshList() {
Profiles.getSelectedProfile().getRepository().refreshVersionsAsync().start();
}

public void filter(String searchText) {
filter.set(searchText);
}

private void applyFilter(String filterText) {
Copy link
Member

Choose a reason for hiding this comment

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

额…… 理论上来说,在 JavaFX 线程跑 filter 应该没有多大问题

看看 Glavo 的意见

if (filterText.startsWith("regex:")) {
try {
String regex = filterText.substring("regex:".length());
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);

List<GameListItem> filteredItems = originalItems.stream()
.filter(item -> pattern.matcher(item.getVersion()).find())
.collect(Collectors.toList());

itemsProperty().setAll(filteredItems);
} catch (PatternSyntaxException e) {
System.err.println("Invalid regex pattern: " + e.getMessage());
}
} else {
String lowerCaseFilterText = filterText.toLowerCase();
if (filterText.isEmpty()) {
itemsProperty().setAll(originalItems);
} else {
List<GameListItem> filteredItems = originalItems.stream()
.filter(item -> item.getVersion().toLowerCase().contains(lowerCaseFilterText))
.collect(Collectors.toList());
itemsProperty().setAll(filteredItems);
}
}
}

@Override
protected GameListSkin createDefaultSkin() {
return new GameListSkin();
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,7 @@ repositories.chooser.title=Select a download source to download JavaFX from
resourcepack=Resource Packs

search=Search
search.hint.regex=Support regex search (regex:+regular expression)
search.hint.chinese=Search queries support both Chinese and English
search.hint.english=Only English is supported
search.enter=Enter text here
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_zh.properties
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ repositories.chooser.title=選擇下載源下載JavaFX
resourcepack=資源包

search=搜尋
search.hint.regex=支援正規表示式搜尋(regex:+正規表示式)
search.hint.chinese=支援中英文搜尋
search.hint.english=僅支援英文搜尋
search.enter=請在此處輸入
Expand Down
1 change: 1 addition & 0 deletions HMCL/src/main/resources/assets/lang/I18N_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ repositories.chooser.title=选择下载源下载 JavaFX
resourcepack=资源包

search=搜索
search.hint.regex=支持正则表达式搜索(regex:+正则表达式)
search.hint.chinese=支持中英文搜索
search.hint.english=仅支持英文搜索
search.enter=可在此处输入
Expand Down