widget

package
v0.1.122 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeEvent

type ChangeEvent struct{}

A ChangeEvent is generated for every user change to the text.

type Editor

type Editor struct {

	// Alignment controls the alignment of text within the editor.
	Alignment text.Alignment
	// LineHeight determines the gap between baselines of text. If zero, a sensible
	// default will be used.
	LineHeight gioUnit.Sp
	// LineHeightScale is multiplied by LineHeight to determine the final gap
	// between baselines. If zero, a sensible default will be used.
	LineHeightScale float32
	// SingleLine force the text to stay on a single line.
	// SingleLine also sets the scrolling direction to
	// horizontal.
	SingleLine bool
	// ReadOnly controls whether the contents of the editor can be altered by
	// user interaction. If set to true, the editor will allow selecting text
	// and copying it interactively, but not modifying it.
	ReadOnly bool
	// Submit enabled translation of carriage return keys to SubmitEvents.
	// If not enabled, carriage returns are inserted as newlines in the text.
	Submit bool
	// Mask replaces the visual display of each rune in the contents with the given rune.
	// Newline characters are not masked. When non-zero, the unmasked contents
	// are accessed by Len, Text, and SetText.
	Mask rune
	// InputHint specifies the type of on-screen keyboard to be displayed.
	InputHint key.InputHint
	// MaxLen limits the editor content to a maximum length. Zero means no limit.
	MaxLen int
	// Filter is the list of characters allowed in the Editor. If Filter is empty,
	// all characters are allowed.
	Filter string
	// WrapPolicy configures how displayed text will be broken into lines.
	WrapPolicy text.WrapPolicy
	// contains filtered or unexported fields
}

Editor implements an editable and scrollable text area.

func (*Editor) CaretCoords

func (e *Editor) CaretCoords() f32.Point

CaretCoords returns the coordinates of the caret, relative to the editor itself.

func (*Editor) CaretPos

func (e *Editor) CaretPos() (line, col int)

CaretPos returns the line & column numbers of the caret.

func (*Editor) ClearSelection

func (e *Editor) ClearSelection()

ClearSelection clears the selection, by setting the selection end equal to the selection start.

func (*Editor) Delete

func (e *Editor) Delete(graphemeClusters int) (deletedRunes int)

Delete runes from the caret position. The sign of the argument specifies the direction to delete: positive is forward, negative is backward.

If there is a selection, it is deleted and counts as a single grapheme cluster.

func (*Editor) Insert

func (e *Editor) Insert(s string) (insertedRunes int)

func (*Editor) Layout

func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size gioUnit.Sp, textMaterial, selectMaterial op.CallOp) layout.Dimensions

Layout lays out the editor using the provided textMaterial as the paint material for the text glyphs+caret and the selectMaterial as the paint material for the selection rectangle.

func (*Editor) Len

func (e *Editor) Len() int

Len is the length of the editor contents, in runes.

func (*Editor) MoveCaret

func (e *Editor) MoveCaret(startDelta, endDelta int)

MoveCaret moves the caret (aka selection start) and the selection end relative to their current positions. Positive distances moves forward, negative distances moves backward. Distances are in grapheme clusters, which closely match what users perceive as "characters" even when the characters are multiple code points long.

func (*Editor) Read

func (e *Editor) Read(p []byte) (int, error)

Read implements io.Reader.

func (*Editor) Regions

func (e *Editor) Regions(start, end int, regions []Region) []Region

Regions returns visible regions covering the rune range [start,end).

func (*Editor) Seek

func (e *Editor) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

func (*Editor) SelectedText

func (e *Editor) SelectedText() string

SelectedText returns the currently selected text (if any) from the editor.

func (*Editor) Selection

func (e *Editor) Selection() (start, end int)

Selection returns the start and end of the selection, as rune offsets. start can be > end.

func (*Editor) SelectionLen

func (e *Editor) SelectionLen() int

SelectionLen returns the length of the selection, in runes; it is equivalent to utf8.RuneCountInString(e.SelectedText()).

func (*Editor) SetCaret

func (e *Editor) SetCaret(start, end int)

SetCaret moves the caret to start, and sets the selection end to end. start and end are in runes, and represent offsets into the editor text.

func (*Editor) SetText

func (e *Editor) SetText(s string)

func (*Editor) Text

func (e *Editor) Text() string

Text returns the contents of the editor.

func (*Editor) Update

func (e *Editor) Update(gtx layout.Context) (EditorEvent, bool)

Update the state of the editor in response to input events. Update consumes editor input events until there are no remaining events or an editor event is generated. To fully update the state of the editor, callers should call Update until it returns false.

func (*Editor) WriteTo

func (e *Editor) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

type EditorEvent

type EditorEvent interface {
	// contains filtered or unexported methods
}

type Label

