Skip to content

Commit

Permalink
Update cursor position correctly when pasting newlines
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Mar 6, 2019
1 parent 1fcaf24 commit 7fdf6db
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
10 changes: 9 additions & 1 deletion widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,15 @@ func (e *Entry) registerShortcut() {
provider := e.textProvider()
runes := []rune(text)
provider.insertAt(e.cursorTextPos(), runes)
e.CursorColumn += len(runes)

newlines := strings.Count(text, "\n")
if newlines == 0 {
e.CursorColumn += len(runes)
} else {
e.CursorRow += newlines
lastNewline := strings.LastIndex(text, "\n")
e.CursorColumn = len(runes) - lastNewline - 1
}
e.updateText(provider.String())
Renderer(e).(*entryRenderer).moveCursor()
})
Expand Down
21 changes: 19 additions & 2 deletions widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,63 +431,80 @@ func TestPasswordEntry_Obfuscation(t *testing.T) {

func TestEntry_OnPaste(t *testing.T) {
clipboard := test.NewClipboard()
shortuct := &fyne.ShortcutPaste{Clipboard: clipboard}
shortcut := &fyne.ShortcutPaste{Clipboard: clipboard}
tests := []struct {
name string
entry *Entry
clipboardContent string
wantText string
wantRow, wantCol int
}{
{
name: "singleline: empty content",
entry: NewEntry(),
clipboardContent: "",
wantText: "",
wantRow: 0,
wantCol: 0,
},
{
name: "singleline: simple text",
entry: NewEntry(),
clipboardContent: "clipboard content",
wantText: "clipboard content",
wantRow: 0,
wantCol: 17,
},
{
name: "singleline: UTF8 text",
entry: NewEntry(),
clipboardContent: "Hié™שרה",
wantText: "Hié™שרה",
wantRow: 0,
wantCol: 7,
},
{
name: "singleline: with new line",
entry: NewEntry(),
clipboardContent: "clipboard\ncontent",
wantText: "clipboard content",
wantRow: 0,
wantCol: 17,
},
{
name: "singleline: with tab",
entry: NewEntry(),
clipboardContent: "clipboard\tcontent",
wantText: "clipboard\tcontent",
wantRow: 0,
wantCol: 17,
},
{
name: "password: with new line",
entry: NewPasswordEntry(),
clipboardContent: "3SB=y+)z\nkHGK(hx6 -e_\"1TZu q^bF3^$u H[:e\"1O.",
wantText: `3SB=y+)z kHGK(hx6 -e_"1TZu q^bF3^$u H[:e"1O.`,
wantRow: 0,
wantCol: 44,
},
{
name: "multiline: with new line",
entry: NewMultiLineEntry(),
clipboardContent: "clipboard\ncontent",
wantText: "clipboard\ncontent",
wantRow: 1,
wantCol: 7,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
clipboard.SetContent(tt.clipboardContent)
handled := tt.entry.TypedShortcut(shortuct)
handled := tt.entry.TypedShortcut(shortcut)
assert.True(t, handled)
assert.Equal(t, tt.wantText, tt.entry.Text)
assert.Equal(t, tt.wantRow, tt.entry.CursorRow)
assert.Equal(t, tt.wantCol, tt.entry.CursorColumn)
})
}
}

0 comments on commit 7fdf6db

Please sign in to comment.