From 27843ae0fdb64b4e2552a365269c208d7ee578df Mon Sep 17 00:00:00 2001 From: Christopher Schnick Date: Sat, 15 Oct 2022 14:09:20 +0200 Subject: [PATCH] Small fixes --- extension/build.gradle | 8 ++- .../io/xpipe/extension/comp/ChoiceComp.java | 14 +++-- .../xpipe/extension/comp/ChoicePaneComp.java | 9 ++- .../extension/comp/FancyTooltipAugment.java | 2 +- .../io/xpipe/extension/comp/FilterComp.java | 10 ++- .../extension/util/DynamicOptionsBuilder.java | 61 ++++++++++--------- 6 files changed, 65 insertions(+), 39 deletions(-) diff --git a/extension/build.gradle b/extension/build.gradle index 8a9bd1454..52114ba40 100644 --- a/extension/build.gradle +++ b/extension/build.gradle @@ -29,11 +29,15 @@ dependencies { compileOnly 'net.synedra:validatorfx:0.3.1' compileOnly 'org.junit.jupiter:junit-jupiter-api:5.9.0' compileOnly 'com.jfoenix:jfoenix:9.0.10' - compileOnly 'io.xpipe:fxcomps:0.2.2' compileOnly 'org.controlsfx:controlsfx:11.1.1' compileOnly 'org.apache.commons:commons-lang3:3.12.0' -} + if (project(':fxcomps') != null) { + compileOnly project(':fxcomps') + } else { + compileOnly 'io.xpipe:fxcomps:0.3.2' + } +} apply from: 'publish.gradle' diff --git a/extension/src/main/java/io/xpipe/extension/comp/ChoiceComp.java b/extension/src/main/java/io/xpipe/extension/comp/ChoiceComp.java index 6b97df3dd..ccfbceede 100644 --- a/extension/src/main/java/io/xpipe/extension/comp/ChoiceComp.java +++ b/extension/src/main/java/io/xpipe/extension/comp/ChoiceComp.java @@ -33,7 +33,7 @@ public class ChoiceComp extends Comp>> { public ChoiceComp(Property value, ObservableValue>> range) { this.value = value; - this.range = range; + this.range = PlatformThread.sync(range); } @Override @@ -60,15 +60,21 @@ public class ChoiceComp extends Comp>> { throw new UnsupportedOperationException(); } }); - SimpleChangeListener.apply(PlatformThread.sync(range), c -> { - + SimpleChangeListener.apply(range, c -> { var list = FXCollections.observableArrayList(c.keySet()); if (!list.contains(null)) { list.add(null); } cb.setItems(list); }); - PlatformThread.connect(value, cb.valueProperty()); + + cb.valueProperty().addListener((observable, oldValue, newValue) -> { + value.setValue(newValue); + }); + SimpleChangeListener.apply(value, val -> { + PlatformThread.runLaterIfNeeded(() -> cb.valueProperty().set(val)); + }); + cb.getStyleClass().add("choice-comp"); return new SimpleCompStructure<>(cb); } diff --git a/extension/src/main/java/io/xpipe/extension/comp/ChoicePaneComp.java b/extension/src/main/java/io/xpipe/extension/comp/ChoicePaneComp.java index 5aa5f3f51..d18c28b7c 100644 --- a/extension/src/main/java/io/xpipe/extension/comp/ChoicePaneComp.java +++ b/extension/src/main/java/io/xpipe/extension/comp/ChoicePaneComp.java @@ -5,6 +5,7 @@ import io.xpipe.fxcomps.Comp; import io.xpipe.fxcomps.CompStructure; import io.xpipe.fxcomps.SimpleCompStructure; import io.xpipe.fxcomps.util.PlatformThread; +import io.xpipe.fxcomps.util.SimpleChangeListener; import javafx.beans.property.Property; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; @@ -65,7 +66,13 @@ public class ChoicePaneComp extends Comp> { } }); - PlatformThread.connect(selected, cb.valueProperty()); + cb.valueProperty().addListener((observable, oldValue, newValue) -> { + selected.setValue(newValue); + }); + SimpleChangeListener.apply(selected, val -> { + PlatformThread.runLaterIfNeeded(() -> cb.valueProperty().set(val)); + }); + vbox.getStyleClass().add("choice-pane-comp"); return new SimpleCompStructure<>(vbox); diff --git a/extension/src/main/java/io/xpipe/extension/comp/FancyTooltipAugment.java b/extension/src/main/java/io/xpipe/extension/comp/FancyTooltipAugment.java index e85ed9744..e688a5063 100644 --- a/extension/src/main/java/io/xpipe/extension/comp/FancyTooltipAugment.java +++ b/extension/src/main/java/io/xpipe/extension/comp/FancyTooltipAugment.java @@ -3,9 +3,9 @@ package io.xpipe.extension.comp; import com.jfoenix.controls.JFXTooltip; import io.xpipe.extension.I18n; import io.xpipe.fxcomps.CompStructure; -import io.xpipe.fxcomps.Shortcuts; import io.xpipe.fxcomps.augment.Augment; import io.xpipe.fxcomps.util.PlatformThread; +import io.xpipe.fxcomps.util.Shortcuts; import javafx.beans.value.ObservableValue; import javafx.scene.Node; import javafx.scene.layout.Region; diff --git a/extension/src/main/java/io/xpipe/extension/comp/FilterComp.java b/extension/src/main/java/io/xpipe/extension/comp/FilterComp.java index 4c57a26f2..8efe7de5d 100644 --- a/extension/src/main/java/io/xpipe/extension/comp/FilterComp.java +++ b/extension/src/main/java/io/xpipe/extension/comp/FilterComp.java @@ -3,6 +3,7 @@ package io.xpipe.extension.comp; import io.xpipe.fxcomps.Comp; import io.xpipe.fxcomps.CompStructure; import io.xpipe.fxcomps.util.PlatformThread; +import io.xpipe.fxcomps.util.SimpleChangeListener; import javafx.beans.binding.Bindings; import javafx.beans.property.Property; import javafx.scene.Node; @@ -41,7 +42,14 @@ public class FilterComp extends Comp { var bgLabel = new Label("Search ...", fi); bgLabel.getStyleClass().add("background"); var filter = new TextField(); - PlatformThread.connect(filterText, filter.textProperty()); + + SimpleChangeListener.apply(filterText, val -> { + PlatformThread.runLaterIfNeeded(() -> filter.setText(val)); + }); + filter.textProperty().addListener((observable, oldValue, newValue) -> { + filterText.setValue(newValue); + }); + bgLabel.visibleProperty().bind(Bindings.createBooleanBinding(() -> (filter.getText() == null || filter.getText().isEmpty()), filter.textProperty(), filter.focusedProperty())); diff --git a/extension/src/main/java/io/xpipe/extension/util/DynamicOptionsBuilder.java b/extension/src/main/java/io/xpipe/extension/util/DynamicOptionsBuilder.java index f9a5d4304..88e25691d 100644 --- a/extension/src/main/java/io/xpipe/extension/util/DynamicOptionsBuilder.java +++ b/extension/src/main/java/io/xpipe/extension/util/DynamicOptionsBuilder.java @@ -6,6 +6,7 @@ import io.xpipe.core.util.SecretValue; import io.xpipe.extension.I18n; import io.xpipe.extension.comp.*; import io.xpipe.fxcomps.Comp; +import io.xpipe.fxcomps.CompStructure; import javafx.beans.property.Property; import javafx.beans.value.ObservableValue; import javafx.scene.control.Label; @@ -18,7 +19,7 @@ import java.util.List; import java.util.Map; import java.util.function.Supplier; -public class DynamicOptionsBuilder { +public class DynamicOptionsBuilder { private final List entries = new ArrayList<>(); private final List> props = new ArrayList<>(); @@ -41,35 +42,35 @@ public class DynamicOptionsBuilder { this.wrap = false; this.title = title; } - public DynamicOptionsBuilder addTitle(String titleKey) { + public DynamicOptionsBuilder addTitle(String titleKey) { return addTitle(I18n.observable(titleKey)); } - public DynamicOptionsBuilder addTitle(ObservableValue title) { + public DynamicOptionsBuilder addTitle(ObservableValue title) { entries.add(new DynamicOptionsComp.Entry(null, Comp.of(() -> new Label(title.getValue())).styleClass("title-header"))); return this; } - public DynamicOptionsBuilder makeLazy() { + public DynamicOptionsBuilder makeLazy() { var p = props.get(props.size() - 1); props.remove(p); lazyProperties.add(p); return this; } - public DynamicOptionsBuilder decorate(Check c) { + public DynamicOptionsBuilder decorate(Check c) { entries.get(entries.size() - 1).comp().apply(s -> c.decorates(s.get())); return this; } - public DynamicOptionsBuilder nonNull(Validator v) { + public DynamicOptionsBuilder nonNull(Validator v) { var e = entries.get(entries.size() - 1); var p = props.get(props.size() - 1); return decorate(Validators.nonNull(v, e.name(), p)); } - public DynamicOptionsBuilder addNewLine(Property prop) { + public DynamicOptionsBuilder addNewLine(Property prop) { var map = new LinkedHashMap>(); for (var e : NewLine.values()) { map.put(e, I18n.observable("extension." + e.getId())); @@ -80,63 +81,63 @@ public class DynamicOptionsBuilder { return this; } - public DynamicOptionsBuilder addCharacter(Property prop, ObservableValue name, Map> names) { + public DynamicOptionsBuilder addCharacter(Property prop, ObservableValue name, Map> names) { var comp = new CharChoiceComp(prop, names, null); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addCharacter(Property prop, ObservableValue name, Map> names, ObservableValue customName) { + public DynamicOptionsBuilder addCharacter(Property prop, ObservableValue name, Map> names, ObservableValue customName) { var comp = new CharChoiceComp(prop, names, customName); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addToggle(Property prop, ObservableValue name, Map> names) { + public DynamicOptionsBuilder addToggle(Property prop, ObservableValue name, Map> names) { var comp = new ToggleGroupComp<>(prop, names); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addChoice(Property prop, ObservableValue name, Map> names) { + public DynamicOptionsBuilder addChoice(Property prop, ObservableValue name, Map> names) { var comp = new ChoiceComp<>(prop, names); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addChoice(Property prop, ObservableValue name, ObservableValue>> names) { + public DynamicOptionsBuilder addChoice(Property prop, ObservableValue name, ObservableValue>> names) { var comp = new ChoiceComp<>(prop, names); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addCharset(Property prop) { + public DynamicOptionsBuilder addCharset(Property prop) { var comp = new CharsetChoiceComp(prop); entries.add(new DynamicOptionsComp.Entry(I18n.observable("extension.charset"), comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addStringArea(String nameKey, Property prop) { + public DynamicOptionsBuilder addStringArea(String nameKey, Property prop) { var comp = new TextAreaComp(prop); entries.add(new DynamicOptionsComp.Entry(I18n.observable(nameKey), comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addString(String nameKey, Property prop) { + public DynamicOptionsBuilder addString(String nameKey, Property prop) { var comp = new TextFieldComp(prop); entries.add(new DynamicOptionsComp.Entry(I18n.observable(nameKey), comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addLazyString(String nameKey, Property prop, Property lazy) { + public DynamicOptionsBuilder addLazyString(String nameKey, Property prop, Property lazy) { var comp = new TextFieldComp(prop, lazy); entries.add(new DynamicOptionsComp.Entry(I18n.observable(nameKey), comp)); props.add(prop); @@ -144,50 +145,50 @@ public class DynamicOptionsBuilder { return this; } - public DynamicOptionsBuilder addString(ObservableValue name, Property prop) { + public DynamicOptionsBuilder addString(ObservableValue name, Property prop) { var comp = new TextFieldComp(prop); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addComp(Comp comp) { + public DynamicOptionsBuilder addComp(Comp comp) { return addComp((ObservableValue) null, comp, null); } - public DynamicOptionsBuilder addComp(Comp comp, Property prop) { + public DynamicOptionsBuilder addComp(Comp comp, Property prop) { return addComp((ObservableValue) null, comp, prop); } - public DynamicOptionsBuilder addComp(String nameKey, Comp comp, Property prop) { + public DynamicOptionsBuilder addComp(String nameKey, Comp comp, Property prop) { return addComp(I18n.observable(nameKey), comp, prop); } - public DynamicOptionsBuilder addComp(ObservableValue name, Comp comp, Property prop) { + public DynamicOptionsBuilder addComp(ObservableValue name, Comp comp, Property prop) { entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addSecret(String nameKey, Property prop) { + public DynamicOptionsBuilder addSecret(String nameKey, Property prop) { return addSecret(I18n.observable(nameKey), prop); } - public DynamicOptionsBuilder addSecret(ObservableValue name, Property prop) { + public DynamicOptionsBuilder addSecret(ObservableValue name, Property prop) { var comp = new SecretFieldComp(prop); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addInteger(ObservableValue name, Property prop) { + public DynamicOptionsBuilder addInteger(ObservableValue name, Property prop) { var comp = new IntFieldComp(prop); entries.add(new DynamicOptionsComp.Entry(name, comp)); props.add(prop); return this; } - public DynamicOptionsBuilder addInteger(String nameKey, Property prop) { + public DynamicOptionsBuilder addInteger(String nameKey, Property prop) { var comp = new IntFieldComp(prop); entries.add(new DynamicOptionsComp.Entry(I18n.observable(nameKey), comp)); props.add(prop); @@ -195,7 +196,7 @@ public class DynamicOptionsBuilder { } @SafeVarargs - public final DynamicOptionsBuilder bind(Supplier creator, Property... toSet) { + public final DynamicOptionsBuilder bind(Supplier creator, Property... toSet) { props.forEach(prop -> { prop.addListener((c, o, n) -> { for (Property p : toSet) { @@ -209,7 +210,7 @@ public class DynamicOptionsBuilder { return this; } - public DynamicOptionsBuilder bindLazy(Supplier creator, Property toLazySet) { + public DynamicOptionsBuilder bindLazy(Supplier creator, Property toLazySet) { lazyProperties.forEach(prop -> { prop.addListener((c,o,n) -> { toLazySet.setValue(creator.get()); @@ -220,7 +221,7 @@ public class DynamicOptionsBuilder { return this; } - public final DynamicOptionsBuilder bindChoice(Supplier> creator, Property toSet) { + public final DynamicOptionsBuilder bindChoice(Supplier> creator, Property toSet) { props.forEach(prop -> { prop.addListener((c,o,n) -> { toSet.unbind(); @@ -231,7 +232,7 @@ public class DynamicOptionsBuilder { return this; } - public Comp buildComp() { + public Comp> buildComp() { if (title != null) { entries.add(0, new DynamicOptionsComp.Entry(null, Comp.of(() -> new Label(title.getValue())).styleClass("title-header"))); } @@ -239,7 +240,7 @@ public class DynamicOptionsBuilder { } - public Region build() { + public Region build() { return buildComp().createRegion(); } }