Skip to content

Commit

Permalink
Make sure that bundled icons are no larger than they need to be
Browse files Browse the repository at this point in the history
Helps to keep app size down too.
Fixes fyne-io#5272
  • Loading branch information
andydotxyz committed Dec 24, 2024
1 parent 03a3827 commit 59ed656
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/meta_development.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func checkLocalMetadata() {
if data.Details.Icon != "" {
res, err := fyne.LoadResourceFromPath(data.Details.Icon)
if err == nil {
meta.Icon = res
meta.Icon = metadata.ScaleIcon(res, 512)
}
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/fyne/internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ func createMetadataInitFile(srcdir string, app *appData) (func(), error) {
return func() { os.Remove(metadataInitFilePath) }, err
}

res = metadata.ScaleIcon(res, 512)

// The return type of fyne.LoadResourceFromPath is always a *fyne.StaticResource.
app.ResGoString = res.(*fyne.StaticResource).GoString()
}
Expand Down
41 changes: 41 additions & 0 deletions internal/metadata/icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package metadata

import (
"bytes"
"image/png"
"strconv"

"github.com/nfnt/resize"

"fyne.io/fyne/v2"
)

func ScaleIcon(data fyne.Resource, size int) fyne.Resource {
img, err := png.Decode(bytes.NewReader(data.Content()))
if err != nil {
fyne.LogError("Failed to decode app icon", err)
return data
}

if img.Bounds().Dx() <= size {
return data
}

sized := resize.Resize(uint(size), uint(size), img, resize.Lanczos3)
smallData := &bytes.Buffer{}
err = png.Encode(smallData, sized)
if err != nil {
fyne.LogError("Failed to encode smaller app icon", err)
return data
}

name := data.Name()
nameLen := len(name)
suffix := "-" + strconv.Itoa(size) + ".png"
if nameLen <= 4 || name[nameLen-4] != '.' {
name = "appicon" + suffix
} else {
name = name[:nameLen-4] + suffix
}
return fyne.NewStaticResource(name, smallData.Bytes())
}
28 changes: 28 additions & 0 deletions internal/metadata/icon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package metadata

import (
"bytes"
"image/png"
"testing"

"github.com/stretchr/testify/assert"

"fyne.io/fyne/v2"
)

func TestScaleIcon(t *testing.T) {
data, err := fyne.LoadResourceFromPath("./testdata/fyne.png")
assert.Nil(t, err)

assert.Equal(t, "fyne.png", data.Name())
img, err := png.Decode(bytes.NewReader(data.Content()))
assert.Nil(t, err)
assert.Equal(t, 512, img.Bounds().Dx())

smallData := ScaleIcon(data, 256)

assert.Equal(t, "fyne-256.png", smallData.Name())
img, err = png.Decode(bytes.NewReader(smallData.Content()))
assert.Nil(t, err)
assert.Equal(t, 256, img.Bounds().Dx())
}

0 comments on commit 59ed656

Please sign in to comment.