textwidget

package
v0.2.0-alpha Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package textwidget provides the theme-free core of the basicwidget text widgets: the editable Text widget and the input helpers it is built on.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsKeyRepeating

func IsKeyRepeating(key ebiten.Key) bool

IsKeyRepeating reports whether key is pressed and its press duration is at a key-repeat firing point.

func IsMouseButtonRepeating

func IsMouseButtonRepeating(button ebiten.MouseButton) bool

IsMouseButtonRepeating reports whether button is pressed and its press duration is at a key-repeat firing point.

Types

type CaretScrollTarget

type CaretScrollTarget struct {
	// LogicalLineIndex is the caret's committed-text logical-line index.
	LogicalLineIndex int

	// X is the caret's textBounds-relative X coordinate.
	X float64

	// Top is the caret's top Y, measured from the start of the logical line.
	Top float64

	// Bottom is the caret's bottom Y, measured from the start of the logical line.
	Bottom float64
}

CaretScrollTarget describes one caret edge for scroll-into-view requests.

type SelectionSide

type SelectionSide int

SelectionSide identifies one endpoint of a selection.

const (
	SelectionSideNone SelectionSide = iota
	SelectionSideStart
	SelectionSideEnd
)

type Text

type Text struct {
	guigui.DefaultWidget
	// contains filtered or unexported fields
}

Text is a theme-free text widget: it owns the value, selection, caret, IME composition, input handling, layout, hit-testing, masking, and clipboard mechanism, and renders with concrete colors and face inputs set by a wrapping widget.

func (*Text) AppendBoundsOfTextRange

func (t *Text) AppendBoundsOfTextRange(dst []image.Rectangle, context *guigui.Context, bounds image.Rectangle, startInBytes, endInBytes int) []image.Rectangle

AppendBoundsOfTextRange appends the bounding rectangles covering the text range [startInBytes, endInBytes) to dst and returns the extended slice, one rectangle per crossed visual line, in order. bounds is the widget's bounds; the rectangles are in the same coordinate space. A masked value appends nothing. Endpoints outside the text are clamped.

func (*Text) AppendHotspotRanges

func (t *Text) AppendHotspotRanges(dst []TextRange) []TextRange

AppendHotspotRanges appends the current hotspot ranges to dst and returns the extended slice, reflecting the adjustments made for edits since Text.SetHotspotRanges.

func (*Text) Build

func (t *Text) Build(context *guigui.Context, adder *guigui.ChildAdder) error

func (*Text) CanCopy

func (t *Text) CanCopy() bool

func (*Text) CanCut

func (t *Text) CanCut() bool

func (*Text) CanPaste

func (t *Text) CanPaste() bool

func (*Text) CanRedo

func (t *Text) CanRedo() bool

func (*Text) CanUndo

func (t *Text) CanUndo() bool

func (*Text) CaretPositionAtTextIndexInBytes

func (t *Text) CaretPositionAtTextIndexInBytes(context *guigui.Context, textIndexInBytes int) (top, bottom image.Point, ok bool)

CaretPositionAtTextIndexInBytes returns the on-screen top and bottom endpoints of a caret drawn at byte offset textIndexInBytes. ok is false when the offset is out of range or the caret's logical line is outside the viewport. Available after the layout phase.

func (*Text) CommitWithCurrentInputValue

func (t *Text) CommitWithCurrentInputValue()

func (*Text) Copy

func (t *Text) Copy() bool

func (*Text) CopyOverrideStyleRunsFrom

func (t *Text) CopyOverrideStyleRunsFrom(runs *textstyle.Runs, record bool)

CopyOverrideStyleRunsFrom replaces the ranged style overrides with a copy of runs. record sets whether the replacement is recorded in the undo history; a recorded replacement leaving the overrides unchanged records nothing.

func (*Text) CursorShape

func (t *Text) CursorShape(context *guigui.Context, widgetBounds *guigui.WidgetBounds) (ebiten.CursorShapeType, bool)

func (*Text) Cut

func (t *Text) Cut() bool

func (*Text) Draw

func (t *Text) Draw(context *guigui.Context, widgetBounds *guigui.WidgetBounds, dst *ebiten.Image)

func (*Text) DrawPlainString

func (t *Text) DrawPlainString(context *guigui.Context, widgetBounds *guigui.WidgetBounds, dst *ebiten.Image, str string, clr color.Color)

