Rebuilding the tree
A setter changes a property of a node that exists. When a change adds or removes nodes, such as a list that grows, the children of a node must be built again. When the data is not there yet, a node must wait for it.
You will learn
- How to build the children again when a signal changes
- How to choose what a change does to a node
- How to keep a node behind a placeholder until its data is ready
Build the children again when a list changes
Section titled “Build the children again when a list changes”The list lives in a ListSignal:
private final ListSignal<String> items = new ListSignal<>(Arrays.asList("First item", "Second item"));The body of the list builds one row per item, and Node.watch runs it again on each change of items:
FlexNode.vertical(660, 340, 600).margin(10D).watch(this.items, WatchProperty.CLEAR_CHILDREN, WatchProperty.BODY).body(flex -> { for (final String item : this.items.peek()) { RectNode .create(0, 0, 600, 60) .color(RebuildUI.ELEMENT) .body(row -> { TextNode.create(20, 30).text(Text.create(item, info)).anchorY(Align.CENTER).attach(row); }) .attach(flex); }}).attach(this);The buttons only change the signal:
RectNode.create(660, 240, 280, 60).color(RebuildUI.ACCENT).onClick(event -> this.items.add("Item " + (this.items.peek().size() + 1))).label(Text.create("Add", label)).attach(this);
RectNode.create(980, 240, 280, 60).color(RebuildUI.ACCENT).onClick(event -> this.items.remove(this.items.peek().size() - 1)).visible(!this.items.isEmpty()).label(Text.create("Remove", label)).attach(this);items.
A body runs once, when the node is created.
Without the watch line, “Add” changes the signal and nothing on screen.
With it, each change of items runs CLEAR_CHILDREN, which removes the children, then BODY, which runs the body again.
The body reads the list with peek(), which reads without following: the watch already decides when the body runs again.
Read with peek() what only decides which nodes the body builds, and keep get() for the values that a setter follows.
Use watch only when nodes come and go.
A text, a color, a size or a visibility that depends on a signal follows it through its setter (Signals), as the visible(...) of the “Remove” button does.
Choose what a change does
Section titled “Choose what a change does”watch(signal, properties...) applies each WatchProperty in the order you list them, on each change of the signal:
| Property | What it does |
|---|---|
WatchProperty.CLEAR_CHILDREN | Removes every child of the node |
WatchProperty.BODY | Runs the body of the node again |
A lambda (node, signal) -> ... | Runs your own action with the node and the signal |
WatchProperty is a functional interface, so an action of your own is a lambda:
.watch(this.size, (node, signal) -> node.width(60D + this.size.peek() * 60D))Node.onWatch calls your listener after the properties of each watch of the node ran; watch(signal) with no property only calls it.
When a property throws, JOID warns once and the next properties of the watch still apply.
watch(signal, condition, properties...) tests the condition on each change, and ignores the change while the condition returns false.
A watch listens while its node is in an open UI: when the node comes back after a change that it missed, the watch applies once.
To build again on several signals, watch one derived value that reads them all (Derived values): the body runs once per change of the result, whatever the number of signals that changed.
Wait for data before showing a node
Section titled “Wait for data before showing a node”A profile comes from a server, a file or a long computation. Until it arrives, the node that shows it waits, and a placeholder takes its place.
The signal starts without a value:
private final Signal<String> profile = new Signal<>();Node.wait gives the condition the node waits for, and Node.skeleton gives the node drawn in its place meanwhile:
RectNode.create(660, 390, 600, 300).color(WaitUI.SURFACE).effect(RoundedNodeEffect.create(12F)).effect(BorderNodeEffect.create(WaitUI.ELEMENT, 1.5F)).wait(card -> this.profile.isPresent()).skeleton(card -> RectNode.create(0, 0, card.getWidth(), card.getHeight()).color(WaitUI.ACCENT)).body(card -> { TextNode.create(40, 40).text(Text.create(this.profile.map(name -> "Name: " + name), info)).attach(card);}).attach(this);Here, a task of the UI sets the value after two seconds:
Task.create(2000L).onRun(event -> this.profile.set("Ada Lovelace")).attach(this);profile holds no value, JOID draws the skeleton in place of the card; once it holds one, the card and its children appear.
The body runs at once, like any body: the text exists from the start and follows profile.
Until every condition passes, the node is not mounted: JOID draws the skeleton in place of the node and its children, and the skeleton receives the mouse in its place.
JOID tests the conditions on every frame, so a node waits for anything you can test: a signal that holds a value, a future that completed, a moment on the clock.
When a condition fails again later, the skeleton comes back.
A node with several wait(...) mounts when they all pass, and a node whose parent is not mounted is not mounted either.
Node.isMounted tells whether the node and its parents passed their conditions, and onMount runs each time the node mounts.
Without a skeleton, the node draws its loading shape instead: a rectangle, a circle for a CircleNode, in Color.LOADING, a grey that pulses slowly (Colors).
A FlexNode, a GridNode or a ContainerNode draws nothing, so the loading shapes of its children show the layout to come.
watchapplies its properties on each change of a signal;CLEAR_CHILDRENthenBODYbuilds the children again.- Use
watchonly when nodes come and go; a value that depends on a signal follows it through its setter. - In the body, read with
peek()what decides which nodes to build. - A
WatchPropertyis a lambda(node, signal) -> ...; a condition makes the watch ignore the changes while it returnsfalse. waitkeeps a node unmounted until its predicate returnstrue, tested on every frame.- The skeleton takes the place of a waiting node; without one, the node draws its loading shape.
Try it
Section titled “Try it”Numbered rows.
In RebuildUI, number the rows: “1. First item”, “2. Second item”…
Solution
The loop can count: the body runs again on each change, so the numbers follow.
for (int index = 0; index < this.items.peek().size(); index++) { final String text = (index + 1) + ". " + this.items.peek().get(index);Then build the row text with Text.create: Text.create(text, info).
A minimum wait.
In WaitUI, keep the skeleton for at least three seconds after the UI opens, even when the profile arrives sooner.
Solution
Add a second condition, fed by a task:
final BooleanSignal shown = new BooleanSignal(false);Task.create(3000L).onRun(event -> shown.set(true)).attach(this);Then add .wait(card -> shown.get()) to the card: it mounts when both conditions pass.
