From d2e06d938930c43ec3183487be7164e11b8bb4f2 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 29 Sep 2020 19:06:26 +0200 Subject: [PATCH] widget: update Editor dimensions after input Fixes gio#162 Signed-off-by: Elias Naur --- widget/editor.go | 1 + widget/editor_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/widget/editor.go b/widget/editor.go index 69b87965..57784cb3 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -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 diff --git a/widget/editor_test.go b/widget/editor_test.go index e670d273..3b003ad7 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -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) {