DrawPlainString draws str in clr with the widget's current text style, laid out as the value would be, without selection or composition decorations.

func (*Text) EffectiveStyleAt

func (t *Text) EffectiveStyleAt(textIndexInBytes int) textstyle.Style

EffectiveStyleAt returns the effective style that text typed at textIndexInBytes adopts: the resolved base style with the overrides adopted from the neighboring byte merged on top.

func (*Text) ForceSetValue

func (t *Text) ForceSetValue(text string)

func (*Text) Generation

func (t *Text) Generation() int64

Generation returns the store's content generation. The generation advances on every content mutation, so an unchanged generation means unchanged text.

func (*Text) HandleButtonInput

func (t *Text) HandleButtonInput(context *guigui.Context, widgetBounds *guigui.WidgetBounds) guigui.HandleInputResult

func (*Text) HandlePointingInput

func (t *Text) HandlePointingInput(context *guigui.Context, widgetBounds *guigui.WidgetBounds) guigui.HandleInputResult

func (*Text) HasValue

func (t *Text) HasValue() bool

HasValue reports whether the text has a non-empty value.

func (*Text) HorizontalAlign

func (t *Text) HorizontalAlign() textutil.HorizontalAlign

func (*Text) IsEditable

func (t *Text) IsEditable() bool

func (*Text) IsMultiline

func (t *Text) IsMultiline() bool

IsMultiline reports whether the value may span multiple lines. It is always false while masking, which is single-line.

func (*Text) IsVisuallyEmpty

func (t *Text) IsVisuallyEmpty() bool

IsVisuallyEmpty reports whether nothing of the value would be rendered: the committed text is empty and no IME composition is in progress.

func (*Text) Layout

func (t *Text) Layout(context *guigui.Context, widgetBounds *guigui.WidgetBounds, layouter *guigui.ChildLayouter)

func (*Text) LayoutWidth

func (t *Text) LayoutWidth(bounds image.Rectangle) int

LayoutWidth returns the width used to lay out the text within bounds: the wrap width when wrapping is bounded, else the bounds width.

func (*Text) LineCount

func (t *Text) LineCount() int

LineCount returns the number of logical lines (spans between hard line breaks) in the value. The empty value has one logical line; a trailing line break creates an extra empty line at the end.

func (*Text) LineHeight

func (t *Text) LineHeight() float64

LineHeight returns the line height in pixels, with the widget scale applied.

func (*Text) LineIndexFromTextIndexInBytes

func (t *Text) LineIndexFromTextIndexInBytes(textIndexInBytes int) int

LineIndexFromTextIndexInBytes returns the index of the logical line containing textIndexInBytes, clamping out-of-range values to the first or last line.

func (*Text) LineStartInBytes

func (t *Text) LineStartInBytes(lineIndex int) int

LineStartInBytes returns the byte offset where the lineIndex-th logical line begins within the value. lineIndex must be in [0, Text.LineCount).

func (*Text) LogicalLineHeight

func (t *Text) LogicalLineHeight(context *guigui.Context, lineIndex, wrapWidth int) float64

LogicalLineHeight returns the rendered height of the lineIndex-th logical line when wrapped at wrapWidth. lineIndex must be in [0, Text.LineCount).

func (*Text) MaxCaretXOfLogicalLine

func (t *Text) MaxCaretXOfLogicalLine(context *guigui.Context, lineIndex, wrapWidth int) float64

MaxCaretXOfLogicalLine returns the maximum caret X coordinate over the visual lines of the lineIndex-th logical line when wrapped at wrapWidth. lineIndex must be in [0, Text.LineCount).

func (*Text) Measure

func (t *Text) Measure(context *guigui.Context, constraints guigui.Constraints) image.Point

func (*Text) MeasureBold

func (t *Text) MeasureBold(context *guigui.Context, constraints guigui.Constraints) image.Point

MeasureBold returns the size of the text under constraints as if it were rendered bold.

func (*Text) OnHandleButtonInput

func (t *Text) OnHandleButtonInput(f func(context *guigui.Context, widgetBounds *guigui.WidgetBounds) guigui.HandleInputResult)

func (*Text) OnHotspotDown

func (t *Text) OnHotspotDown(f func(context *guigui.Context, textRange TextRange))

OnHotspotDown sets the event handler that is called when the left mouse button is pressed on a hotspot range. The handler is given the pressed range.

