Skip to content

Commit

Permalink
use the existing rowColFromTextPos to set CursorRow and CursorColumn …
Browse files Browse the repository at this point in the history
…in entry.pasteFromClipboard
  • Loading branch information
fpabl0 committed Mar 9, 2021
1 parent 401e1bc commit 912d8d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
17 changes: 3 additions & 14 deletions widget/entry.go
Expand Up @@ -821,21 +821,10 @@ func (e *Entry) pasteFromClipboard(clipboard fyne.Clipboard) {
}
provider := e.textProvider()
runes := []rune(text)
provider.insertAt(e.cursorTextPos(), runes)
pos := e.cursorTextPos()
provider.insertAt(pos, runes)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))

newlines := strings.Count(text, "\n")
if newlines == 0 {
e.CursorColumn += len(runes)
} else {
e.CursorRow += newlines
lastNewlineIndex := 0
for i, r := range runes {
if r == '\n' {
lastNewlineIndex = i
}
}
e.CursorColumn = len(runes) - lastNewlineIndex - 1
}
e.updateText(provider.String())
e.Refresh()
}
Expand Down
29 changes: 29 additions & 0 deletions widget/entry_internal_test.go
Expand Up @@ -255,6 +255,35 @@ func TestEntry_PasteFromClipboard(t *testing.T) {
assert.Equal(t, entry.Text, testContent)
}

func TestEntry_PasteFromClipboard_MultilineWrapping(t *testing.T) {
entry := NewMultiLineEntry()
entry.Wrapping = fyne.TextWrapWord

w := test.NewApp().NewWindow("")
w.SetContent(entry)
w.Resize(fyne.NewSize(100, 64))

test.Type(entry, "T")
assert.Equal(t, 0, entry.CursorRow)
assert.Equal(t, 1, entry.CursorColumn)

clipboard := fyne.CurrentApp().Driver().AllWindows()[0].Clipboard()
clipboard.SetContent("esting entry")

entry.pasteFromClipboard(clipboard)

assert.Equal(t, entry.Text, "Testing entry")
assert.Equal(t, 1, entry.CursorRow)
assert.Equal(t, 5, entry.CursorColumn)

clipboard.SetContent(" paste\ncontent")
entry.pasteFromClipboard(clipboard)

assert.Equal(t, "Testing entry paste\ncontent", entry.Text)
assert.Equal(t, 2, entry.CursorRow)
assert.Equal(t, 7, entry.CursorColumn)
}

func TestEntry_Tab(t *testing.T) {
e := NewEntry()
e.SetText("a\n\tb\nc")
Expand Down

0 comments on commit 912d8d8

Please sign in to comment.