Skip to content

Commit

Permalink
Use atomics for basewidget size as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 3, 2024
1 parent 73007bc commit da5aea9
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 58 deletions.
7 changes: 3 additions & 4 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,6 @@ func (r *entryRenderer) Refresh() {
content := r.entry.content
focusedAppearance := r.entry.focused && !r.entry.disabled
scroll := r.entry.Scroll
size := r.entry.size
wrapping := r.entry.Wrapping
r.entry.propertyLock.RUnlock()

Expand All @@ -1679,7 +1678,7 @@ func (r *entryRenderer) Refresh() {
r.entry.placeholder.Refresh()

// correct our scroll wrappers if the wrap mode changed
entrySize := size.Subtract(fyne.NewSize(r.trailingInset(), theme.InputBorderSize()*2))
entrySize := r.entry.Size().Subtract(fyne.NewSize(r.trailingInset(), theme.InputBorderSize()*2))
if wrapping == fyne.TextWrapOff && scroll == widget.ScrollNone && r.scroll.Content != nil {
r.scroll.Hide()
r.scroll.Content = nil
Expand Down Expand Up @@ -1742,7 +1741,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)
r.Layout(r.entry.Size())

r.entry.Validate()

Expand Down Expand Up @@ -1774,7 +1773,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)
r.Layout(e.Size())
return r
}

Expand Down
5 changes: 3 additions & 2 deletions widget/progressbarinfinite.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ func (p *infProgressRenderer) MinSize() fyne.Size {
}

