Skip to content

Commit

Permalink
unexport setFieldsAndRefresh and relocate as Entry helper
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed May 19, 2024
1 parent aecc4f0 commit 3bfd5dc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
26 changes: 19 additions & 7 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (e *Entry) DoubleTapped(p *fyne.PointEvent) {
return
}

e.SetFieldsAndRefresh(func() {
e.setFieldsAndRefresh(func() {
if !e.selectKeyDown {
e.selectRow = e.CursorRow
e.selectColumn = start
Expand Down Expand Up @@ -316,7 +316,7 @@ func (e *Entry) ExtendBaseWidget(wid fyne.Widget) {
//
// Implements: fyne.Focusable
func (e *Entry) FocusGained() {
e.SetFieldsAndRefresh(func() {
e.setFieldsAndRefresh(func() {
e.dirty = true
e.focused = true
})
Expand All @@ -329,7 +329,7 @@ func (e *Entry) FocusGained() {
//
// Implements: fyne.Focusable
func (e *Entry) FocusLost() {
e.SetFieldsAndRefresh(func() {
e.setFieldsAndRefresh(func() {
e.focused = false
e.selectKeyDown = false
})
Expand Down Expand Up @@ -1145,7 +1145,7 @@ func (e *Entry) registerShortcut() {
return
}

e.SetFieldsAndRefresh(func() {
e.setFieldsAndRefresh(func() {
if s.(*desktop.CustomShortcut).KeyName == fyne.KeyLeft {
if e.CursorColumn == 0 {
if e.CursorRow > 0 {
Expand Down Expand Up @@ -1246,7 +1246,7 @@ func (e *Entry) selectAll() {
if e.textProvider().len() == 0 {
return
}
e.SetFieldsAndRefresh(func() {
e.setFieldsAndRefresh(func() {
e.selectRow = 0
e.selectColumn = 0

Expand Down Expand Up @@ -1294,7 +1294,7 @@ func (e *Entry) selectingKeyHandler(key *fyne.KeyEvent) bool {
case fyne.KeyReturn, fyne.KeyEnter:
if e.MultiLine {
// clear the selection -- return unhandled to add the newline
e.SetFieldsAndRefresh(e.eraseSelectionAndUpdate)
e.setFieldsAndRefresh(e.eraseSelectionAndUpdate)
}
return false
}
Expand Down Expand Up @@ -1594,6 +1594,18 @@ func (e *Entry) selectCurrentRow() {
e.Refresh()
}

func (e *Entry) setFieldsAndRefresh(f func()) {
e.propertyLock.Lock()
f()
e.propertyLock.Unlock()

impl := e.super()
if impl == nil {
return
}
impl.Refresh()
}

var _ fyne.WidgetRenderer = (*entryRenderer)(nil)

type entryRenderer struct {
Expand Down Expand Up @@ -1685,7 +1697,7 @@ func (r *entryRenderer) Layout(size fyne.Size) {
resizedTextPos := r.entry.textPosFromRowCol(r.entry.CursorRow, r.entry.CursorColumn)
r.entry.propertyLock.Unlock()
if textPos != resizedTextPos {
r.entry.SetFieldsAndRefresh(func() {
r.entry.setFieldsAndRefresh(func() {
r.entry.CursorRow, r.entry.CursorColumn = r.entry.rowColFromTextPos(textPos)

if r.entry.selecting {
Expand Down
2 changes: 1 addition & 1 deletion widget/entry_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (r *passwordRevealer) Tapped(*fyne.PointEvent) {
return
}

r.entry.SetFieldsAndRefresh(func() {
r.entry.setFieldsAndRefresh(func() {
r.entry.Password = !r.entry.Password
})
fyne.CurrentApp().Driver().CanvasForObject(r).Focus(r.entry.super().(fyne.Focusable))
Expand Down
29 changes: 9 additions & 20 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ func (w *BaseWidget) Show() {
return
}

w.SetFieldsAndRefresh(func() {
w.Hidden = false
})
w.propertyLock.Lock()
w.Hidden = false
w.propertyLock.Unlock()

impl := w.super()
if impl == nil {
return
}
impl.Refresh()
}

// Hide this widget so it is no longer visible
Expand Down Expand Up @@ -156,23 +162,6 @@ func (w *BaseWidget) themeWithLock() fyne.Theme {
return cached
}

// SetFieldsAndRefresh helps to make changes to a widget that should be followed by a refresh.
// This method is a guaranteed thread-safe way of directly manipulating widget fields.
// Widgets extending BaseWidget should use this in their setter functions.
//
// Since: 2.5
func (w *BaseWidget) SetFieldsAndRefresh(f func()) {
w.propertyLock.Lock()
f()
w.propertyLock.Unlock()

impl := w.super()
if impl == nil {
return
}
impl.Refresh()
}

// super will return the actual object that this represents.
// If extended then this is the extending widget, otherwise it is nil.
func (w *BaseWidget) super() fyne.Widget {
Expand Down

0 comments on commit 3bfd5dc

Please sign in to comment.