Documentation
¶
Index ¶
- func SelectionArea(key any, child base.Element) base.Element
- func SetLinkOpener(fn func(url string))
- type RichTextElement
- type Span
- type TextElement
- func (*TextElement) CacheSafe() bool
- func (t *TextElement) Describe() string
- func (t *TextElement) FlexPercent(axis layout.Axis) int
- func (t *TextElement) FlexWeight(axis layout.Axis) int
- func (t *TextElement) InlineText() string
- func (t *TextElement) Layout(gtx layout.Context) layout.Dimensions
- func (t *TextElement) OnHover(st styles.Styles) *TextElement
- func (t *TextElement) Selectable(key any) *TextElement
- func (t *TextElement) ZIndex() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SelectionArea ¶
SelectionArea makes the Text and RichText elements inside child selectable as one, like a web page: with a mouse, drag across them to select, double-click for a word, triple-click for a whole element, Shift+click to extend, and press Ctrl+C (Cmd+C) to copy; Ctrl+A (Cmd+A) selects everything and Escape clears. A right-click on a selection offers Copy. On a touch screen, a long-press selects a word and dragging the held finger extends it; letting go offers Copy. Copied text joins the elements with newlines.
key identifies the area across frames. Text inside keeps its own styles; the selection is drawn in its color at 30% opacity. Elements that are themselves Selectable keep their own selection and aren't part of the area's, nor is the text of buttons, menus, inputs and other interactive components (see kit.Unselectable). The area works through Memo and LazyList: cached subtrees rebuild only when the selection changes. Boxes and components that cache their own output don't inside an area: they're laid out every frame.
func SetLinkOpener ¶
func SetLinkOpener(fn func(url string))
SetLinkOpener sets what opens a Link whose paragraph has no OnLink handler. The app sets it to open the browser; component authors don't need to.
Types ¶
type RichTextElement ¶
type RichTextElement struct {
// Contextual gives it a context menu (Context).
base.Contextual
// contains filtered or unexported fields
}
RichTextElement is a wrapping paragraph of differently styled spans.
func RichText ¶
func RichText(st styles.Styles, spans ...Span) *RichTextElement
RichText lays out spans as one wrapping paragraph:
giode.RichText(giode.Styles{FontSize: 15, LineHeight: 22},
giode.SpanText("Run "),
giode.SpanText("go test", giode.Styles{FontFamily: "JetBrains Mono", Background: "theme.surface"}),
giode.SpanText(" or read the "),
giode.Link("docs", "https://gioui.org"),
).OnLink(func(url string) { ... })
st styles the paragraph: Color, FontSize, FontWeight, FontStyle and FontFamily are the defaults spans inherit; TextAlign, LineHeight, TabWidth, MaxLines and TextOverflow (TextOverflowEllipsis ends the last line in "…" when text is cut, and alone means one line) apply to the whole paragraph, and Width, MinWidth and MaxWidth size it, as for Text. Like Text it's stateless: build it in the view function every frame.
func (*RichTextElement) CacheSafe ¶
func (*RichTextElement) CacheSafe() bool
CacheSafe reports true: the element depends only on its fields, which don't change once it's built, and on what caches already watch (the theme, the selection, its input passes).
func (*RichTextElement) Layout ¶
func (r *RichTextElement) Layout(gtx layout.Context) layout.Dimensions
Layout shapes and paints the paragraph.
func (*RichTextElement) OnLink ¶
func (r *RichTextElement) OnLink(fn func(url string)) *RichTextElement
OnLink sets the handler for taps on links, replacing the default of opening them in the browser. It returns r for chaining.
func (*RichTextElement) PlainText ¶
func (r *RichTextElement) PlainText() string
PlainText returns the paragraph's text without styling.
func (*RichTextElement) Selectable ¶
func (r *RichTextElement) Selectable(key any) *RichTextElement
Selectable lets the user copy the paragraph: a long-press on a touch screen, or a right-click, selects it all and offers Copy. key identifies the paragraph across frames (a message ID, say), since RichText itself is rebuilt every frame. Selecting part of a rich paragraph isn't supported; Text.Selectable is. It returns r for chaining.
type Span ¶
type Span struct {
// contains filtered or unexported fields
}
Span is one run of text in a RichText paragraph, with its own styles merged over the paragraph's. Create it with SpanText or Link.
func Link ¶
Link returns a tappable run of text that opens url: through the paragraph's OnLink handler when set, otherwise in the browser (see SetLinkOpener). It's underlined in the theme's primary color unless st says otherwise.
func SpanElement ¶
SpanElement returns an element set in the paragraph's line, such as an inline formula: it sits on the text's baseline (its Dimensions' Baseline, from its bottom) and wraps to the next line as a word does, making its line taller when it reaches past the text. text is what it stands for when the paragraph is selected and copied, and in its accessible label. The element is laid out at its own size, no wider than the paragraph.
type TextElement ¶
type TextElement struct {
// Contextual gives it a context menu (Context).
base.Contextual
kit.HoverTracker
// contains filtered or unexported fields
}
TextElement renders a single string with its styles. Chain OnHover to add hover styles.
func Text ¶
func Text(str string, st ...styles.Styles) *TextElement
Text renders a string with the given styles. The styles argument is optional.
func (*TextElement) CacheSafe ¶
func (*TextElement) CacheSafe() bool
CacheSafe reports true: the element depends only on its fields, which don't change once it's built, and on what caches already watch (the theme, the selection, its input passes).
func (*TextElement) Describe ¶
func (t *TextElement) Describe() string
Describe names the text for diagnostics.
func (*TextElement) FlexPercent ¶
func (t *TextElement) FlexPercent(axis layout.Axis) int
func (*TextElement) FlexWeight ¶
func (t *TextElement) FlexWeight(axis layout.Axis) int
func (*TextElement) InlineText ¶
func (t *TextElement) InlineText() string
InlineText returns the text content, for diagnostics.
func (*TextElement) Layout ¶
func (t *TextElement) Layout(gtx layout.Context) layout.Dimensions
Layout shapes and paints the text with its decoration.
func (*TextElement) OnHover ¶
func (t *TextElement) OnHover(st styles.Styles) *TextElement
OnHover sets styles applied while the pointer is over the text, merged on top of its base styles - color, decoration, weight, anything Styles' text properties support:
label := giode.Text("Docs", giode.Styles{Color: "#94a3b8"}).OnHover(giode.Styles{
Color: "#f8fafc",
TextDecoration: giode.TextDecorationUnderline,
Cursor: giode.CursorPointer,
})
Hover state lives in the element, so create it once and reuse it every frame; rebuilding it in the view function resets the hover on each frame.
func (*TextElement) Selectable ¶
func (t *TextElement) Selectable(key any) *TextElement
Selectable makes the text selectable. With a mouse, drag to select, double-click for a word, and press Ctrl+C (Cmd+C) to copy; a right-click selects it all and offers Copy. On a touch screen, a long-press selects it all and offers Copy, and dragging over the text still scrolls whatever it's in. key identifies the text across frames (a message ID, say), since Text itself is rebuilt every frame; the selection lives in the keyed store (see Keyed). It returns t for chaining.
func (*TextElement) ZIndex ¶
func (t *TextElement) ZIndex() int