From 4936ff3f170a4dba808e41169bf13cfa046f0769 Mon Sep 17 00:00:00 2001 From: isXander Date: Sun, 18 Sep 2022 22:25:56 +0100 Subject: instant application property --- src/main/java/dev/isxander/yacl/api/Option.java | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'src/main/java') diff --git a/src/main/java/dev/isxander/yacl/api/Option.java b/src/main/java/dev/isxander/yacl/api/Option.java index b17743e..1df90a8 100644 --- a/src/main/java/dev/isxander/yacl/api/Option.java +++ b/src/main/java/dev/isxander/yacl/api/Option.java @@ -123,6 +123,8 @@ public interface Option { private boolean available = true; + private boolean instant = false; + private final Set flags = new HashSet<>(); private final Class typeClass; @@ -249,6 +251,17 @@ public interface Option { return this; } + /** + * Instantly invokes the binder's setter when modified in the GUI. + * Prevents the user from undoing the change + *

+ * Does not support {@link Option#flags()}! + */ + public Builder instant(boolean instant) { + this.instant = instant; + return this; + } + /** * Dictates whether the option should require a restart. * {@link Option#requiresRestart()} @@ -264,6 +277,7 @@ public interface Option { public Option build() { Validate.notNull(controlGetter, "`control` must not be null when building `Option`"); Validate.notNull(binding, "`binding` must not be null when building `Option`"); + Validate.isTrue(!instant || flags.isEmpty(), "instant application does not support option flags"); Function concatenatedTooltipGetter = value -> { MutableText concatenatedTooltip = Text.empty(); @@ -278,7 +292,11 @@ public interface Option { return concatenatedTooltip; }; - return new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); + OptionImpl option = new OptionImpl<>(name, concatenatedTooltipGetter, controlGetter, binding, available, ImmutableSet.copyOf(flags), typeClass); + if (instant) { + option.addListener((opt, pendingValue) -> opt.applyValue()); + } + return option; } } } -- cgit