Skip to content

Commit

Permalink
Minor refactor of refresh code in base widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 9, 2025
1 parent e6225b5 commit 7114445
Showing 1 changed file with 26 additions and 37 deletions.
63 changes: 26 additions & 37 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"sync/atomic"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/internal/async"
"fyne.io/fyne/v2/internal/cache"
internalWidget "fyne.io/fyne/v2/internal/widget"
Expand Down Expand Up @@ -90,12 +89,7 @@ func (w *BaseWidget) Show() {
}

w.Hidden = false

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

// Hide this widget so it is no longer visible
Expand All @@ -105,12 +99,7 @@ func (w *BaseWidget) Hide() {
}

w.Hidden = true

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

// Refresh causes this widget to be redrawn in its current state
Expand All @@ -121,9 +110,7 @@ func (w *BaseWidget) Refresh() {
}

w.themeCache = nil

render := cache.Renderer(impl)
render.Refresh()
cache.Renderer(impl).Refresh()
}

// Theme returns a cached Theme instance for this widget (or its extending widget).
Expand All @@ -134,19 +121,14 @@ func (w *BaseWidget) Theme() fyne.Theme {
return w.themeWithLock()
}

func (w *BaseWidget) themeWithLock() fyne.Theme {
cached := w.themeCache
if cached == nil {
cached = cache.WidgetTheme(w.super())
// don't cache the default as it may change
if cached == nil {
return theme.Current()
}

w.themeCache = cached
// refreshSuper does a refresh of the widget that this represents.
func (w *BaseWidget) refreshSuper() {
impl := w.super()
if impl == nil {
return
}

return cached
impl.Refresh()
}

// super will return the actual object that this represents.
Expand All @@ -160,6 +142,21 @@ func (w *BaseWidget) super() fyne.Widget {
return *impl
}

func (w *BaseWidget) themeWithLock() fyne.Theme {
cached := w.themeCache
if cached == nil {
cached = cache.WidgetTheme(w.super())
// don't cache the default as it may change
if cached == nil {
return theme.Current()
}

w.themeCache = cached
}

return cached
}

// DisableableWidget describes an extension to BaseWidget which can be disabled.
// Disabled widgets should have a visually distinct style when disabled, normally using theme.DisabledButtonColor.
type DisableableWidget struct {
Expand All @@ -174,11 +171,7 @@ func (w *DisableableWidget) Enable() {
return // Enabled already
}

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

// Disable this widget so that it cannot be interacted with, updating any style appropriately.
Expand All @@ -187,11 +180,7 @@ func (w *DisableableWidget) Disable() {
return // Disabled already
}

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

// Disabled returns true if this widget is currently disabled or false if it can currently be interacted with.
Expand Down

0 comments on commit 7114445

Please sign in to comment.