From 01c7abdcfd4d6962e7d230d41eaf5ff2c2e95c3b Mon Sep 17 00:00:00 2001 From: Andy Williams Date: Thu, 7 Mar 2024 09:23:56 +0000 Subject: [PATCH] Fix looking up icon names for wrapped resources --- internal/guidefs/icon.go | 7 +++---- internal/guidefs/res.go | 20 +++++++++++++++++--- internal/guidefs/widget.go | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/guidefs/icon.go b/internal/guidefs/icon.go index 40d999f..e75cde1 100644 --- a/internal/guidefs/icon.go +++ b/internal/guidefs/icon.go @@ -1,7 +1,6 @@ package guidefs import ( - "fmt" "sort" "fyne.io/fyne/v2" @@ -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. @@ -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 } diff --git a/internal/guidefs/res.go b/internal/guidefs/res.go index ee6f69a..f3765ae 100644 --- a/internal/guidefs/res.go +++ b/internal/guidefs/res.go @@ -1,8 +1,6 @@ package guidefs import ( - "fmt" - "fyne.io/fyne/v2" ) @@ -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 +} diff --git a/internal/guidefs/widget.go b/internal/guidefs/widget.go index 515d42d..4548849 100644 --- a/internal/guidefs/widget.go +++ b/internal/guidefs/widget.go @@ -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) @@ -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 { @@ -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 {