diff --git a/app/ime_test.go b/app/ime_test.go index f0fb511a..f81208d0 100644 --- a/app/ime_test.go +++ b/app/ime_test.go @@ -31,10 +31,10 @@ func FuzzIME(f *testing.F) { f.Fuzz(func(t *testing.T, cmds []byte) { cache := text.NewShaper(text.WithCollection(gofont.Collection())) e := new(widget.Editor) - e.Focus() var r input.Router gtx := layout.Context{Ops: new(op.Ops), Source: r.Source()} + e.Focus(gtx) // 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/editor.go b/widget/editor.go index adc029a2..b86411cc 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -69,11 +69,10 @@ type Editor struct { buffer *editBuffer // scratch is a byte buffer that is reused to efficiently read portions of text // from the textView. - scratch []byte - eventKey int - blinkStart time.Time - focused bool - requestFocus bool + scratch []byte + eventKey int + blinkStart time.Time + focused bool // ime tracks the state relevant to input methods. ime struct { @@ -246,7 +245,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.requestFocus = true + e.Focus(gtx) if e.scroller.State() != gesture.StateFlinging { e.scrollCaret = true } @@ -492,8 +491,9 @@ func (e *Editor) command(gtx layout.Context, k key.Event) { } // Focus requests the input focus for the Editor. -func (e *Editor) Focus() { - e.requestFocus = true +func (e *Editor) Focus(gtx layout.Context) { + gtx.Queue(key.FocusCmd{Tag: &e.eventKey}) + gtx.Queue(key.SoftKeyboardCmd{Show: true}) } // Focused returns whether the editor is focused or not. @@ -644,11 +644,6 @@ func (e *Editor) layout(gtx layout.Context, textMaterial, selectMaterial op.Call } } key.InputOp{Tag: &e.eventKey, Hint: e.InputHint, Keys: keys}.Add(gtx.Ops) - if e.requestFocus { - gtx.Queue(key.FocusCmd{Tag: &e.eventKey}) - gtx.Queue(key.SoftKeyboardCmd{Show: true}) - } - e.requestFocus = false var scrollRange image.Rectangle if e.SingleLine { diff --git a/widget/editor_test.go b/widget/editor_test.go index fba25455..43236b91 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() + e.Focus(gtx) layoutEditor := func() layout.Dimensions { return e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) } @@ -511,7 +511,7 @@ func TestEditorDimensions(t *testing.T) { cache := text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Collection())) fontSize := unit.Sp(10) font := font.Font{} - e.Focus() + e.Focus(gtx) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue(key.EditEvent{Text: "A"}) @@ -904,7 +904,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() + e.Focus(gtx) r.Frame(gtx.Ops) gtx.Source = r.Source() // Build the selection events @@ -998,7 +998,7 @@ func TestSelectMove(t *testing.T) { fontSize := unit.Sp(10) // Layout once to populate e.lines and get focus. - e.Focus() + e.Focus(gtx) 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. @@ -1085,7 +1085,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() + e.Focus(gtx) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue( @@ -1121,7 +1121,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() + e.Focus(gtx) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue( @@ -1151,7 +1151,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() + e.Focus(gtx) e.Layout(gtx, cache, font, fontSize, op.CallOp{}, op.CallOp{}) r.Frame(gtx.Ops) r.Queue(