~eliasnaur/gio

f88a8216e9d7fb149d3c5137dd910c0d5443012d — Larry Clapp 4 months ago 3427394
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 <larry@theclapp.org>
2 files changed, 3 insertions(+), 0 deletions(-)

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

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