richtext

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2024 License: MIT, Unlicense Imports: 11 Imported by: 19

README

richtext

Provides a widget that renders text in different styles.

Documentation

Overview

Package richtext provides rendering of text containing multiple fonts, styles, and levels of interactivity.

Example
package main

import (
	"image/color"
	"log"

	"gioui.org/app"
	"gioui.org/font/gofont"
	"gioui.org/gesture"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/unit"
	"gioui.org/widget/material"
	"gioui.org/x/richtext"
)

func main() {
	var (
		fonts = gofont.Collection()
		th    = material.NewTheme()
		black = color.NRGBA{A: 255}
		green = color.NRGBA{G: 170, A: 255}
		blue  = color.NRGBA{B: 170, A: 255}
		red   = color.NRGBA{R: 170, A: 255}
	)
	th.Shaper = text.NewShaper(text.WithCollection(fonts))
	go func() {
		w := new(app.Window)

		// allocate persistent state for interactive text. This
		// needs to be persisted across frames.
		var state richtext.InteractiveText

		interactColors := []color.NRGBA{black, green, blue, red}
		interactColorIndex := 0

		var ops op.Ops
		for {
			e := w.Event()
			switch e := e.(type) {
			case app.DestroyEvent:
				panic(e.Err)
			case app.FrameEvent:
				gtx := app.NewContext(&ops, e)

				// define the text that you want to present. This can be persisted
				// across frames, recomputed every frame, or modified in any way between
				// frames.
				var spans []richtext.SpanStyle = []richtext.SpanStyle{
					{
						Content: "Hello ",
						Color:   black,
						Size:    unit.Sp(24),
						Font:    fonts[0].Font,
					},
					{
						Content: "in ",
						Color:   green,
						Size:    unit.Sp(36),
						Font:    fonts[0].Font,
					},
					{
						Content: "rich ",
						Color:   blue,
						Size:    unit.Sp(30),
						Font:    fonts[0].Font,
					},
					{
						Content: "text\n",
						Color:   red,
						Size:    unit.Sp(40),
						Font:    fonts[0].Font,
					},
					{
						Content:     "Interact with me!",
						Color:       interactColors[interactColorIndex%len(interactColors)],
						Size:        unit.Sp(40),
						Font:        fonts[0].Font,
						Interactive: true,
					},
				}

				// process any interactions with the text since the last frame.
				for {
					span, event, ok := state.Update(gtx)
					if !ok {
						break
					}
					content, _ := span.Content()
					switch event.Type {
					case richtext.Click:
						log.Println(event.ClickData.Kind)
						if event.ClickData.Kind == gesture.KindClick {
							interactColorIndex++
							gtx.Execute(op.InvalidateCmd{})
						}
					case richtext.Hover:
						w.Option(app.Title("Hovered: " + content))
					case richtext.Unhover:
						w.Option(app.Title("Unhovered: " + content))
					case richtext.LongPress:
						w.Option(app.Title("Long-pressed: " + content))
					}
				}

				// render the rich text into the operation list
				richtext.Text(&state, th.Shaper, spans...).Layout(gtx)

				// render the operation list
				e.Frame(gtx.Ops)
			}
		}
	}()
	app.Main()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var LongPressDuration time.Duration = 250 * time.Millisecond

LongPressDuration is the default duration of a long press gesture. Override this variable to change the detection threshold.

Functions

This section is empty.

Types

type Event

type Event struct {
	Type EventType
	// ClickData is only populated if Type == Clicked
	ClickData gesture.ClickEvent
}

Event describes an interaction with rich text.

type EventType

type EventType uint8

EventType describes a kind of iteraction with rich text.

const (
	Hover EventType = iota
	Unhover
	LongPress
	Click
)

type InteractiveSpan

type InteractiveSpan struct {
	// contains filtered or unexported fields
}

InteractiveSpan holds the persistent state of rich text that can be interacted with by the user. It can report clicks, hovers, and long-presses on the text.

func (*InteractiveSpan) Content

func (i *InteractiveSpan) Content() (string, map[string]interface{})

Content returns the text content of the interactive span as well as the metadata associated with it.

func (*InteractiveSpan) Get

func (i *InteractiveSpan) Get(key string) interface{}

Get looks up a metadata property on the interactive span.

func (*InteractiveSpan) Layout

Layout adds the pointer input op for this interactive span and updates its state. It uses the most recent pointer.AreaOp as its input area.

func (*InteractiveSpan) Update added in v0.5.0

func (i *InteractiveSpan) Update(gtx layout.Context) (Event, bool)

type InteractiveText

type InteractiveText struct {
	Spans []InteractiveSpan
	// contains filtered or unexported fields
}

InteractiveText holds persistent state for a block of text containing spans that may be interactive.

func (*InteractiveText) Update added in v0.5.0

Update returns the first span with unprocessed events and the events that need processing for it.

type SpanStyle

type SpanStyle struct {
	Font        font.Font
	Size        unit.Sp
	Color       color.NRGBA
	Content     string
	Interactive bool
	// contains filtered or unexported fields
}

SpanStyle describes the appearance of a span of styled text.

func (SpanStyle) DeepCopy

func (ss SpanStyle) DeepCopy() SpanStyle

DeepCopy returns an identical SpanStyle with its own copy of its metadata.

func (*SpanStyle) Set

func (ss *SpanStyle) Set(key string, value interface{})

Set configures a metadata key-value pair on the span that can be retrieved if the span is interacted with. If the provided value is empty, the key will be deleted from the metadata.

type TextStyle

type TextStyle struct {
	State      *InteractiveText
	Styles     []SpanStyle
	Alignment  text.Alignment
	WrapPolicy styledtext.WrapPolicy
	*text.Shaper
}

TextStyle presents rich text.

func Text

func Text(state *InteractiveText, shaper *text.Shaper, styles ...SpanStyle) TextStyle

Text constructs a TextStyle.

func (TextStyle) Layout

func (t TextStyle) Layout(gtx layout.Context) layout.Dimensions

Layout renders the TextStyle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL