Skip to content

Commit

Permalink
Fix looking up icon names for wrapped resources
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Mar 7, 2024
1 parent 43f2dbe commit 01c7abd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
7 changes: 3 additions & 4 deletions internal/guidefs/icon.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package guidefs

import (
"fmt"
"sort"

"fyne.io/fyne/v2"
Expand All @@ -12,7 +11,7 @@ var (
// IconNames is an array with the list of names of all the Icons
IconNames []string

// iconsReverse Contains the key value pair where the key is the address of the icon and the value is the Name
// IconReverse Contains the key value pair where the key is the address of the icon and the value is the Name
IconReverse map[string]string

// Icons Has the hashmap of Icons from the standard theme.
Expand Down Expand Up @@ -150,8 +149,8 @@ func extractIconNames() []string {
func reverseIconMap() map[string]string {
var iconReverseFromData = make(map[string]string, len(Icons))
for k, v := range Icons {
s := fmt.Sprintf("%p", v)
iconReverseFromData[s] = k
iconReverseFromData[v.Name()] = k
}

return iconReverseFromData
}
20 changes: 17 additions & 3 deletions internal/guidefs/res.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package guidefs

import (
"fmt"

"fyne.io/fyne/v2"
)

Expand All @@ -11,10 +9,26 @@ type jsonResource struct {
}

func (r *jsonResource) MarshalJSON() ([]byte, error) {
icon := "\"" + IconReverse[fmt.Sprintf("%p", r.Resource)] + "\""
icon := "\"" + IconName(r.Resource) + "\""

return []byte(icon), nil
}

func WrapResource(r fyne.Resource) fyne.Resource {
return &jsonResource{r}
}

func IconName(res fyne.Resource) string {
name := res.Name()
// strip prefix numbers to unwrap
for name[0] >= '0' && name[0] <= '9' {
name = name[1:]
}

ret, ok := IconReverse[name]
if !ok {
return "BrokenImageIcon"
}

return ret
}
6 changes: 3 additions & 3 deletions internal/guidefs/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func initWidgets() {
b.SetIcon(Icons[selected])
})
if b.Icon != nil {
name := IconReverse[fmt.Sprintf("%p", b.Icon)]
name := IconName(b.Icon)
for _, n := range IconNames {
if n == name {
icon.SetSelected(n)
Expand Down Expand Up @@ -88,7 +88,7 @@ func initWidgets() {
}
}

icon := "theme." + IconReverse[fmt.Sprintf("%p", b.Icon)] + "()"
icon := "theme." + IconName(b.Icon) + "()"
if b.Importance == widget.MediumImportance {
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButtonWithIcon(\"%s\", %s, func() {})", escapeLabel(b.Text), icon))
} else {
Expand Down Expand Up @@ -208,7 +208,7 @@ func initWidgets() {
Gostring: func(obj fyne.CanvasObject, props map[fyne.CanvasObject]map[string]string, defs map[string]string) string {
i := obj.(*widget.Icon)

res := "theme." + IconReverse[fmt.Sprintf("%p", i.Resource)] + "()"
res := "theme." + IconName(i.Resource) + "()"
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewIcon(%s)", res))
},
Packages: func(obj fyne.CanvasObject) []string {
Expand Down

0 comments on commit 01c7abd

Please sign in to comment.