From 627e028d3cf3bdceecb88cb6873e36d503f483f8 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Fri, 20 Oct 2023 21:00:09 -0500 Subject: [PATCH] widget: [API] re-implement Clickable.Focus with a command Signed-off-by: Elias Naur --- widget/button.go | 9 ++------- widget/button_test.go | 4 ++-- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/widget/button.go b/widget/button.go index 90070dd0..5e97850c 100644 --- a/widget/button.go +++ b/widget/button.go @@ -23,7 +23,6 @@ type Clickable struct { history []Press keyTag struct{} - requestFocus bool requestClicks int focused bool pressedKey string @@ -79,8 +78,8 @@ func (b *Clickable) Pressed() bool { } // Focus requests the input focus for the element. -func (b *Clickable) Focus() { - b.requestFocus = true +func (b *Clickable) Focus(gtx layout.Context) { + gtx.Queue(key.FocusCmd{Tag: &b.keyTag}) } // Focused reports whether b has focus. @@ -121,10 +120,6 @@ func (b *Clickable) Update(gtx layout.Context) []Click { if !gtx.Enabled() { b.focused = false } - if b.requestFocus { - gtx.Queue(key.FocusCmd{Tag: &b.keyTag}) - b.requestFocus = false - } for len(b.history) > 0 { c := b.history[0] if c.End.IsZero() || gtx.Now.Sub(c.End) < 1*time.Second { diff --git a/widget/button_test.go b/widget/button_test.go index 92c9e3f9..caf104b0 100644 --- a/widget/button_test.go +++ b/widget/button_test.go @@ -37,7 +37,7 @@ func TestClickable(t *testing.T) { r.Frame(gtx.Ops) } // frame: request focus for button 1 - b1.Focus() + b1.Focus(gtx) frame() // frame: gain focus for button 1 frame() @@ -77,7 +77,7 @@ func TestClickable(t *testing.T) { t.Error("button 1 got clicked, even if it only got return press") } // frame: request focus for button 2 - b2.Focus() + b2.Focus(gtx) frame() // frame: gain focus for button 2 frame()