Skip to content

Commit 75738ac

Browse files
committed
better search
1 parent 6968a17 commit 75738ac

File tree

4 files changed

+34
-5
lines changed

4 files changed

+34
-5
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ plugins {
1616
val ciRun = System.getenv().containsKey("GITHUB_ACTIONS")
1717

1818
group = "dev.isxander"
19-
version = "1.2.0"
19+
version = "1.2.1"
2020

2121
if (ciRun)
2222
version = "$version-SNAPSHOT"

changelogs/1.2.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Search can now query categories and groups

src/main/java/dev/isxander/yacl/gui/OptionListWidget.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
public class OptionListWidget extends ElementListWidget<OptionListWidget.Entry> {
3131
private final YACLScreen yaclScreen;
32+
private boolean singleCategory = false;
3233

3334
public OptionListWidget(YACLScreen screen, MinecraftClient client, int width, int height) {
3435
super(client, width / 3 * 2, height, 0, height, 22);
@@ -48,6 +49,7 @@ public void refreshOptions() {
4849
} else {
4950
categories.add(yaclScreen.config.categories().get(yaclScreen.currentCategoryIdx));
5051
}
52+
singleCategory = categories.size() == 1;
5153

5254
for (ConfigCategory category : categories) {
5355
for (OptionGroup group : category.groups()) {
@@ -63,7 +65,7 @@ public void refreshOptions() {
6365

6466
List<OptionEntry> optionEntries = new ArrayList<>();
6567
for (Option<?> option : group.options()) {
66-
OptionEntry entry = new OptionEntry(option.controller().provideWidget(yaclScreen, Dimension.ofInt(getRowLeft(), 0, getRowWidth(), 20)), viewableSupplier);
68+
OptionEntry entry = new OptionEntry(category, group, option.controller().provideWidget(yaclScreen, Dimension.ofInt(getRowLeft(), 0, getRowWidth(), 20)), viewableSupplier);
6769
addEntry(entry);
6870
optionEntries.add(entry);
6971
}
@@ -229,11 +231,16 @@ protected boolean isHovered() {
229231
}
230232
}
231233

232-
private class OptionEntry extends Entry {
234+
public class OptionEntry extends Entry {
235+
public final ConfigCategory category;
236+
public final OptionGroup group;
237+
233238
public final AbstractWidget widget;
234239
private final Supplier<Boolean> viewableSupplier;
235240

236-
public OptionEntry(AbstractWidget widget, Supplier<Boolean> viewableSupplier) {
241+
private OptionEntry(ConfigCategory category, OptionGroup group, AbstractWidget widget, Supplier<Boolean> viewableSupplier) {
242+
this.category = category;
243+
this.group = group;
237244
this.widget = widget;
238245
this.viewableSupplier = viewableSupplier;
239246
}
@@ -267,7 +274,7 @@ public boolean charTyped(char chr, int modifiers) {
267274

268275
@Override
269276
public boolean isViewable() {
270-
return viewableSupplier.get() && widget.matchesSearch(yaclScreen.searchFieldWidget.getText().trim());
277+
return viewableSupplier.get() && yaclScreen.searchFieldWidget.matches(this, singleCategory);
271278
}
272279

273280
@Override

src/main/java/dev/isxander/yacl/gui/SearchFieldWidget.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package dev.isxander.yacl.gui;
22

3+
import dev.isxander.yacl.api.ConfigCategory;
4+
import dev.isxander.yacl.api.OptionGroup;
35
import net.minecraft.client.font.TextRenderer;
46
import net.minecraft.client.gui.widget.TextFieldWidget;
57
import net.minecraft.client.util.math.MatrixStack;
@@ -45,6 +47,25 @@ public void eraseCharacters(int characterOffset) {
4547
super.eraseCharacters(characterOffset);
4648
}
4749

50+
public boolean matches(OptionListWidget.OptionEntry optionEntry, boolean ignoreCategory) {
51+
return (matchesCategory(optionEntry.category) && !ignoreCategory) || matchesGroup(optionEntry.group) || matchesWidget(optionEntry.widget);
52+
}
53+
54+
public boolean matchesCategory(ConfigCategory category) {
55+
return category.name().getString().toLowerCase().contains(getText().trim());
56+
}
57+
58+
public boolean matchesGroup(OptionGroup group) {
59+
if (group.isRoot())
60+
return false;
61+
62+
return group.name().getString().toLowerCase().contains(getText().trim());
63+
}
64+
65+
public boolean matchesWidget(AbstractWidget widget) {
66+
return widget.matchesSearch(getText().trim());
67+
}
68+
4869
public Text getEmptyText() {
4970
return emptyText;
5071
}

0 commit comments

Comments
 (0)