type Label struct {
	// Alignment specifies the text alignment.
	Alignment text.Alignment
	// MaxLines limits the number of lines. Zero means no limit.
	MaxLines int
	// Truncator is the text that will be shown at the end of the final
	// line if MaxLines is exceeded. Defaults to "…" if empty.
	Truncator string
	// WrapPolicy configures how displayed text will be broken into lines.
	WrapPolicy text.WrapPolicy
	// LineHeight controls the distance between the baselines of lines of text.
	// If zero, a sensible default will be used.
	LineHeight gioUnit.Sp
	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
	// sensible default will be used.
	LineHeightScale float32
}

Label is a widget for laying out and drawing text. Labels are always non-interactive text. They cannot be selected or copied.

func (Label) Layout

func (l Label) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size gioUnit.Sp, txt string, textMaterial op.CallOp) layout.Dimensions

Layout the label with the given shaper, font, size, text, and material.

func (Label) LayoutDetailed

func (l Label) LayoutDetailed(gtx layout.Context, lt *text.Shaper, font font.Font, size gioUnit.Sp, txt string, textMaterial op.CallOp) (layout.Dimensions, TextInfo)

Layout the label with the given shaper, font, size, text, and material, returning metadata about the shaped text.

type Region

type Region struct {
	// Bounds is the coordinates of the bounding box relative to the containing
	// widget.
	Bounds image.Rectangle
	// Baseline is the quantity of vertical pixels between the baseline and
	// the bottom of bounds.
	Baseline int
}

Region describes the position and baseline of an area of interest within shaped text.

type SelectEvent

type SelectEvent struct{}

A SelectEvent is generated when the user selects some text, or changes the selection (e.g. with a shift-click), including if they remove the selection. The selected text is not part of the event, on the theory that it could be a relatively expensive operation (for a large editor), most applications won't actually care about it, and those that do can call Editor.SelectedText() (which can be empty).

type Selectable

type Selectable struct {
	// Alignment controls the alignment of the text.
	Alignment text.Alignment
	// MaxLines is the maximum number of lines of text to be displayed.
	MaxLines int
	// Truncator is the symbol to use at the end of the final line of text
	// if text was cut off. Defaults to "…" if left empty.
	Truncator string
	// WrapPolicy configures how displayed text will be broken into lines.
	WrapPolicy text.WrapPolicy
	// LineHeight controls the distance between the baselines of lines of text.
	// If zero, a sensible default will be used.
	LineHeight gioUnit.Sp
	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
	// sensible default will be used.
	LineHeightScale float32
	// contains filtered or unexported fields
}

Selectable displays selectable text.

func (*Selectable) ClearSelection

func (l *Selectable) ClearSelection()

ClearSelection clears the selection, by setting the selection end equal to the selection start.

func (*Selectable) Focused

func (l *Selectable) Focused() bool

Focused returns whether the label is focused or not.

func (*Selectable) Layout

func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size gioUnit.Sp, textMaterial, selectionMaterial op.CallOp) layout.Dimensions

Layout clips to the dimensions of the selectable, updates the shaped text, configures input handling, and paints the text and selection rectangles. The provided textMaterial and selectionMaterial ops are used to set the paint material for the text and selection rectangles, respectively.

func (*Selectable) Regions

func (l *Selectable) Regions(start, end int, regions []Region) []Region

Regions returns visible regions covering the rune range [start,end).

func (*Selectable) SelectedText

func (l *Selectable) SelectedText() string

SelectedText returns the currently selected text (if any) from the editor.

func (*Selectable) Selection

func (l *Selectable) Selection() (start, end int)

Selection returns the start and end of the selection, as rune offsets. start can be > end.

func (*Selectable) SelectionLen

func (l *Selectable) SelectionLen() int

SelectionLen returns the length of the selection, in runes; it is equivalent to utf8.RuneCountInString(e.SelectedText()).

func (*Selectable) SetCaret

func (l *Selectable) SetCaret(start, end int)

SetCaret moves the caret to start, and sets the selection end to end. start and end are in runes, and represent offsets into the editor text.

func (*Selectable) SetText

func (l *Selectable) SetText(s string)

SetText updates the text to s if it does not already contain s. Updating the text will clear the selection unless the selectable already contains s.

func (*Selectable) Text

func (l *Selectable) Text() string

Text returns the contents of the label.

func (*Selectable) Truncated

func (l *Selectable) Truncated() bool

Truncated returns whether the text has been truncated by the text shaper to fit within available constraints.

func (*Selectable) Update

func (l *Selectable) Update(gtx layout.Context) bool

Update the state of the selectable in response to input events. It returns whether the text selection changed during event processing.

type SubmitEvent

type SubmitEvent struct {
	Text string
}

A SubmitEvent is generated when Submit is set and a carriage return key is pressed.

type TextInfo

