Skip to content

Commit

Permalink
Use atomics for position
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Jan 3, 2024
1 parent f47e844 commit 73007bc
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package widget // import "fyne.io/fyne/v2/widget"

import (
"math"
"sync"
"sync/atomic"

Expand All @@ -14,7 +15,7 @@ import (
// BaseWidget provides a helper that handles basic widget behaviours.
type BaseWidget struct {
size fyne.Size
position fyne.Position
position atomic.Uint64
Hidden bool

impl atomic.Pointer[fyne.Widget]
Expand Down Expand Up @@ -62,19 +63,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 {
w.propertyLock.RLock()
defer w.propertyLock.RUnlock()

return w.position
return fyne.NewPos(twoFloat32FromUint64(w.position.Load()))
}

// 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.propertyLock.Lock()
w.position = pos
w.propertyLock.Unlock()

w.position.Store(uint64fromTwoFloat32(pos.X, pos.Y))
internalWidget.Repaint(w.super())
}

Expand Down Expand Up @@ -208,3 +203,15 @@ func (w *DisableableWidget) Disabled() bool {
func NewSimpleRenderer(object fyne.CanvasObject) fyne.WidgetRenderer {
return internalWidget.NewSimpleRenderer(object)
}

func uint64fromTwoFloat32(a, b float32) uint64 {
x := uint64(math.Float32bits(a))
y := uint64(math.Float32bits(b))
return (y << 32) | x
}

func twoFloat32FromUint64(combined uint64) (float32, float32) {
x := uint32(combined & 0x00000000FFFFFFFF)
y := uint32(combined & 0xFFFFFFFF00000000 >> 32)
return math.Float32frombits(x), math.Float32frombits(y)
}

0 comments on commit 73007bc

Please sign in to comment.