Skip to content

Commit

Permalink
RFC for adding a cache for MinSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Feb 27, 2024
1 parent 6b7246b commit adf3c93
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
19 changes: 3 additions & 16 deletions widget/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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 @@ -401,9 +400,7 @@ 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()
cached := e.GetMinSizeCache()
if !cached.IsZero() {
return cached
}
Expand All @@ -412,17 +409,15 @@ func (e *Entry) MinSize() fyne.Size {

th := e.Theme()
iconSpace := th.Size(theme.SizeNameInlineIcon) + th.Size(theme.SizeNameLineSpacing)
min := e.BaseWidget.MinSize()
min := e.BaseWidget.MinSizeFromRenderer()
if e.ActionItem != nil {
min = min.Add(fyne.NewSize(iconSpace, 0))
}
if e.Validator != nil {
min = min.Add(fyne.NewSize(iconSpace, 0))
}

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

Expand Down Expand Up @@ -488,14 +483,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
25 changes: 25 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,16 @@ 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
}

return w.MinSizeFromRenderer()
}

// MinSizeFromRenderer returns the MinSize has defined by this widget's renderer.
func (w *BaseWidget) MinSizeFromRenderer() fyne.Size {
impl := w.super()

r := cache.Renderer(impl)
Expand All @@ -88,6 +99,18 @@ func (w *BaseWidget) Visible() bool {
return !w.Hidden
}

// GetMinSizeCache returns the currently cached MinSize value.
// This value is set to zero on calling Refresh().
func (w *BaseWidget) GetMinSizeCache() fyne.Size {
return w.minCache.Load()
}

// SetMinSizeCache updates the internal cache for the MinSize.
// This cached value will be used until the next Refresh.
func (w *BaseWidget) SetMinSizeCache(cache fyne.Size) {
w.minCache.Store(cache)
}

// Show this widget so it becomes visible
func (w *BaseWidget) Show() {
if w.Visible() {
Expand Down Expand Up @@ -123,6 +146,8 @@ func (w *BaseWidget) Refresh() {
return
}

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

w.propertyLock.Lock()
w.themeCache = nil
w.propertyLock.Unlock()
Expand Down

0 comments on commit adf3c93

Please sign in to comment.