Skip to content

Commit

Permalink
Save an action item for Button widgets (OnTapped)
Browse files Browse the repository at this point in the history
If a widget has an action set then persist that info
  • Loading branch information
andydotxyz committed Sep 14, 2024
1 parent a4c8d35 commit fefb862
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
16 changes: 10 additions & 6 deletions internal/guidefs/widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,25 @@ func initWidgets() {
},
Gostring: func(obj fyne.CanvasObject, props map[fyne.CanvasObject]map[string]string, defs map[string]string) string {
b := obj.(*widget.Button)
action := props[obj]["OnTapped"]
if action == "" {
action = "func() {}"
}
if b.Icon == nil {
if b.Importance == widget.MediumImportance {
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButton(\"%s\", func() {})", escapeLabel(b.Text)))
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButton(\"%s\", %s)", escapeLabel(b.Text), action))
} else {

Check failure on line 115 in internal/guidefs/widget.go

View workflow job for this annotation

GitHub Actions / checks

if block ends with a return statement, so drop this else and outdent its block
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, OnTapped: func() {}}",
escapeLabel(b.Text), b.Importance))
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, action))
}
}

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))
return widgetRef(props[obj], defs, fmt.Sprintf("widget.NewButtonWithIcon(\"%s\", %s, %s)", escapeLabel(b.Text), icon, action))
} else {

Check failure on line 124 in internal/guidefs/widget.go

View workflow job for this annotation

GitHub Actions / checks

if block ends with a return statement, so drop this else and outdent its block
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Icon: %s, OnTapped: func() {}}",
escapeLabel(b.Text), b.Importance, icon))
return widgetRef(props[obj], defs, fmt.Sprintf("&widget.Button{Text: \"%s\", Importance: %d, Icon: %s, OnTapped: %s}",
escapeLabel(b.Text), b.Importance, icon, action))
}
},
Packages: func(obj fyne.CanvasObject) []string {
Expand Down
52 changes: 37 additions & 15 deletions pkg/gui/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (
const jsonKeyObject = "Object"

type canvObj struct {
Type string
Name string
Struct fyne.CanvasObject `json:",omitempty"`
Type string
Name string `json:",omitempty"`
Actions map[string]string `json:",omitempty"`
Struct fyne.CanvasObject `json:",omitempty"`
}

type cntObj struct {
Expand All @@ -31,7 +32,7 @@ type cntObj struct {

type form struct {
Type string
Name string
Name string `json:",omitempty"`
Struct map[string]interface{} `json:",omitempty"`
}

Expand All @@ -43,7 +44,7 @@ type formItem struct {
type cont struct {
canvObj
Layout string `json:",omitempty"`
Name string
Name string `json:",omitempty"`
Objects []interface{}
Properties map[string]string `json:",omitempty"`
}
Expand Down Expand Up @@ -157,6 +158,14 @@ func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]s
props["name"] = name.(string)
}

if set, ok := m["Actions"]; ok {
if actions, ok := set.(map[string]any); ok {
for k, v := range actions {
props[k] = v.(string)
}
}
}

meta[obj] = props
return obj, nil
}
Expand All @@ -183,35 +192,42 @@ func EncodeMap(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]stri

props := meta[obj]
name := ""
actions := map[string]string{}
if props == nil {
props = make(map[string]string)
meta[obj] = props
} else if props["name"] != "" {
} else {
name = props["name"]

for k, v := range props {
if len(k) > 2 && k[0:2] == "On" {
actions[k] = v
}
}
}

switch c := obj.(type) {
case *widget.Button:
if c.Icon == nil {
return encodeWidget(c, name), nil
return encodeWidget(c, name, actions), nil
}

ic := c.Icon
c.Icon = guidefs.WrapResource(c.Icon)
wid := encodeWidget(c, name)
wid := encodeWidget(c, name, actions)
go func() { // TODO find a better way to reset this after encoding
time.Sleep(time.Millisecond * 100)
c.Icon = ic
}()
return wid, nil
case *widget.Icon:
if c.Resource == nil {
return encodeWidget(c, name), nil
return encodeWidget(c, name, actions), nil
}

ic := c.Resource
c.Resource = guidefs.WrapResource(c.Resource)
wid := encodeWidget(c, name)
wid := encodeWidget(c, name, actions)
go func() { // TODO find a better way to reset this after encoding
time.Sleep(time.Millisecond * 100)
c.Resource = ic
Expand All @@ -234,7 +250,7 @@ func EncodeMap(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]stri
}
}

return encodeWidget(c, name), nil
return encodeWidget(c, name, actions), nil
case *container.Scroll:
node := &cntObj{Struct: make(map[string]interface{})}
node.Type = "*container.Scroll"
Expand All @@ -259,7 +275,7 @@ func EncodeMap(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]stri
if form, ok := c.(*widget.Form); ok {
return encodeForm(form, name), nil
}
return encodeWidget(c, name), nil
return encodeWidget(c, name, actions), nil
case *fyne.Container:
var node cont
node.Type = "*fyne.Container"
Expand Down Expand Up @@ -295,7 +311,7 @@ func encodeForm(obj *widget.Form, name string) interface{} {
&formItem{
HintText: o.HintText,
Text: o.Text,
Widget: encodeWidget(o.Widget, ""),
Widget: encodeWidget(o.Widget, "", nil),
})
}

Expand All @@ -312,8 +328,14 @@ func encodeForm(obj *widget.Form, name string) interface{} {
return &node
}

func encodeWidget(obj fyne.CanvasObject, name string) *canvObj {
return &canvObj{Type: reflect.TypeOf(obj).String(), Name: name, Struct: obj}
func encodeWidget(obj fyne.CanvasObject, name string, actions map[string]string) *canvObj {
w := &canvObj{Type: reflect.TypeOf(obj).String(), Name: name, Struct: obj}

if len(actions) > 0 {
w.Actions = actions
}

return w
}

func decodeFormItem(m map[string]interface{}) *widget.FormItem {
Expand Down

0 comments on commit fefb862

Please sign in to comment.