~eliasnaur/gio-patches

widget: add some utility methods on Editor v2 PROPOSED

Larry Clapp: 1
 widget: add some utility methods on Editor

 1 files changed, 24 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~eliasnaur/gio-patches/patches/9737/mbox | git am -3
Learn more about email & git

[PATCH v2] widget: add some utility methods on Editor Export this patch

- Focused: returns whether editor is focused
- CaretPos: returns the text line & column numbers of the caret.
- CaretCoords: returns the x & y pixel coordinates of the caret.
- NumLines: returns the number of text lines in the editor

Signed-off-by: Larry Clapp <larry@theclapp.org>
---
 widget/editor.go | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/widget/editor.go b/widget/editor.go
index 0e41c89..35bac2e 100644
--- a/widget/editor.go
+++ b/widget/editor.go
@@ -224,6 +224,11 @@ func (e *Editor) Focus() {
	e.requestFocus = true
}

// Focused returns whether the editor is focused or not.
func (e *Editor) Focused() bool {
	return e.focused
}

// Layout lays out the editor.
func (e *Editor) Layout(gtx *layout.Context, sh text.Shaper, font text.Font, size unit.Value) {
	// Flush events from before the previous frame.
@@ -456,6 +461,19 @@ func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
	return lines, dims
}

// CaretPos returns the line & column numbers of the caret.
func (e *Editor) CaretPos() (line, col int) {
	line, col, _, _ = e.layoutCaret()
	return
}

// CaretCoords returns the x & y coordinates of the caret, relative to the
// editor itself.
func (e *Editor) CaretCoords() (x fixed.Int26_6, y int) {
	_, _, x, y = e.layoutCaret()
	return
}

func (e *Editor) layoutCaret() (carLine, carCol int, x fixed.Int26_6, y int) {
	var idx int
	var prevDesc fixed.Int26_6
@@ -647,5 +665,11 @@ func (e *Editor) scrollToCaret() {
	}
}

// NumLines returns the number of lines in the editor.
func (e *Editor) NumLines() int {
	e.makeValid()
	return len(e.lines)
}

func (s ChangeEvent) isEditorEvent() {}
func (s SubmitEvent) isEditorEvent() {}
-- 
2.23.0
Thanks, applied.
View this thread in the archives