type TextInfo struct {
	// Truncated contains the number of runes of text that are represented by a truncator
	// symbol in the text. If zero, there is no truncator symbol.
	Truncated int
}

TextInfo provides metadata about shaped text.

type TextSource

type TextSource interface {
	io.ReaderAt
	// Size returns the total length of the data in bytes.
	Size() int64
	// Changed returns whether the contents have changed since the last call
	// to Changed.
	Changed() bool
	// ReplaceRunes replaces runeCount runes starting at byteOffset within the
	// data with the provided string. Implementations of read-only text sources
	// are free to make this a no-op.
	ReplaceRunes(byteOffset int64, runeCount int64, replacement string)
}

TextSource provides text data for use in widgets. If the underlying data type can fail due to I/O errors, it is the responsibility of that type to provide its own mechanism to surface and handle those errors. They will not always be returned by widgets using these functions.

type TextView

type TextView struct {
	Alignment text.Alignment
	// LineHeight controls the distance between the baselines of lines of text.
	// If zero, a sensible default will be used.
	LineHeight gioUnit.Sp
	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
	// sensible default will be used.
	LineHeightScale float32
	// SingleLine forces the text to stay on a single line.
	// SingleLine also sets the scrolling direction to
	// horizontal.
	SingleLine bool
	// MaxLines limits the shaped text to a specific quantity of shaped lines.
	MaxLines int
	// Truncator is the text that will be shown at the end of the final
	// line if MaxLines is exceeded. Defaults to "…" if empty.
	Truncator string
	// WrapPolicy configures how displayed text will be broken into lines.
	WrapPolicy text.WrapPolicy
	// DisableSpaceTrim configures whether trailing whitespace on a line will have its
	// width zeroed. Set to true for editors, but false for non-editable text.
	DisableSpaceTrim bool
	// Mask replaces the visual display of each rune in the contents with the given rune.
	// Newline characters are not masked. When non-zero, the unmasked contents
	// are accessed by Len, Text, and SetText.
	Mask rune
	// contains filtered or unexported fields
}

TextView provides efficient shaping and indexing of interactive text. When provided with a TextSource, TextView will shape and cache the runes within that source. It provides methods for configuring a viewport onto the shaped text which can be scrolled, and for configuring and drawing text selection boxes.

func (*TextView) ByteOffset

func (e *TextView) ByteOffset(runeOffset int) int64

ByteOffset returns the start byte of the rune at the given rune offset, clamped to the size of the text.

func (*TextView) CaretCoords

func (e *TextView) CaretCoords() f32.Point

CaretCoords returns the coordinates of the caret, relative to the editor itself.

func (*TextView) CaretInfo

func (e *TextView) CaretInfo() (pos image.Point, ascent, descent int)

func (*TextView) CaretPos

func (e *TextView) CaretPos() (line, col int)

CaretPos returns the line & column numbers of the caret.

func (*TextView) Changed

func (e *TextView) Changed() bool

func (*TextView) ClearSelection

func (e *TextView) ClearSelection()

ClearSelection clears the selection, by setting the selection end equal to the selection start.

func (*TextView) Dimensions

func (e *TextView) Dimensions() layout.Dimensions

Dimensions returns the dimensions of the visible text.

func (*TextView) FullDimensions

func (e *TextView) FullDimensions() layout.Dimensions

FullDimensions returns the dimensions of all shaped text, including text that isn't visible within the current viewport.

func (*TextView) Layout

func (e *TextView) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, size gioUnit.Sp)

Layout the text, reshaping it as necessary.

func (*TextView) Len

func (e *TextView) Len() int

Len is the length of the editor contents, in runes.

func (*TextView) MoveCaret

func (e *TextView) MoveCaret(startDelta, endDelta int)

MoveCaret moves the caret (aka selection start) and the selection end relative to their current positions. Positive distances moves forward, negative distances moves backward. Distances are in grapheme clusters which better match the expectations of users than runes.

func (*TextView) MoveCoord

func (e *TextView) MoveCoord(pos image.Point)

MoveCoord moves the caret to the position closest to the provided point that is aligned to a grapheme cluster boundary.

func (*TextView) MoveLineEnd

func (e *TextView) MoveLineEnd(selAct selectionAction)

MoveLineEnd moves the caret to the end of the current line, ensuring that the resulting cursor position is on a grapheme cluster boundary.

func (*TextView) MoveLineStart

func (e *TextView) MoveLineStart(selAct selectionAction)

MoveLineStart moves the caret to the start of the current line, ensuring that the resulting cursor position is on a grapheme cluster boundary.

func (*TextView) MoveLines

func (e *TextView) MoveLines(distance int, selAct selectionAction)

MaxLines moves the cursor the specified number of lines vertically, ensuring that the resulting position is aligned to a grapheme cluster.

func (*TextView) MovePages

func (e *TextView) MovePages(pages int, selAct selectionAction)

