/
mapview.go
326 lines (286 loc) · 8.43 KB
/
mapview.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
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package giv
import (
"reflect"
"cogentcore.org/core/events"
"cogentcore.org/core/gi"
"cogentcore.org/core/gti"
"cogentcore.org/core/icons"
"cogentcore.org/core/ki"
"cogentcore.org/core/laser"
"cogentcore.org/core/styles"
)
// MapView represents a map, creating a property editor of the values --
// constructs Children widgets to show the key / value pairs, within an
// overall frame.
type MapView struct {
gi.Frame
// the map that we are a view onto
Map any `set:"-"`
// Value for the map itself, if this was created within value view framework; otherwise nil
MapValView Value
// has the map been edited?
Changed bool `set:"-"`
// Value representations of the map keys
Keys []Value `json:"-" xml:"-" set:"-"`
// Value representations of the map values
Values []Value `json:"-" xml:"-" set:"-"`
// sort by values instead of keys
SortVals bool
// the number of columns in the map; do not set externally;
// generally only access internally
NCols int `set:"-"`
// a record of parent View names that have led up to this view.
// Displayed as extra contextual information in view dialog windows.
ViewPath string
}
func (mv *MapView) OnInit() {
mv.Frame.OnInit()
mv.SetStyles()
}
func (mv *MapView) SetStyles() {
mv.Style(func(s *styles.Style) {
s.Direction = styles.Column
s.Grow.Set(1, 1)
})
mv.OnWidgetAdded(func(w gi.Widget) {
switch w.PathFrom(mv) {
case "map-grid":
w.Style(func(s *styles.Style) {
s.Display = styles.Grid
s.Columns = mv.NCols
s.Overflow.Set(styles.OverflowAuto)
s.Grow.Set(1, 1)
s.Min.X.Em(20)
s.Min.Y.Em(10)
})
}
// if w.Parent().Name() == "map-grid" {
// }
})
}
// SetMap sets the source map that we are viewing.
// Rebuilds the children to represent this map
func (mv *MapView) SetMap(mp any) *MapView {
// note: because we make new maps, and due to the strangeness of reflect, they
// end up not being comparable types, so we can't check if equal
mv.Map = mp
mv.Update()
return mv
}
// UpdateValues updates the widget display of slice values, assuming same slice config
func (mv *MapView) UpdateValues() {
// maps have to re-read their values -- can't get pointers
mv.Update()
}
// Config configures the view
func (mv *MapView) Config() {
if !mv.HasChildren() {
gi.NewFrame(mv, "map-grid")
}
mv.ConfigMapGrid()
}
// IsConfiged returns true if the widget is fully configured
func (mv *MapView) IsConfiged() bool {
return len(mv.Kids) != 0
}
// MapGrid returns the MapGrid grid layout widget, which contains all the fields and values
func (mv *MapView) MapGrid() *gi.Frame {
return mv.ChildByName("map-grid", 0).(*gi.Frame)
}
// KiPropTag returns the PropTag value from Ki owner of this map, if it is..
func (mv *MapView) KiPropTag() string {
if mv.MapValView == nil {
return ""
}
vd := mv.MapValView.AsValueData()
if vd.Owner == nil {
return ""
}
if ownki, ok := vd.Owner.(ki.Ki); ok {
pt := ownki.PropTag()
return pt
}
return ""
}
func (mv *MapView) ContextMenu(m *gi.Scene, keyv reflect.Value) {
if mv.IsReadOnly() {
return
}
gi.NewButton(m).SetText("Add").SetIcon(icons.Add).OnClick(func(e events.Event) {
mv.MapAdd()
})
gi.NewButton(m).SetText("Delete").SetIcon(icons.Delete).OnClick(func(e events.Event) {
mv.MapDelete(keyv)
})
}
// ConfigMapGrid configures the MapGrid for the current map
func (mv *MapView) ConfigMapGrid() {
if laser.AnyIsNil(mv.Map) {
return
}
sg := mv.MapGrid()
config := ki.Config{}
// always start fresh!
mv.Keys = make([]Value, 0)
mv.Values = make([]Value, 0)
sg.DeleteChildren()
mpv := reflect.ValueOf(mv.Map)
mpvnp := laser.NonPtrValue(mpv)
valtyp := laser.NonPtrType(reflect.TypeOf(mv.Map)).Elem()
ncol := 2
ifaceType := false
typeTag := ""
strtyp := reflect.TypeOf(typeTag)
if valtyp.Kind() == reflect.Interface && valtyp.String() == "interface {}" {
ifaceType = true
ncol = 3
typeTag = mv.KiPropTag()
// todo: need some way of setting & getting
// this for given domain mapview could have a structview parent and
// the source node of that struct, if a Ki, could have a property --
// unlike inline case, plain mapview is not a child of struct view
// directly -- but field on struct view does create the mapview
// dialog.. a bit hacky and indirect..
}
// valtypes := append(kit.Types.AllTagged(typeTag), kit.Enums.AllTagged(typeTag)...)
// valtypes = append(valtypes, kit.Types.AllTagged("basic-type")...)
// valtypes = append(valtypes, kit.TypeFor[reflect.Type]())
valtypes := gti.AllEmbeddersOf(ki.NodeType) // todo: this is not right
mv.NCols = ncol
keys := laser.MapSort(mv.Map, !mv.SortVals, true) // note: this is a slice of reflect.Value!
for _, key := range keys {
kv := ToValue(key.Interface(), "")
if kv == nil { // shouldn't happen
continue
}
kv.SetMapKey(key, mv.Map)
val := laser.OnePtrUnderlyingValue(mpvnp.MapIndex(key))
vv := ToValue(val.Interface(), "")
if vv == nil { // shouldn't happen
continue
}
vv.SetMapValue(val, mv.Map, key.Interface(), kv, mv.ViewPath) // needs key value value to track updates
keytxt := laser.ToString(key.Interface())
keynm := "key-" + keytxt
valnm := "value-" + keytxt
config.Add(kv.WidgetType(), keynm)
config.Add(vv.WidgetType(), valnm)
if ifaceType {
typnm := "type-" + keytxt
config.Add(gi.ChooserType, typnm)
}
mv.Keys = append(mv.Keys, kv)
mv.Values = append(mv.Values, vv)
}
if sg.ConfigChildren(config) {
sg.NeedsLayout()
}
for i, vv := range mv.Values {
kv := mv.Keys[i]
vv.OnChange(func(e events.Event) { mv.SendChange(e) })
kv.OnChange(func(e events.Event) {
mv.SendChange(e)
mv.Update()
})
keyw := sg.Child(i * ncol).(gi.Widget)
w := sg.Child(i*ncol + 1).(gi.Widget)
Config(kv, keyw)
Config(vv, w)
vv.AsWidgetBase().OnInput(mv.HandleEvent)
kv.AsWidgetBase().OnInput(mv.HandleEvent)
w.Style(func(s *styles.Style) {
s.SetTextWrap(false)
})
keyw.Style(func(s *styles.Style) {
s.SetTextWrap(false)
})
w.AddContextMenu(func(m *gi.Scene) {
mv.ContextMenu(m, kv.Val())
})
keyw.AddContextMenu(func(m *gi.Scene) {
mv.ContextMenu(m, kv.Val())
})
if ifaceType {
typw := sg.Child(i*ncol + 2).(*gi.Chooser)
typw.SetTypes(valtypes...)
vtyp := laser.NonPtrType(reflect.TypeOf(vv.Val().Interface()))
if vtyp == nil {
vtyp = strtyp // default to string
}
typw.SetCurrentValue(vtyp)
}
}
}
// SetChanged sets the Changed flag and emits the ViewSig signal for the
// MapView, indicating that some kind of edit / change has taken place to
// the table data.
func (mv *MapView) SetChanged() {
mv.Changed = true
mv.SendChange()
}
// MapChangeValueType changes the type of the value for given map element at
// idx -- for maps with any values
func (mv *MapView) MapChangeValueType(idx int, typ reflect.Type) {
if laser.AnyIsNil(mv.Map) {
return
}
keyv := mv.Keys[idx]
ck := laser.NonPtrValue(keyv.Val()) // current key value
valv := mv.Values[idx]
cv := laser.NonPtrValue(valv.Val()) // current val value
// create a new item of selected type, and attempt to convert existing to it
var evn reflect.Value
if cv.IsZero() {
evn = laser.MakeOfType(typ)
} else {
evn = laser.CloneToType(typ, cv.Interface())
}
ov := laser.NonPtrValue(reflect.ValueOf(mv.Map))
valv.AsValueData().Value = evn.Elem()
ov.SetMapIndex(ck, evn.Elem())
mv.ConfigMapGrid()
mv.SetChanged()
mv.NeedsRender()
}
// ToggleSort toggles sorting by values vs. keys
func (mv *MapView) ToggleSort() {
mv.SortVals = !mv.SortVals
mv.ConfigMapGrid()
}
// MapAdd adds a new entry to the map
func (mv *MapView) MapAdd() {
if laser.AnyIsNil(mv.Map) {
return
}
laser.MapAdd(mv.Map)
mv.SetChanged()
mv.Update()
}
// MapDelete deletes a key-value from the map
func (mv *MapView) MapDelete(key reflect.Value) {
if laser.AnyIsNil(mv.Map) {
return
}
laser.MapDeleteValue(mv.Map, laser.NonPtrValue(key))
mv.SetChanged()
mv.Update()
}
// ConfigToolbar configures a [gi.Toolbar] for this view
func (mv *MapView) ConfigToolbar(tb *gi.Toolbar) {
if laser.AnyIsNil(mv.Map) {
return
}
gi.NewButton(tb, "sort").SetText("Sort").SetIcon(icons.Sort).SetTooltip("Switch between sorting by the keys vs. the values").
OnClick(func(e events.Event) {
mv.ToggleSort()
})
if !mv.IsReadOnly() {
gi.NewButton(tb, "add").SetText("Add").SetIcon(icons.Add).SetTooltip("Add a new element to the map").
OnClick(func(e events.Event) {
mv.MapAdd()
})
}
}