From e0cf570339bdf82bbb634ccba467ed238f0a5d52 Mon Sep 17 00:00:00 2001 From: Larry Clapp Date: Mon, 16 Jan 2023 12:27:25 -0500 Subject: [PATCH] widget: add a Focus() method to widget.Clickable Signed-off-by: Larry Clapp --- widget/button.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/widget/button.go b/widget/button.go index e84d4bc9..dcd81180 100644 --- a/widget/button.go +++ b/widget/button.go @@ -25,8 +25,9 @@ type Clickable struct { prevClicks int history []Press - keyTag struct{} - focused bool + keyTag struct{} + requestFocus bool + focused bool } // Click represents a click. @@ -80,6 +81,11 @@ func (b *Clickable) Pressed() bool { return b.click.Pressed() } +// Focus requests the input focus for the element. +func (b *Clickable) Focus() { + b.requestFocus = true +} + // Focused reports whether b has focus. func (b *Clickable) Focused() bool { return b.focused @@ -115,6 +121,10 @@ func (b *Clickable) Layout(gtx layout.Context, w layout.Widget) layout.Dimension keys = "" } key.InputOp{Tag: &b.keyTag, Keys: keys}.Add(gtx.Ops) + if b.requestFocus { + key.FocusOp{Tag: &b.keyTag}.Add(gtx.Ops) + b.requestFocus = false + } } else { b.focused = false }