From 9830d0e471ac823bb0f56e84769a994fdb9b5408 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 6 Jan 2024 18:35:53 +0100 Subject: [PATCH] Simplify atomics with CAS operation --- internal/widget/base.go | 12 ++++-------- widget/widget.go | 14 +++++--------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/internal/widget/base.go b/internal/widget/base.go index a3a080b889..d3c7e16302 100644 --- a/internal/widget/base.go +++ b/internal/widget/base.go @@ -81,12 +81,10 @@ func (w *Base) Visible() bool { // Show this widget so it becomes visible func (w *Base) Show() { - if w.Visible() { - return + if !w.hidden.CompareAndSwap(true, false) { + return // Visible already } - w.hidden.Store(false) - impl := w.super() if impl == nil { return @@ -96,12 +94,10 @@ func (w *Base) Show() { // Hide this widget so it is no longer visible func (w *Base) Hide() { - if !w.Visible() { - return + if !w.hidden.CompareAndSwap(false, true) { + return // Hidden already } - w.hidden.Store(true) - impl := w.super() if impl == nil { return diff --git a/widget/widget.go b/widget/widget.go index adf0d07927..6bab4583f3 100644 --- a/widget/widget.go +++ b/widget/widget.go @@ -71,7 +71,7 @@ func (w *BaseWidget) MinSize() fyne.Size { r := cache.Renderer(impl) if r == nil { - return fyne.NewSize(0, 0) + return fyne.Size{} } return r.MinSize() @@ -160,12 +160,10 @@ type DisableableWidget struct { // Enable this widget, updating any style or features appropriately. func (w *DisableableWidget) Enable() { - if !w.Disabled() { - return + if !w.disabled.CompareAndSwap(true, false) { + return // Enabled already } - w.disabled.Store(false) - impl := w.super() if impl == nil { return @@ -175,12 +173,10 @@ func (w *DisableableWidget) Enable() { // Disable this widget so that it cannot be interacted with, updating any style appropriately. func (w *DisableableWidget) Disable() { - if w.Disabled() { - return + if !w.disabled.CompareAndSwap(false, true) { + return // Disabled already } - w.disabled.Store(true) - impl := w.super() if impl == nil { return