~eliasnaur/gio

d2e06d938930c43ec3183487be7164e11b8bb4f2 — Elias Naur a month ago 4bab6fc
widget: update Editor dimensions after input

Fixes gio#162

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2 files changed, 32 insertions(+), 0 deletions(-)

M widget/editor.go
M widget/editor_test.go
M widget/editor.go => widget/editor.go +1 -0
@@ 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

M widget/editor_test.go => widget/editor_test.go +31 -0
@@ 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) {