/
json.go
364 lines (318 loc) · 9.07 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package gui
import (
"encoding/json"
"io"
"net/url"
"reflect"
"strings"
"time"
"github.com/fyne-io/defyne/internal/guidefs"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/widget"
)
const jsonKeyObject = "Object"
type canvObj struct {
Type string
Name string
Struct fyne.CanvasObject `json:",omitempty"`
}
type form struct {
Type string
Name string
Struct map[string]interface{} `json:",omitempty"`
}
type formItem struct {
HintText, Text string
Widget *canvObj
}
type cont struct {
canvObj
Layout string `json:",omitempty"`
Name string
Objects []interface{}
Properties map[string]string `json:",omitempty"`
}
// DecodeJSON returns a tree of `CanvasObject` elements from the provided JSON `Reader` and
// updates the metadata map to include any additional information.
func DecodeJSON(r io.Reader) (fyne.CanvasObject, map[fyne.CanvasObject]map[string]string, error) {
guidefs.InitOnce()
var data interface{}
err := json.NewDecoder(r).Decode(&data)
if err != nil || data == nil {
return nil, nil, err
}
meta := make(map[fyne.CanvasObject]map[string]string)
root := data.(map[string]interface{})
obj, err := DecodeMap(root, meta)
return obj, meta, err
}
// DecodeMap returns a tree of `CanvasObject` elements from the provided JSON map and
// updates the metadata map to include any additional information.
func DecodeMap(m map[string]interface{}, meta map[fyne.CanvasObject]map[string]string) (fyne.CanvasObject, error) {
guidefs.InitOnce()
if m["Type"] == "*fyne.Container" {
obj := &fyne.Container{}
name := m["Layout"].(string)
props := map[string]string{"layout": name}
if m["Properties"] != nil {
for k, v := range m["Properties"].(map[string]interface{}) {
props[k] = v.(string)
}
}
if name == "HBox" {
props["dir"] = "horizontal"
} else if name == "VBox" {
props["dir"] = "vertical"
}
if m["Objects"] != nil {
for _, data := range m["Objects"].([]interface{}) {
if data == nil {
// Nil object?
continue
}
child, _ := DecodeMap(data.(map[string]interface{}), meta)
obj.Objects = append(obj.Objects, child)
}
}
obj.Layout = guidefs.Layouts[name].Create(obj, props)
if name, ok := m["Name"]; ok {
props["name"] = name.(string)
}
meta[obj] = props
return obj, nil
}
obj := decodeWidget(m)
obj.Refresh()
props := map[string]string{}
if name, ok := m["Name"]; ok {
props["name"] = name.(string)
}
meta[obj] = props
return obj, nil
}
// EncodeJSON writes a JSON stream for the tree of `CanvasObject` elements provided.
// If an error occurs it will be returned, otherwise nil.
func EncodeJSON(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]string, w io.Writer) error {
guidefs.InitOnce()
if meta == nil {
meta = make(map[fyne.CanvasObject]map[string]string)
}
tree, _ := EncodeMap(obj, meta)
e := json.NewEncoder(w)
e.SetIndent("", " ")
return e.Encode(tree)
}
// EncodeMap returns a JSON map for the tree of `CanvasObject` elements provided, using additional metadata if required.
// If an error occurs it will be returned, otherwise nil.
func EncodeMap(obj fyne.CanvasObject, meta map[fyne.CanvasObject]map[string]string) (interface{}, error) {
guidefs.InitOnce()
props := meta[obj]
name := ""
if props == nil {
props = make(map[string]string)
meta[obj] = props
} else if props["name"] != "" {
name = props["name"]
}
switch c := obj.(type) {
case *widget.Button:
if c.Icon == nil {
return encodeWidget(c, name), nil
}
ic := c.Icon
c.Icon = guidefs.WrapResource(c.Icon)
wid := encodeWidget(c, name)
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
}
ic := c.Resource
c.Resource = guidefs.WrapResource(c.Resource)
wid := encodeWidget(c, name)
go func() { // TODO find a better way to reset this after encoding
time.Sleep(time.Millisecond * 100)
c.Resource = ic
}()
return wid, nil
case *widget.Toolbar:
for id, i := range c.Items {
switch t := i.(type) {
case *widget.ToolbarAction:
ic := t.Icon
t.Icon = guidefs.WrapResource(t.Icon)
go func() { // TODO find a better way to reset this after encoding
time.Sleep(time.Millisecond * 100)
t.Icon = ic
}()
case *widget.ToolbarSeparator:
c.Items[id] = toolbarItem{Type: "Separator"}
case *widget.ToolbarSpacer:
c.Items[id] = toolbarItem{Type: "Spacer"}
}
}
return encodeWidget(c, name), nil
case fyne.Widget:
if form, ok := c.(*widget.Form); ok {
return encodeForm(form, name), nil
}
return encodeWidget(c, name), nil
case *fyne.Container:
var node cont
node.Type = "*fyne.Container"
node.Layout = strings.Split(reflect.TypeOf(c.Layout).String(), ".")[1]
node.Layout = strings.ToTitle(node.Layout[0:1]) + node.Layout[1:]
node.Name = name
p := strings.Index(node.Layout, "Layout")
if p > 0 {
node.Layout = node.Layout[:p]
}
if node.Layout == "Box" {
if props["dir"] == "horizontal" {
node.Layout = "HBox"
} else {
node.Layout = "VBox"
}
}
for _, o := range c.Objects {
enc, _ := EncodeMap(o, meta)
node.Objects = append(node.Objects, enc)
}
node.Properties = meta[c]
return &node, nil
}
return nil, nil
}
func encodeForm(obj *widget.Form, name string) interface{} {
var items []*formItem
for _, o := range obj.Items {
items = append(items,
&formItem{
HintText: o.HintText,
Text: o.Text,
Widget: encodeWidget(o.Widget, ""),
})
}
var node form
node.Type = "*widget.Form"
node.Name = name
node.Struct = map[string]interface{}{
"Hidden": obj.Hidden,
"Items": items,
"SubmitText": obj.SubmitText,
"CancelText": obj.CancelText,
}
return &node
}
func encodeWidget(obj fyne.CanvasObject, name string) *canvObj {
return &canvObj{Type: reflect.TypeOf(obj).String(), Name: name, Struct: obj}
}
func decodeFormItem(m map[string]interface{}) *widget.FormItem {
f := &widget.FormItem{}
if str, ok := m["HintText"]; ok {
f.HintText = str.(string)
}
if str, ok := m["Text"]; ok {
f.Text = str.(string)
}
if wid, ok := m["Widget"]; ok {
f.Widget = decodeWidget(wid.(map[string]interface{}))
}
return f
}
func decodeFromMap(m map[string]interface{}, in interface{}) {
t := reflect.ValueOf(in).Elem()
for k, v := range m {
val := t.FieldByName(k)
if val.Type().Kind() == reflect.Ptr {
continue
}
val.Set(reflect.ValueOf(v))
}
}
func decodeTextStyle(m map[string]interface{}) (s fyne.TextStyle) {
if m["Bold"] == true {
s.Bold = true
}
if m["Italic"] == true {
s.Italic = true
}
if m["Monospace"] == true {
s.Monospace = true
}
if m["TabWidth"] != 0 {
s.TabWidth = int(m["TabWidth"].(float64))
}
return
}
func decodePosition(m map[string]interface{}) fyne.Position {
x := m["X"].(float64)
y := m["Y"].(float64)
return fyne.NewPos(float32(x), float32(y))
}
func decodeToolbarItem(m map[string]interface{}) widget.ToolbarItem {
if v, ok := m["Type"]; ok {
switch v {
case "Separator":
return widget.NewToolbarSeparator()
default:
return widget.NewToolbarSpacer()
}
}
return widget.NewToolbarAction(guidefs.Icons[m["Icon"].(string)], nil)
}
func decodeWidget(m map[string]interface{}) fyne.Widget {
class := m["Type"].(string)
obj := guidefs.Widgets[class].Create().(fyne.Widget)
e := reflect.ValueOf(obj).Elem()
for k, v := range m["Struct"].(map[string]interface{}) {
f := e.FieldByName(k)
typeName := f.Type().String()
switch typeName {
case "fyne.TextAlign", "fyne.TextTruncation", "fyne.TextWrap", "widget.ButtonAlign", "widget.ButtonImportance", "widget.ButtonIconPlacement", "widget.Importance", "widget.ScrollDirection":
f.SetInt(int64(reflect.ValueOf(v).Float()))
case "fyne.TextStyle":
f.Set(reflect.ValueOf(decodeTextStyle(reflect.ValueOf(v).Interface().(map[string]interface{}))))
case "fyne.Position":
f.Set(reflect.ValueOf(decodePosition(reflect.ValueOf(v).Interface().(map[string]interface{}))))
case "fyne.Resource":
res := guidefs.Icons[reflect.ValueOf(v).String()]
if res != nil {
f.Set(reflect.ValueOf(res))
}
case "[]*widget.FormItem":
var items []*widget.FormItem
for _, item := range reflect.ValueOf(v).Interface().([]interface{}) {
items = append(items, decodeFormItem(item.(map[string]interface{})))
}
f.Set(reflect.ValueOf(items))
case "[]widget.ToolbarItem":
var items []widget.ToolbarItem
for _, item := range reflect.ValueOf(v).Interface().([]interface{}) {
items = append(items, decodeToolbarItem(item.(map[string]interface{})))
}
f.Set(reflect.ValueOf(items))
case "fyne.CanvasObject":
fyne.LogError("Unsupported object type: "+class, nil)
case "*url.URL":
u := &url.URL{}
decodeFromMap(reflect.ValueOf(v).Interface().(map[string]interface{}), u)
f.Set(reflect.ValueOf(u))
default:
if strings.Index(typeName, "int") == 0 {
f.SetInt(int64(reflect.ValueOf(v).Float()))
} else if v != nil {
f.Set(reflect.ValueOf(v))
}
}
}
return obj
}
type toolbarItem struct {
Type string
}
func (toolbarItem) ToolbarObject() fyne.CanvasObject { return nil }