Skip to content

Commit

Permalink
Add a cache for MinSize in BaseWidget
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed May 2, 2024
1 parent 1c4ef38 commit bc83a80
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 31 deletions.
16 changes: 8 additions & 8 deletions widget/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Check struct {
hovered bool

binder basicBinder

minSize fyne.Size // cached for hover/tap position calculations
}

// NewCheck creates a new check widget with the set label and change handler
Expand Down Expand Up @@ -119,8 +117,9 @@ func (c *Check) MouseMoved(me *desktop.MouseEvent) {

// only hovered if cached minSize has not been initialized (test code)
// or the pointer is within the "active" area of the widget (its minSize)
c.hovered = c.minSize.IsZero() ||
(me.Position.X <= c.minSize.Width && me.Position.Y <= c.minSize.Height)
minSize := c.MinSize()
c.hovered = minSize.IsZero() ||
(me.Position.X <= minSize.Width && me.Position.Y <= minSize.Height)

if oldHovered != c.hovered {
c.Refresh()
Expand All @@ -132,8 +131,10 @@ func (c *Check) Tapped(pe *fyne.PointEvent) {
if c.Disabled() {
return
}
if !c.minSize.IsZero() &&
(pe.Position.X > c.minSize.Width || pe.Position.Y > c.minSize.Height) {

minSize := c.MinSize()
if !minSize.IsZero() &&
(pe.Position.X > minSize.Width || pe.Position.Y > minSize.Height) {
// tapped outside the active area of the widget
return
}
Expand All @@ -151,8 +152,7 @@ func (c *Check) Tapped(pe *fyne.PointEvent) {
// MinSize returns the size that this widget should not shrink below
func (c *Check) MinSize() fyne.Size {
c.ExtendBaseWidget(c)
c.minSize = c.BaseWidget.MinSize()
return c.minSize
return c.BaseWidget.MinSize()
}

// CreateRenderer is a private method to Fyne which links this widget to its renderer
Expand Down
23 changes: 1 addition & 22 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ type Entry struct {
ActionItem fyne.CanvasObject `json:"-"`
binder basicBinder
conversionError error
minCache fyne.Size
multiLineRows int // override global default number of visible lines

// undoStack stores the data necessary for undo/redo functionality
Expand Down Expand Up @@ -402,20 +401,8 @@ func (e *Entry) KeyUp(key *fyne.KeyEvent) {
//
// Implements: fyne.Widget
func (e *Entry) MinSize() fyne.Size {
e.propertyLock.RLock()
cached := e.minCache
e.propertyLock.RUnlock()
if !cached.IsZero() {
return cached
}

e.ExtendBaseWidget(e)
min := e.BaseWidget.MinSize()

e.propertyLock.Lock()
e.minCache = min
e.propertyLock.Unlock()
return min
return e.BaseWidget.MinSize()
}

// MouseDown called on mouse click, this triggers a mouse click which can move the cursor,
Expand Down Expand Up @@ -480,14 +467,6 @@ func (e *Entry) Redo() {
e.Refresh()
}

func (e *Entry) Refresh() {
e.propertyLock.Lock()
e.minCache = fyne.Size{}
e.propertyLock.Unlock()

e.BaseWidget.Refresh()
}

// SelectedText returns the text currently selected in this Entry.
// If there is no selection it will return the empty string.
func (e *Entry) SelectedText() string {
Expand Down
2 changes: 1 addition & 1 deletion widget/select_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (e *SelectEntry) Disable() {
// Implements: fyne.Widget
func (e *SelectEntry) MinSize() fyne.Size {
e.ExtendBaseWidget(e)
return e.Entry.MinSize()
return e.BaseWidget.MinSize()
}

// Move changes the relative position of the select entry.
Expand Down
9 changes: 9 additions & 0 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// BaseWidget provides a helper that handles basic widget behaviours.
type BaseWidget struct {
size async.Size
minCache async.Size
position async.Position
Hidden bool

Expand Down Expand Up @@ -69,6 +70,11 @@ func (w *BaseWidget) Move(pos fyne.Position) {

// MinSize for the widget - it should never be resized below this value.
func (w *BaseWidget) MinSize() fyne.Size {
minCache := w.minCache.Load()
if !minCache.IsZero() {
return minCache
}

impl := w.super()

r := cache.Renderer(impl)
Expand Down Expand Up @@ -123,6 +129,7 @@ func (w *BaseWidget) Refresh() {
return
}

w.minCache.Store(fyne.Size{})
w.propertyLock.Lock()
w.themeCache = nil
w.propertyLock.Unlock()
Expand Down Expand Up @@ -170,6 +177,8 @@ func (w *BaseWidget) SetFieldsAndRefresh(f func()) {
if impl == nil {
return
}

w.minCache.Store(fyne.Size{})
impl.Refresh()
}

Expand Down

0 comments on commit bc83a80

Please sign in to comment.