~eliasnaur/gio-patches

gio: io: improve InputOp focus and blur v1 PROPOSED

~inkeliz
~inkeliz: 1
 io: improve InputOp focus and blur

 4 files changed, 117 insertions(+), 80 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/15399/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH gio] io: improve InputOp focus and blur Export this patch

~inkeliz
From: Inkeliz <inkeliz@inkeliz.com>

The existing implementation cannot remove the focus of some widget,
doesn't have an option to focus without display the on-screen keyboard
and that automatically focus the first InputOp, aggressively.

That change aims to make possible: Remove focus from any widget. Add
focus without display the on-screen-keyboard/soft keyboard. Don't
automatically focus any widget. Don't recover focus when the widget is
visible again.

Fixes gio#180.

Signed-off-by: Inkeliz <inkeliz@inkeliz.com>
---
 internal/opconst/ops.go |  45 +++++++++--------
 io/key/key.go           |  37 ++++++++++----
 io/router/key.go        | 109 +++++++++++++++++++++++-----------------
 widget/editor.go        |   6 ++-
 4 files changed, 117 insertions(+), 80 deletions(-)

diff --git a/internal/opconst/ops.go b/internal/opconst/ops.go
index 80a3bc6..dba0f54 100644
--- a/internal/opconst/ops.go
+++ b/internal/opconst/ops.go
@@ -21,7 +21,8 @@ const (
	TypePointerInput
	TypePass
	TypeKeyInput
	TypeHideInput
	TypeKeyFocus
	TypeKeySoftKeyboard
	TypePush
	TypePop
	TypeAux
@@ -30,25 +31,26 @@ const (
)

const (
	TypeMacroLen          = 1 + 4 + 4
	TypeCallLen           = 1 + 4 + 4
	TypeTransformLen      = 1 + 4*6
	TypeLayerLen          = 1
	TypeRedrawLen         = 1 + 8
	TypeImageLen          = 1
	TypePaintLen          = 1
	TypeColorLen          = 1 + 4
	TypeLinearGradientLen = 1 + 8*2 + 4*2
	TypeAreaLen           = 1 + 1 + 4*4
	TypePointerInputLen   = 1 + 1 + 1
	TypePassLen           = 1 + 1
	TypeKeyInputLen       = 1 + 1
	TypeHideInputLen      = 1
	TypePushLen           = 1
	TypePopLen            = 1
	TypeAuxLen            = 1
	TypeClipLen           = 1 + 4*4 + 4 + 2 + 4
	TypeProfileLen        = 1
	TypeMacroLen           = 1 + 4 + 4
	TypeCallLen            = 1 + 4 + 4
	TypeTransformLen       = 1 + 4*6
	TypeLayerLen           = 1
	TypeRedrawLen          = 1 + 8
	TypeImageLen           = 1
	TypePaintLen           = 1
	TypeColorLen           = 1 + 4
	TypeLinearGradientLen  = 1 + 8*2 + 4*2
	TypeAreaLen            = 1 + 1 + 4*4
	TypePointerInputLen    = 1 + 1 + 1
	TypePassLen            = 1 + 1
	TypeKeyInputLen        = 1
	TypeKeyFocusLen        = 1 + 1
	TypeKeySoftKeyboardLen = 1 + 1
	TypePushLen            = 1
	TypePopLen             = 1
	TypeAuxLen             = 1
	TypeClipLen            = 1 + 4*4 + 4 + 2 + 4
	TypeProfileLen         = 1
)

func (t OpType) Size() int {
@@ -66,7 +68,8 @@ func (t OpType) Size() int {
		TypePointerInputLen,
		TypePassLen,
		TypeKeyInputLen,
		TypeHideInputLen,
		TypeKeyFocusLen,
		TypeKeySoftKeyboardLen,
		TypePushLen,
		TypePopLen,
		TypeAuxLen,
diff --git a/io/key/key.go b/io/key/key.go
index 09655b8..7ea4ab8 100644
--- a/io/key/key.go
+++ b/io/key/key.go
@@ -20,16 +20,23 @@ import (

// InputOp declares a handler ready for key events.
// Key events are in general only delivered to the
// focused key handler. Set the Focus flag to request
// the focus.
// focused key handler.
type InputOp struct {
	Tag   event.Tag
	Focus bool
	Tag event.Tag
}

// SoftKeyboardOp request to show or hide any
// on-screen keyboard.
type SoftKeyboardOp struct {
	Show bool
}

// HideInputOp request that any on screen text input
// be hidden.
type HideInputOp struct{}
// FocusOp if true it will declare the focus to the
// current InputOp. If  Focus is "False" it will remove
// any active focus.
type FocusOp struct {
	Focus bool
}

// A FocusEvent is generated when a handler gains or loses
// focus.
@@ -115,14 +122,22 @@ func (m Modifiers) Contain(m2 Modifiers) bool {
func (h InputOp) Add(o *op.Ops) {
	data := o.Write1(opconst.TypeKeyInputLen, h.Tag)
	data[0] = byte(opconst.TypeKeyInput)
	if h.Focus {
}

func (h SoftKeyboardOp) Add(o *op.Ops) {
	data := o.Write(opconst.TypeKeySoftKeyboardLen)
	data[0] = byte(opconst.TypeKeySoftKeyboard)
	if h.Show {
		data[1] = 1
	}
}

func (h HideInputOp) Add(o *op.Ops) {
	data := o.Write(opconst.TypeHideInputLen)
	data[0] = byte(opconst.TypeHideInput)
func (h FocusOp) Add(o *op.Ops) {
	data := o.Write(opconst.TypeKeyFocusLen)
	data[0] = byte(opconst.TypeKeyFocus)
	if h.Focus {
		data[1] = 1
	}
}

func (EditEvent) ImplementsEvent()  {}
diff --git a/io/router/key.go b/io/router/key.go
index a64544b..dc9bbdd 100644
--- a/io/router/key.go
+++ b/io/router/key.go
@@ -20,15 +20,17 @@ type keyQueue struct {
}

type keyHandler struct {
	active bool
	// visible will be true if the InputOp is present
	// in the current frame.
	visible bool
}

type listenerPriority uint8

const (
	priNone listenerPriority = iota
	priDefault
	priDefault listenerPriority = iota
	priCurrentFocus
	priNone
	priNewFocus
)

@@ -49,19 +51,18 @@ func (q *keyQueue) Frame(root *op.Ops, events *handlerEvents) {
		q.handlers = make(map[event.Tag]*keyHandler)
	}
	for _, h := range q.handlers {
		h.active = false
		h.visible = false
	}
	q.reader.Reset(root)
	focus, pri, hide := q.resolveFocus(events)

	focus, _, keyboard := q.resolveFocus(events)
	for k, h := range q.handlers {
		if !h.active {
		if k != focus && h.visible {
			delete(q.handlers, k)
			if q.focus == k {
				q.focus = nil
				hide = true
			}
			events.Add(k, key.FocusEvent{Focus: false})
		}
	}

	if focus != q.focus {
		if q.focus != nil {
			events.Add(q.focus, key.FocusEvent{Focus: false})
@@ -70,17 +71,11 @@ func (q *keyQueue) Frame(root *op.Ops, events *handlerEvents) {
		if q.focus != nil {
			events.Add(q.focus, key.FocusEvent{Focus: true})
		} else {
			hide = true
			keyboard = TextInputClose
		}
	}
	switch {
	case pri == priNewFocus:
		q.state = TextInputOpen
	case hide:
		q.state = TextInputClose
	default:
		q.state = TextInputKeep
	}

	q.state = keyboard
}

func (q *keyQueue) Push(e event.Event, events *handlerEvents) {
@@ -89,49 +84,52 @@ func (q *keyQueue) Push(e event.Event, events *handlerEvents) {
	}
}

func (q *keyQueue) resolveFocus(events *handlerEvents) (event.Tag, listenerPriority, bool) {
	var k event.Tag
	var pri listenerPriority
	var hide bool
func (q *keyQueue) resolveFocus(events *handlerEvents) (tag event.Tag, pri listenerPriority, keyboard TextInputState) {
loop:
	for encOp, ok := q.reader.Decode(); ok; encOp, ok = q.reader.Decode() {
		switch opconst.OpType(encOp.Data[0]) {
		case opconst.TypeKeyFocus:
			op := decodeFocusOp(encOp.Data, encOp.Refs)
			if op.Focus {
				pri = priNewFocus
			} else {
				pri, keyboard = priNone, TextInputClose
			}
		case opconst.TypeKeySoftKeyboard:
			op := decodeSoftKeyboardOp(encOp.Data, encOp.Refs)
			if op.Show {
				keyboard = TextInputOpen
			} else {
				keyboard = TextInputClose
			}
		case opconst.TypeKeyInput:
			op := decodeKeyInputOp(encOp.Data, encOp.Refs)
			var newPri listenerPriority
			switch {
			case op.Focus:
				newPri = priNewFocus
			case op.Tag == q.focus:
				newPri = priCurrentFocus
			default:
				newPri = priDefault
			}
			// Switch focus if higher priority or if focus requested.
			if newPri.replaces(pri) {
				k, pri = op.Tag, newPri

			tag = op.Tag
			if tag == q.focus {
				pri = priCurrentFocus
			}
			h, ok := q.handlers[op.Tag]

			h, ok := q.handlers[tag]
			if !ok {
				h = new(keyHandler)
				q.handlers[op.Tag] = h
				// Reset the handler on (each) first appearance.
				events.Add(op.Tag, key.FocusEvent{Focus: false})
				q.handlers[tag] = h
			}
			h.active = true
		case opconst.TypeHideInput:
			hide = true
			h.visible = true
		case opconst.TypePush:
			newK, newPri, h := q.resolveFocus(events)
			hide = hide || h
			newK, newPri, newKeyboard := q.resolveFocus(events)
			if newKeyboard > keyboard {
				keyboard = newKeyboard
			}
			if newPri.replaces(pri) {
				k, pri = newK, newPri
				tag, pri = newK, newPri
			}
		case opconst.TypePop:
			break loop
		}
	}
	return k, pri, hide

	return tag, pri, keyboard
}

func (p listenerPriority) replaces(p2 listenerPriority) bool {
@@ -144,7 +142,24 @@ func decodeKeyInputOp(d []byte, refs []interface{}) key.InputOp {
		panic("invalid op")
	}
	return key.InputOp{
		Tag:   refs[0].(event.Tag),
		Tag: refs[0].(event.Tag),
	}
}

func decodeSoftKeyboardOp(d []byte, refs []interface{}) key.SoftKeyboardOp {
	if opconst.OpType(d[0]) != opconst.TypeKeySoftKeyboard {
		panic("invalid op")
	}
	return key.SoftKeyboardOp{
		Show: d[1] != 0,
	}
}

func decodeFocusOp(d []byte, refs []interface{}) key.FocusOp {
	if opconst.OpType(d[0]) != opconst.TypeKeyFocus {
		panic("invalid op")
	}
	return key.FocusOp{
		Focus: d[1] != 0,
	}
}
diff --git a/widget/editor.go b/widget/editor.go
index 10f7ae0..cfd88bc 100644
--- a/widget/editor.go
+++ b/widget/editor.go
@@ -405,7 +405,11 @@ func (e *Editor) layout(gtx layout.Context) layout.Dimensions {
		e.shapes = append(e.shapes, line{off, path})
	}

	key.InputOp{Tag: &e.eventKey, Focus: e.requestFocus}.Add(gtx.Ops)
	key.InputOp{Tag: &e.eventKey}.Add(gtx.Ops)
	if e.requestFocus {
		key.FocusOp{Focus: true}.Add(gtx.Ops)
		key.SoftKeyboardOp{Show: true}.Add(gtx.Ops)
	}
	e.requestFocus = false
	pointerPadding := gtx.Px(unit.Dp(4))
	r := image.Rectangle{Max: e.viewSize}
-- 
2.26.2