Skip to content

Commit 5e288e9

Browse files
committed
1.4.4
Fix search not behaving properly with default collapsed option groups Minor refactors
1 parent f77914f commit 5e288e9

7 files changed

Lines changed: 40 additions & 36 deletions

File tree

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.4.3"
19+
version = "1.4.4"
2020

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

changelogs/1.4.4.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Fix search not behaving properly with default collapsed option groups
2+
- Minor refactors

src/main/java/dev/isxander/yacl/api/YetAnotherConfigLib.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public Builder category(@NotNull ConfigCategory category) {
9494
*
9595
* @see YetAnotherConfigLib#categories()
9696
*/
97-
public Builder categories(@NotNull Collection<ConfigCategory> categories) {
98-
Validate.notEmpty(categories, "`categories` cannot be empty");
97+
public Builder categories(@NotNull Collection<? extends ConfigCategory> categories) {
98+
Validate.notNull(categories, "`categories` cannot be null");
9999

100100
this.categories.addAll(categories);
101101
return this;
@@ -128,6 +128,7 @@ public Builder screenInit(@NotNull Consumer<YACLScreen> initConsumer) {
128128
public YetAnotherConfigLib build() {
129129
Validate.notNull(title, "`title must not be null to build `YetAnotherConfigLib`");
130130
Validate.notEmpty(categories, "`categories` must not be empty to build `YetAnotherConfigLib`");
131+
Validate.isTrue(!categories.stream().allMatch(category -> category instanceof PlaceholderCategory), "At least one regular category is required to build `YetAnotherConfigLib`");
131132

132133
return new YetAnotherConfigLibImpl(title, ImmutableList.copyOf(categories), saveFunction, initConsumer);
133134
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public CategoryWidget(YACLScreen screen, ConfigCategory category, int categoryIn
1515
}
1616

1717
private boolean isCurrentCategory() {
18-
return ((YACLScreen) screen).currentCategoryIdx == categoryIndex;
18+
return ((YACLScreen) screen).getCurrentCategoryIdx() == categoryIndex;
1919
}
2020

2121
@Override

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public void refreshOptions() {
4141
clearEntries();
4242

4343
List<ConfigCategory> categories = new ArrayList<>();
44-
if (yaclScreen.currentCategoryIdx == -1) {
44+
if (yaclScreen.getCurrentCategoryIdx() == -1) {
4545
categories.addAll(yaclScreen.config.categories());
4646
} else {
47-
categories.add(yaclScreen.config.categories().get(yaclScreen.currentCategoryIdx));
47+
categories.add(yaclScreen.config.categories().get(yaclScreen.getCurrentCategoryIdx()));
4848
}
4949
singleCategory = categories.size() == 1;
5050

@@ -77,6 +77,14 @@ public void refreshOptions() {
7777
setScrollAmount(0);
7878
}
7979

80+
public void expandAllGroups() {
81+
for (Entry entry : super.children()) {
82+
if (entry instanceof GroupSeparatorEntry groupSeparatorEntry) {
83+
groupSeparatorEntry.setExpanded(true);
84+
}
85+
}
86+
}
87+
8088
/*
8189
below code is licensed from cloth-config under LGPL3
8290
modified to inherit vanilla's EntryListWidget and use yarn mappings
@@ -282,6 +290,7 @@ public boolean charTyped(char chr, int modifiers) {
282290
@Override
283291
public boolean isViewable() {
284292
String query = yaclScreen.searchFieldWidget.getText();
293+
System.out.println(viewableSupplier.get());
285294
return viewableSupplier.get()
286295
&& (yaclScreen.searchFieldWidget.isEmpty()
287296
|| (!singleCategory && categoryName.contains(query))

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

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SearchFieldWidget extends TextFieldWidget {
1414

1515
public SearchFieldWidget(YACLScreen yaclScreen, TextRenderer textRenderer, int x, int y, int width, int height, Text text, Text emptyText) {
1616
super(textRenderer, x, y, width, height, text);
17+
setChangedListener(string -> update());
1718
setTextPredicate(string -> !string.endsWith(" ") && !string.startsWith(" "));
1819
this.yaclScreen = yaclScreen;
1920
this.textRenderer = textRenderer;
@@ -28,33 +29,23 @@ public void renderButton(MatrixStack matrices, int mouseX, int mouseY, float del
2829
}
2930
}
3031

31-
@Override
32-
public void write(String text) {
33-
update();
34-
super.write(text);
35-
postUpdate();
36-
}
32+
private void update() {
33+
boolean wasEmpty = isEmpty;
34+
isEmpty = getText().isEmpty();
3735

38-
@Override
39-
public void eraseCharacters(int characterOffset) {
40-
update();
41-
super.eraseCharacters(characterOffset);
42-
postUpdate();
43-
}
36+
if (isEmpty && wasEmpty)
37+
return;
4438

45-
private void update() {
46-
yaclScreen.optionList.setScrollAmount(0);
47-
yaclScreen.categoryList.setScrollAmount(0);
48-
for (OptionListWidget.Entry entry : yaclScreen.optionList.children()) {
49-
if (entry instanceof OptionListWidget.GroupSeparatorEntry groupSeparatorEntry) {
50-
groupSeparatorEntry.setExpanded(true);
51-
}
52-
}
53-
}
39+
if (!isEmpty && yaclScreen.getCurrentCategoryIdx() != -1)
40+
yaclScreen.changeCategory(-1);
41+
if (isEmpty && yaclScreen.getCurrentCategoryIdx() == -1)
42+
yaclScreen.changeCategory(0);
5443

55-
private void postUpdate() {
56-
isEmpty = getText().isEmpty();
44+
yaclScreen.optionList.expandAllGroups();
5745
yaclScreen.optionList.recacheViewableChildren();
46+
47+
yaclScreen.optionList.setScrollAmount(0);
48+
yaclScreen.categoryList.setScrollAmount(0);
5849
}
5950

6051
public boolean isEmpty() {

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
public class YACLScreen extends Screen {
2323
public final YetAnotherConfigLib config;
24-
public int currentCategoryIdx;
24+
private int currentCategoryIdx;
2525

2626
private final Screen parent;
2727

@@ -139,6 +139,9 @@ public boolean charTyped(char chr, int modifiers) {
139139
}
140140

141141
public void changeCategory(int idx) {
142+
if (idx == currentCategoryIdx)
143+
return;
144+
142145
if (idx != -1 && config.categories().get(idx) instanceof PlaceholderCategory placeholderCategory) {
143146
client.setScreen(placeholderCategory.screen().apply(client, this));
144147
} else {
@@ -147,6 +150,10 @@ public void changeCategory(int idx) {
147150
}
148151
}
149152

153+
public int getCurrentCategoryIdx() {
154+
return currentCategoryIdx;
155+
}
156+
150157
private void updateActionAvailability() {
151158
boolean pendingChanges = pendingChanges();
152159

@@ -160,12 +167,6 @@ private void updateActionAvailability() {
160167
@Override
161168
public void tick() {
162169
searchFieldWidget.tick();
163-
if (!searchFieldWidget.getText().isEmpty() && currentCategoryIdx != -1) {
164-
changeCategory(-1);
165-
}
166-
if (searchFieldWidget.getText().isEmpty() && currentCategoryIdx == -1) {
167-
changeCategory(0);
168-
}
169170

170171
updateActionAvailability();
171172

0 commit comments

Comments
 (0)