Skip to content

Commit

Permalink
Fix #1196: Add svg support for app/window icon (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChandanChainani authored and andydotxyz committed Nov 30, 2021
1 parent 5526d02 commit c1e65aa
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
24 changes: 14 additions & 10 deletions internal/driver/glfw/window.go
Expand Up @@ -13,12 +13,14 @@ import (
"github.com/go-gl/glfw/v3.3/glfw"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/internal"
"fyne.io/fyne/v2/internal/app"
"fyne.io/fyne/v2/internal/cache"
"fyne.io/fyne/v2/internal/driver"
"fyne.io/fyne/v2/internal/driver/common"
"fyne.io/fyne/v2/internal/painter"
"fyne.io/fyne/v2/internal/painter/gl"
)

Expand All @@ -28,6 +30,7 @@ const (
scrollSpeed = float32(10)
doubleClickDelay = 300 // ms (maximum interval between clicks for double click detection)
dragMoveThreshold = 2 // how far can we move before it is a drag
windowIconSize = 256
)

var (
Expand Down Expand Up @@ -246,24 +249,25 @@ func (w *window) SetIcon(icon fyne.Resource) {
return
}

if string(icon.Content()[:4]) == "<svg" {
fyne.LogError("Window icon does not support vector images", nil)
return
}

w.runOnMainWhenCreated(func() {
if w.icon == nil {
w.viewport.SetIcon(nil)
return
}

pix, _, err := image.Decode(bytes.NewReader(w.icon.Content()))
if err != nil {
fyne.LogError("Failed to decode image for window icon", err)
return
var img image.Image
if painter.IsResourceSVG(w.icon) {
img = painter.PaintImage(&canvas.Image{Resource: w.icon}, nil, windowIconSize, windowIconSize)
} else {
pix, _, err := image.Decode(bytes.NewReader(w.icon.Content()))
if err != nil {
fyne.LogError("Failed to decode image for window icon", err)
return
}
img = pix
}

w.viewport.SetIcon([]image.Image{pix})
w.viewport.SetIcon([]image.Image{img})
})
}

Expand Down
5 changes: 3 additions & 2 deletions internal/painter/image.go
Expand Up @@ -55,7 +55,7 @@ func PaintImage(img *canvas.Image, c fyne.Canvas, width, height int) image.Image
if img.Resource != nil {
name = img.Resource.Name()
file = bytes.NewReader(img.Resource.Content())
isSVG = isResourceSVG(img.Resource)
isSVG = IsResourceSVG(img.Resource)
} else {
name = img.File
handle, err := os.Open(img.File)
Expand Down Expand Up @@ -201,7 +201,8 @@ func isFileSVG(path string) bool {
return strings.ToLower(filepath.Ext(path)) == ".svg"
}

func isResourceSVG(res fyne.Resource) bool {
// IsResourceSVG checks if the resource is an SVG or not.
func IsResourceSVG(res fyne.Resource) bool {
if strings.ToLower(filepath.Ext(res.Name())) == ".svg" {
return true
}
Expand Down
8 changes: 4 additions & 4 deletions internal/painter/image_internal_test.go
Expand Up @@ -18,15 +18,15 @@ func TestIsFileSVG(t *testing.T) {
func TestIsResourceSVG(t *testing.T) {
res, err := fyne.LoadResourceFromPath("./testdata/stroke.svg")
assert.Nil(t, err)
assert.True(t, isResourceSVG(res))
assert.True(t, IsResourceSVG(res))

res.(*fyne.StaticResource).StaticName = "stroke"
assert.True(t, isResourceSVG(res))
assert.True(t, IsResourceSVG(res))

svgString := "<svg version=\"1.1\"></svg>"
res.(*fyne.StaticResource).StaticContent = []byte(svgString)
assert.True(t, isResourceSVG(res))
assert.True(t, IsResourceSVG(res))

res.(*fyne.StaticResource).StaticContent = []byte("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + svgString)
assert.True(t, isResourceSVG(res))
assert.True(t, IsResourceSVG(res))
}

0 comments on commit c1e65aa

Please sign in to comment.