Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Text and links

A run of text that wraps where layout puts it, and a word that goes somewhere when pressed.

By the end of this chapter you can put a label on screen at any rank of the type scale, bind it to a value, and make a word open a screen or a web page.

text

A text is one run of text. It wraps at the width layout gives it and nothing else about it is decided in Java.

Three lines: The Red Book as a title, a caption in muted ink, and a bound status lineThree lines: The Red Book as a title, a caption in muted ink, and a bound status line

A title, a caption and a bound value.

column {
  text style="title" "The Red Book"
  text class="caption" "Marked in a hand that was not steady"
  text bind="app.status" "checking…"
}
import dev.goldberry.widgets.text.Text;
import dev.goldberry.widgets.text.TextRank;

new Column(
        new Text("The Red Book").style(TextRank.TITLE),
        new Text("Marked in a hand that was not steady").styled("caption"),
        Text.of("checking…", Models.observable(app, "app.status"))
);

The argument is what the text says. With bind=, the argument is the fallback shown until the bound value answers, and a null value draws as nothing rather than as the word null. The value is read at render, so a change that lands between a build and a frame is in that frame.

Attributes

AttributeTypeDefaultWhat it does
argumentstring""the text, or the fallback when bind= is set
bindpathnonethe value to follow, read-only
stylerank namenoneone of the seven ranks below, added as a class; a name that is not one is refused
classstringnoneCSS classes, space-separated
idstringnonethe node’s id, which is also its reconciliation key
tooltipstringnonetext shown on hover
context-menustringnonethe name of a menu a right-click opens
namestringnonethe accessible name

style= and class= are two spellings of one thing. text style="title" and text class="title" both put the class title on the node, and a rule written text.title matches either. The difference is that style= is checked when the document inflates.

The type scale

The ranks are classes in controls.css. They inherit, so a class on a container reaches every label under it.

ClassFaceSize / line
displayInter 60030 / 36
titleInter 60022 / 28
headingInter 60017 / 22
bodyInter 40015 / 20
body-strongInter 60015 / 20
captionInter 40013 / 16
monoJetBrains Mono 40015 / 20

The sizes are the theme’s tokens, --gb-font-title and so on, so a large-text theme moves every rank at once. The showcase’s rank-display and card-title classes are its own stylesheet’s and not the toolkit’s.

Wrapping and cutting

A text is a measured leaf: layout proposes a width, the paragraph wraps at it, and the height that comes back sizes the box. The paragraph is shaped once and re-wrapped by arithmetic.

To keep a label on one line, the stylesheet says so:

text.name { white-space: nowrap; text-overflow: ellipsis }

nowrap stops the wrap and ellipsis marks the cut. text-align places the lines inside the box.

A log line keeps its spacing and cuts a word too long for the box:

text.log { white-space: pre-wrap; overflow-wrap: anywhere }

The paragraph never collapses spaces, so pre-wrap and pre-line wrap as normal does and pre stays on one line as nowrap does. A word wider than the whole line overflows unless overflow-wrap: anywhere cuts it between grapheme clusters. word-break: break-all cuts any word at the edge.

Static text cannot be selected or copied. A text-input with read-only=#true shows text a user can select, in Fields and forms.

A sentence with two styles is a rich-text. Its individually styled runs wrap as one paragraph.

Styling

  • CSS type text.
  • No parts.
  • No pseudo-classes of its own. It is not focusable and takes no hover.
  • Classes: the seven ranks above, and whatever the document writes.

A text sets no colour. color inherits from the nearest ancestor that sets one, which is a card, a panel or the window.

Keyboard

None. A text is not a Tab stop.

rich-text

A rich-text is one paragraph made of runs, each in a style of its own. A keyword can be gold and bold in the middle of a sentence, and the sentence still wraps as one text.

Two lines showing styled Bleeding and Order keywordsTwo lines showing styled Bleeding and Order keywords

Styled runs inside a paragraph.

column {
  rich-text {
    run "Give it "
    run "Bleeding" class="keyword"
    run " equal to the amount of boost it lost."
  }
  rich-text {
    run "Order" class="keyword"
    run ": Reset the power of a unit."
  }
}
import dev.goldberry.widgets.text.RichText;
import dev.goldberry.widgets.text.Run;

new Column(
        new RichText(
                new Run("Give it "),
                new Run("Bleeding", Set.of("keyword")),
                new Run(" equal to the amount of boost it lost.")
        ),
        new RichText(
                new Run("Order", Set.of("keyword")),
                new Run(": Reset the power of a unit.")
        )
);
rich-text > run.keyword { color: var(--gb-warning-text); font-weight: bold }

A row of text nodes does not do this. Each text wraps inside its own box, so a sentence split across three of them wraps as three columns. A rich-text shapes each run in its own font, joins them into one paragraph and breaks the lines over the whole of it. A line can end in the middle of a run and the next one start there.

A line is as tall as the tallest font on it: the deepest ascent above the baseline and the deepest share below it. A run at a larger size makes its own line taller and leaves the others alone. Runs that look like the paragraph add nothing, so a rich-text whose runs are unstyled draws exactly what a text with the same words draws.

