Skip to content

Commit

Permalink
change signature of svg.Colorize
Browse files Browse the repository at this point in the history
  • Loading branch information
dweymouth committed Jan 3, 2025
1 parent 6de4ad9 commit cd91b99
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
2 changes: 1 addition & 1 deletion canvas/canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Refresh(obj fyne.CanvasObject) {
//
// Since: 2.6
func RecolorSVG(resource fyne.Resource, color color.Color) ([]byte, error) {
return svg.ColorizeError(resource.Content(), color)
return svg.Colorize(resource.Content(), color)
}

// repaint instructs the containing canvas to redraw, even if nothing changed.
Expand Down
6 changes: 5 additions & 1 deletion canvas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,11 @@ func (i *Image) updateReader() (io.ReadCloser, error) {
th := cache.WidgetTheme(i)
if th != nil {
col := th.Color(res.ThemeColorName(), fyne.CurrentApp().Settings().ThemeVariant())
content = svg.Colorize(content, col)
var err error
content, err = svg.Colorize(content, col)
if err != nil {
fyne.LogError("", err)
}
}
}
return io.NopCloser(bytes.NewReader(content)), nil
Expand Down
6 changes: 5 additions & 1 deletion internal/driver/glfw/menu_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,13 @@ func insertNativeMenuItem(nsMenu unsafe.Pointer, item *fyne.MenuItem, nextItemID
if _, isThemed := rsc.(*theme.ThemedResource); isThemed {
var r, g, b, a C.int
C.getTextColorRGBA(&r, &g, &b, &a)
content, err := svg.Colorize(rsc.Content(), color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)})
if err != nil {
fyne.LogError("", err)
}
rsc = &fyne.StaticResource{
StaticName: rsc.Name(),
StaticContent: svg.Colorize(rsc.Content(), color.NRGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: uint8(a)}),
StaticContent: content,
}
}
size := int(C.menuFontSize())
Expand Down
11 changes: 1 addition & 10 deletions internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@ import (
)

// Colorize creates a new SVG from a given one by replacing all fill colors by the given color.
func Colorize(src []byte, clr color.Color) []byte {
content, err := ColorizeError(src, clr)
if err != nil {
fyne.LogError("", err)
}
return content
}

// ColorizeError is the same as Colorize, except returning instead of logging any error.
func ColorizeError(src []byte, clr color.Color) ([]byte, error) {
func Colorize(src []byte, clr color.Color) ([]byte, error) {
rdr := bytes.NewReader(src)
s, err := svgFromXML(rdr)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func TestColorize(t *testing.T) {
t.Run(name, func(t *testing.T) {
bytes, err := os.ReadFile(filepath.Join("testdata", tt.svgFile))
require.NoError(t, err)
got := helperDrawSVG(t, Colorize(bytes, tt.color))
content, _ := Colorize(bytes, tt.color)
got := helperDrawSVG(t, content)
test.AssertImageMatches(t, tt.wantImage, got)
})
}
Expand Down
20 changes: 15 additions & 5 deletions theme/icons.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package theme

import (
"image/color"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/internal/svg"
)
Expand Down Expand Up @@ -712,7 +714,7 @@ func (res *ThemedResource) ThemeColorName() fyne.ThemeColorName {

// Content returns the underlying content of the resource adapted to the current text color.
func (res *ThemedResource) Content() []byte {
return svg.Colorize(unwrapResource(res.source).Content(), Color(res.ThemeColorName()))
return colorizeLogError(unwrapResource(res.source).Content(), Color(res.ThemeColorName()))
}

// Error returns a different resource for indicating an error.
Expand Down Expand Up @@ -742,7 +744,7 @@ func (res *InvertedThemedResource) Name() string {
// Content returns the underlying content of the resource adapted to the current background color.
func (res *InvertedThemedResource) Content() []byte {
clr := Color(ColorNameBackground)
return svg.Colorize(unwrapResource(res.source).Content(), clr)
return colorizeLogError(unwrapResource(res.source).Content(), clr)
}

// ThemeColorName returns the fyne.ThemeColorName that is used as foreground color.
Expand Down Expand Up @@ -775,7 +777,7 @@ func (res *ErrorThemedResource) Name() string {

// Content returns the underlying content of the resource adapted to the current background color.
func (res *ErrorThemedResource) Content() []byte {
return svg.Colorize(unwrapResource(res.source).Content(), Color(ColorNameError))
return colorizeLogError(unwrapResource(res.source).Content(), Color(ColorNameError))
}

// Original returns the underlying resource that this error themed resource was adapted from
Expand Down Expand Up @@ -810,7 +812,7 @@ func (res *PrimaryThemedResource) Name() string {

// Content returns the underlying content of the resource adapted to the current background color.
func (res *PrimaryThemedResource) Content() []byte {
return svg.Colorize(unwrapResource(res.source).Content(), Color(ColorNamePrimary))
return colorizeLogError(unwrapResource(res.source).Content(), Color(ColorNamePrimary))
}

// Original returns the underlying resource that this primary themed resource was adapted from
Expand Down Expand Up @@ -839,7 +841,7 @@ func (res *DisabledResource) Name() string {

// Content returns the disabled style content of the correct resource for the current theme
func (res *DisabledResource) Content() []byte {
return svg.Colorize(unwrapResource(res.source).Content(), Color(ColorNameDisabled))
return colorizeLogError(unwrapResource(res.source).Content(), Color(ColorNameDisabled))
}

// ThemeColorName returns the fyne.ThemeColorName that is used as foreground color.
Expand Down Expand Up @@ -1399,3 +1401,11 @@ func unwrapResource(res fyne.Resource) fyne.Resource {
}
}
}

func colorizeLogError(src []byte, clr color.Color) []byte {
content, err := svg.Colorize(src, clr)
if err != nil {
fyne.LogError("", err)
}
return content
}

0 comments on commit cd91b99

Please sign in to comment.