widget: update Editor dimensions after input

Fixes gio#162

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-09-29 19:06:26 +02:00
parent 4bab6fcf32
commit d2e06d9389
2 changed files with 32 additions and 0 deletions
+1
View File
@@ -354,6 +354,7 @@ func (e *Editor) Layout(gtx layout.Context, sh text.Shaper, font text.Font, size
e.makeValid()
e.processEvents(gtx)
e.makeValid()
if viewSize := gtx.Constraints.Constrain(e.dims.Size); viewSize != e.viewSize {
e.viewSize = viewSize
+31
View File
@@ -13,6 +13,8 @@ import (
"gioui.org/f32"
"gioui.org/font/gofont"
"gioui.org/io/event"
"gioui.org/io/key"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/text"
@@ -68,6 +70,35 @@ func TestEditor(t *testing.T) {
}
}
func TestEditorDimensions(t *testing.T) {
e := new(Editor)
tq := &testQueue{
events: []event.Event{
key.EditEvent{Text: "A"},
},
}
gtx := layout.Context{
Ops: new(op.Ops),
Constraints: layout.Constraints{Max: image.Pt(100, 100)},
Queue: tq,
}
cache := text.NewCache(gofont.Collection())
fontSize := unit.Px(10)
font := text.Font{}
dims := e.Layout(gtx, cache, font, fontSize)
if dims.Size.X == 0 {
t.Errorf("EditEvent was not reflected in Editor width")
}
}
type testQueue struct {
events []event.Event
}
func (q *testQueue) Events(_ event.Tag) []event.Event {
return q.events
}
// assertCaret asserts that the editor caret is at a particular line
// and column, and that the byte position matches as well.
func assertCaret(t *testing.T, e *Editor, line, col, bytes int) {