Skip to content

Commit

Permalink
Store super with atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 3, 2024
1 parent deca79c commit f47e844
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
5 changes: 3 additions & 2 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,11 @@ func (e *Entry) ExtendBaseWidget(wid fyne.Widget) {
return
}

e.impl.Store(&wid)

e.propertyLock.Lock()
defer e.propertyLock.Unlock()
e.BaseWidget.impl = wid
e.registerShortcut()
e.propertyLock.Unlock()
}

// FocusGained is called when the Entry has been given focus.
Expand Down
26 changes: 13 additions & 13 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package widget // import "fyne.io/fyne/v2/widget"

import (
"sync"
"sync/atomic"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
Expand All @@ -16,7 +17,7 @@ type BaseWidget struct {
position fyne.Position
Hidden bool

impl fyne.Widget
impl atomic.Pointer[fyne.Widget]
propertyLock sync.RWMutex
}

Expand All @@ -27,9 +28,7 @@ func (w *BaseWidget) ExtendBaseWidget(wid fyne.Widget) {
return
}

w.propertyLock.Lock()
defer w.propertyLock.Unlock()
w.impl = wid
w.impl.Store(&wid)
}

// Size gets the current size of this widget.
Expand All @@ -45,7 +44,6 @@ func (w *BaseWidget) Size() fyne.Size {
func (w *BaseWidget) Resize(size fyne.Size) {
w.propertyLock.RLock()
baseSize := w.size
impl := w.impl
w.propertyLock.RUnlock()
if baseSize == size {
return
Expand All @@ -55,6 +53,7 @@ func (w *BaseWidget) Resize(size fyne.Size) {
w.size = size
w.propertyLock.Unlock()

impl := w.super()
if impl == nil {
return
}
Expand All @@ -74,10 +73,9 @@ func (w *BaseWidget) Position() fyne.Position {
func (w *BaseWidget) Move(pos fyne.Position) {
w.propertyLock.Lock()
w.position = pos
impl := w.impl
w.propertyLock.Unlock()

internalWidget.Repaint(impl)
internalWidget.Repaint(w.super())
}

// MinSize for the widget - it should never be resized below this value.
Expand Down Expand Up @@ -120,9 +118,9 @@ func (w *BaseWidget) Hide() {

w.propertyLock.Lock()
w.Hidden = true
impl := w.impl
w.propertyLock.Unlock()

impl := w.super()
if impl == nil {
return
}
Expand All @@ -145,9 +143,9 @@ func (w *BaseWidget) Refresh() {
func (w *BaseWidget) setFieldsAndRefresh(f func()) {
w.propertyLock.Lock()
f()
impl := w.impl
w.propertyLock.Unlock()

impl := w.super()
if impl == nil {
return
}
Expand All @@ -157,10 +155,12 @@ func (w *BaseWidget) setFieldsAndRefresh(f func()) {
// 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 {
w.propertyLock.RLock()
impl := w.impl
w.propertyLock.RUnlock()
return impl
impl := w.impl.Load()
if impl == nil {
return nil
}

return *impl
}

// DisableableWidget describes an extension to BaseWidget which can be disabled.
Expand Down

0 comments on commit f47e844

Please sign in to comment.