editor

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 35 Imported by: 2

README

Most of the code in this package was copied from the Gio project.

Changes made is listed below:

  1. Enable the input of Tab.
  2. Exported APIs to calculate viewport offset and setting scrollbar offset.
  3. Added colorful glyphs painting.

Documentation

Overview

source codes of this package are copied from package gioui.org/widget and modified

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Disabled

func Disabled(c color.NRGBA) (d color.NRGBA)

Disabled blends color towards the luminance and multiplies alpha. Blending towards luminance will desaturate the color. Multiplying alpha blends the color together more with the background.

func MulAlpha

func MulAlpha(c color.NRGBA, alpha uint8) color.NRGBA

MulAlpha applies the alpha to the color.

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 unit.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
	// Submit enabled translation of carriage return keys to SubmitEvents.
	// If not enabled, carriage returns are inserted as newlines in the text.
	Submit 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

	// 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

	// Keep editor focused is set to true, the editor will keep the focus.
	// This is useful when the editor is used with a menu. so even when menu is focused, the editor will highlight the
	// selected text.
	KeepFocus bool
	// 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 unit.Sp, textMaterial, selectMaterial op.CallOp, lineMaterial op.CallOp, matchMaterial 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) NextMatch added in v0.8.0

func (e *Editor) NextMatch(index int)

NextMatch is used to switch between the [MatchRange]s. This also selects the next match and causes the selection background drawn under the matched text.

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) ReplaceAll added in v0.8.0

func (e *Editor) ReplaceAll(newStr string) int

ReplaceAll assumes a context of "Find & Replace". newStr applies to a list of text MatchRange, and the matched text is replaced with newStr one by one. The number of replacement is saved to be used during undo/redo. It returns the number of occurrences replaced.

func (*Editor) ScrollByRatio

func (e *Editor) ScrollByRatio(gtx layout.Context, ratio float32)

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) SetMatches added in v0.8.0

func (e *Editor) SetMatches(matches []MatchRange)

SetMatches sets the matched text ranges after a find operation.

func (*Editor) SetText

func (e *Editor) SetText(s string, addHistory bool)

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) UpdateTextStyles

func (e *Editor) UpdateTextStyles(styles []*TextStyle)

func (*Editor) ViewPortRatio

func (e *Editor) ViewPortRatio() (float32, float32)

returns start and end offset ratio of viewport

func (*Editor) VisibleLines added in v0.5.0

func (e *Editor) VisibleLines() ([]*LineInfo, error)

func (*Editor) WriteTo

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

WriteTo implements io.WriterTo.

type EditorConf added in v0.0.2

type EditorConf struct {
	Shaper             *text.Shaper
	TextColor          color.NRGBA
	Bg                 color.NRGBA
	SelectionColor     color.NRGBA
	LineHighlightColor color.NRGBA
	LineNumberColor    color.NRGBA
	TextMatchColor     color.NRGBA
	// typeface for editing
	TypeFace        font.Typeface
	TextSize        unit.Sp
	Weight          font.Weight
	LineHeight      unit.Sp
	LineHeightScale float32
	//May be helpful for code syntax highlighting.
	ColorScheme string
	ShowLineNum bool
	// padding between line number and the editor content.
	LineNumPadding unit.Dp
}

type EditorEvent

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

type EditorStyle

type EditorStyle struct {
	Font font.Font
	// LineHeight controls the distance between the baselines of lines of text.
	// If zero, a sensible default will be used.
	LineHeight unit.Sp
	// LineHeightScale applies a scaling factor to the LineHeight. If zero, a
	// sensible default will be used.
	LineHeightScale float32
	TextSize        unit.Sp
	// Color is the text color.
	Color color.NRGBA
	// Hint contains the text displayed when the editor is empty.
	Hint string
	// HintColor is the color of hint text.
	HintColor color.NRGBA
	// SelectionColor is the color of the background for selected text.
	SelectionColor color.NRGBA
	//LineHighlightColor is the color used to highlight the clicked logical line.
	// If not set, line will not be highlighted.
	LineHighlightColor color.NRGBA
	// TextMatchColor use the color used to highlight the matched substring.
	TextMatchColor color.NRGBA

	Editor      *Editor
	ShowLineNum bool
	// contains filtered or unexported fields
}

func NewEditor

func NewEditor(editor *Editor, conf *EditorConf, hint string) EditorStyle

func (EditorStyle) Layout

func (e EditorStyle) Layout(gtx layout.Context) layout.Dimensions

type LineInfo added in v0.5.0

type LineInfo struct {
	// line number starting from 1.
	LineNum int
	// offset in the cross axis.
	YOffset int
	// offset of the start rune the line.
	Start int
	// offset of the end rune the line.
	End int
}

type MatchRange added in v0.8.0

type MatchRange struct {
	// offset of the start rune the match.
	Start int
	// offset of the end rune the match.
	End int
}

Matched substring range.

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 SubmitEvent added in v0.5.0

type SubmitEvent struct {
	Text string
}

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

type TextStyle

type TextStyle struct {
	Line int
	// offset of start in the text
	Start int
	// offset of end in the text
	End        int
	Color      op.CallOp
	Background op.CallOp
}

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