widget: [API] re-implement Editor.Focus in terms of commands

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2023-10-20 20:55:56 -05:00
parent 5dd41f74d3
commit 8334d2abb4
3 changed files with 16 additions and 21 deletions
+1 -1
View File
@@ -31,10 +31,10 @@ func FuzzIME(f *testing.F) {
f.Fuzz(func(t *testing.T, cmds []byte) { f.Fuzz(func(t *testing.T, cmds []byte) {
cache := text.NewShaper(text.WithCollection(gofont.Collection())) cache := text.NewShaper(text.WithCollection(gofont.Collection()))
e := new(widget.Editor) e := new(widget.Editor)
e.Focus()
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)
// 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)
+8 -13
View File
@@ -69,11 +69,10 @@ type Editor struct {
buffer *editBuffer buffer *editBuffer
// scratch is a byte buffer that is reused to efficiently read portions of text // scratch is a byte buffer that is reused to efficiently read portions of text
// from the textView. // from the textView.
scratch []byte scratch []byte
eventKey int eventKey int
blinkStart time.Time blinkStart time.Time
focused bool focused bool
requestFocus bool
// ime tracks the state relevant to input methods. // ime tracks the state relevant to input methods.
ime struct { ime struct {
@@ -246,7 +245,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.requestFocus = true e.Focus(gtx)
if e.scroller.State() != gesture.StateFlinging { if e.scroller.State() != gesture.StateFlinging {
e.scrollCaret = true 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. // Focus requests the input focus for the Editor.
func (e *Editor) Focus() { func (e *Editor) Focus(gtx layout.Context) {
e.requestFocus = true gtx.Queue(key.FocusCmd{Tag: &e.eventKey})
gtx.Queue(key.SoftKeyboardCmd{Show: true})
} }
// Focused returns whether the editor is focused or not. // 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) 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 var scrollRange image.Rectangle
if e.SingleLine { if e.SingleLine {
+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() e.Focus(gtx)
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{})
} }
@@ -511,7 +511,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() e.Focus(gtx)
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"})
@@ -904,7 +904,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() e.Focus(gtx)
r.Frame(gtx.Ops) r.Frame(gtx.Ops)
gtx.Source = r.Source() gtx.Source = r.Source()
// Build the selection events // Build the selection events
@@ -998,7 +998,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() e.Focus(gtx)
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.
@@ -1085,7 +1085,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() e.Focus(gtx)
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(
@@ -1121,7 +1121,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() e.Focus(gtx)
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(
@@ -1151,7 +1151,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() e.Focus(gtx)
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(