Skip to content

Commit

Permalink
add entry.Insert API and unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Dec 20, 2024
1 parent a032b58 commit 75ff8f8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 44 deletions.
96 changes: 52 additions & 44 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,57 @@ func (e *Entry) Unbind() {
e.binder.Unbind()
}

// Insert inserts the given text into the Entry at the current cursor position.
// If text is currently selected, the selected text will be replaced
// with the inserted content.
//
// Since: 2.6
func (e *Entry) Insert(text string) {
if text == "" {
e.propertyLock.Lock()
changed := e.selecting && e.eraseSelection()
e.propertyLock.Unlock()

if changed {
e.Refresh()
}

return // Nothing to paste into the text content.
}

if !e.MultiLine {
// format clipboard content to be compatible with single line entry
text = strings.Replace(text, "\n", " ", -1)
}

e.propertyLock.Lock()
if e.selecting {
e.eraseSelection()
}

runes := []rune(text)
pos := e.cursorTextPos()
provider := e.textProvider()
provider.insertAt(pos, runes)

e.undoStack.Add(&entryModifyAction{
Position: pos,
Text: runes,
})
content := provider.String()
e.updateText(content, false)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))
cb := e.OnChanged
e.propertyLock.Unlock()

e.validate()
if cb != nil {
cb(content) // We know that the text has changed.
}

e.Refresh() // placing the cursor (and refreshing) happens last
}

// copyToClipboard copies the current selection to a given clipboard.
// This does nothing if it is a concealed entry.
func (e *Entry) copyToClipboard(clipboard fyne.Clipboard) {
Expand Down Expand Up @@ -1084,50 +1135,7 @@ func (e *Entry) getRowCol(p fyne.Position) (int, int) {
// pasteFromClipboard inserts text from the clipboard content,
// starting from the cursor position.
func (e *Entry) pasteFromClipboard(clipboard fyne.Clipboard) {
text := clipboard.Content()
if text == "" {
e.propertyLock.Lock()
changed := e.selecting && e.eraseSelection()
e.propertyLock.Unlock()

if changed {
e.Refresh()
}

return // Nothing to paste into the text content.
}

if !e.MultiLine {
// format clipboard content to be compatible with single line entry
text = strings.Replace(text, "\n", " ", -1)
}

e.propertyLock.Lock()
if e.selecting {
e.eraseSelection()
}

runes := []rune(text)
pos := e.cursorTextPos()
provider := e.textProvider()
provider.insertAt(pos, runes)

e.undoStack.Add(&entryModifyAction{
Position: pos,
Text: runes,
})
content := provider.String()
e.updateText(content, false)
e.CursorRow, e.CursorColumn = e.rowColFromTextPos(pos + len(runes))
cb := e.OnChanged
e.propertyLock.Unlock()

e.validate()
if cb != nil {
cb(content) // We know that the text has changed.
}

e.Refresh() // placing the cursor (and refreshing) happens last
e.Insert(clipboard.Content())
}

// placeholderProvider returns the placeholder text handler for this entry
Expand Down
76 changes: 76 additions & 0 deletions widget/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,82 @@ func TestEntry_OnPaste(t *testing.T) {
}
}

func TestEntry_Insert(t *testing.T) {
tests := []struct {
name string
entry *widget.Entry
insertText string
wantText string
wantRow, wantCol int
}{
{
name: "singleline: empty content",
entry: widget.NewEntry(),
insertText: "",
wantText: "",
wantRow: 0,
wantCol: 0,
},
{
name: "singleline: simple text",
entry: widget.NewEntry(),
insertText: "clipboard content",
wantText: "clipboard content",
wantRow: 0,
wantCol: 17,
},
{
name: "singleline: UTF8 text",
entry: widget.NewEntry(),
insertText: "Hié™שרה",
wantText: "Hié™שרה",
wantRow: 0,
wantCol: 7,
},
{
name: "singleline: with new line",
entry: widget.NewEntry(),
insertText: "clipboard\ncontent",
wantText: "clipboard content",
wantRow: 0,
wantCol: 17,
},
{
name: "singleline: with tab",
entry: widget.NewEntry(),
insertText: "clipboard\tcontent",
wantText: "clipboard\tcontent",
wantRow: 0,
wantCol: 17,
},
{
name: "password: with new line",
entry: widget.NewPasswordEntry(),
insertText: "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: widget.NewMultiLineEntry(),
insertText: "clipboard\ncontent",
wantText: "clipboard\ncontent",
wantRow: 1,
wantCol: 7,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.entry.Insert(tt.insertText)
assert.Equal(t, tt.wantText, tt.entry.Text)
assert.Equal(t, tt.wantRow, tt.entry.CursorRow)
assert.Equal(t, tt.wantCol, tt.entry.CursorColumn)
})
}
}

func TestEntry_PageUpDown(t *testing.T) {
t.Run("single line", func(*testing.T) {
e, window := setupImageTest(t, false)
Expand Down

0 comments on commit 75ff8f8

Please sign in to comment.