widget: add some utility methods on Editor v1 PROPOSED

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

 1 files changed, 15 insertions(+), 0 deletions(-)
Done.  See v2.

On Thu, Feb 6, 2020 at 2:17 AM Elias Naur <mail@eliasnaur.com> wrote:
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/9733/mbox | git am -3
Learn more about email & git
View this thread in the archives

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

- Focused: returns whether editor is focused
- CaretPos: returns the text line & column number, and the x & y pixel
  coordinates of the caret.

  Note: Returns them in that order. Which seems odd: line & column is
  basically "y, x", but then the pixel coordinates are x & y.  So
  wondering if swapping line & column in CaretPos (and possibly in
  Editor.layoutCaret which it calls) would be appropriate?
- NumLines: returns the number of text lines in the editor

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

diff --git a/widget/editor.go b/widget/editor.go
index 0e41c89..0af965b 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,10 @@ func (e *Editor) layoutText(s text.Shaper) ([]text.Line, layout.Dimensions) {
	return lines, dims

func (e *Editor) CaretPos() (carLine, carCol int, x fixed.Int26_6, y int) {
	return e.layoutCaret()

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

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

func (s ChangeEvent) isEditorEvent() {}
func (s SubmitEvent) isEditorEvent() {}