类型 TextareaLayout

API 文档由 Go 源码生成,内容为英文。

type TextareaLayout struct {

	// the width rows were wrapped to

	// What the last paint showed.

	// The caret the view last followed.

	// A run of Up and Down keeps the column the caret began at.

	// A caret at the end of a wrapped row is also the start of the next; it is
	// drawn at the end of the first when it got there by going to that end.

}

TextareaLayout is the visual-row layout of one Textarea, held by the application next to its editor and given to every paint through TextareaSnapshot.Layout. The text and its byte offsets stay the application's; the layout knows only where they fall on the screen:

  • it keeps the wrapped rows and the scroll position between frames, so a steady paint builds nothing, and scrolls to keep the caret in view when the caret moves;

  • Move turns an arrow, Home, End or Page key into the byte offset it leads to, keeping the column of a run of Up and Down;

  • OffsetAt and CaretBounds, which the runtime reads through the hit region (ui.TextPlacement), put the caret where a press lands and the input-method window where the caret is;

  • ContentHeight sizes a box that grows with its text.

The zero value is ready. It is not safe for concurrent use, and one layout serves one textarea.

方法

CaretBounds#

func (layout *TextareaLayout) CaretBounds() (ui.Rect, bool)

CaretBounds is the row the caret was painted on, one pixel wide, in window coordinates; false while the caret is scrolled out of view. It implements ui.TextPlacement.

ContentHeight#

func (layout *TextareaLayout) ContentHeight(canvas *ui.Canvas, theme ui.Theme, text string, width float32, noWrap bool) float32

ContentHeight is the height a box of outer width width needs to show text without scrolling, never below the 64 pixels of the minimum: the `field-sizing: content` of shadcn's textarea. The application caps it where it wants the box to start scrolling.

Move#

func (layout *TextareaLayout) Move(text string, caret int, movement TextareaMovement) int

Move returns the byte offset of text that movement leads to from caret, laid out as the last paint laid it out. Up on the first row goes to the start of the text and Down on the last to its end. It records the column of a run of vertical moves and where a caret at the end of a wrapped row is drawn, so call it for every movement and then put the caret at the offset it returns.

OffsetAt#

func (layout *TextareaLayout) OffsetAt(point ui.Point) (int, bool)

OffsetAt is the byte offset of the caret position nearest to a window point, for the runtime to place the caret on a press or drag. It implements ui.TextPlacement.

RowCount#

func (layout *TextareaLayout) RowCount(canvas *ui.Canvas, theme ui.Theme, text string, width float32, noWrap bool) int

RowCount is how many rows text takes in a box of outer width width: one for each line that fits and as many as a longer one wraps to, and an empty one after a final newline. The rows it wraps are the ones the next paint at that width uses.

ScrollBy#

func (layout *TextareaLayout) ScrollBy(dy float32)

ScrollBy scrolls the box by dy pixels, down when positive, as a wheel does. The next paint keeps the offset until the caret moves.