func (*Text) OnHotspotUp

func (t *Text) OnHotspotUp(f func(context *guigui.Context, textRange TextRange))

OnHotspotUp sets the event handler that is called when the left mouse button is released on the hotspot range it was pressed on. For selectable text, the click is canceled when the cursor moved or a selection was made between the press and the release: a drag selects, it does not click. The handler is given the released range.

func (*Text) OnInsertionStyleReset

func (t *Text) OnInsertionStyleReset(f func(context *guigui.Context))

OnInsertionStyleReset sets an event handler invoked when the widget resets the insertion style: after applying it to inserted text, or when discarding it without applying, such as on a selection change. The handler is not invoked for Text.SetInsertionStyle.

func (*Text) OnScrollDelta

func (t *Text) OnScrollDelta(f func(context *guigui.Context, deltaX, deltaY float64))

OnScrollDelta registers a handler invoked when input handling needs the containing scrollable area to scroll by a delta in pixels.

func (*Text) OnScrollIntoView

func (t *Text) OnScrollIntoView(f func(context *guigui.Context, start, end CaretScrollTarget))

OnScrollIntoView registers a handler invoked when the selection needs to be brought into view. start and end are the selection endpoints (start <= end as byte indices); both are equal when the selection has zero width.

func (*Text) OnValueChanged

func (t *Text) OnValueChanged(f func(context *guigui.Context, text string, committed bool))

OnValueChanged sets the event handler that is called when the text value changes. The handler receives the current text and whether the change is committed. Dispatch and commit semantics are documented on the wrapping widget's OnValueChanged.

func (*Text) OnValueChangedWithoutText

func (t *Text) OnValueChangedWithoutText(f func(context *guigui.Context, committed bool))

OnValueChangedWithoutText sets a handler that fires under the same conditions as Text.OnValueChanged but is not given the current text, so the value is not materialized into a string on every change.

func (*Text) Paste

func (t *Text) Paste() bool

func (*Text) PasteWithoutStyles

func (t *Text) PasteWithoutStyles() bool

PasteWithoutStyles pastes the clipboard text without the ranged styles copied along with it, even when the widget is rich text editable. The inserted text adopts the style of the surrounding text.

func (*Text) ReadBaseStyle

func (t *Text) ReadBaseStyle(dst *textstyle.Style)

ReadBaseStyle writes the base style's overridable properties to dst.

func (*Text) ReadEffectiveStyleRuns

func (t *Text) ReadEffectiveStyleRuns(dst *textstyle.Runs)

ReadEffectiveStyleRuns replaces dst's runs with the effective styles of the whole value: the resolved base style with the ranged overrides merged on top.

func (*Text) ReadEffectiveStyleRunsInRange

func (t *Text) ReadEffectiveStyleRunsInRange(dst *textstyle.Runs, startInBytes, endInBytes int)

ReadEffectiveStyleRunsInRange replaces dst's runs with the effective styles of [startInBytes, endInBytes): the resolved base style with the ranged overrides merged on top, rebased so that startInBytes maps to 0.

func (*Text) ReadOverrideStyleRuns

func (t *Text) ReadOverrideStyleRuns(dst *textstyle.Runs)

ReadOverrideStyleRuns replaces dst's runs with a copy of the ranged style overrides, reflecting the adjustments made for edits since the overrides were set.

func (*Text) ReadOverrideStyleRunsInRange

func (t *Text) ReadOverrideStyleRunsInRange(dst *textstyle.Runs, startInBytes, endInBytes int)

ReadOverrideStyleRunsInRange replaces dst's runs with a copy of the ranged style overrides in [startInBytes, endInBytes), rebased so that startInBytes maps to 0.

func (*Text) ReadValueFrom

func (t *Text) ReadValueFrom(r io.Reader) (int64, error)

ReadValueFrom resets the value to the bytes read from r until EOF and returns the number of bytes read. The undo history is cleared and the selection is reset to (0, 0). On a non-EOF error, the value is reset to empty and the error is returned.

func (*Text) Redo

func (t *Text) Redo() bool

func (*Text) ReplaceOverrideStyleRunsInRange

func (t *Text) ReplaceOverrideStyleRunsInRange(runs *textstyle.Runs, startInBytes, endInBytes int, record bool)