Attributes

AttributeTypeDefaultWhat it does
childrenrunnonethe runs, in reading order; any other child is refused
classstringnoneCSS classes, space-separated
idstringnonethe node’s id, which is also its reconciliation key
tooltipstringnonetext shown on hover
context-menustringnonethe name of a menu a right-click opens
selectablebool#falsewhether its words can be dragged across, selected and copied

A rich-text takes no argument. Its words are its runs, and an argument is refused when the document inflates.

Styling

  • CSS type rich-text.
  • Its children are run nodes, styled by the cascade like any child, and one rich-text-selection part whose background is the selection’s highlight.
  • No pseudo-classes of its own. A selectable one is focusable.
  • A run’s background is drawn behind its words, on every line they reach.
  • run.pressable is a run with a press handler, which takes the link ink and the pointer cursor; the pointer over any run takes that run’s cursor.
  • run.spoiler is covered until pressed: run.spoiler:not(.revealed) draws its words in no colour on a block of the muted ink, and a press adds revealed.

The paragraph’s white-space, text-overflow, text-align, overflow-wrap, word-break, padding and background are the rich-text’s. color, the font and text-decoration are each run’s, inherited from the rich-text where a run does not set them.

Keyboard

None, unless it is selectable: then it is a Tab stop, Ctrl+A selects all of it and Ctrl+C copies what is selected. Losing the focus clears the selection. Its accessible name is its runs’ text end to end, and its role is text.

Pressing and selecting

new RichText(List.of(
        new Run("Thanks "),
        new Run("@frodo", Set.of("mention")).onPress(() -> openProfile("frodo")),
        new Run(", the ending is "),
        new Run("he leaves").spoiler(true)
), Attributes.NONE)
        .selectable(true)

A click on a pressed run’s words, on any line they reach, runs its handler. A click on a covered spoiler reveals it and runs nothing. A drag across a selectable paragraph selects, and a drag that selected something is not a click. The selection belongs to one paragraph.

run

A run is one stretch of a rich-text: its argument is the words, spaces included, and its classes are what the stylesheet styles it by. Runs are joined as written, so the space between two words belongs to one of them.

rich-text {
  run "Deal "
  run "3" class="number"
  run " damage."
}
AttributeTypeDefaultWhat it does
argumentstring""the words
classstringnoneCSS classes, space-separated
idstringnonethe node’s id
spoilerbool#falsecovers the words until the run is pressed

A run decides how its own words look: font-family, font-size, font-weight, font-style, color, text-decoration and a solid background, drawn behind its words. A run’s white-space, text-align and padding are read by nothing, because a run has no box of its own inside the paragraph. A press handler is code: Run.onPress(Runnable) in Java. A selector reaches it as a child of its paragraph, rich-text > run.keyword, or by its classes alone.

Outside a rich-text, a run draws as a text with the same words would.

  • CSS type run.
  • No parts and no pseudo-classes.

A link is a word that does something: it runs an action, opens a URL through the desktop, or both.

Three links: Read the docs, Goldberry on the web with an external arrow, and Write to us in the visited inkThree links: Read the docs, Goldberry on the web with an external arrow, and Write to us in the visited ink

An action link, an external link and a visited one.

column {
  link action="app.show-docs" "Read the docs"
  link href="https://goldberry.dev" "Goldberry on the web"
  link href="mailto:hello@example.org" visited=#true "Write to us"
}
import dev.goldberry.widgets.text.Link;

new Column(
        new Link("Read the docs", actions::showDocs),
        Link.external("Goldberry on the web", "https://goldberry.dev"),
        Link.external("Write to us", "mailto:hello@example.org").visited(true)
);

action= is in-app navigation. href= is handed to the desktop’s own handler for its scheme through Host.openExternal, and the link logs a warning when the platform refuses it. A link with both runs the action and then opens the target. A link with neither is a word in the link ink that takes no focus.

An external link draws the toolkit’s 12 px external-link icon after the word. That icon is the one icon the toolkit builds itself, and the link’s state owns it.

Attributes

AttributeTypeDefaultWhat it does
argumentstringrequiredthe word; an empty label is refused
actionaction namenonewhat an in-app link runs
hrefstringnonewhat an external link opens
visitedboolean#falseadds the visited class; the toolkit keeps no history, so this is the application’s to set
class, id, tooltip, context-menu, nameas on every widget

Styling

  • CSS type link. The record builds a node of this type. There are no parts.
  • Pseudo-classes: :hover underlines, :focus-visible takes the standard ring.
  • Classes: visited takes --gb-text-muted; external is set when href= is present.

The underline is text-decoration: underline in controls.css, drawn at the face’s own position and thickness. The ink is --gb-button-link-text, a token measured for 4.5:1 on every surface, and not the accent.

A link is block-level: a row of a word and sometimes an icon, on a line of its own. It is not button.link, which is a button that reads as text and sits in a toolbar. See Buttons, badges and chips.

Keyboard

KeyDoes
Tabreaches it, when it has an action or an href
Enterfollows it

Space does not activate a link, which is every browser’s rule. A link with nothing behind it is not a Tab stop.