MovePages moves the caret position by vertical pages of text, ensuring that the final position is aligned to a grapheme cluster boundary.

func (*TextView) MoveTextEnd

func (e *TextView) MoveTextEnd(selAct selectionAction)

MoveTextEnd moves the caret to the end of the text.

func (*TextView) MoveTextStart

func (e *TextView) MoveTextStart(selAct selectionAction)

MoveTextStart moves the caret to the start of the text.

func (*TextView) MoveWord

func (e *TextView) MoveWord(distance int, selAct selectionAction)

MoveWord moves the caret to the next word in the specified direction. Positive is forward, negative is backward. Absolute values greater than one will skip that many words. The final caret position will be aligned to a grapheme cluster boundary. BUG(whereswaldon): this method's definition of a "word" is currently whitespace-delimited. Languages that do not use whitespace to delimit words will experience counter-intuitive behavior when navigating by word.

func (*TextView) PaintCaret

func (e *TextView) PaintCaret(gtx layout.Context, material op.CallOp)

PaintCaret clips and paints the caret rectangle, adding material immediately before painting to set the appropriate paint material.

func (*TextView) PaintSelection

func (e *TextView) PaintSelection(gtx layout.Context, material op.CallOp)

PaintSelection clips and paints the visible text selection rectangles using the provided material to fill the rectangles.

func (*TextView) PaintText

func (e *TextView) PaintText(gtx layout.Context, material op.CallOp)

PaintText clips and paints the visible text glyph outlines using the provided material to fill the glyphs.

func (*TextView) Read

func (e *TextView) Read(p []byte) (int, error)

Read implements io.Reader.

func (*TextView) ReadAt

func (e *TextView) ReadAt(p []byte, offset int64) (int, error)

ReadAt implements io.ReaderAt.

func (*TextView) ReadRuneAt

func (e *TextView) ReadRuneAt(off int64) (rune, int, error)

ReadRuneAt reads the rune starting at the given byte offset, if any.

func (*TextView) ReadRuneBefore

func (e *TextView) ReadRuneBefore(off int64) (rune, int, error)

ReadRuneAt reads the run prior to the given byte offset, if any.

func (*TextView) Regions

func (e *TextView) Regions(start, end int, regions []Region) []Region

Regions returns visible regions covering the rune range [start,end).

func (*TextView) Replace

func (e *TextView) Replace(start, end int, s string) int

Replace the text between start and end with s. Indices are in runes. It returns the number of runes inserted.

func (*TextView) ScrollBounds

func (e *TextView) ScrollBounds() image.Rectangle

func (*TextView) ScrollOff

func (e *TextView) ScrollOff() image.Point

ScrollOff returns the scroll offset of the text viewport.

func (*TextView) ScrollRel

func (e *TextView) ScrollRel(dx, dy int)

func (*TextView) ScrollToCaret

func (e *TextView) ScrollToCaret()

func (*TextView) Seek

func (e *TextView) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker.

func (*TextView) SelectedText

func (e *TextView) SelectedText(buf []byte) []byte

SelectedText returns the currently selected text (if any) from the editor, filling the provided byte slice if it is large enough or allocating and returning a new byte slice if the provided one is insufficient. Callers can guarantee that the buf is large enough by providing a buffer with capacity e.SelectionLen()*utf8.UTFMax.

func (*TextView) Selection

func (e *TextView) Selection() (start, end int)

Selection returns the start and end of the selection, as rune offsets. start can be > end.

func (*TextView) SelectionLen

func (e *TextView) SelectionLen() int

SelectionLen returns the length of the selection, in runes; it is equivalent to utf8.RuneCountInString(e.SelectedText()).

func (*TextView) SetCaret

func (e *TextView) SetCaret(start, end int)

SetCaret moves the caret to start, and sets the selection end to end. Then the two ends are clamped to the nearest grapheme cluster boundary. start and end are in runes, and represent offsets into the editor text.

func (*TextView) SetSource

func (e *TextView) SetSource(source TextSource)

SetSource initializes the underlying data source for the Text. This must be done before invoking any other methods on Text.

func (*TextView) Text

func (e *TextView) Text(buf []byte) []byte

Text returns the contents of the editor. If the provided buf is large enough, it will be filled and returned. Otherwise a new buffer will be allocated. Callers can guarantee that buf is large enough by giving it capacity e.Len()*utf8.UTFMax.

func (*TextView) Truncated

func (e *TextView) Truncated() bool

Truncated returns whether the text in the textView is currently truncated due to a restriction on the number of lines.

func (*TextView) WriteTo

func (e *TextView) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

Notes

Bugs

  • this method's definition of a "word" is currently whitespace-delimited. Languages that do not use whitespace to delimit words will experience counter-intuitive behavior when navigating by word.

Jump to

Keyboard shortcuts

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