From 02b14558af59d67bf576c977b4900c3f57ae44a3 Mon Sep 17 00:00:00 2001 From: Jacalz Date: Tue, 26 Dec 2023 16:59:56 +0100 Subject: [PATCH] More atomic.Value cleanups --- app.go | 15 +++------------ internal/cache/base.go | 9 +++++---- internal/cache/text.go | 4 ++-- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/app.go b/app.go index bffcf9c1ff..74ac0bf89a 100644 --- a/app.go +++ b/app.go @@ -81,20 +81,11 @@ type App interface { SetCloudProvider(CloudProvider) // configure cloud for this app } -// app contains an App variable, but due to atomic.Value restrictions on -// interfaces we need to use an indirect type, i.e. appContainer. -var app atomic.Pointer[appContainer] // appContainer - -// appContainer is a dummy container that holds an App instance. This -// struct exists to guarantee that atomic.Value can store objects with -// same type. -type appContainer struct { - current App -} +var app atomic.Pointer[App] // SetCurrentApp is an internal function to set the app instance currently running. func SetCurrentApp(current App) { - app.Store(&appContainer{current}) + app.Store(¤t) } // CurrentApp returns the current application, for which there is only 1 per process. @@ -104,7 +95,7 @@ func CurrentApp() App { LogError("Attempt to access current Fyne app when none is started", nil) return nil } - return val.current + return *val } // AppMetadata captures the build metadata for an application. diff --git a/internal/cache/base.go b/internal/cache/base.go index 204e8f4604..92b0167e11 100644 --- a/internal/cache/base.go +++ b/internal/cache/base.go @@ -155,7 +155,7 @@ func ResetThemeCaches() { }) fontSizeLock.Lock() - fontSizeCache = map[fontSizeEntry]fontMetric{} + fontSizeCache = map[fontSizeEntry]*fontMetric{} fontSizeLock.Unlock() } @@ -215,7 +215,7 @@ func matchesACanvas(cinfo *canvasInfo, canvases []fyne.Canvas) bool { } type expiringCache struct { - expires atomic.Value // time.time + expires atomic.Pointer[time.Time] } // isExpired check if the cache data is expired. @@ -224,12 +224,13 @@ func (c *expiringCache) isExpired(now time.Time) bool { if t == nil { return (time.Time{}).Before(now) } - return t.(time.Time).Before(now) + return (*t).Before(now) } // setAlive updates expiration time. func (c *expiringCache) setAlive() { - c.expires.Store(timeNow().Add(cacheDuration)) + time := timeNow().Add(cacheDuration) + c.expires.Store(&time) } type expiringCacheNoLock struct { diff --git a/internal/cache/text.go b/internal/cache/text.go index 13ab450577..465caec5bf 100644 --- a/internal/cache/text.go +++ b/internal/cache/text.go @@ -8,7 +8,7 @@ import ( ) var ( - fontSizeCache = map[fontSizeEntry]fontMetric{} + fontSizeCache = map[fontSizeEntry]*fontMetric{} fontSizeLock = sync.RWMutex{} ) @@ -40,7 +40,7 @@ func GetFontMetrics(text string, fontSize float32, style fyne.TextStyle) (size f // SetFontMetrics stores a calculated font size and baseline for parameters that were missing from the cache. func SetFontMetrics(text string, fontSize float32, style fyne.TextStyle, size fyne.Size, base float32) { ent := fontSizeEntry{text, fontSize, style} - metric := fontMetric{size: size, baseLine: base} + metric := &fontMetric{size: size, baseLine: base} metric.setAlive() fontSizeLock.Lock() fontSizeCache[ent] = metric