From f88a8216e9d7fb149d3c5137dd910c0d5443012d Mon Sep 17 00:00:00 2001 From: Larry Clapp Date: Sun, 24 Jan 2021 11:06:55 -0500 Subject: [PATCH] widget: fix Editor panic If you created an Editor and immediately SetCaret, it panicked because e.lines was nil and it looked at e.lines[0]. - Add e.makeValid at the top of SetCaret. - Add a test case for this situation. Signed-off-by: Larry Clapp --- widget/editor.go | 1 + widget/editor_test.go | 2 ++ 2 files changed, 3 insertions(+) diff --git a/widget/editor.go b/widget/editor.go index bc8167af..99c66b96 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -1186,6 +1186,7 @@ func (e *Editor) Selection() (start, end int) { // and end are in bytes, and represent offsets into the editor text. start and // end must be at a rune boundary. func (e *Editor) SetCaret(start, end int) { + e.makeValid() // Constrain start and end to [0, e.Len()]. l := e.Len() start = max(min(start, l), 0) diff --git a/widget/editor_test.go b/widget/editor_test.go index 55f7acbb..0f34371e 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -34,6 +34,8 @@ func TestEditor(t *testing.T) { fontSize := unit.Px(10) font := text.Font{} + e.SetCaret(0, 0) // shouldn't panic + assertCaret(t, e, 0, 0, 0) e.SetText("æbc\naøå•") e.Layout(gtx, cache, font, fontSize) assertCaret(t, e, 0, 0, 0)