Skip to content

Commit

Permalink
Improving safety
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz committed Dec 27, 2024
1 parent 3383de4 commit c19f969
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
3 changes: 3 additions & 0 deletions internal/guibuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ func (b *Builder) buildLibrary() fyne.CanvasObject {
}, func() fyne.CanvasObject {
return widget.NewLabel("")
}, func(i widget.ListItemID, obj fyne.CanvasObject) {
if i >= len(tempNames) {
return
}
obj.(*widget.Label).SetText(guidefs.Lookup(tempNames[i]).Name)
})
list.OnSelected = func(i widget.ListItemID) {
Expand Down
25 changes: 19 additions & 6 deletions pkg/gui/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import (
"errors"
"image/color"
"io"
"log"
"net/url"
"reflect"
"strings"
"time"

"github.com/fyne-io/defyne/internal/guidefs"

"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"github.com/fyne-io/defyne/internal/guidefs"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
Expand Down Expand Up @@ -98,7 +100,9 @@ func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]s
continue
}
child, _ := DecodeMap(data.(map[string]interface{}), meta)
obj.Objects = append(obj.Objects, child)
if child != nil {
obj.Objects = append(obj.Objects, child)
}
}
}
obj.Layout = guidefs.Layouts[name].Create(obj, props)
Expand Down Expand Up @@ -127,7 +131,9 @@ func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]s
}
}
item.Content, _ = DecodeMap(data["Content"].(map[string]interface{}), meta)
obj.Append(item)
if item != nil {
obj.Append(item)
}
}
}

Expand Down Expand Up @@ -205,6 +211,9 @@ func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]s
}

obj := decodeWidget(m)
if obj == nil {
return nil, errors.New("failed to parse object from JSON")
}
obj.Refresh()
props := map[string]string{}
if name, ok := m["Name"]; ok {
Expand Down Expand Up @@ -635,9 +644,13 @@ func decodeFields(e reflect.Value, in map[string]interface{}) error {
return nil
}

func decodeWidget(m map[string]interface{}) fyne.Widget {
class := m["Type"].(string)
obj := guidefs.Lookup(class).Create().(fyne.Widget)
func decodeWidget(m map[string]interface{}) fyne.CanvasObject {
class, ok := m["Type"].(string)
if !ok {
log.Println("Failed to detect type of object")
return nil
}
obj := guidefs.Lookup(class).Create()
e := reflect.ValueOf(obj).Elem()

data, ok := m["Struct"]
Expand Down
2 changes: 1 addition & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (g *gui) makeUI() fyne.CanvasObject {

err = writeFile(dir, "go.mod", `module `+name+`
require fyne.io/fyne/v2 v2.4.0
require fyne.io/fyne/v2 v2.5.3
`)
if err != nil {
fyne.LogError("Failed to write go.mod", err) // we can just return the partial project
Expand Down

0 comments on commit c19f969

Please sign in to comment.