Skip to content

Commit

Permalink
Use atomics for disabled widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 3, 2024
1 parent da5aea9 commit cb0202e
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion widget/button.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ func (r *buttonRenderer) applyTheme() {
r.button.applyButtonTheme()
r.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameForeground
switch {
case r.button.disabled:
case r.button.Disabled():
r.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameDisabled
case r.button.Importance == HighImportance || r.button.Importance == DangerImportance || r.button.Importance == WarningImportance || r.button.Importance == SuccessImportance:
if r.button.focused {
Expand Down
6 changes: 3 additions & 3 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (c *checkRenderer) Layout(size fyne.Size) {
func (c *checkRenderer) applyTheme() {
c.label.Color = theme.ForegroundColor()
c.label.TextSize = theme.TextSize()
if c.check.disabled {
if c.check.Disabled() {
c.label.Color = theme.DisabledColor()
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ func (c *checkRenderer) updateResource() {
res.ColorName = theme.ColorNamePrimary
bgRes.ColorName = theme.ColorNameBackground
}
if c.check.disabled {
if c.check.Disabled() {
if c.check.Checked {
res = theme.NewThemedResource(theme.CheckButtonCheckedIcon())
}
Expand All @@ -98,7 +98,7 @@ func (c *checkRenderer) updateResource() {
}

func (c *checkRenderer) updateFocusIndicator() {
if c.check.disabled {
if c.check.Disabled() {
c.focusIndicator.FillColor = color.Transparent
} else if c.check.focused {
c.focusIndicator.FillColor = theme.FocusColor()
Expand Down
4 changes: 2 additions & 2 deletions widget/check_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (r *CheckGroup) update() {

item.Text = r.Options[i]
item.Checked = contains
item.DisableableWidget.disabled = r.disabled
item.DisableableWidget.disabled.Store(r.Disabled())
item.Refresh()
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ func (r *checkGroupRenderer) updateItems() {
}
item.Text = r.checks.Options[i]
item.Checked = contains
item.disabled = r.checks.disabled
item.disabled.Store(r.checks.Disabled())
item.Refresh()
}
}
4 changes: 2 additions & 2 deletions widget/check_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestCheck_Focused(t *testing.T) {
}

check.Disable()
assert.True(t, check.disabled)
assert.True(t, check.Disabled())
assert.Equal(t, color.Transparent, render.focusIndicator.FillColor)

check.Enable()
Expand Down Expand Up @@ -185,7 +185,7 @@ func TestCheck_Hovered(t *testing.T) {
}

check.Disable()
assert.True(t, check.disabled)
assert.True(t, check.Disabled())
assert.True(t, check.hovered)
assert.Equal(t, color.Transparent, render.focusIndicator.FillColor)

Expand Down
11 changes: 6 additions & 5 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (e *Entry) Disable() {
//
// Implements: fyne.Disableable
func (e *Entry) Disabled() bool {
return e.DisableableWidget.disabled
return e.DisableableWidget.Disabled()
}

// DoubleTapped is called when this entry has been double tapped so we should select text below the pointer
Expand Down Expand Up @@ -1305,7 +1305,8 @@ func (e *Entry) textPosFromRowCol(row, col int) int {
func (e *Entry) syncSegments() {
colName := theme.ColorNameForeground
wrap := e.textWrap()
if e.disabled {
disabled := e.Disabled()
if disabled {
colName = theme.ColorNameDisabled
}
e.textProvider().Wrapping = wrap
Expand All @@ -1324,7 +1325,7 @@ func (e *Entry) syncSegments() {
Text: e.Text,
}}
colName = theme.ColorNamePlaceHolder
if e.disabled {
if disabled {
colName = theme.ColorNameDisabled
}
e.placeholderProvider().Wrapping = wrap
Expand Down Expand Up @@ -1666,7 +1667,7 @@ func (r *entryRenderer) Objects() []fyne.CanvasObject {
func (r *entryRenderer) Refresh() {
r.entry.propertyLock.RLock()
content := r.entry.content
focusedAppearance := r.entry.focused && !r.entry.disabled
focusedAppearance := r.entry.focused && !r.entry.Disabled()
scroll := r.entry.Scroll
wrapping := r.entry.Wrapping
r.entry.propertyLock.RUnlock()
Expand Down Expand Up @@ -1841,7 +1842,7 @@ func (r *entryContentRenderer) Refresh() {
r.content.entry.propertyLock.RLock()
provider := r.content.entry.textProvider()
placeholder := r.content.entry.placeholderProvider()
focusedAppearance := r.content.entry.focused && !r.content.entry.disabled
focusedAppearance := r.content.entry.focused && !r.content.entry.Disabled()
selections := r.selection
r.updateScrollDirections()
r.content.entry.propertyLock.RUnlock()
Expand Down
2 changes: 1 addition & 1 deletion widget/entry_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *passwordRevealerRenderer) Refresh() {
r.icon.Resource = theme.VisibilityOffIcon()
}

if r.entry.disabled {
if r.entry.Disabled() {
r.icon.Resource = theme.NewDisabledResource(r.icon.Resource)
}
canvas.Refresh(r.icon)
Expand Down
2 changes: 1 addition & 1 deletion widget/entry_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *validationStatusRenderer) MinSize() fyne.Size {
func (r *validationStatusRenderer) Refresh() {
r.entry.propertyLock.RLock()
defer r.entry.propertyLock.RUnlock()
if r.entry.disabled {
if r.entry.Disabled() {
r.icon.Hide()
return
}
Expand Down
4 changes: 2 additions & 2 deletions widget/radio_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r *RadioGroup) update() {
for i, item := range r.items {
item.Label = r.Options[i]
item.Selected = item.Label == r.Selected
item.DisableableWidget.disabled = r.disabled
item.DisableableWidget.disabled.Store(r.Disabled())
item.Refresh()
}
}
Expand Down Expand Up @@ -207,7 +207,7 @@ func (r *radioGroupRenderer) updateItems() {
for i, item := range r.items {
item.Label = r.radio.Options[i]
item.Selected = item.Label == r.radio.Selected
item.disabled = r.radio.disabled
item.disabled.Store(r.radio.Disabled())
item.Refresh()
}
}
Expand Down
8 changes: 4 additions & 4 deletions widget/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (s *Select) CreateRenderer() fyne.WidgetRenderer {
txtProv.inset = fyne.NewSize(theme.Padding(), theme.Padding())
txtProv.ExtendBaseWidget(txtProv)
txtProv.Truncation = fyne.TextTruncateEllipsis
if s.disabled {
if s.Disabled() {
txtProv.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameDisabled
}

Expand Down Expand Up @@ -342,7 +342,7 @@ func (s *selectRenderer) Refresh() {
}

func (s *selectRenderer) bgColor() color.Color {
if s.combo.disabled {
if s.combo.Disabled() {
return theme.DisabledButtonColor()
}
if s.combo.focused {
Expand All @@ -355,7 +355,7 @@ func (s *selectRenderer) bgColor() color.Color {
}

func (s *selectRenderer) updateIcon() {
if s.combo.disabled {
if s.combo.Disabled() {
s.icon.Resource = theme.NewDisabledResource(theme.MenuDropDownIcon())
} else {
s.icon.Resource = theme.MenuDropDownIcon()
Expand All @@ -369,7 +369,7 @@ func (s *selectRenderer) updateLabel() {
}

s.label.Segments[0].(*TextSegment).Style.Alignment = s.combo.Alignment
if s.combo.disabled {
if s.combo.Disabled() {
s.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameDisabled
} else {
s.label.Segments[0].(*TextSegment).Style.ColorName = theme.ColorNameForeground
Expand Down
27 changes: 16 additions & 11 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func (w *BaseWidget) super() fyne.Widget {
type DisableableWidget struct {
BaseWidget

disabled bool
disabled atomic.Bool
}

// Enable this widget, updating any style or features appropriately.
Expand All @@ -164,9 +164,13 @@ func (w *DisableableWidget) Enable() {
return
}

w.setFieldsAndRefresh(func() {
w.disabled = false
})
w.disabled.Store(false)

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

// Disable this widget so that it cannot be interacted with, updating any style appropriately.
Expand All @@ -175,17 +179,18 @@ func (w *DisableableWidget) Disable() {
return
}

w.setFieldsAndRefresh(func() {
w.disabled = true
})
w.disabled.Store(true)

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

// Disabled returns true if this widget is currently disabled or false if it can currently be interacted with.
func (w *DisableableWidget) Disabled() bool {
w.propertyLock.RLock()
defer w.propertyLock.RUnlock()

return w.disabled
return w.disabled.Load()
}

// NewSimpleRenderer creates a new SimpleRenderer to render a widget using a
Expand Down

0 comments on commit cb0202e

Please sign in to comment.