From 1aeb76dc7c823737e8198c449fa170cb6863949b Mon Sep 17 00:00:00 2001 From: Jacalz Date: Sat, 11 Jan 2025 13:32:03 +0100 Subject: [PATCH] widget: Clean up locks within the BaseWidget --- widget/check.go | 6 ++--- widget/check_group.go | 4 ++-- widget/entry.go | 22 +++++++----------- widget/entry_password.go | 2 +- widget/entry_validation.go | 2 +- widget/hyperlink.go | 6 ++--- widget/progressbarinfinite.go | 2 +- widget/radio_group.go | 4 ++-- widget/richtext.go | 8 +++---- widget/select.go | 21 +++++++++-------- widget/slider.go | 2 +- widget/table.go | 8 +++---- widget/tree.go | 8 +++---- widget/widget.go | 44 +++++++++++++++-------------------- 14 files changed, 64 insertions(+), 75 deletions(-) diff --git a/widget/check.go b/widget/check.go index de22f44d53..3df6629776 100644 --- a/widget/check.go +++ b/widget/check.go @@ -322,7 +322,7 @@ func (c *checkRenderer) Layout(size fyne.Size) { func (c *checkRenderer) applyTheme(th fyne.Theme, v fyne.ThemeVariant) { c.label.Color = th.Color(theme.ColorNameForeground, v) c.label.TextSize = th.Size(theme.SizeNameText) - if c.check.disabled.Load() { + if c.check.Disabled() { c.label.Color = th.Color(theme.ColorNameDisabled, v) } } @@ -359,7 +359,7 @@ func (c *checkRenderer) updateResource(th fyne.Theme) { res.ColorName = theme.ColorNamePrimary bgRes.ColorName = theme.ColorNameBackground } - if c.check.disabled.Load() { + if c.check.Disabled() { if c.check.Checked { res = theme.NewThemedResource(theme.CheckButtonCheckedIcon()) } @@ -374,7 +374,7 @@ func (c *checkRenderer) updateResource(th fyne.Theme) { // must be called while holding c.check.propertyLock for reading func (c *checkRenderer) updateFocusIndicator(th fyne.Theme, v fyne.ThemeVariant) { - if c.check.disabled.Load() { + if c.check.Disabled() { c.focusIndicator.FillColor = color.Transparent } else if c.check.focused { c.focusIndicator.FillColor = th.Color(theme.ColorNameFocus, v) diff --git a/widget/check_group.go b/widget/check_group.go index 5c9c5764db..7c035848a3 100644 --- a/widget/check_group.go +++ b/widget/check_group.go @@ -164,7 +164,7 @@ func (r *CheckGroup) update() { item.Text = r.Options[i] item.Checked = contains - item.DisableableWidget.disabled.Store(r.disabled.Load()) + item.DisableableWidget.disabled = r.Disabled() item.Refresh() } } @@ -257,7 +257,7 @@ func (r *checkGroupRenderer) updateItems() { } item.Text = r.checks.Options[i] item.Checked = contains - item.disabled.Store(r.checks.disabled.Load()) + item.disabled = r.checks.Disabled() item.Refresh() } } diff --git a/widget/entry.go b/widget/entry.go index 2f54a07bce..4057466ff3 100644 --- a/widget/entry.go +++ b/widget/entry.go @@ -229,7 +229,7 @@ func (e *Entry) Disable() { // // Implements: fyne.Disableable func (e *Entry) Disabled() bool { - return e.DisableableWidget.disabled.Load() + return e.DisableableWidget.Disabled() } // DoubleTapped is called when this entry has been double tapped so we should select text below the pointer @@ -298,13 +298,7 @@ func (e *Entry) Enable() { // ExtendBaseWidget is used by an extending widget to make use of BaseWidget functionality. func (e *Entry) ExtendBaseWidget(wid fyne.Widget) { - impl := e.super() - if impl != nil { - return - } - - e.impl.Store(&wid) - + e.BaseWidget.ExtendBaseWidget(wid) e.registerShortcut() } @@ -1328,7 +1322,7 @@ func (e *Entry) textPosFromRowCol(row, col int) int { func (e *Entry) syncSegments() { colName := theme.ColorNameForeground wrap := e.textWrap() - disabled := e.disabled.Load() + disabled := e.Disabled() if disabled { colName = theme.ColorNameDisabled } @@ -1696,7 +1690,7 @@ func (r *entryRenderer) Objects() []fyne.CanvasObject { func (r *entryRenderer) Refresh() { content := r.entry.content - focusedAppearance := r.entry.focused && !r.entry.disabled.Load() + focusedAppearance := r.entry.focused && !r.entry.Disabled() scroll := r.entry.Scroll wrapping := r.entry.Wrapping @@ -1711,7 +1705,7 @@ func (r *entryRenderer) Refresh() { inputBorder := th.Size(theme.SizeNameInputBorder) // correct our scroll wrappers if the wrap mode changed - entrySize := r.entry.size.Load().Subtract(fyne.NewSize(r.trailingInset(), inputBorder*2)) + entrySize := r.entry.Size().Subtract(fyne.NewSize(r.trailingInset(), inputBorder*2)) if wrapping == fyne.TextWrapOff && scroll == widget.ScrollNone && r.scroll.Content != nil { r.scroll.Hide() r.scroll.Content = nil @@ -1774,7 +1768,7 @@ func (r *entryRenderer) ensureValidationSetup() { if r.entry.validationStatus == nil { r.entry.validationStatus = newValidationStatus(r.entry) r.objects = append(r.objects, r.entry.validationStatus) - r.Layout(r.entry.size.Load()) + r.Layout(r.entry.Size()) r.entry.validate() r.Refresh() @@ -1803,7 +1797,7 @@ func (e *entryContent) CreateRenderer() fyne.WidgetRenderer { r := &entryContentRenderer{e.entry.cursorAnim.cursor, []fyne.CanvasObject{}, objects, provider, placeholder, e} r.updateScrollDirections() - r.Layout(e.size.Load()) + r.Layout(e.Size()) return r } @@ -1870,7 +1864,7 @@ func (r *entryContentRenderer) Refresh() { provider := r.content.entry.textProvider() placeholder := r.content.entry.placeholderProvider() focused := r.content.entry.focused - focusedAppearance := focused && !r.content.entry.disabled.Load() + focusedAppearance := focused && !r.content.entry.Disabled() selections := r.selection r.updateScrollDirections() diff --git a/widget/entry_password.go b/widget/entry_password.go index 9bbcf03a24..7b7ceefcda 100644 --- a/widget/entry_password.go +++ b/widget/entry_password.go @@ -78,7 +78,7 @@ func (r *passwordRevealerRenderer) Refresh() { r.icon.Resource = th.Icon(theme.IconNameVisibilityOff) } - if r.entry.disabled.Load() { + if r.entry.Disabled() { r.icon.Resource = theme.NewDisabledResource(r.icon.Resource) } r.icon.Refresh() diff --git a/widget/entry_validation.go b/widget/entry_validation.go index 5095cd25cd..09a16904a6 100644 --- a/widget/entry_validation.go +++ b/widget/entry_validation.go @@ -116,7 +116,7 @@ func (r *validationStatusRenderer) MinSize() fyne.Size { func (r *validationStatusRenderer) Refresh() { th := r.entry.Theme() - if r.entry.disabled.Load() { + if r.entry.Disabled() { r.icon.Hide() return } diff --git a/widget/hyperlink.go b/widget/hyperlink.go index 04c3083a26..7ff915de35 100644 --- a/widget/hyperlink.go +++ b/widget/hyperlink.go @@ -125,7 +125,7 @@ func (hl *Hyperlink) focusWidth() float32 { th := hl.Theme() innerPad := th.Size(theme.SizeNameInnerPadding) - return fyne.Min(hl.size.Load().Width, hl.textSize.Width+innerPad+th.Size(theme.SizeNamePadding)*2) - innerPad + return fyne.Min(hl.Size().Width, hl.textSize.Width+innerPad+th.Size(theme.SizeNamePadding)*2) - innerPad } func (hl *Hyperlink) focusXPos() float32 { @@ -135,9 +135,9 @@ func (hl *Hyperlink) focusXPos() float32 { case fyne.TextAlignLeading: return innerPad / 2 case fyne.TextAlignCenter: - return (hl.size.Load().Width - hl.focusWidth()) / 2 + return (hl.Size().Width - hl.focusWidth()) / 2 case fyne.TextAlignTrailing: - return (hl.size.Load().Width - hl.focusWidth()) - innerPad/2 + return (hl.Size().Width - hl.focusWidth()) - innerPad/2 default: return 0 // unreached } diff --git a/widget/progressbarinfinite.go b/widget/progressbarinfinite.go index da2a3b565a..0b250cbf51 100644 --- a/widget/progressbarinfinite.go +++ b/widget/progressbarinfinite.go @@ -35,7 +35,7 @@ func (p *infProgressRenderer) MinSize() fyne.Size { } func (p *infProgressRenderer) updateBar(done float32) { - size := p.progress.size.Load() + size := p.progress.Size() progressWidth := size.Width spanWidth := progressWidth + (progressWidth * (maxProgressBarInfiniteWidthRatio / 2)) maxBarWidth := progressWidth * maxProgressBarInfiniteWidthRatio diff --git a/widget/radio_group.go b/widget/radio_group.go index 3cf9bcb11f..702c8b7939 100644 --- a/widget/radio_group.go +++ b/widget/radio_group.go @@ -236,8 +236,8 @@ func (r *radioGroupRenderer) updateItems(refresh bool) { item.Selected = sel changed = true } - if d := r.radio.disabled.Load(); d != item.disabled.Load() { - item.disabled.Store(d) + if d := r.radio.Disabled(); d != item.Disabled() { + item.disabled = d changed = true } diff --git a/widget/richtext.go b/widget/richtext.go index 4d44203215..1baf9d9f7d 100644 --- a/widget/richtext.go +++ b/widget/richtext.go @@ -118,11 +118,11 @@ func (t *RichText) Refresh() { // // Implements: fyne.Widget func (t *RichText) Resize(size fyne.Size) { - if size == t.size.Load() { + if size == t.Size() { return } - t.size.Store(size) + t.size = size skipResize := !t.minCache.IsZero() && size.Width >= t.minCache.Width && size.Height >= t.minCache.Height && t.Wrapping == fyne.TextWrapOff && t.Truncation == fyne.TextTruncateOff @@ -389,14 +389,14 @@ func (t *RichText) rows() int { func (t *RichText) updateRowBounds() { th := t.Theme() innerPadding := th.Size(theme.SizeNameInnerPadding) - fitSize := t.size.Load() + fitSize := t.Size() if t.scr != nil { fitSize = t.scr.Content.MinSize() } fitSize.Height -= (innerPadding + t.inset.Height) * 2 var bounds []rowBoundary - maxWidth := t.size.Load().Width - 2*innerPadding + 2*t.inset.Width + maxWidth := t.Size().Width - 2*innerPadding + 2*t.inset.Width wrapWidth := maxWidth var currentBound *rowBoundary diff --git a/widget/select.go b/widget/select.go index 8fd45f6e2b..a7f982918f 100644 --- a/widget/select.go +++ b/widget/select.go @@ -96,7 +96,7 @@ func (s *Select) CreateRenderer() fyne.WidgetRenderer { txtProv.inset = fyne.NewSquareSize(th.Size(theme.SizeNamePadding)) txtProv.ExtendBaseWidget(txtProv) txtProv.Truncation = fyne.TextTruncateEllipsis - if s.disabled.Load() { + if s.Disabled() { txtProv.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameDisabled } @@ -421,7 +421,7 @@ func (s *selectRenderer) Refresh() { if s.combo.popUp != nil { s.combo.popUp.alignment = s.combo.Alignment s.combo.popUp.Move(s.combo.popUpPos()) - s.combo.popUp.Resize(fyne.NewSize(s.combo.size.Load().Width, s.combo.popUp.MinSize().Height)) + s.combo.popUp.Resize(fyne.NewSize(s.combo.Size().Width, s.combo.popUp.MinSize().Height)) s.combo.popUp.Refresh() } s.background.Refresh() @@ -429,7 +429,7 @@ func (s *selectRenderer) Refresh() { } func (s *selectRenderer) bgColor(th fyne.Theme, v fyne.ThemeVariant) color.Color { - if s.combo.disabled.Load() { + if s.combo.Disabled() { return th.Color(theme.ColorNameDisabledButton, v) } if s.combo.focused { @@ -443,7 +443,7 @@ func (s *selectRenderer) bgColor(th fyne.Theme, v fyne.ThemeVariant) color.Color func (s *selectRenderer) updateIcon(th fyne.Theme) { icon := th.Icon(theme.IconNameArrowDropDown) - if s.combo.disabled.Load() { + if s.combo.Disabled() { s.icon.Resource = theme.NewDisabledResource(icon) } else { s.icon.Resource = icon @@ -456,16 +456,17 @@ func (s *selectRenderer) updateLabel() { s.combo.PlaceHolder = defaultPlaceHolder } - s.label.Segments[0].(*TextSegment).Style.Alignment = s.combo.Alignment - if s.combo.disabled.Load() { - s.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameDisabled + segment := s.label.Segments[0].(*TextSegment) + segment.Style.Alignment = s.combo.Alignment + if s.combo.Disabled() { + segment.Style.ColorName = theme.ColorNameDisabled } else { - s.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameForeground + segment.Style.ColorName = theme.ColorNameForeground } if s.combo.Selected == "" { - s.label.Segments[0].(*TextSegment).Text = s.combo.PlaceHolder + segment.Text = s.combo.PlaceHolder } else { - s.label.Segments[0].(*TextSegment).Text = s.combo.Selected + segment.Text = s.combo.Selected } s.label.Refresh() } diff --git a/widget/slider.go b/widget/slider.go index 781a5c08a2..35f90c7068 100644 --- a/widget/slider.go +++ b/widget/slider.go @@ -230,7 +230,7 @@ func (s *Slider) getRatio(e *fyne.PointEvent) float64 { x := e.Position.X y := e.Position.Y - size := s.size.Load() + size := s.Size() switch s.Orientation { case Vertical: diff --git a/widget/table.go b/widget/table.go index 18c6af40ad..1ebbb3f721 100644 --- a/widget/table.go +++ b/widget/table.go @@ -861,7 +861,7 @@ func (t *Table) visibleColumnWidths(colWidth float32, cols int) (visible map[int padding := t.Theme().Size(theme.SizeNamePadding) stick := t.StickyColumnCount - size := t.size.Load() + size := t.Size() if len(t.columnWidths) == 0 { paddedWidth := colWidth + padding @@ -961,7 +961,7 @@ func (t *Table) visibleRowHeights(rowHeight float32, rows int) (visible map[int] padding := t.Theme().Size(theme.SizeNamePadding) stick := t.StickyRowCount - size := t.size.Load() + size := t.Size() if len(t.rowHeights) == 0 { paddedHeight := rowHeight + padding @@ -1448,7 +1448,7 @@ func (r *tableCellsRenderer) moveIndicators() { r.cells.t.dividerLayer.Content.Refresh() } - size := r.cells.t.size.Load() + size := r.cells.t.Size() divs := 0 i := 0 @@ -1552,7 +1552,7 @@ func (r *tableCellsRenderer) moveMarker(marker fyne.CanvasObject, row, col int, } y2 := y1 + heights[row] - size := r.cells.t.size.Load() + size := r.cells.t.Size() if x2 < 0 || x1 > size.Width || y2 < 0 || y1 > size.Height { marker.Hide() } else { diff --git a/widget/tree.go b/widget/tree.go index de48c91339..569130f89b 100644 --- a/widget/tree.go +++ b/widget/tree.go @@ -229,11 +229,11 @@ func (t *Tree) OpenBranch(uid TreeNodeID) { // Resize sets a new size for a widget. func (t *Tree) Resize(size fyne.Size) { - if size == t.size.Load() { + if size == t.Size() { return } - t.size.Store(size) + t.size = size t.Refresh() // trigger a redraw } @@ -584,11 +584,11 @@ func (c *treeContent) CreateRenderer() fyne.WidgetRenderer { } func (c *treeContent) Resize(size fyne.Size) { - if size == c.size.Load() { + if size == c.Size() { return } - c.size.Store(size) + c.size = size c.Refresh() // trigger a redraw } diff --git a/widget/widget.go b/widget/widget.go index 0fabbb3038..fb38511750 100644 --- a/widget/widget.go +++ b/widget/widget.go @@ -2,11 +2,8 @@ package widget // import "fyne.io/fyne/v2/widget" 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" "fyne.io/fyne/v2/theme" @@ -14,27 +11,26 @@ import ( // BaseWidget provides a helper that handles basic widget behaviours. type BaseWidget struct { - size async.Size - position async.Position + size fyne.Size + position fyne.Position Hidden bool - impl atomic.Pointer[fyne.Widget] + impl fyne.Widget themeCache fyne.Theme } // ExtendBaseWidget is used by an extending widget to make use of BaseWidget functionality. func (w *BaseWidget) ExtendBaseWidget(wid fyne.Widget) { - impl := w.super() - if impl != nil { + if w.super() != nil { return } - w.impl.Store(&wid) + w.impl = wid } // Size gets the current size of this widget. func (w *BaseWidget) Size() fyne.Size { - return w.size.Load() + return w.size } // Resize sets a new size for a widget. @@ -44,7 +40,7 @@ func (w *BaseWidget) Resize(size fyne.Size) { return } - w.size.Store(size) + w.size = size impl := w.super() if impl == nil { @@ -55,13 +51,13 @@ func (w *BaseWidget) Resize(size fyne.Size) { // Position gets the current position of this widget, relative to its parent. func (w *BaseWidget) Position() fyne.Position { - return w.position.Load() + return w.position } // Move the widget to a new position, relative to its parent. // Note this should not be used if the widget is being managed by a Layout within a Container. func (w *BaseWidget) Move(pos fyne.Position) { - w.position.Store(pos) + w.position = pos internalWidget.Repaint(w.super()) } @@ -122,8 +118,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). @@ -149,12 +144,7 @@ func (w *BaseWidget) Theme() fyne.Theme { // 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 { - impl := w.impl.Load() - if impl == nil { - return nil - } - - return *impl + return w.impl } // DisableableWidget describes an extension to BaseWidget which can be disabled. @@ -162,15 +152,17 @@ func (w *BaseWidget) super() fyne.Widget { type DisableableWidget struct { BaseWidget - disabled atomic.Bool + disabled bool } // Enable this widget, updating any style or features appropriately. func (w *DisableableWidget) Enable() { - if !w.disabled.CompareAndSwap(true, false) { + if !w.Disabled() { return // Enabled already } + w.disabled = false + impl := w.super() if impl == nil { return @@ -180,10 +172,12 @@ 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.CompareAndSwap(false, true) { + if w.Disabled() { return // Disabled already } + w.disabled = true + impl := w.super() if impl == nil { return @@ -193,7 +187,7 @@ func (w *DisableableWidget) Disable() { // Disabled returns true if this widget is currently disabled or false if it can currently be interacted with. func (w *DisableableWidget) Disabled() bool { - return w.disabled.Load() + return w.disabled } // NewSimpleRenderer creates a new SimpleRenderer to render a widget using a