From eed93aaffef68a543e3687985a1fe711dd5f2a59 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 9 Oct 2023 17:08:47 -0500 Subject: [PATCH] io/input,io/key: [API] replace SnippetOp with command Signed-off-by: Elias Naur --- internal/ops/ops.go | 3 --- io/input/key.go | 6 +++--- io/input/router.go | 14 ++------------ io/key/key.go | 22 +++++++--------------- widget/editor.go | 7 ++----- 5 files changed, 14 insertions(+), 38 deletions(-) diff --git a/internal/ops/ops.go b/internal/ops/ops.go index 92ccc5d2..bf87e5fa 100644 --- a/internal/ops/ops.go +++ b/internal/ops/ops.go @@ -82,7 +82,6 @@ const ( TypeSemanticClass TypeSemanticSelected TypeSemanticEnabled - TypeSnippet TypeActionInput ) @@ -164,7 +163,6 @@ const ( TypeSemanticClassLen = 2 TypeSemanticSelectedLen = 2 TypeSemanticEnabledLen = 2 - TypeSnippetLen = 1 + 4 + 4 TypeActionInputLen = 1 + 1 ) @@ -444,7 +442,6 @@ var opProps = [0x100]opProp{ TypeSemanticClass: {Size: TypeSemanticClassLen, NumRefs: 0}, TypeSemanticSelected: {Size: TypeSemanticSelectedLen, NumRefs: 0}, TypeSemanticEnabled: {Size: TypeSemanticEnabledLen, NumRefs: 0}, - TypeSnippet: {Size: TypeSnippetLen, NumRefs: 2}, TypeActionInput: {Size: TypeActionInputLen, NumRefs: 0}, } diff --git a/io/input/key.go b/io/input/key.go index 07d900ae..5999652e 100644 --- a/io/input/key.go +++ b/io/input/key.go @@ -315,9 +315,9 @@ func (q *keyQueue) editorState() EditorState { return s } -func (q *keyQueue) snippetOp(op key.SnippetOp) { - if op.Tag == q.focus { - q.content.Snippet = op.Snippet +func (q *keyQueue) setSnippet(req key.SnippetCmd) { + if req.Tag == q.focus { + q.content.Snippet = req.Snippet } } diff --git a/io/input/router.go b/io/input/router.go index dbcc7f18..103a1c35 100644 --- a/io/input/router.go +++ b/io/input/router.go @@ -219,6 +219,8 @@ func (q *Router) executeCommands() { q.key.queue.Focus(req.Tag, &q.handlers) case key.SoftKeyboardCmd: q.key.queue.softKeyboard(req.Show) + case key.SnippetCmd: + q.key.queue.setSnippet(req) } } q.commands = nil @@ -518,18 +520,6 @@ func (q *Router) collect() { b := pc.currentAreaBounds() pc.keyInputOp(op) kq.inputOp(op, t, a, b) - case ops.TypeSnippet: - op := key.SnippetOp{ - Tag: encOp.Refs[0].(event.Tag), - Snippet: key.Snippet{ - Range: key.Range{ - Start: int(int32(bo.Uint32(encOp.Data[1:]))), - End: int(int32(bo.Uint32(encOp.Data[5:]))), - }, - Text: *(encOp.Refs[1].(*string)), - }, - } - kq.snippetOp(op) // Semantic ops. case ops.TypeSemanticLabel: diff --git a/io/key/key.go b/io/key/key.go index 40f27879..b71a2531 100644 --- a/io/key/key.go +++ b/io/key/key.go @@ -10,7 +10,6 @@ events. package key import ( - "encoding/binary" "strings" "gioui.org/f32" @@ -45,6 +44,12 @@ type SelectionCmd struct { Caret } +// SnippetCmd updates the content snippet for an input handler. +type SnippetCmd struct { + Tag event.Tag + Snippet +} + // Set is an expression that describes a set of key combinations, in the form // "-|...". Modifiers are separated by dashes, optional // modifiers are enclosed by parentheses. A key set is either a literal key @@ -61,12 +66,6 @@ type SelectionCmd struct { // - Shift-(Ctrl)-A matches A if shift is pressed, and optionally ctrl. type Set string -// SnippetOp updates the content snippet for an input handler. -type SnippetOp struct { - Tag event.Tag - Snippet -} - // Range represents a range of text, such as an editor's selection. // Start and End are in runes. type Range struct { @@ -335,14 +334,6 @@ func (h InputOp) Add(o *op.Ops) { data[1] = byte(h.Hint) } -func (s SnippetOp) Add(o *op.Ops) { - data := ops.Write2String(&o.Internal, ops.TypeSnippetLen, s.Tag, s.Text) - data[0] = byte(ops.TypeSnippet) - bo := binary.LittleEndian - bo.PutUint32(data[1:], uint32(s.Range.Start)) - bo.PutUint32(data[5:], uint32(s.Range.End)) -} - func (EditEvent) ImplementsEvent() {} func (Event) ImplementsEvent() {} func (FocusEvent) ImplementsEvent() {} @@ -352,6 +343,7 @@ func (SelectionEvent) ImplementsEvent() {} func (FocusCmd) ImplementsCommand() {} func (SoftKeyboardCmd) ImplementsCommand() {} func (SelectionCmd) ImplementsCommand() {} +func (SnippetCmd) ImplementsCommand() {} func (m Modifiers) String() string { var strs []string diff --git a/widget/editor.go b/widget/editor.go index df98bad1..c9e8866e 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -554,7 +554,7 @@ func (e *Editor) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, siz return e.layout(gtx, textMaterial, selectMaterial) } -// updateSnippet adds a key.SnippetOp if the snippet content or position +// updateSnippet queues a key.SnippetCmd if the snippet content or position // have changed. off and len are in runes. func (e *Editor) updateSnippet(gtx layout.Context, start, end int) { if start > end { @@ -594,10 +594,7 @@ func (e *Editor) updateSnippet(gtx layout.Context, start, end int) { return } e.ime.snippet = newSnip - key.SnippetOp{ - Tag: &e.eventKey, - Snippet: newSnip, - }.Add(gtx.Ops) + gtx.Queue(key.SnippetCmd{Tag: &e.eventKey, Snippet: newSnip}) } func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.CallOp) layout.Dimensions {