ReplaceOverrideStyleRunsInRange replaces the ranged style overrides in [startInBytes, endInBytes) with runs' overrides in [0, endInBytes-startInBytes), shifted so that 0 maps to startInBytes. record sets whether the replacement is recorded in the undo history; a recorded replacement leaving the overrides unchanged records nothing.

func (*Text) ReplaceValueAtSelection

func (t *Text) ReplaceValueAtSelection(text string)

func (*Text) Scale

func (t *Text) Scale() float64

Scale returns the base text scale.

func (*Text) SelectAll

func (t *Text) SelectAll()

SelectAll selects the entire value.

func (*Text) Selection

func (t *Text) Selection() (start, end int)

func (*Text) SetBaseStyle

func (t *Text) SetBaseStyle(style textstyle.Style)

SetBaseStyle replaces the base style's overridable properties with style, except the font family, the text color and the language, which Text.SetFontFamily, Text.SetTextColor and Text.SetLang keep owning.

func (*Text) SetCaretBlinking

func (t *Text) SetCaretBlinking(caretBlinking bool)

SetCaretBlinking sets whether the caret blinks. The default value is true.

func (*Text) SetCaretColor

func (t *Text) SetCaretColor(clr color.Color)

SetCaretColor sets the concrete color of the caret.

func (*Text) SetCompositionColors

func (t *Text) SetCompositionColors(inactive, active color.Color)

SetCompositionColors sets the concrete colors of the underlines drawn below the inactive and active parts of an IME composition.

func (*Text) SetEditable

func (t *Text) SetEditable(editable bool)

func (*Text) SetEllipsisString

func (t *Text) SetEllipsisString(str string)

func (*Text) SetFirstLogicalLineInViewport

func (t *Text) SetFirstLogicalLineInViewport(idx int)

SetFirstLogicalLineInViewport sets the logical line that sits at widget-local Y=0. The default 0 means line 0 at the top; virtualizing parents set the topmost visible logical line so drawing, hit testing, and caret positioning need not walk the document prefix.

func (*Text) SetFontFamily

func (t *Text) SetFontFamily(fontFamily *font.Family)

SetFontFamily sets the resolved font family used to render the value. A nil family renders with the registered face source stack alone.

func (*Text) SetFontSize

func (t *Text) SetFontSize(size float64)

SetFontSize sets the font size at scale 1. The rendered size is the base size multiplied by the scale set via Text.SetScale.

func (*Text) SetHorizontalAlign

func (t *Text) SetHorizontalAlign(align textutil.HorizontalAlign)

func (*Text) SetHotspotRanges

func (t *Text) SetHotspotRanges(ranges []TextRange)

SetHotspotRanges sets the hotspot ranges: over their rectangles the cursor turns into a pointer, and mouse presses and releases fire the hotspot down and up events. The ranges follow the text through edits like the ranged styles. While the value is editable, the hotspots are inert.

func (*Text) SetInsertionStyle

func (t *Text) SetInsertionStyle(style textstyle.Style)

SetInsertionStyle replaces the insertion style with style. Its set properties are applied as ranged style overrides over the next text inserted at the caret, on top of the adopted overrides, and the style is then reset. The widget also resets it without applying on other interactions, such as a selection change, a deletion, or an undo; neither setting nor resetting is recorded in the undo history.

func (*Text) SetKeepTailingSpace

func (t *Text) SetKeepTailingSpace(keep bool)

SetKeepTailingSpace sets whether spaces at the end of a visual line keep their advance instead of collapsing.

func (*Text) SetLang

func (t *Text) SetLang(lang language.Tag)

SetLang sets the language used to select the face and its features when shaping the value.

func (*Text) SetLineHeight

func (t *Text) SetLineHeight(lineHeight float64)

SetLineHeight sets the line height at scale 1. The rendered line height is the base line height multiplied by the scale set via Text.SetScale.

func (*Text) SetLineHeightMode

func (t *Text) SetLineHeightMode(lineHeightMode textutil.LineHeightMode)

SetLineHeightMode sets how a visual line's height responds to the font sizes on it.

func (*Text) SetMaskRune

func (t *Text) SetMaskRune(maskRune rune)

SetMaskRune sets the character drawn in place of each grapheme cluster of the value. A non-zero rune masks the text and forces it to a single line; the zero value renders it normally.

func (*Text) SetMultiline

func (t *Text) SetMultiline(multiline bool)

func (*Text) SetPaddingForScrollOffset

func (t *Text) SetPaddingForScrollOffset(padding guigui.Padding)

