Build a UI kit
A skin gives one control its look; a UI kit gathers a skin for each control behind one interface. This guide writes two kits for a checkbox and a slider, and switches the kit of a screen while it runs.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- Abstract controls, their parts and how a skin sets them: Give your controls a look.
- Binding a control to a signal: Controls.
- Rebuilding the children of a node when a signal changes: Rebuilding the tree.
1. Write a skin for each control
Section titled “1. Write a skin for each control”A skin extends a control of JOID, such as CheckboxNode or IntegerSliderNode, and gives it its parts in its constructor.
The round checkbox sets a box and a mark with rounded corners:
private static final Color ELEMENT = new Color(38, 43, 53).lock();private static final Color ACCENT = new Color(91, 140, 255).lock();
protected RoundGuideCheckboxNode(final double x, final double y, final double width, final double height) { super(x, y, width, height);
final RectNode box = RectNode .create(0, 0, 0, 0) .color(RoundGuideCheckboxNode.ELEMENT) .effect(RoundedNodeEffect.create(12F)) .effect(BorderNodeEffect.create(RoundGuideCheckboxNode.ACCENT, 2F)); final RectNode mark = RectNode .create(0, 0, width / 2D, height / 2D) .color(RoundGuideCheckboxNode.ACCENT) .effect(RoundedNodeEffect.create(6F));
super.box(box); super.mark(mark);}Full file: RoundGuideCheckboxNode.java
Write the round slider the same way, with a pill-shaped track and a CircleNode as its thumb, then a plain checkbox and a plain slider with square parts: PlainGuideCheckboxNode, PlainGuideSliderNode, RoundGuideCheckboxNode and RoundGuideSliderNode in the examples.
Check: put RoundGuideCheckboxNode.create(100, 100, 50, 50) in a UI: it checks and unchecks on click.
2. Gather the skins behind an interface
Section titled “2. Gather the skins behind an interface”A screen that names the skins would have to change to switch kits. Declare what a kit provides in an interface:
package com.example.guides;
import dev.joid.lib.color.Color;import dev.joid.lib.ui.node.impl.control.checkbox.CheckboxNode;import dev.joid.lib.ui.node.impl.control.slider.IntegerSliderNode;
public interface IGuideKit {
public CheckboxNode checkbox(final double x, final double y); public IntegerSliderNode slider(final double x, final double y, final double width);
public Color getSurface();
}Each kit implements it with its own skins and colors:
public class RoundGuideKit implements IGuideKit {
private static final Color SURFACE = new Color(54, 61, 77).lock();
@Override public CheckboxNode checkbox(final double x, final double y) { return RoundGuideCheckboxNode.create(x, y, 50, 50); }
@Override public IntegerSliderNode slider(final double x, final double y, final double width) { return RoundGuideSliderNode.create(x, y, width, 30); }
@Override public Color getSurface() { return RoundGuideKit.SURFACE; }
}PlainGuideKit returns the plain skins with the same signatures, and a dark surface.
Check: put new RoundGuideKit().checkbox(100, 100) in a UI: it shows the round checkbox of step 1.
3. Build the screen from the kit
Section titled “3. Build the screen from the kit”Keep the kit in a signal, and the values in signals of their own, outside the controls:
private final IntegerSignal volume = new IntegerSignal(60);private final BooleanSignal checked = new BooleanSignal(true);private final Signal<IGuideKit> kit = new Signal<>(new PlainGuideKit());The card rebuilds its children each time the kit changes (Rebuilding the tree), and its color derives from the kit with map(...).
The controls bind the same signals in each rebuild, so the values survive the change of kit:
RectNode.create(600, 330, 720, 340).color(this.kit.map(IGuideKit::getSurface)).watch(this.kit, WatchProperty.CLEAR_CHILDREN, WatchProperty.BODY).body(card -> { final IGuideKit current = this.kit.peek(); current.checkbox(60, 60).checked(this.checked).attach(card); current.slider(60, 200, 600).range(0, 100).value(this.volume).attach(card);}).attach(this);Check: the card shows the plain checkbox and slider.
4. Switch kits at run time
Section titled “4. Switch kits at run time”A click sets the kit signal, and the card rebuilds itself:
RectNode.create(600, 710, 340, 70).color(KitGuideUI.ELEMENT).label(Text.create("Plain kit", info)).onClick(event -> this.kit.set(new PlainGuideKit())).attach(this);
RectNode.create(980, 710, 340, 70).color(KitGuideUI.ELEMENT).label(Text.create("Round kit", info)).onClick(event -> this.kit.set(new RoundGuideKit())).attach(this);Check: uncheck the box, move the slider, then click Round kit. The card turns a lighter grey, the controls change their look, the box stays unchecked and the slider keeps its value.
If it does not work
Section titled “If it does not work”- Attaching a skin throws
IllegalStateException: The … has no mark, give it one with mark(...): the skin does not set that part in its constructor. - The values reset when the kit changes: the controls received values instead of signals; bind them to signals declared outside the rebuilt body.
- Nothing changes on click: the card does not watch the kit signal, or the body reads the kit outside the card.
- A screen keeps an old skin: it creates the skin directly; go through the kit interface.
