Skip to content

Commit dc6ccce

Browse files
authoredApr 21, 2025
Correctly reset an image when resource goes to nil (#5669)
1 parent 3f2a886 commit dc6ccce

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

‎canvas/image.go

+8
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type Image struct {
7070
Translucency float64 // Set a translucency value > 0.0 to fade the image
7171
FillMode ImageFill // Specify how the image should expand to fill or fit the available space
7272
ScaleMode ImageScale // Specify the type of scaling interpolation applied to the image
73+
74+
previousRender bool // did we successfully draw before? if so a nil content will need a reset
7375
}
7476

7577
// Alpha is a convenience function that returns the alpha value for an image
@@ -131,6 +133,11 @@ func (i *Image) Refresh() {
131133
return
132134
}
133135
rc = io.NopCloser(r)
136+
} else if i.previousRender {
137+
i.previousRender = false
138+
139+
Refresh(i)
140+
return
134141
} else {
135142
return
136143
}
@@ -165,6 +172,7 @@ func (i *Image) Refresh() {
165172
}
166173
}
167174

175+
i.previousRender = true
168176
Refresh(i)
169177
}
170178

‎canvas/image_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"strings"
1010
"testing"
1111

12+
"fyne.io/fyne/v2"
1213
"fyne.io/fyne/v2/canvas"
1314
"fyne.io/fyne/v2/storage"
1415
_ "fyne.io/fyne/v2/test"
16+
"fyne.io/fyne/v2/theme"
1517

1618
"github.com/stretchr/testify/assert"
1719
)
@@ -28,6 +30,22 @@ func TestImage_TranslucencyDefault(t *testing.T) {
2830
assert.Equal(t, 0.0, img.Translucency)
2931
}
3032

33+
func TestImage_RefreshBlank(t *testing.T) {
34+
img := &canvas.Image{}
35+
img.Resize(fyne.NewSize(64, 64))
36+
img.Refresh()
37+
assert.Nil(t, img.Image)
38+
39+
img.Resource = theme.HomeIcon()
40+
img.Refresh()
41+
assert.NotNil(t, img.Image)
42+
43+
img.Image = nil
44+
img.Resource = nil
45+
img.Refresh()
46+
assert.Nil(t, img.Image)
47+
}
48+
3149
func TestNewImageFromFile(t *testing.T) {
3250
pwd, _ := os.Getwd()
3351
path := filepath.Join(filepath.Dir(pwd), "theme", "icons", "fyne.png")

0 commit comments

Comments
 (0)