-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliceviewinline.go
221 lines (201 loc) · 6.91 KB
/
sliceviewinline.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
// Copyright (c) 2018, The GoKi Authors. 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 (
"fmt"
"reflect"
"github.com/goki/gi/gi"
"github.com/goki/gi/units"
"github.com/goki/ki/ints"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// SliceViewInline represents a slice as a single line widget, for smaller
// slices and those explicitly marked inline -- constructs widgets in Parts to
// show the key names and editor vals for each value.
type SliceViewInline struct {
gi.PartsWidgetBase
Slice interface{} `desc:"the slice that we are a view onto"`
SliceValView ValueView `desc:"ValueView for the slice itself, if this was created within value view framework -- otherwise nil"`
IsArray bool `desc:"whether the slice is actually an array -- no modifications"`
IsFixedLen bool `desc:"whether the slice has a fixed-len flag on it"`
Changed bool `desc:"has the slice been edited?"`
Values []ValueView `json:"-" xml:"-" desc:"ValueView representations of the fields"`
TmpSave ValueView `json:"-" xml:"-" desc:"value view that needs to have SaveTmp called on it whenever a change is made to one of the underlying values -- pass this down to any sub-views created from a parent"`
ViewSig ki.Signal `json:"-" xml:"-" desc:"signal for valueview -- only one signal sent when a value has been set -- all related value views interconnect with each other to update when others update"`
ViewPath string `desc:"a record of parent View names that have led up to this view -- displayed as extra contextual information in view dialog windows"`
}
var KiT_SliceViewInline = kit.Types.AddType(&SliceViewInline{}, SliceViewInlineProps)
func (sv *SliceViewInline) Disconnect() {
sv.PartsWidgetBase.Disconnect()
sv.ViewSig.DisconnectAll()
}
// SetSlice sets the source slice that we are viewing -- rebuilds the children to represent this slice
func (sv *SliceViewInline) SetSlice(sl interface{}) {
updt := false
if sv.Slice != sl {
updt = sv.UpdateStart()
sv.Slice = sl
sv.IsArray = kit.NonPtrType(reflect.TypeOf(sl)).Kind() == reflect.Array
sv.IsFixedLen = false
if sv.SliceValView != nil {
_, sv.IsFixedLen = sv.SliceValView.Tag("fixed-len")
}
sv.SetFullReRender()
}
sv.UpdateFromSlice()
sv.UpdateEnd(updt)
}
var SliceViewInlineProps = ki.Props{
"EnumType:Flag": gi.KiT_NodeFlags,
"min-width": units.NewCh(20),
}
// ConfigParts configures Parts for the current slice
func (sv *SliceViewInline) ConfigParts() {
if kit.IfaceIsNil(sv.Slice) {
return
}
sv.Parts.Lay = gi.LayoutHoriz
sv.Parts.SetProp("overflow", gi.OverflowHidden) // no scrollbars!
config := kit.TypeAndNameList{}
// always start fresh!
sv.Values = make([]ValueView, 0)
mv := reflect.ValueOf(sv.Slice)
mvnp := kit.NonPtrValue(mv)
sz := ints.MinInt(mvnp.Len(), SliceInlineLen)
for i := 0; i < sz; i++ {
val := kit.OnePtrUnderlyingValue(mvnp.Index(i)) // deal with pointer lists
vv := ToValueView(val.Interface(), "")
if vv == nil { // shouldn't happen
fmt.Printf("nil value view!\n")
continue
}
vv.SetSliceValue(val, sv.Slice, i, sv.TmpSave, sv.ViewPath)
vtyp := vv.WidgetType()
idxtxt := fmt.Sprintf("%05d", i)
valnm := fmt.Sprintf("value-%v", idxtxt)
config.Add(vtyp, valnm)
sv.Values = append(sv.Values, vv)
}
if !sv.IsArray && !sv.IsFixedLen {
config.Add(gi.KiT_Action, "add-action")
}
config.Add(gi.KiT_Action, "edit-action")
mods, updt := sv.Parts.ConfigChildren(config, ki.UniqueNames)
if !mods {
updt = sv.Parts.UpdateStart()
}
for i, vv := range sv.Values {
vvb := vv.AsValueViewBase()
vvb.ViewSig.ConnectOnly(sv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svv, _ := recv.Embed(KiT_SliceViewInline).(*SliceViewInline)
svv.SetChanged()
})
widg := sv.Parts.Child(i).(gi.Node2D)
if sv.SliceValView != nil {
vv.SetTags(sv.SliceValView.AllTags())
}
vv.ConfigWidget(widg)
if sv.IsInactive() {
widg.AsNode2D().SetInactive()
}
}
if !sv.IsArray && !sv.IsFixedLen {
adack, err := sv.Parts.Children().ElemFromEndTry(1)
if err == nil {
adac := adack.(*gi.Action)
adac.SetIcon("plus")
adac.Tooltip = "add an element to the slice"
adac.ActionSig.ConnectOnly(sv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svv, _ := recv.Embed(KiT_SliceViewInline).(*SliceViewInline)
svv.SliceNewAt(-1, true)
})
}
}
edack, err := sv.Parts.Children().ElemFromEndTry(0)
if err == nil {
edac := edack.(*gi.Action)
edac.SetIcon("edit")
edac.Tooltip = "edit slice in a dialog window"
edac.ActionSig.ConnectOnly(sv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svv, _ := recv.Embed(KiT_SliceViewInline).(*SliceViewInline)
vpath := svv.ViewPath
title := ""
if svv.SliceValView != nil {
newPath := ""
isZero := false
title, newPath, isZero = svv.SliceValView.AsValueViewBase().Label()
if isZero {
return
}
vpath = svv.ViewPath + "/" + newPath
} else {
elType := kit.NonPtrType(reflect.TypeOf(svv.Slice).Elem().Elem())
title = "Slice of " + kit.NonPtrType(elType).Name()
}
dlg := SliceViewDialog(svv.Viewport, svv.Slice, DlgOpts{Title: title, TmpSave: svv.TmpSave, ViewPath: vpath}, nil, nil, nil)
svvvk := dlg.Frame().ChildByType(KiT_SliceView, ki.Embeds, 2)
if svvvk != nil {
svvv := svvvk.(*SliceView)
svvv.SliceValView = svv.SliceValView
svvv.ViewSig.ConnectOnly(svv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svvvv, _ := recv.Embed(KiT_SliceViewInline).(*SliceViewInline)
svvvv.ViewSig.Emit(svvvv.This(), 0, nil)
})
}
})
}
sv.Parts.UpdateEnd(updt)
}
// SetChanged sets the Changed flag and emits the ViewSig signal for the
// SliceView, indicating that some kind of edit / change has taken place to
// the table data. It isn't really practical to record all the different
// types of changes, so this is just generic.
func (sv *SliceViewInline) SetChanged() {
sv.Changed = true
sv.ViewSig.Emit(sv.This(), 0, nil)
}
// SliceNewAt inserts a new blank element at given index in the slice -- -1
// means the end
func (sv *SliceViewInline) SliceNewAt(idx int, reconfig bool) {
if sv.IsArray || sv.IsFixedLen {
return
}
updt := sv.UpdateStart()
defer sv.UpdateEnd(updt)
kit.SliceNewAt(sv.Slice, idx)
if sv.TmpSave != nil {
sv.TmpSave.SaveTmp()
}
sv.SetChanged()
if reconfig {
sv.SetFullReRender()
sv.UpdateFromSlice()
}
}
func (sv *SliceViewInline) UpdateFromSlice() {
sv.ConfigParts()
}
func (sv *SliceViewInline) UpdateValues() {
updt := sv.UpdateStart()
for _, vv := range sv.Values {
vv.UpdateWidget()
}
sv.UpdateEnd(updt)
}
func (sv *SliceViewInline) Style2D() {
sv.ConfigParts()
sv.PartsWidgetBase.Style2D()
}
func (sv *SliceViewInline) Render2D() {
if sv.FullReRenderIfNeeded() {
return
}
if sv.PushBounds() {
sv.ConfigParts()
sv.Render2DParts()
sv.Render2DChildren()
sv.PopBounds()
}
}