Skip to content

Commit

Permalink
Merge pull request fyne-io#4517 from Jacalz/entry-onchanged-locking
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz authored Jan 6, 2024
2 parents c264577 + 614949f commit 7206953
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
14 changes: 8 additions & 6 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,12 @@ func (e *Entry) cutToClipboard(clipboard fyne.Clipboard) {

e.copyToClipboard(clipboard)
e.setFieldsAndRefresh(e.eraseSelection)
e.propertyLock.Lock()
e.propertyLock.RLock()
content := e.Text
e.propertyLock.RUnlock()
if e.OnChanged != nil {
e.OnChanged(e.Text)
e.OnChanged(content)
}
e.propertyLock.Unlock()
e.Validate()
}

Expand Down Expand Up @@ -1092,11 +1093,12 @@ func (e *Entry) selectingKeyHandler(key *fyne.KeyEvent) bool {
case fyne.KeyBackspace, fyne.KeyDelete:
// clears the selection -- return handled
e.setFieldsAndRefresh(e.eraseSelection)
e.propertyLock.Lock()
e.propertyLock.RLock()
content := e.Text
e.propertyLock.RUnlock()
if e.OnChanged != nil {
e.OnChanged(e.Text)
e.OnChanged(content)
}
e.propertyLock.Unlock()
e.Validate()
return true
case fyne.KeyReturn, fyne.KeyEnter:
Expand Down
15 changes: 15 additions & 0 deletions widget/entry_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ func TestEntry_EraseSelection(t *testing.T) {
assert.Equal(t, -1, b)
}

func TestEntry_CallbackLocking(t *testing.T) {
e := &Entry{}
called := 0
e.OnChanged = func(_ string) {
e.propertyLock.Lock()
called++ // Just to not have an empty critical section.
e.propertyLock.Unlock()
}

test.Type(e, "abc123")
e.selectAll()
e.TypedKey(&fyne.KeyEvent{Name: fyne.KeyBackspace})
assert.Equal(t, 7, called)
}

func TestEntry_MouseClickAndDragOutsideText(t *testing.T) {
entry := NewEntry()
entry.SetText("A\nB\n")
Expand Down

0 comments on commit 7206953

Please sign in to comment.