func (p *infProgressRenderer) updateBar(done float32) {
progressWidth := p.progress.size.Width
size := p.progress.Size()
progressWidth := size.Width
spanWidth := progressWidth + (progressWidth * (maxProgressBarInfiniteWidthRatio / 2))
maxBarWidth := progressWidth * maxProgressBarInfiniteWidthRatio

barCenterX := spanWidth*done - (spanWidth-progressWidth)/2
barPos := fyne.NewPos(barCenterX-maxBarWidth/2, 0)
barSize := fyne.NewSize(maxBarWidth, p.progress.size.Height)
barSize := fyne.NewSize(maxBarWidth, size.Height)
if barPos.X < 0 {
barSize.Width += barPos.X
barPos.X = 0
Expand Down
15 changes: 7 additions & 8 deletions widget/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,16 @@ func (t *RichText) Refresh() {
//
// Implements: fyne.Widget
func (t *RichText) Resize(size fyne.Size) {
if size == t.Size() {
return
}

t.size.Store(uint64fromTwoFloat32(size.Width, size.Height))

t.propertyLock.RLock()
baseSize := t.size
segments := t.Segments
skipResize := !t.minCache.IsZero() && size.Width >= t.minCache.Width && size.Height >= t.minCache.Height && t.Wrapping == fyne.TextWrapOff && t.Truncation == fyne.TextTruncateOff
t.propertyLock.RUnlock()
if baseSize == size {
return
}
t.propertyLock.Lock()
t.size = size
t.propertyLock.Unlock()

if skipResize {
if len(segments) < 2 { // we can simplify :)
Expand Down Expand Up @@ -364,7 +363,7 @@ func (t *RichText) updateRowBounds() {

t.propertyLock.RLock()
var bounds []rowBoundary
maxWidth := t.size.Width - 2*innerPadding + 2*t.inset.Width
maxWidth := t.Size().Width - 2*innerPadding + 2*t.inset.Width
wrapWidth := maxWidth

var currentBound *rowBoundary
Expand Down
2 changes: 1 addition & 1 deletion widget/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,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.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()
Expand Down
9 changes: 5 additions & 4 deletions widget/slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,24 @@ func (s *Slider) getRatio(e *fyne.PointEvent) float64 {

x := e.Position.X
y := e.Position.Y
size := s.Size()

switch s.Orientation {
case Vertical:
if y > s.size.Height-pad {
if y > size.Height-pad {
return 0.0
} else if y < pad {
return 1.0
} else {
return 1 - float64(y-pad)/float64(s.size.Height-pad*2)
return 1 - float64(y-pad)/float64(size.Height-pad*2)
}
case Horizontal:
if x > s.size.Width-pad {
if x > size.Width-pad {
return 1.0
} else if x < pad {
return 0.0
} else {
return float64(x-pad) / float64(s.size.Width-pad*2)
return float64(x-pad) / float64(size.Width-pad*2)
}
}
return 0.0
Expand Down
21 changes: 12 additions & 9 deletions widget/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,13 +854,14 @@ func (t *Table) visibleColumnWidths(colWidth float32, cols int) (visible map[int
// theme.Padding is a slow call, so we cache it
padding := theme.Padding()
stick := t.StickyColumnCount
size := t.Size()

if len(t.columnWidths) == 0 {
paddedWidth := colWidth + padding

offX = float32(math.Floor(float64(t.offset.X/paddedWidth))) * paddedWidth
minCol = int(math.Floor(float64(offX / paddedWidth)))
maxCol = int(math.Ceil(float64((t.offset.X + t.size.Width) / paddedWidth)))
maxCol = int(math.Ceil(float64((t.offset.X + size.Width) / paddedWidth)))

if minCol > cols-1 {
minCol = cols - 1
Expand Down Expand Up @@ -896,7 +897,7 @@ func (t *Table) visibleColumnWidths(colWidth float32, cols int) (visible map[int
offX = colOffset
isVisible = true
}
if colOffset < t.offset.X+t.size.Width {
if colOffset < t.offset.X+size.Width {
maxCol = i + 1
} else {
break
Expand Down Expand Up @@ -954,13 +955,14 @@ func (t *Table) visibleRowHeights(rowHeight float32, rows int) (visible map[int]
// theme.Padding is a slow call, so we cache it
padding := theme.Padding()
stick := t.StickyRowCount
size := t.Size()

if len(t.rowHeights) == 0 {
paddedHeight := rowHeight + padding

offY = float32(math.Floor(float64(t.offset.Y/paddedHeight))) * paddedHeight
minRow = int(math.Floor(float64(offY / paddedHeight)))
maxRow = int(math.Ceil(float64((t.offset.Y + t.size.Height) / paddedHeight)))
maxRow = int(math.Ceil(float64((t.offset.Y + size.Height) / paddedHeight)))

if minRow > rows-1 {
minRow = rows - 1
Expand Down Expand Up @@ -996,7 +998,7 @@ func (t *Table) visibleRowHeights(rowHeight float32, rows int) (visible map[int]
offY = rowOffset
isVisible = true
}
if rowOffset < t.offset.Y+t.size.Height {
if rowOffset < t.offset.Y+size.Height {
maxRow = i + 1
} else {
break
Expand Down Expand Up @@ -1448,7 +1450,7 @@ func (r *tableCellsRenderer) moveIndicators() {
i++

xPos := x + dividerOff
r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.Size().Height))
r.dividers[divs].Move(fyne.NewPos(xPos, 0))
r.dividers[divs].Show()
divs++
Expand All @@ -1459,7 +1461,7 @@ func (r *tableCellsRenderer) moveIndicators() {
i++

xPos := x - r.cells.t.content.Offset.X + dividerOff
r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.size.Height))
r.dividers[divs].Resize(fyne.NewSize(separatorThickness, r.cells.t.Size().Height))
r.dividers[divs].Move(fyne.NewPos(xPos, 0))
r.dividers[divs].Show()
divs++
Expand All @@ -1471,7 +1473,7 @@ func (r *tableCellsRenderer) moveIndicators() {
i++

yPos := y + dividerOff
r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
r.dividers[divs].Resize(fyne.NewSize(r.cells.t.Size().Width, separatorThickness))
r.dividers[divs].Move(fyne.NewPos(0, yPos))
r.dividers[divs].Show()
divs++
Expand All @@ -1482,7 +1484,7 @@ func (r *tableCellsRenderer) moveIndicators() {
i++

yPos := y - r.cells.t.content.Offset.Y + dividerOff
r.dividers[divs].Resize(fyne.NewSize(r.cells.t.size.Width, separatorThickness))
r.dividers[divs].Resize(fyne.NewSize(r.cells.t.Size().Width, separatorThickness))
r.dividers[divs].Move(fyne.NewPos(0, yPos))
r.dividers[divs].Show()
divs++
Expand Down Expand Up @@ -1543,7 +1545,8 @@ func (r *tableCellsRenderer) moveMarker(marker fyne.CanvasObject, row, col int,
}
y2 := y1 + heights[row]

if x2 < 0 || x1 > r.cells.t.size.Width || y2 < 0 || y1 > r.cells.t.size.Height {
size := r.cells.t.Size()
if x2 < 0 || x1 > size.Width || y2 < 0 || y1 > size.Height {
marker.Hide()
} else {
left := x1
Expand Down
2 changes: 1 addition & 1 deletion widget/textgrid.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ func (t *textGridRenderer) Refresh() {
t.updateCellSize()

TextGridStyleWhitespace = &CustomTextGridStyle{FGColor: theme.DisabledColor()}
t.updateGridSize(t.text.size)
t.updateGridSize(t.text.Size())
t.refreshGrid()
}

Expand Down
22 changes: 5 additions & 17 deletions widget/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,11 @@ func (t *Tree) OpenBranch(uid TreeNodeID) {

// Resize sets a new size for a widget.
func (t *Tree) Resize(size fyne.Size) {
t.propertyLock.RLock()
s := t.size
t.propertyLock.RUnlock()

if s == size {
if size == t.Size() {
return
}

t.propertyLock.Lock()
t.size = size
t.propertyLock.Unlock()
t.size.Store(uint64fromTwoFloat32(size.Width, size.Height))

t.Refresh() // trigger a redraw
}
Expand Down Expand Up @@ -592,17 +586,11 @@ func (c *treeContent) CreateRenderer() fyne.WidgetRenderer {
}

func (c *treeContent) Resize(size fyne.Size) {
c.propertyLock.RLock()
s := c.size
c.propertyLock.RUnlock()

if s == size {
if size == c.Size() {
return
}

c.propertyLock.Lock()
c.size = size
c.propertyLock.Unlock()
c.size.Store(uint64fromTwoFloat32(size.Width, size.Height))

c.Refresh() // trigger a redraw
}
Expand Down Expand Up @@ -976,7 +964,7 @@ func (r *treeNodeRenderer) partialRefresh() {
r.background.Hide()
}
r.background.Refresh()
r.Layout(r.treeNode.size)
r.Layout(r.treeNode.Size())
canvas.Refresh(r.treeNode.super())
}

Expand Down
16 changes: 4 additions & 12 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

// BaseWidget provides a helper that handles basic widget behaviours.
type BaseWidget struct {
size fyne.Size
size atomic.Uint64
position atomic.Uint64
Hidden bool

Expand All @@ -34,25 +34,17 @@ func (w *BaseWidget) ExtendBaseWidget(wid fyne.Widget) {

// Size gets the current size of this widget.
func (w *BaseWidget) Size() fyne.Size {
w.propertyLock.RLock()
defer w.propertyLock.RUnlock()

return w.size
return fyne.NewSize(twoFloat32FromUint64(w.size.Load()))
}

// Resize sets a new size for a widget.
// Note this should not be used if the widget is being managed by a Layout within a Container.
func (w *BaseWidget) Resize(size fyne.Size) {
w.propertyLock.RLock()
baseSize := w.size
w.propertyLock.RUnlock()
if baseSize == size {
if size == w.Size() {
return
}

w.propertyLock.Lock()
w.size = size
w.propertyLock.Unlock()
w.size.Store(uint64fromTwoFloat32(size.Width, size.Height))

impl := w.super()
if impl == nil {
Expand Down

0 comments on commit da5aea9

Please sign in to comment.