forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure that bundled icons are no larger than they need to be
Helps to keep app size down too. Fixes fyne-io#5272
- Loading branch information
1 parent
03a3827
commit 59ed656
Showing
4 changed files
with
72 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |