Skip to content

Commit

Permalink
Implement cache reset and make rich text use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed May 2, 2024
1 parent bc83a80 commit 2193724
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
13 changes: 6 additions & 7 deletions widget/richtext.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ type RichText struct {

visualCache map[RichTextSegment][]fyne.CanvasObject
cacheLock sync.Mutex
minCache fyne.Size
}

// NewRichText returns a new RichText widget that renders the given text and segments.
Expand Down Expand Up @@ -89,19 +88,18 @@ func (t *RichText) CreateRenderer() fyne.WidgetRenderer {
// MinSize calculates the minimum size of a rich text widget.
// This is based on the contained text with a standard amount of padding added.
func (t *RichText) MinSize() fyne.Size {
// we don't return the minCache here, as any internal segments could have caused it to change...
t.ExtendBaseWidget(t)

min := t.BaseWidget.MinSize()
t.minCache = min
return min
// We don't return the cached value as any internal segments could have caused it to change...
t.ResetMinSizeCache()
return t.BaseWidget.MinSize()
}

// Refresh triggers a redraw of the rich text.
//
// Implements: fyne.Widget
func (t *RichText) Refresh() {
t.minCache = fyne.Size{}
t.ResetMinSizeCache()
t.updateRowBounds()

for _, s := range t.Segments {
Expand All @@ -123,10 +121,11 @@ func (t *RichText) Resize(size fyne.Size) {
}

t.size.Store(size)
minSize := t.MinSize()

t.propertyLock.RLock()
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
skipResize := !minSize.IsZero() && size.Width >= minSize.Width && size.Height >= minSize.Height && t.Wrapping == fyne.TextWrapOff && t.Truncation == fyne.TextTruncateOff
t.propertyLock.RUnlock()

if skipResize {
Expand Down
7 changes: 7 additions & 0 deletions widget/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ func (w *BaseWidget) themeWithLock() fyne.Theme {
return cached
}

// ResetMinSizeCache resets the cached MinSize for this widget.
//
// Since: 2.5.0
func (w *BaseWidget) ResetMinSizeCache() {
w.minCache.Store(fyne.Size{})
}

// SetFieldsAndRefresh helps to make changes to a widget that should be followed by a refresh.
// This method is a guaranteed thread-safe way of directly manipulating widget fields.
// Widgets extending BaseWidget should use this in their setter functions.
Expand Down

0 comments on commit 2193724

Please sign in to comment.