-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5339 from andydotxyz/fix/5272
- Loading branch information
Showing
5 changed files
with
73 additions
and
2 deletions.
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()) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.