Skip to content

Commit

Permalink
Fix crash with SetText not limiting cursor position
Browse files Browse the repository at this point in the history
fixes #466
  • Loading branch information
andydotxyz committed Sep 27, 2019
1 parent bde57a9 commit 0f43af5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions widget/entry.go
Expand Up @@ -302,6 +302,15 @@ func (e *Entry) SetText(text string) {
e.CursorRow = 0
e.Unlock()
Renderer(e).(*entryRenderer).moveCursor()
} else {
provider := e.textProvider()
if e.CursorRow >= provider.rows() {
e.CursorRow = provider.rows() - 1
}
rowLength := provider.rowLength(e.CursorRow)
if e.CursorColumn >= rowLength {
e.CursorColumn = rowLength
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions widget/entry_test.go
Expand Up @@ -95,6 +95,30 @@ func TestEntry_SetTextEmptyString(t *testing.T) {
assert.Equal(t, 0, entry.CursorRow)
}

func TestEntry_SetText_Overflow(t *testing.T) {
entry := NewEntry()

assert.Equal(t, 0, entry.CursorColumn)

test.Type(entry, "test")
assert.Equal(t, 4, entry.CursorColumn)

entry.SetText("x")
assert.Equal(t, 1, entry.CursorColumn)

key := &fyne.KeyEvent{Name: fyne.KeyDelete}
entry.TypedKey(key)

assert.Equal(t, 1, entry.CursorColumn)
assert.Equal(t, "x", entry.Text)

key = &fyne.KeyEvent{Name: fyne.KeyBackspace}
entry.TypedKey(key)

assert.Equal(t, 0, entry.CursorColumn)
assert.Equal(t, "", entry.Text)
}

func TestEntry_OnKeyDown(t *testing.T) {
entry := NewEntry()

Expand Down

1 comment on commit 0f43af5

@noxer
Copy link

@noxer noxer commented on 0f43af5 Sep 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Please sign in to comment.