diff options
author | Crendgrim <Crendgrim@users.noreply.github.com> | 2023-07-21 23:57:05 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-21 22:57:05 +0100 |
commit | 8a4d6df67917fee4b600cfad1afb7d39a75d75f1 (patch) | |
tree | c89cad79d18a5a86e5709e42b8d6129b43f54d7f /common/src | |
parent | a25e2d754ae856c0c9d2c8ebb9ea16a7a458cf04 (diff) | |
download | YetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.tar.gz YetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.tar.bz2 YetAnotherConfigLib-8a4d6df67917fee4b600cfad1afb7d39a75d75f1.zip |
Small improvements to ListOption (#89)
Diffstat (limited to 'common/src')
4 files changed, 80 insertions, 8 deletions
diff --git a/common/src/main/java/dev/isxander/yacl3/api/ListOption.java b/common/src/main/java/dev/isxander/yacl3/api/ListOption.java index 8094ee7..0371c35 100644 --- a/common/src/main/java/dev/isxander/yacl3/api/ListOption.java +++ b/common/src/main/java/dev/isxander/yacl3/api/ListOption.java @@ -30,7 +30,16 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { @NotNull ImmutableList<ListOptionEntry<T>> options(); @ApiStatus.Internal - ListOptionEntry<T> insertNewEntryToTop(); + int numberOfEntries(); + + @ApiStatus.Internal + int maximumNumberOfEntries(); + + @ApiStatus.Internal + int minimumNumberOfEntries(); + + @ApiStatus.Internal + ListOptionEntry<T> insertNewEntry(); @ApiStatus.Internal void insertEntry(int index, ListOptionEntry<?> entry); @@ -106,6 +115,24 @@ public interface ListOption<T> extends OptionGroup, Option<List<T>> { Builder<T> available(boolean available); /** + * Sets a minimum size for the list. Once this size is reached, + * no further entries may be removed. + */ + Builder<T> minimumNumberOfEntries(int number); + + /** + * Sets a maximum size for the list. Once this size is reached, + * no further entries may be added. + */ + Builder<T> maximumNumberOfEntries(int number); + + /** + * Dictates if new entries should be added to the end of the list + * rather than the top. + */ + Builder<T> insertEntriesAtEnd(boolean insertAtEnd); + + /** * Adds a flag to the option. * Upon applying changes, all flags are executed. * {@link Option#flags()} diff --git a/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java index 54d58f4..3197c44 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/OptionListWidget.java @@ -485,7 +485,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr this.addListButton = new TooltipButtonWidget(yaclScreen, resetListButton.getX() - 20, -50, 20, 20, Component.literal("+"), Component.translatable("yacl.list.add_top"), btn -> { - group.insertNewEntryToTop(); + group.insertNewEntry(); setExpanded(true); }); @@ -519,7 +519,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr super.updateExpandMinimizeText(); expandMinimizeButton.active = listOption == null || listOption.available(); if (addListButton != null) - addListButton.active = expandMinimizeButton.active; + addListButton.active = expandMinimizeButton.active && listOption.numberOfEntries() < listOption.maximumNumberOfEntries(); } @Override diff --git a/common/src/main/java/dev/isxander/yacl3/gui/controllers/ListEntryWidget.java b/common/src/main/java/dev/isxander/yacl3/gui/controllers/ListEntryWidget.java index 15deff2..7e71cc7 100644 --- a/common/src/main/java/dev/isxander/yacl3/gui/controllers/ListEntryWidget.java +++ b/common/src/main/java/dev/isxander/yacl3/gui/controllers/ListEntryWidget.java @@ -80,7 +80,7 @@ public class ListEntryWidget extends AbstractWidget implements ContainerEventHan } protected void updateButtonStates() { - removeButton.active = listOption.available(); + removeButton.active = listOption.available() && listOption.numberOfEntries() > listOption.minimumNumberOfEntries(); moveUpButton.active = listOption.indexOf(listOptionEntry) > 0 && listOption.available(); moveDownButton.active = listOption.indexOf(listOptionEntry) < listOption.options().size() - 1 && listOption.available(); } diff --git a/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java b/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java index 82f5576..25ff7ba 100644 --- a/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java +++ b/common/src/main/java/dev/isxander/yacl3/impl/ListOptionImpl.java @@ -25,12 +25,15 @@ public final class ListOptionImpl<T> implements ListOption<T> { private final List<ListOptionEntry<T>> entries; private final boolean collapsed; private boolean available; + private final int minimumNumberOfEntries; + private final int maximumNumberOfEntries; + private final boolean insertEntriesAtEnd; private final ImmutableSet<OptionFlag> flags; private final EntryFactory entryFactory; private final List<BiConsumer<Option<List<T>>, List<T>>> listeners; private final List<Runnable> refreshListeners; - public ListOptionImpl(@NotNull Component name, @NotNull OptionDescription description, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available, Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) { + public ListOptionImpl(@NotNull Component name, @NotNull OptionDescription description, @NotNull Binding<List<T>> binding, @NotNull T initialValue, @NotNull Function<ListOptionEntry<T>, Controller<T>> controllerFunction, ImmutableSet<OptionFlag> flags, boolean collapsed, boolean available, int minimumNumberOfEntries, int maximumNumberOfEntries, boolean insertEntriesAtEnd, Collection<BiConsumer<Option<List<T>>, List<T>>> listeners) { this.name = name; this.description = description; this.binding = binding; @@ -40,6 +43,9 @@ public final class ListOptionImpl<T> implements ListOption<T> { this.collapsed = collapsed; this.flags = flags; this.available = available; + this.minimumNumberOfEntries = minimumNumberOfEntries; + this.maximumNumberOfEntries = maximumNumberOfEntries; + this.insertEntriesAtEnd = insertEntriesAtEnd; this.listeners = new ArrayList<>(); this.listeners.addAll(listeners); this.refreshListeners = new ArrayList<>(); @@ -98,9 +104,14 @@ public final class ListOptionImpl<T> implements ListOption<T> { } @Override - public ListOptionEntry<T> insertNewEntryToTop() { + public ListOptionEntry<T> insertNewEntry() { ListOptionEntry<T> newEntry = entryFactory.create(initialValue); - entries.add(0, newEntry); + if (insertEntriesAtEnd) { + entries.add(newEntry); + } else { + // insert at top + entries.add(0, newEntry); + } onRefresh(); return newEntry; } @@ -163,6 +174,19 @@ public final class ListOptionImpl<T> implements ListOption<T> { } @Override + public int numberOfEntries() { + return this.entries.size(); + } + @Override + public int maximumNumberOfEntries() { + return this.maximumNumberOfEntries; + } + @Override + public int minimumNumberOfEntries() { + return this.minimumNumberOfEntries; + } + + @Override public void addListener(BiConsumer<Option<List<T>>, List<T>> changedListener) { this.listeners.add(changedListener); } @@ -213,6 +237,9 @@ public final class ListOptionImpl<T> implements ListOption<T> { private T initialValue; private boolean collapsed = false; private boolean available = true; + private int minimumNumberOfEntries = 0; + private int maximumNumberOfEntries = Integer.MAX_VALUE; + private boolean insertEntriesAtEnd = false; private final List<BiConsumer<Option<List<T>>, List<T>>> listeners = new ArrayList<>(); @Override @@ -280,6 +307,24 @@ public final class ListOptionImpl<T> implements ListOption<T> { } @Override + public Builder<T> minimumNumberOfEntries(int number) { + this.minimumNumberOfEntries = number; + return this; + } + + @Override + public Builder<T> maximumNumberOfEntries(int number) { + this.maximumNumberOfEntries = number; + return this; + } + + @Override + public Builder<T> insertEntriesAtEnd(boolean insertAtEnd) { + this.insertEntriesAtEnd = insertAtEnd; + return this; + } + + @Override public Builder<T> flag(@NotNull OptionFlag... flag) { Validate.notNull(flag, "`flag` must not be null"); @@ -319,7 +364,7 @@ public final class ListOptionImpl<T> implements ListOption<T> { Validate.notNull(binding, "`binding` must not be null"); Validate.notNull(initialValue, "`initialValue` must not be null"); - return new ListOptionImpl<>(name, description, binding, initialValue, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, listeners); + return new ListOptionImpl<>(name, description, binding, initialValue, controllerFunction, ImmutableSet.copyOf(flags), collapsed, available, minimumNumberOfEntries, maximumNumberOfEntries, insertEntriesAtEnd, listeners); } } } |