SetPaddingForScrollOffset sets the padding kept between the caret and the viewport edges when scrolling the selection into view.

func (*Text) SetRichTextEditable

func (t *Text) SetRichTextEditable(richTextEditable bool)

SetRichTextEditable sets whether pasting applies the ranged styles copied along with the text. The default is false: pasting inserts plain text and the inserted text adopts the style of the surrounding text.

func (*Text) SetScale

func (t *Text) SetScale(scale float64)

SetScale sets the base text scale, which ranged scale overrides multiply.

func (*Text) SetSelectable

func (t *Text) SetSelectable(selectable bool)

func (*Text) SetSelection

func (t *Text) SetSelection(start, end int)

SetSelection sets the selection to [start, end) in bytes. Offsets inside a UTF-8 sequence are snapped to rune boundaries: an empty selection moves to the start of the rune containing it, and a non-empty one expands to cover whole runes.

func (*Text) SetSelectionColor

func (t *Text) SetSelectionColor(clr color.Color)

SetSelectionColor sets the concrete color of the selection highlight.

func (*Text) SetSelectionVisibleWhenUnfocused

func (t *Text) SetSelectionVisibleWhenUnfocused(visible bool)

SetSelectionVisibleWhenUnfocused sets whether the selection range stays drawn while the widget is not focused. The default is false.

func (*Text) SetSelectionWithSide

func (t *Text) SetSelectionWithSide(start, end int, shiftSide SelectionSide, adjustScroll bool)

SetSelectionWithSide sets the selection to the range spanned by start and end and records shiftSide as the endpoint moved by Shift and arrow keys.

func (*Text) SetTabWidth

func (t *Text) SetTabWidth(tabWidth float64)

func (*Text) SetTextColor

func (t *Text) SetTextColor(clr color.Color)

SetTextColor sets the concrete color the value is drawn in.

func (*Text) SetValue

func (t *Text) SetValue(text string)

func (*Text) SetVerticalAlign

func (t *Text) SetVerticalAlign(align textutil.VerticalAlign)

func (*Text) SetWrapMode

func (t *Text) SetWrapMode(wrapMode textutil.WrapMode)

SetWrapMode selects how visual lines wrap when text exceeds the available width. See textutil.WrapMode for the available modes.

func (*Text) SetWrapWidth

func (t *Text) SetWrapWidth(width int)

SetWrapWidth sets the width text wraps at, keeping wrapping tied to a viewport even when the widget bounds are widened to cover horizontally overflowing content. A non-positive width wraps at the widget bounds.

func (*Text) ShiftSelectionSide

func (t *Text) ShiftSelectionSide() SelectionSide

ShiftSelectionSide returns the selection endpoint moved by Shift and arrow keys.

func (*Text) Tick

func (t *Text) Tick(context *guigui.Context, widgetBounds *guigui.WidgetBounds) error

func (*Text) Undo

func (t *Text) Undo() bool

func (*Text) Value

func (t *Text) Value() string

Value returns the current value as a string.

func (*Text) VerticalAlign

func (t *Text) VerticalAlign() textutil.VerticalAlign

func (*Text) VisualLineCountOfLogicalLine

func (t *Text) VisualLineCountOfLogicalLine(context *guigui.Context, lineIndex, wrapWidth int) int

VisualLineCountOfLogicalLine returns the number of visual lines the lineIndex-th logical line occupies when wrapped at wrapWidth. lineIndex must be in [0, Text.LineCount).

func (*Text) WrapMode

func (t *Text) WrapMode() textutil.WrapMode

WrapMode reports how visual lines wrap when text exceeds the available width. The default is textutil.WrapModeNone.

func (*Text) WriteStateKey

func (t *Text) WriteStateKey(context *guigui.Context, w *guigui.StateKeyWriter)

func (*Text) WriteValueRangeTo

func (t *Text) WriteValueRangeTo(w io.Writer, startInBytes, endInBytes int) (int64, error)

WriteValueRangeTo writes the bytes of the current value in [startInBytes, endInBytes), clamped to the value, to w.

func (*Text) WriteValueTo

func (t *Text) WriteValueTo(w io.Writer) (int64, error)

WriteValueTo writes the current value to w and returns the number of bytes written.

type TextRange

type TextRange = piecetable.TextRange

TextRange is a byte range of a text value.

Jump to

Keyboard shortcuts

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