From 9de80749e12254ef4e54a019a1291ab6e0a51132 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 20 Oct 2023 20:58:36 -0500 Subject: [PATCH] widget: [API] re-implement Selectable.Focus in terms of commands Signed-off-by: Elias Naur --- widget/selectable.go | 29 ++++++++++++----------------- widget/selectable_test.go | 2 +- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/widget/selectable.go b/widget/selectable.go index 80fde43b..10fe805d 100644 --- a/widget/selectable.go +++ b/widget/selectable.go @@ -70,15 +70,14 @@ type Selectable struct { source stringSource // scratch is a buffer reused to efficiently read text out of the // textView. - scratch []byte - lastValue string - text textView - focused bool - requestFocus bool - dragging bool - dragger gesture.Drag - scroller gesture.Scroll - scrollOff image.Point + scratch []byte + lastValue string + text textView + focused bool + dragging bool + dragger gesture.Drag + scroller gesture.Scroll + scrollOff image.Point clicker gesture.Click // events is the list of events not yet processed. @@ -99,8 +98,9 @@ func (l *Selectable) initialize() { } // Focus requests the input focus for the label. -func (l *Selectable) Focus() { - l.requestFocus = true +func (l *Selectable) Focus(gtx layout.Context) { + gtx.Queue(key.FocusCmd{Tag: l}) + gtx.Queue(key.SoftKeyboardCmd{Show: true}) } // Focused returns whether the label is focused or not. @@ -209,11 +209,6 @@ func (l *Selectable) Layout(gtx layout.Context, lt *text.Shaper, font font.Font, keys = keyFilter } key.InputOp{Tag: l, Keys: keys}.Add(gtx.Ops) - if l.requestFocus { - gtx.Queue(key.FocusCmd{Tag: l}) - gtx.Queue(key.SoftKeyboardCmd{Show: true}) - } - l.requestFocus = false l.clicker.Add(gtx.Ops) l.dragger.Add(gtx.Ops) @@ -249,7 +244,7 @@ func (e *Selectable) processPointer(gtx layout.Context) { X: int(math.Round(float64(evt.Position.X))), Y: int(math.Round(float64(evt.Position.Y))), }) - e.requestFocus = true + e.Focus(gtx) if evt.Modifiers == key.ModShift { start, end := e.text.Selection() // If they clicked closer to the end, then change the end to diff --git a/widget/selectable_test.go b/widget/selectable_test.go index b4f514ca..cb5defeb 100644 --- a/widget/selectable_test.go +++ b/widget/selectable_test.go @@ -49,7 +49,7 @@ func TestSelectableMove(t *testing.T) { // Layout once to populate e.lines and get focus. s := new(Selectable) - s.Focus() + s.Focus(gtx) s.SetText(str) // Set up selection so the Selectable filters for all 4 directional keys. s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})