Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry text position should not go beyond the total rows, fixes #1989 #2070

Merged
merged 2 commits into from Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions widget/entry.go
Expand Up @@ -876,14 +876,15 @@ func (e *Entry) registerShortcut() {
func (e *Entry) rowColFromTextPos(pos int) (row int, col int) {
provider := e.textProvider()
canWrap := e.Wrapping == fyne.TextWrapBreak || e.Wrapping == fyne.TextWrapWord
for i := 0; i < provider.rows(); i++ {
totalRows := provider.rows()
for i := 0; i < totalRows; i++ {
b := provider.rowBoundary(i)
if b[0] <= pos {
if b[1] < pos {
row++
}
col = pos - b[0]
if canWrap && b[0] == pos && col == 0 && pos != 0 {
if canWrap && b[0] == pos && col == 0 && pos != 0 && row < (totalRows-1) {
row++
}
} else {
Expand Down
28 changes: 28 additions & 0 deletions widget/entry_test.go
Expand Up @@ -331,6 +331,34 @@ func TestEntry_MultilineSelect(t *testing.T) {
assert.Equal(t, "ng\nTe", e.SelectedText())
}

func TestEntry_MultilineWrapping_DeleteWithBackspace(t *testing.T) {
entry := widget.NewMultiLineEntry()
entry.Wrapping = fyne.TextWrapWord
entry.Resize(fyne.NewSize(64, 64))
test.Type(entry, "line1")
test.Type(entry, "\nline2")
test.Type(entry, "\nline3")

assert.Equal(t, 5, entry.CursorColumn)
assert.Equal(t, 2, entry.CursorRow)

for i := 0; i < 4; i++ {
entry.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
assert.Equal(t, 4-i, entry.CursorColumn)
assert.Equal(t, 2, entry.CursorRow)
}

entry.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
assert.Equal(t, 0, entry.CursorColumn)
assert.Equal(t, 2, entry.CursorRow)

assert.NotPanics(t, func() {
entry.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
})
assert.Equal(t, 5, entry.CursorColumn)
assert.Equal(t, 1, entry.CursorRow)
}

func TestEntry_Notify(t *testing.T) {
entry := widget.NewEntry()
changed := false
Expand Down