-
Notifications
You must be signed in to change notification settings - Fork 0
/
structviewinline.go
156 lines (144 loc) · 5.23 KB
/
structviewinline.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
// 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/gist"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// StructViewInline represents a struct as a single line widget, for smaller
// structs and those explicitly marked inline in the kit type registry type
// properties -- constructs widgets in Parts to show the field names and
// editor fields for each field
type StructViewInline struct {
gi.PartsWidgetBase
Struct interface{} `desc:"the struct that we are a view onto"`
StructValView ValueView `desc:"ValueView for the struct itself, if this was created within value view framework -- otherwise nil"`
AddAction bool `desc:"if true add an edit action button at the end -- other users of this widget can then configure that -- it is called 'edit-action'"`
FieldViews []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:"-" view:"-" 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"`
HasDefs bool `json:"-" xml:"-" view:"inactive" desc:"if true, some fields have default values -- update labels when values change"`
}
var KiT_StructViewInline = kit.Types.AddType(&StructViewInline{}, StructViewInlineProps)
func (sv *StructViewInline) Disconnect() {
sv.PartsWidgetBase.Disconnect()
sv.ViewSig.DisconnectAll()
}
// SetStruct sets the source struct that we are viewing -- rebuilds the
// children to represent this struct
func (sv *StructViewInline) SetStruct(st interface{}) {
updt := false
if sv.Struct != st {
updt = sv.UpdateStart()
sv.Struct = st
if k, ok := st.(ki.Ki); ok {
k.NodeSignal().Connect(sv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svv, _ := recv.Embed(KiT_StructViewInline).(*StructViewInline)
svv.UpdateFields() // this never gets called, per below!
// fmt.Printf("struct view inline ki update values\n")
svv.ViewSig.Emit(svv.This(), 0, k)
})
}
}
sv.ConfigParts()
sv.UpdateEnd(updt)
}
var StructViewInlineProps = ki.Props{
"EnumType:Flag": gi.KiT_NodeFlags,
}
// ConfigParts configures Parts for the current struct
func (sv *StructViewInline) ConfigParts() {
if kit.IfaceIsNil(sv.Struct) {
return
}
sv.Parts.Lay = gi.LayoutHoriz
config := kit.TypeAndNameList{}
// always start fresh!
sv.FieldViews = make([]ValueView, 0)
kit.FlatFieldsValueFunc(sv.Struct, func(fval interface{}, typ reflect.Type, field reflect.StructField, fieldVal reflect.Value) bool {
// todo: check tags, skip various etc
vwtag := field.Tag.Get("view")
if vwtag == "-" {
return true
}
vv := FieldToValueView(sv.Struct, field.Name, fval)
if vv == nil { // shouldn't happen
return true
}
vvp := fieldVal.Addr()
vv.SetStructValue(vvp, sv.Struct, &field, sv.TmpSave, sv.ViewPath)
vtyp := vv.WidgetType()
// todo: other things with view tag..
labnm := fmt.Sprintf("label-%v", field.Name)
valnm := fmt.Sprintf("value-%v", field.Name)
config.Add(gi.KiT_Label, labnm)
config.Add(vtyp, valnm) // todo: extend to diff types using interface..
sv.FieldViews = append(sv.FieldViews, vv)
return true
})
if sv.AddAction {
config.Add(gi.KiT_Action, "edit-action")
}
mods, updt := sv.Parts.ConfigChildren(config)
if !mods {
updt = sv.Parts.UpdateStart()
}
sv.HasDefs = false
for i, vv := range sv.FieldViews {
lbl := sv.Parts.Child(i * 2).(*gi.Label)
vvb := vv.AsValueViewBase()
vvb.ViewPath = sv.ViewPath
lbl.Redrawable = true
lbl.SetProp("horizontal-align", gist.AlignLeft)
widg := sv.Parts.Child((i * 2) + 1).(gi.Node2D)
hasDef, inactTag := StructViewFieldTags(vv, lbl, widg, sv.IsInactive()) // in structview.go
if hasDef {
sv.HasDefs = true
}
vv.ConfigWidget(widg)
if !sv.IsInactive() && !inactTag {
vvb.ViewSig.ConnectOnly(sv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
svv, _ := recv.Embed(KiT_StructViewInline).(*StructViewInline)
svv.UpdateDefaults()
// note: updating here is redundant
svv.ViewSig.Emit(svv.This(), 0, nil)
})
}
}
sv.Parts.UpdateEnd(updt)
}
func (sv *StructViewInline) UpdateFields() {
updt := sv.UpdateStart()
for _, vv := range sv.FieldViews {
vv.UpdateWidget()
}
sv.UpdateEnd(updt)
}
func (sv *StructViewInline) UpdateDefaults() {
if !sv.HasDefs {
return
}
updt := sv.UpdateStart()
sv.SetFullReRender() // key to regen
for i, vv := range sv.FieldViews {
lbl := sv.Parts.Child(i * 2).(*gi.Label)
StructViewFieldDefTag(vv, lbl)
}
sv.UpdateEnd(updt)
}
func (sv *StructViewInline) Render2D() {
if sv.FullReRenderIfNeeded() {
return
}
if sv.PushBounds() {
sv.Render2DParts()
sv.Render2DChildren()
sv.PopBounds()
}
}