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 <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-11-25 14:52:30 -06:00
parent 6dcebf205f
commit ab9f42c820
7 changed files with 13 additions and 29 deletions
+1 -1
View File
@@ -34,7 +34,7 @@ func FuzzIME(f *testing.F) {
var r input.Router var r input.Router
gtx := layout.Context{Ops: new(op.Ops), Source: r.Source()} gtx := layout.Context{Ops: new(op.Ops), Source: r.Source()}
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
// Layout once to register focus. // Layout once to register focus.
e.Layout(gtx, cache, font.Font{}, unit.Sp(10), op.CallOp{}, op.CallOp{}) e.Layout(gtx, cache, font.Font{}, unit.Sp(10), op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
-5
View File
@@ -66,11 +66,6 @@ func (b *Clickable) Pressed() bool {
return b.click.Pressed() 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. // Focused reports whether b has focus.
func (b *Clickable) Focused() bool { func (b *Clickable) Focused() bool {
return b.focused return b.focused
+2 -2
View File
@@ -38,7 +38,7 @@ func TestClickable(t *testing.T) {
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
} }
// frame: request focus for button 1 // frame: request focus for button 1
b1.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: &b1})
frame() frame()
// frame: gain focus for button 1 // frame: gain focus for button 1
frame() frame()
@@ -78,7 +78,7 @@ func TestClickable(t *testing.T) {
t.Error("button 1 got clicked, even if it only got return press") t.Error("button 1 got clicked, even if it only got return press")
} }
// frame: request focus for button 2 // frame: request focus for button 2
b2.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: &b2})
frame() frame()
// frame: gain focus for button 2 // frame: gain focus for button 2
frame() frame()
+1 -6
View File
@@ -257,7 +257,7 @@ func (e *Editor) processPointer(gtx layout.Context) {
X: int(math.Round(float64(evt.Position.X))), X: int(math.Round(float64(evt.Position.X))),
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
}) })
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
if e.scroller.State() != gesture.StateFlinging { if e.scroller.State() != gesture.StateFlinging {
e.scrollCaret = true 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. // Focused returns whether the editor is focused or not.
func (e *Editor) Focused() bool { func (e *Editor) Focused() bool {
return e.focused return e.focused
+7 -7
View File
@@ -115,7 +115,7 @@ func TestEditorReadOnly(t *testing.T) {
if cStart != cEnd { if cStart != cEnd {
t.Errorf("unexpected initial caret positions") t.Errorf("unexpected initial caret positions")
} }
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
layoutEditor := func() layout.Dimensions { layoutEditor := func() layout.Dimensions {
return e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) 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())) cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
fontSize := unit.Sp(10) fontSize := unit.Sp(10)
font := font.Font{} font := font.Font{}
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
r.Queue(key.EditEvent{Text: "A"}) 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.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
e.Events() // throw away any events from this layout e.Events() // throw away any events from this layout
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
gtx.Source = r.Source() gtx.Source = r.Source()
// Build the selection events // Build the selection events
@@ -1000,7 +1000,7 @@ func TestSelectMove(t *testing.T) {
fontSize := unit.Sp(10) fontSize := unit.Sp(10)
// Layout once to populate e.lines and get focus. // 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{}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
// Set up selecton so the Editor key handler filters for all 4 directional keys. // 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())) cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
fontSize := unit.Sp(10) fontSize := unit.Sp(10)
font := font.Font{} font := font.Font{}
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
r.Queue( r.Queue(
@@ -1126,7 +1126,7 @@ func TestEditor_Filter(t *testing.T) {
cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
fontSize := unit.Sp(10) fontSize := unit.Sp(10)
font := font.Font{} font := font.Font{}
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
r.Queue( r.Queue(
@@ -1156,7 +1156,7 @@ func TestEditor_Submit(t *testing.T) {
cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection()))
fontSize := unit.Sp(10) fontSize := unit.Sp(10)
font := font.Font{} font := font.Font{}
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{})
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
r.Queue( r.Queue(
+1 -7
View File
@@ -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. // Focused returns whether the label is focused or not.
func (l *Selectable) Focused() bool { func (l *Selectable) Focused() bool {
return l.focused return l.focused
@@ -240,7 +234,7 @@ func (e *Selectable) processPointer(gtx layout.Context) {
X: int(math.Round(float64(evt.Position.X))), X: int(math.Round(float64(evt.Position.X))),
Y: int(math.Round(float64(evt.Position.Y))), Y: int(math.Round(float64(evt.Position.Y))),
}) })
e.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: e})
if evt.Modifiers == key.ModShift { if evt.Modifiers == key.ModShift {
start, end := e.text.Selection() start, end := e.text.Selection()
// If they clicked closer to the end, then change the end to // If they clicked closer to the end, then change the end to
+1 -1
View File
@@ -49,7 +49,7 @@ func TestSelectableMove(t *testing.T) {
// Layout once to populate e.lines and get focus. // Layout once to populate e.lines and get focus.
s := new(Selectable) s := new(Selectable)
s.Focus(gtx) gtx.Execute(key.FocusCmd{Tag: s})
s.SetText(str) s.SetText(str)
// Set up selection so the Selectable filters for all 4 directional keys. // Set up selection so the Selectable filters for all 4 directional keys.
s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{}) s.Layout(gtx, cache, font.Font{}, fontSize, op.CallOp{}, op.CallOp{})