From ab9f42c820df21787bb29e44c9aab45c4db91d7b Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 25 Nov 2023 14:52:30 -0600 Subject: [PATCH] widget: [API] replace Focus methods with explicit FocusCmds Now that widgets by convention may be focused by issuing FocusCmd directly, remove the now redundant Focus methods on Clickable, Editor, Selectable. Signed-off-by: Elias Naur --- app/ime_test.go | 2 +- widget/button.go | 5 ----- widget/button_test.go | 4 ++-- widget/editor.go | 7 +------ widget/editor_test.go | 14 +++++++------- widget/selectable.go | 8 +------- widget/selectable_test.go | 2 +- 7 files changed, 13 insertions(+), 29 deletions(-) diff --git a/app/ime_test.go b/app/ime_test.go index f81208d0..fc2dc4c3 100644 --- a/app/ime_test.go +++ b/app/ime_test.go @@ -34,7 +34,7 @@ func FuzzIME(f *testing.F) { var r input.Router gtx := layout.Context{Ops: new(op.Ops), Source: r.Source()} - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) // Layout once to register focus. e.Layout(gtx, cache, font.Font{}, unit.Sp(10), op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) diff --git a/widget/button.go b/widget/button.go index 5b1454f0..28e43ad8 100644 --- a/widget/button.go +++ b/widget/button.go @@ -66,11 +66,6 @@ func (b *Clickable) Pressed() bool { return b.click.Pressed() } -// Focus requests the input focus for the element. -func (b *Clickable) Focus(gtx layout.Context) { - gtx.Execute(key.FocusCmd{Tag: b}) -} - // Focused reports whether b has focus. func (b *Clickable) Focused() bool { return b.focused diff --git a/widget/button_test.go b/widget/button_test.go index bc0ccf8d..7c8b52fd 100644 --- a/widget/button_test.go +++ b/widget/button_test.go @@ -38,7 +38,7 @@ func TestClickable(t *testing.T) { r.Frame(gtx.Ops) } // frame: request focus for button 1 - b1.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: &b1}) frame() // frame: gain focus for button 1 frame() @@ -78,7 +78,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(gtx) + gtx.Execute(key.FocusCmd{Tag: &b2}) frame() // frame: gain focus for button 2 frame() diff --git a/widget/editor.go b/widget/editor.go index 9cfb00b8..ed5f9e16 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -257,7 +257,7 @@ func (e *Editor) processPointer(gtx layout.Context) { X: int(math.Round(float64(evt.Position.X))), Y: int(math.Round(float64(evt.Position.Y))), }) - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) if e.scroller.State() != gesture.StateFlinging { e.scrollCaret = true } @@ -560,11 +560,6 @@ func (e *Editor) command(gtx layout.Context, k key.Event) { } } -// Focus requests the input focus for the Editor. -func (e *Editor) Focus(gtx layout.Context) { - gtx.Execute(key.FocusCmd{Tag: e}) -} - // Focused returns whether the editor is focused or not. func (e *Editor) Focused() bool { return e.focused diff --git a/widget/editor_test.go b/widget/editor_test.go index 6d09c140..ae204b5d 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -115,7 +115,7 @@ func TestEditorReadOnly(t *testing.T) { if cStart != cEnd { t.Errorf("unexpected initial caret positions") } - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) layoutEditor := func() layout.Dimensions { return e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) } @@ -514,7 +514,7 @@ func TestEditorDimensions(t *testing.T) { cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) fontSize := unit.Sp(10) font := font.Font{} - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue(key.EditEvent{Text: "A"}) @@ -906,7 +906,7 @@ g 2 4 6 8 g e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) e.Events() // throw away any events from this layout - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) r.Frame(gtx.Ops) gtx.Source = r.Source() // Build the selection events @@ -1000,7 +1000,7 @@ func TestSelectMove(t *testing.T) { fontSize := unit.Sp(10) // Layout once to populate e.lines and get focus. - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) // Set up selecton so the Editor key handler filters for all 4 directional keys. @@ -1090,7 +1090,7 @@ func TestEditor_MaxLen(t *testing.T) { cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) fontSize := unit.Sp(10) font := font.Font{} - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue( @@ -1126,7 +1126,7 @@ func TestEditor_Filter(t *testing.T) { cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) fontSize := unit.Sp(10) font := font.Font{} - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue( @@ -1156,7 +1156,7 @@ func TestEditor_Submit(t *testing.T) { cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) fontSize := unit.Sp(10) font := font.Font{} - e.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue( diff --git a/widget/selectable.go b/widget/selectable.go index 14497dce..f6956578 100644 --- a/widget/selectable.go +++ b/widget/selectable.go @@ -98,12 +98,6 @@ func (l *Selectable) initialize() { } } -// Focus requests the input focus for the label. -func (l *Selectable) Focus(gtx layout.Context) { - gtx.Execute(key.FocusCmd{Tag: l}) - gtx.Execute(key.SoftKeyboardCmd{Show: true}) -} - // Focused returns whether the label is focused or not. func (l *Selectable) Focused() bool { return l.focused @@ -240,7 +234,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.Focus(gtx) + gtx.Execute(key.FocusCmd{Tag: e}) 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 8c7d9795..b1db23c9 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(gtx) + gtx.Execute(key.FocusCmd{Tag: s}) 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{})