Skip to content

Commit

Permalink
Lat the renderer define the MinSize
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Feb 27, 2024
1 parent f3d85d0 commit 7de68d1
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 29 deletions.
15 changes: 7 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,9 @@ 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 +151,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
1 change: 0 additions & 1 deletion widget/gridwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (l *GridWrap) FocusLost() {
// MinSize returns the size that this widget should not shrink below.
func (l *GridWrap) MinSize() fyne.Size {
l.ExtendBaseWidget(l)

return l.BaseWidget.MinSize()
}

Expand Down
2 changes: 1 addition & 1 deletion widget/hyperlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (hl *Hyperlink) MinSize() fyne.Size {
hl.syncSegments()
}

return hl.provider.MinSize()
return hl.BaseWidget.MinSize()
}

// Resize sets a new size for the hyperlink.
Expand Down
9 changes: 2 additions & 7 deletions widget/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package widget
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/theme"
)

Expand Down Expand Up @@ -77,12 +76,8 @@ func (l *Label) CreateRenderer() fyne.WidgetRenderer {
//
// Implements: fyne.Widget
func (l *Label) MinSize() fyne.Size {
if l.provider == nil {
l.ExtendBaseWidget(l)
cache.Renderer(l.super())
}

return l.provider.MinSize()
l.ExtendBaseWidget(l)
return l.BaseWidget.MinSize()
}

// Refresh triggers a redraw of the label.
Expand Down
1 change: 0 additions & 1 deletion widget/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ func (l *List) FocusLost() {
// MinSize returns the size that this widget should not shrink below.
func (l *List) MinSize() fyne.Size {
l.ExtendBaseWidget(l)

return l.BaseWidget.MinSize()
}

Expand Down
11 changes: 3 additions & 8 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,14 @@ 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
return t.BaseWidget.MinSize()
}

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

for _, s := range t.Segments {
Expand All @@ -123,10 +117,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
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
3 changes: 1 addition & 2 deletions widget/separator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ func (s *Separator) CreateRenderer() fyne.WidgetRenderer {
// Implements: fyne.Widget
func (s *Separator) MinSize() fyne.Size {
s.ExtendBaseWidget(s)
t := s.Theme().Size(theme.SizeNameSeparatorThickness)
return fyne.NewSize(t, t)
return s.BaseWidget.MinSize()
}

var _ fyne.WidgetRenderer = (*separatorRenderer)(nil)
Expand Down

0 comments on commit 7de68d1

Please sign in to comment.