2828public class OptionListWidget extends YACLSelectionList <OptionListWidget .Entry > {
2929 private final YACLScreen yaclScreen ;
3030 private final ConfigCategory category ;
31- private ImmutableList <Entry > viewableChildren ;
3231 private String searchQuery = "" ;
3332 private final Consumer <DescriptionWithName > hoverEvent ;
3433 private DescriptionWithName lastHoveredOption ;
@@ -84,22 +83,28 @@ public void refreshOptions() {
8483 }
8584 }
8685
87- recacheViewableChildren ();
8886 setScrollAmount (0 );
89- //resetSmoothScrolling();
9087 }
9188
89+ //? if >=1.21.9 {
90+ @ Override
91+ protected int addEntry (Entry entry ) {
92+ // instead of using super.defaultEntryHeight, use the height the entry wants to be - our entries set their height in the constructor
93+ return this .addEntry (entry , entry .getHeight ());
94+ }
95+ //?}
96+
9297 private void refreshListEntries (ListOption <?> listOption , ConfigCategory category ) {
9398 // find group separator for group
94- ListGroupSeparatorEntry groupSeparator = super .children ().stream ().filter (e -> e instanceof ListGroupSeparatorEntry gs && gs .group == listOption ).map (ListGroupSeparatorEntry .class ::cast ).findAny ().orElse (null );
99+ ListGroupSeparatorEntry groupSeparator = this .children ().stream ().filter (e -> e instanceof ListGroupSeparatorEntry gs && gs .group == listOption ).map (ListGroupSeparatorEntry .class ::cast ).findAny ().orElse (null );
95100
96101 if (groupSeparator == null ) {
97102 YACLConstants .LOGGER .warn ("Can't find group seperator to refresh list option entries for list option " + listOption .name ());
98103 return ;
99104 }
100105
101106 for (Entry entry : groupSeparator .childEntries )
102- super .removeEntry (entry );
107+ this .removeEntry (entry );
103108 groupSeparator .childEntries .clear ();
104109
105110 // if no entries, below loop won't run where addEntryBelow() recaches viewable children
@@ -143,8 +148,11 @@ public int getRowWidth() {
143148
144149 public void updateSearchQuery (String query ) {
145150 this .searchQuery = query ;
151+ for (Entry entry : this .children ()) {
152+ entry .updateSearchQuery (query );
153+ }
146154 expandAllGroups ();
147- recacheViewableChildren ();
155+ repositionEntries ();
148156 }
149157
150158 @ Override
@@ -197,31 +205,18 @@ public boolean charTyped(char chr, int modifiers) {
197205 return super .charTyped (chr , modifiers );
198206 }
199207
200- public void recacheViewableChildren () {
201- this .viewableChildren = ImmutableList .copyOf (super .children ().stream ().filter (Entry ::isViewable ).toList ());
202-
203- // update y positions before they need to be rendered are rendered
204- int i = 0 ;
205- for (Entry entry : viewableChildren ) {
206- if (entry instanceof OptionEntry optionEntry )
207- optionEntry .widget .setDimension (optionEntry .widget .getDimension ().withY (getRowTop (i )));
208- i ++;
209- }
210- }
211-
212- @ Override @ NotNull
213- public List <Entry > children () {
214- return viewableChildren ;
215- }
216-
217208 private List <Entry > superModifiableChildren () {
209+ //? if >=1.21.9 {
218210 // noinspection unchecked
219211 return (List <Entry >) ((AbstractSelectionListAccessor ) this ).getChildren ();
212+ //?} else {
213+ /*return this.children();
214+ */ //?}
220215 }
221216
222217 public void addEntryAtIndex (int index , Entry entry ) {
223218 superModifiableChildren ().add (index , entry );
224- recacheViewableChildren ();
219+ this . repositionEntries ();
225220 }
226221
227222 public void addEntryBelow (Entry below , Entry entry ) {
@@ -239,21 +234,6 @@ public void addEntryBelowWithoutScroll(Entry below, Entry entry) {
239234 setScrollAmount (this .contentHeight () - d );
240235 }
241236
242- //? if >=1.21.9 {
243- @ Override
244- protected void removeEntry (Entry entry ) {
245- super .removeEntry (entry );
246- recacheViewableChildren ();
247- }
248- //?} else {
249- /*@Override
250- public boolean removeEntry(Entry entry) {
251- boolean ret = super.removeEntry(entry);
252- recacheViewableChildren();
253- return ret;
254- }
255- */ //?}
256-
257237 private void setHoverDescription (DescriptionWithName description ) {
258238 if (description != lastHoveredOption ) {
259239 lastHoveredOption = description ;
@@ -273,12 +253,26 @@ protected boolean isValidMouseClick(int button) {
273253 }
274254
275255 public abstract class Entry extends YACLSelectionList .Entry <Entry > {
256+ protected boolean searchQueryMatches = true ;
257+
276258 public Entry () {
277259 super (OptionListWidget .this );
278260 }
279261
262+ public boolean updateSearchQuery (String searchQuery ) {
263+ return this .searchQueryMatches ;
264+ }
265+
280266 public boolean isViewable () {
281- return true ;
267+ return this .searchQueryMatches ;
268+ }
269+
270+ @ Override
271+ public int getHeight () {
272+ if (!isViewable ()) {
273+ return 0 ;
274+ }
275+ return super .getHeight ();
282276 }
283277 }
284278
@@ -314,11 +308,16 @@ public OptionEntry(Option<?> option, ConfigCategory category, OptionGroup group,
314308 } else {
315309 this .resetButton = null ;
316310 }
311+ this .updateHeight ();
317312 }
318313
319314 @ Override
320315 public void renderContent (GuiGraphics graphics , int mouseX , int mouseY , boolean hovered , float deltaTicks ) {
321- this .setHeight (Math .max (widget .getDimension ().height (), resetButton != null ? resetButton .getHeight () : 0 ) + 2 );
316+ if (!this .isViewable ()) {
317+ return ;
318+ }
319+
320+ this .updateHeight ();
322321
323322 widget .setDimension (widget .getDimension ().withY (this .getY ()));
324323
@@ -350,11 +349,20 @@ public boolean charTyped(char chr, int modifiers) {
350349 }
351350
352351 @ Override
353- public boolean isViewable () {
354- return (groupSeparatorEntry == null || groupSeparatorEntry .isExpanded ())
355- && (searchQuery .isEmpty ()
352+ public boolean updateSearchQuery (String searchQuery ) {
353+ return this .searchQueryMatches = searchQuery .isEmpty ()
356354 || groupName .contains (searchQuery )
357- || widget .matchesSearch (searchQuery ));
355+ || widget .matchesSearch (searchQuery );
356+ }
357+
358+ @ Override
359+ public boolean isViewable () {
360+ return super .isViewable ()
361+ && (groupSeparatorEntry == null || groupSeparatorEntry .isExpanded ());
362+ }
363+
364+ private void updateHeight () {
365+ this .setHeight (Math .max (widget .getDimension ().height (), resetButton != null ? resetButton .getHeight () : 0 ) + 2 );
358366 }
359367
360368 @ Override
@@ -403,11 +411,16 @@ private GroupSeparatorEntry(OptionGroup group, Screen screen) {
403411 this .groupExpanded = !group .collapsed ();
404412 this .expandMinimizeButton = new LowProfileButtonWidget (0 , 0 , 20 , 20 , Component .empty (), btn -> onExpandButtonPress ());
405413 updateExpandMinimizeText ();
414+ updateHeight ();
406415 }
407416
408417 @ Override
409418 public void renderContent (GuiGraphics graphics , int mouseX , int mouseY , boolean hovered , float deltaTicks ) {
410- this .setHeight (Math .max (wrappedName .getLineCount (), 1 ) * font .lineHeight + getYPadding () * 2 );
419+ if (!this .isViewable ()) {
420+ return ;
421+ }
422+
423+ this .updateHeight ();
411424
412425 int buttonY = this .getY () + this .getHeight () / 2 - expandMinimizeButton .getHeight () / 2 + 1 ;
413426
@@ -436,7 +449,7 @@ public void setExpanded(boolean expanded) {
436449
437450 this .groupExpanded = expanded ;
438451 updateExpandMinimizeText ();
439- recacheViewableChildren ();
452+ repositionEntries ();
440453 }
441454
442455 protected void onExpandButtonPress () {
@@ -453,8 +466,8 @@ public void setChildEntries(List<? extends Entry> childEntries) {
453466 }
454467
455468 @ Override
456- public boolean isViewable ( ) {
457- return searchQuery .isEmpty () || childEntries .stream ().anyMatch (Entry :: isViewable );
469+ public boolean updateSearchQuery ( String searchQuery ) {
470+ return this . searchQueryMatches = searchQuery .isEmpty () || childEntries .stream ().anyMatch (e -> e . updateSearchQuery ( searchQuery ) );
458471 }
459472
460473 private int getYPadding () {
@@ -468,6 +481,10 @@ public void setFocused(boolean focused) {
468481 setHoverDescription (DescriptionWithName .of (group .name (), group .description ()));
469482 }
470483
484+ private void updateHeight () {
485+ this .setHeight (Math .max (wrappedName .getLineCount (), 1 ) * font .lineHeight + getYPadding () * 2 );
486+ }
487+
471488 @ Override
472489 public @ NotNull List <? extends NarratableEntry > narratables () {
473490 return ImmutableList .of (new NarratableEntry () {
@@ -517,6 +534,10 @@ private ListGroupSeparatorEntry(ListOption<?> group, Screen screen) {
517534
518535 @ Override
519536 public void renderContent (GuiGraphics graphics , int mouseX , int mouseY , boolean hovered , float deltaTicks ) {
537+ if (!this .isViewable ()) {
538+ return ;
539+ }
540+
520541 updateExpandMinimizeText (); // update every render because option could become available/unavailable at any time
521542
522543 super .renderContent (graphics , mouseX , mouseY , hovered , deltaTicks );
@@ -572,9 +593,14 @@ public void renderContent(GuiGraphics graphics, int mouseX, int mouseY, boolean
572593 graphics .drawCenteredString (Minecraft .getInstance ().font , Component .translatable ("yacl.list.empty" ).withStyle (ChatFormatting .DARK_GRAY , ChatFormatting .ITALIC ), this .getX () + this .getWidth () / 2 , this .getY (), -1 );
573594 }
574595
596+ @ Override
597+ public boolean updateSearchQuery (String searchQuery ) {
598+ return this .searchQueryMatches = searchQuery .isEmpty () || groupName .contains (searchQuery );
599+ }
600+
575601 @ Override
576602 public boolean isViewable () {
577- return parent .isExpanded () && ( searchQuery . isEmpty () || groupName . contains ( searchQuery ) );
603+ return parent .isExpanded () && super . isViewable ( );
578604 }
579605
580606 @ Override
0 commit comments