richtext

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2025 License: Unlicense Imports: 12 Imported by: 0

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"
	"realy.lol/gui/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 (
	Horizontal   = _l.Horizontal
	Vertical     = _l.Vertical
	Start        = _l.Start
	End          = _l.End
	Middle       = _l.Middle
	Baseline     = _l.Baseline
	SpaceSides   = _l.SpaceSides
	SpaceStart   = _l.SpaceStart
	SpaceEvenly  = _l.SpaceEvenly
	SpaceAround  = _l.SpaceAround
	SpaceBetween = _l.SpaceBetween
	SpaceEnd     = _l.SpaceEnd
)
View Source
var (
	Exact = _l.Exact
	Pt    = _i.Pt
)
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 Alignment

type Alignment = _l.Alignment

type Axis

type Axis = _l.Axis

type Constraints

type Constraints = _l.Constraints

type Dim

type Dim = _l.Dimensions

type Dp

type Dp = _u.Dp

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 Gx

type Gx = _l.Context

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

func (i *InteractiveSpan) Layout(g Gx) Dim

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

func (i *InteractiveSpan) Update(g Gx) (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

func (i *InteractiveText) Update(g Gx) (*InteractiveSpan, Event, bool)

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

type List

type List = _l.List

type Point

type Point = _i.Point

type Sp

type Sp = _u.Sp

type Spacing

type Spacing = _l.Spacing

type SpanStyle

type SpanStyle struct {
	Font        font.Font
	Size        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(g Gx) Dim

Layout renders the TextStyle.

type Widget

type Widget = _l.Widget

Jump to

Keyboard shortcuts

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