Skip to content

Commit

Permalink
Merge pull request #5339 from andydotxyz/fix/5272
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz authored Dec 25, 2024
2 parents 03a3827 + 6562358 commit da388dd
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
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
4 changes: 3 additions & 1 deletion cmd/fyne/internal/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,12 @@ func createMetadataInitFile(srcdir string, app *appData) (func(), error) {
if app.icon != "" {
res, err := fyne.LoadResourceFromPath(app.icon)
if err != nil {
fyne.LogError("Unable to load medadata icon file "+app.icon, err)
fyne.LogError("Unable to load metadata icon file "+app.icon, err)
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())
}
Binary file added internal/metadata/testdata/fyne.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit da388dd

Please sign in to comment.