Skip to content

Hover, tooltips and cursors

The pointer over a node is the first sign that the node does something. JOID tracks which node the pointer hovers, animates the change, and shows a tooltip or a cursor of your choice.

You will learn

  • How to change the look of a node under the pointer, with an animation
  • Which nodes count as hovered
  • How to react when the pointer enters and leaves a node
  • How to show a tooltip made of text, of nodes or of your own drawing
  • How to set the cursor over a node

RectNode.hoveredColor gives the color of the node under the pointer. JOID blends the two colors over Node.hoverDuration milliseconds, along the curve of Node.hoverEquation:

RectNode
.create(60, 100, 220, 60)
.color(HoverUI.ELEMENT)
.hoveredColor(HoverUI.HOVER)
.hoverDuration(300L)
.hoverEquation(TweenEquations.QUAD_OUT)
.label(Text.create("Shop", info))
.attach(card);

Full file: HoverUI.java

The color follows the hover progress, both when the pointer enters and when it leaves.

The hover progress runs from 0F (not hovered) to 1F (hovered). Node.hoverValue multiplies a value by it, to animate any property of your own. It reads the hover animation of the node, a signal, so super.hoverValue(...) in a node of your own follows it. On a node held in a local variable, as here, give the setter a lambda (What JOID follows):

final RectNode link = RectNode
.create(320, 100, 220, 60)
.color(HoverUI.ACCENT)
.label(Text.create("Settings", dark))
.attach(card);
RectNode
.create(320, 170, 0, 4)
.color(HoverUI.ACCENT)
.width(() -> (double) link.hoverValue(220F))
.interactive(false)
.attach(card);

Full file: HoverUI.java

The underline is 0 px wide at rest and 220 px wide once the button is hovered.

CircleNode and ResourceNode have a hoveredColor too. The easing curves of TweenEquations all work as a hoverEquation.

The pointer hovers the frontmost node under it that is interactive, and every parent of that node: Node.isHovered is true for all of them. UI.getHoveredNode returns the frontmost one, or null. JOID finds the hovered nodes again on every frame and on every mouse event, so a click reaches the node under the pointer at that moment, even when no frame was drawn since the pointer moved.

  • A node set with interactive(false) lets the pointer through to the node behind it (Events).
  • A node set with enabled(false) is hovered, but its hover animation stays at rest.
  • Only the top screen or popup, and the top overlay that takes input, have hovered nodes (How UIs stack).
final IntegerSignal entries = new IntegerSignal(0);
final DoubleSignal dotX = new DoubleSignal(150D);
final DoubleSignal dotY = new DoubleSignal(100D);
RectNode
.create(60, 40, 300, 200)
.color(EnterUI.ELEMENT)
.onHoverStart(event -> entries.increment())
.onHover(event -> {
dotX.set(event.getMouseX() - event.getNode().getAbsoluteX());
dotY.set(event.getMouseY() - event.getNode().getAbsoluteY());
})
.onHoverEnd(event -> {
dotX.set(150D);
dotY.set(100D);
})
.body(area -> {
CircleNode
.create(0, 0, 20)
.color(EnterUI.ACCENT)
.x(dotX.get() - 10D)
.y(dotY.get() - 10D)
.interactive(false)
.attach(area);
})
.attach(card);

Full file: EnterUI.java

The dot follows the pointer through onHover, and goes back to the center through onHoverEnd.

Each listener receives a NodeHoverEvent with the position of the pointer in design pixels. These events run for a disabled node too.

A tooltip appears next to the pointer while the node is hovered. Node.hover takes a line of text, or a list of lines:

RectNode
.create(60, 60, 200, 60)
.color(TooltipUI.ELEMENT)
.onClick(event -> coins.add(10))
.hover(() -> "Coins: " + coins.peek())
.label(Text.create("Wallet", info))
.attach(card);
RectNode
.create(300, 60, 200, 60)
.color(TooltipUI.ELEMENT)
.hover(() -> Arrays.asList("Shop", "Opens at noon"))
.label(Text.create("Shop", info))
.attach(card);

Full file: TooltipUI.java

The supplier of a tooltip runs on every frame the tooltip shows, so the line is always up to date.

