diff --git a/internal/widget/base.go b/internal/widget/base.go index 1ce844c02f..2d17d4bcc6 100644 --- a/internal/widget/base.go +++ b/internal/widget/base.go @@ -76,7 +76,9 @@ func (w *Base) MinSize() fyne.Size { return fyne.NewSize(0, 0) } - return r.MinSize() + minSize := r.MinSize() + w.minCache.Store(minSize) + return minSize } // Visible returns whether or not this widget should be visible. diff --git a/widget/widget.go b/widget/widget.go index 3e43ae644c..639f70bd9f 100644 --- a/widget/widget.go +++ b/widget/widget.go @@ -82,7 +82,9 @@ func (w *BaseWidget) MinSize() fyne.Size { return fyne.Size{} } - return r.MinSize() + minSize := r.MinSize() + w.minCache.Store(minSize) + return minSize } // Visible returns whether or not this widget should be visible. diff --git a/widget/widget_test.go b/widget/widget_test.go index 780ff2ec8e..cb09024bbd 100644 --- a/widget/widget_test.go +++ b/widget/widget_test.go @@ -79,21 +79,49 @@ func TestSimpleRenderer(t *testing.T) { test.AssertImageMatches(t, "simple_renderer.png", window.Canvas().Capture()) } +func TestMinSizeCache(t *testing.T) { + label := NewLabel("a") + + wid := newTestWidget(label) + assert.Equal(t, 0, wid.minSizeCalls) + + minSize := wid.MinSize() + assert.NotEqual(t, 0, minSize) + assert.Equal(t, 1, wid.minSizeCalls) + + wid.MinSize() + assert.Equal(t, 1, wid.minSizeCalls) +} + type testWidget struct { BaseWidget - obj fyne.CanvasObject + obj fyne.CanvasObject + minSizeCalls int } -func newTestWidget(o fyne.CanvasObject) fyne.Widget { +func newTestWidget(o fyne.CanvasObject) *testWidget { t := &testWidget{obj: o} t.ExtendBaseWidget(t) return t } func (t *testWidget) CreateRenderer() fyne.WidgetRenderer { - return NewSimpleRenderer(t.obj) + return &testRenderer{ + WidgetRenderer: NewSimpleRenderer(t.obj), + widget: t, + } } func waitForBinding() { time.Sleep(time.Millisecond * 100) // data resolves on background thread } + +type testRenderer struct { + widget *testWidget + fyne.WidgetRenderer +} + +func (r *testRenderer) MinSize() fyne.Size { + r.widget.minSizeCalls++ + return r.WidgetRenderer.MinSize() +}