A supplier that returns null, or a list supplier that returns an empty list, adds no tooltip on that frame, and the nearest parent with a tooltip shows its own. The UI bridge draws text tooltips, with the font of WindowUIBridge.hoverInfo: set it once when you start JOID (Style the window UI bridge).

A tooltip can also be made of nodes, or of your own drawing. NodeHoverElement shows a node, and its factory says where:

FactoryWhere the tooltip sits
follow(node)At the pointer, offset by the position of the node
relative(node)At the hovered node, offset by the position of the node
fixed(node)At the position of the node in the design canvas
RectNode
.create(180, 140, 200, 60)
.color(TooltipUI.ELEMENT)
.hover(NodeHoverElement.relative(RectNode.create(0, -60, 200, 50).color(TooltipUI.ACCENT).label(Text.create("Up", up))))
.label(Text.create("Profile", info))
.attach(card);

Full file: TooltipUI.java

A relative tooltip stays at the same place over the node, wherever the pointer is on it.

CustomHoverElement has the same three factories and takes your drawing: the origin of the drawing is the point that the factory names, here the pointer.

RectNode
.create(180, 140, 200, 60)
.color(TooltipUI.ELEMENT)
.hover(CustomHoverElement.follow((node, mouseX, mouseY) -> {
DrawUtils.SHAPE.drawRect(-16D, -1.5D, 32D, 3D, TooltipUI.ACCENT);
DrawUtils.SHAPE.drawRect(-1.5D, -16D, 3D, 32D, TooltipUI.ACCENT);
}))
.label(Text.create("Target", info))
.attach(card);

Full file: TooltipUI.java

A custom tooltip draws with the drawing API, centered on the pointer.

The hovered node shows its own tooltips; a node without one shows the tooltips of its nearest parent that has some. A node tooltip stays inside the window. Node.removeHover removes one element, Node.clearHover every tooltip of the node.

Each node of CursorUI gives the window its own cursor:

RectNode
.create(60, 60, 220, 60)
.color(CursorUI.ELEMENT)
.cursor(Cursor.POINTER)
.label(Text.create("Shop", info))
.attach(card);
TextFieldNode
.create(320, 60, 220, 60)
.info(info)
.placeholder("Name")
.placeholderColor(CursorUI.MUTED)
.background(RectNode.create(0, 0, 0, 0).color(CursorUI.ELEMENT))
.marginHorizontal(12D)
.attach(card);
RectNode
.create(60, 180, 480, 60)
.color(CursorUI.ELEMENT)
.cursor(Cursor.MOVE)
.draggable(DraggableProperty.parent())
.label(Text.create("Drag me", info))
.attach(card);

Full file: CursorUI.java

The cursor tells the user what a node does before any click: press, type or drag.

Node.cursor sets the Cursor of the window over the node. A node without a cursor shows the cursor of its parent, and the cursor of the node under a press stays until the release. Text fields show Cursor.TEXT. POINTER suits what reacts to a click, TEXT a text to type or select and MOVE what you drag; the catalog gives every cursor and its use.

  • hoveredColor blends to a color under the pointer; hoverDuration and hoverEquation shape the animation, and hoverValue applies it to any value.
  • A node is hovered when it is the frontmost interactive node under the pointer, or one of its parents, in the top UI.
  • onHoverStart and onHoverEnd run once per entry and exit, onHover on every hovered frame.
  • hover(...) adds a tooltip: a line, lines, a node (NodeHoverElement) or a drawing (CustomHoverElement); its suppliers run on every frame it shows.
  • cursor(...) sets the cursor; a node without one shows the cursor of its parent.

Grow the button under the pointer. Make the “Shop” button of HoverUI grow from 220 to 260 px wide while it is hovered.

Solution

hoverValue is read in a lambda, on every frame:

.self(rect -> rect.width(() -> 220D + rect.hoverValue(40F)))

self hands you the button, so that its width can read its own hover progress.

A tooltip only when there is news. Show “New items” over the “Shop” button only while a BooleanSignal news is true.

Solution

A line that is null shows no tooltip:

.hover(() -> news.peek() ? "New items" : null)

Report a problem with this page