-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueview.go
920 lines (827 loc) · 28.8 KB
/
valueview.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
// 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"
"log"
"reflect"
"strings"
"time"
"github.com/goki/gi/gi"
"github.com/goki/gi/histyle"
"github.com/goki/gi/oswin/key"
"github.com/goki/gi/units"
"github.com/goki/ki"
"github.com/goki/ki/kit"
)
func init() {
gi.TheViewIFace = &ViewIFace{}
}
// MapInlineLen is the number of map elements at or below which an inline
// representation of the map will be presented -- more convenient for small
// #'s of props
var MapInlineLen = 3
// StructInlineLen is the number of elemental struct fields at or below which an inline
// representation of the struct will be presented -- more convenient for small structs
var StructInlineLen = 6
// SliceInlineLen is the number of slice elements below which inline will be used
var SliceInlineLen = 6
////////////////////////////////////////////////////////////////////////////////////////
// ValueView -- an interface for representing values (e.g., fields) in Views
// ValueViewer interface supplies the appropriate type of ValueView -- called
// on a given receiver item if defined for that receiver type (tries both
// pointer and non-pointer receivers) -- can use this for custom types to
// provide alternative custom interfaces -- must call Init on ValueView before
// returning it
type ValueViewer interface {
ValueView() ValueView
}
// example implementation of ValueViewer interface -- can't implement on
// non-local types, so all the basic types are handled separately:
//
// func (s string) ValueView() ValueView {
// vv := ValueViewBase{}
// vv.Init(&vv)
// return &vv
// }
// FieldValueViewer interface supplies the appropriate type of ValueView for a
// given field name and current field value on the receiver parent struct --
// called on a given receiver struct if defined for that receiver type (tries
// both pointer and non-pointer receivers) -- if a struct implements this
// interface, then it is used first for structs -- return nil to fall back on
// the default ToValueView result
type FieldValueViewer interface {
FieldValueView(field string, fval interface{}) ValueView
}
// ToValueView returns the appropriate ValueView for given item, based only on
// its type -- attempts to get the ValueViewer interface and failing that,
// falls back on default Kind-based options. tags are optional tags, e.g.,
// from the field in a struct, that control the view properties -- see the gi wiki
// for details on supported tags -- these are NOT set for the view element, only
// used for options that affect what kind of view to create.
// See FieldToValueView for version that takes into account the properties of the owner.
func ToValueView(it interface{}, tags string) ValueView {
if it == nil {
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
}
if vv, ok := it.(ValueViewer); ok {
vvo := vv.ValueView()
if vvo != nil {
return vvo
}
}
// try pointer version..
if vv, ok := kit.PtrInterface(it).(ValueViewer); ok {
vvo := vv.ValueView()
if vvo != nil {
return vvo
}
}
typ := reflect.TypeOf(it)
nptyp := kit.NonPtrType(typ)
vk := typ.Kind()
// fmt.Printf("vv val %v: typ: %v nptyp: %v kind: %v\n", it, typ.String(), nptyp.String(), vk)
if nptyp == reflect.TypeOf(gi.IconName("")) {
vv := IconValueView{}
vv.Init(&vv)
return &vv
}
if nptyp == reflect.TypeOf(gi.FontName("")) {
vv := FontValueView{}
vv.Init(&vv)
return &vv
}
if nptyp == reflect.TypeOf(gi.FileName("")) {
vv := FileValueView{}
vv.Init(&vv)
return &vv
}
if nptyp == reflect.TypeOf(gi.KeyMapName("")) {
vv := KeyMapValueView{}
vv.Init(&vv)
return &vv
}
if nptyp == reflect.TypeOf(key.Chord("")) {
vv := KeyChordValueView{}
vv.Init(&vv)
return &vv
}
if nptyp == reflect.TypeOf(histyle.StyleName("")) {
vv := HiStyleValueView{}
vv.Init(&vv)
return &vv
}
forceInline := false
forceNoInline := false
tprops := kit.Types.Properties(typ, false) // don't make
if tprops != nil {
if inprop, ok := kit.TypeProp(*tprops, "inline"); ok {
forceInline, ok = kit.ToBool(inprop)
}
if inprop, ok := kit.TypeProp(*tprops, "no-inline"); ok {
forceNoInline, ok = kit.ToBool(inprop)
}
}
if tags != "" {
stag := reflect.StructTag(tags)
if vwtag, ok := stag.Lookup("view"); ok {
switch vwtag {
case "inline":
forceInline = true
case "no-inline":
forceNoInline = true
}
}
}
switch {
case vk >= reflect.Int && vk <= reflect.Uint64:
if kit.Enums.TypeRegistered(nptyp) { // todo: bitfield
vv := EnumValueView{}
vv.Init(&vv)
return &vv
} else if _, ok := it.(fmt.Stringer); ok { // use stringer
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
} else {
vv := IntValueView{}
vv.Init(&vv)
return &vv
}
case nptyp == reflect.TypeOf(time.Time{}): // todo: could do better..
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
case nptyp == reflect.TypeOf(FileTime{}): // todo: could do better..
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
case vk == reflect.Bool:
vv := BoolValueView{}
vv.Init(&vv)
return &vv
case vk >= reflect.Float32 && vk <= reflect.Float64:
vv := FloatValueView{} // handles step, min / max etc
vv.Init(&vv)
return &vv
case vk >= reflect.Complex64 && vk <= reflect.Complex128:
// todo: special edit with 2 fields..
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
case vk == reflect.Ptr:
if ki.IsKi(nptyp) {
vv := KiPtrValueView{}
vv.Init(&vv)
return &vv
}
if kit.IfaceIsNil(it) {
return nil
}
v := reflect.ValueOf(it)
if !kit.ValueIsZero(v) {
return ToValueView(v.Elem().Interface(), tags)
}
case nptyp == ki.KiT_Signal:
return nil
case vk == reflect.Array:
fallthrough
case vk == reflect.Slice:
v := reflect.ValueOf(it)
sz := v.Len()
eltyp := kit.SliceElType(it)
if _, ok := it.([]byte); ok {
vv := ByteSliceValueView{}
vv.Init(&vv)
return &vv
}
if _, ok := it.([]rune); ok {
vv := RuneSliceValueView{}
vv.Init(&vv)
return &vv
}
isstru := (kit.NonPtrType(eltyp).Kind() == reflect.Struct)
if !forceNoInline && (forceInline || (!isstru && sz <= SliceInlineLen && !ki.IsKi(eltyp))) {
vv := SliceInlineValueView{}
vv.Init(&vv)
return &vv
} else {
vv := SliceValueView{}
vv.Init(&vv)
return &vv
}
case vk == reflect.Map:
v := reflect.ValueOf(it)
sz := v.Len()
sz = kit.MapStructElsN(it)
if !forceNoInline && (forceInline || sz <= MapInlineLen) {
vv := MapInlineValueView{}
vv.Init(&vv)
return &vv
} else {
vv := MapValueView{}
vv.Init(&vv)
return &vv
}
case vk == reflect.Struct:
// note: we need to handle these here b/c cannot define new methods for gi types
if nptyp == gi.KiT_Color {
vv := ColorValueView{}
vv.Init(&vv)
return &vv
}
nfld := kit.AllFieldsN(typ)
if !forceNoInline && (forceInline || nfld <= StructInlineLen) {
vv := StructInlineValueView{}
vv.Init(&vv)
return &vv
} else {
vv := StructValueView{}
vv.Init(&vv)
return &vv
}
case vk == reflect.Interface:
fmt.Printf("interface kind: %v %v %v\n", nptyp, nptyp.Name(), nptyp.String())
switch {
case nptyp == reflect.TypeOf((*reflect.Type)(nil)).Elem():
vv := TypeValueView{}
vv.Init(&vv)
return &vv
}
}
// fallback.
vv := ValueViewBase{}
vv.Init(&vv)
return &vv
}
// FieldToValueView returns the appropriate ValueView for given field on a
// struct -- attempts to get the FieldValueViewer interface, and falls back on
// ToValueView otherwise, using field value (fval)
func FieldToValueView(it interface{}, field string, fval interface{}) ValueView {
if it == nil || field == "" {
return ToValueView(fval, "")
}
if vv, ok := it.(FieldValueViewer); ok {
vvo := vv.FieldValueView(field, fval)
if vvo != nil {
return vvo
}
}
// try pointer version..
if vv, ok := kit.PtrInterface(it).(FieldValueViewer); ok {
vvo := vv.FieldValueView(field, fval)
if vvo != nil {
return vvo
}
}
typ := reflect.TypeOf(it)
nptyp := kit.NonPtrType(typ)
ftyp, ok := nptyp.FieldByName(field)
if ok {
return ToValueView(fval, string(ftyp.Tag))
}
return ToValueView(fval, "")
}
// ValueView is an interface for managing the GUI representation of values
// (e.g., fields, map values, slice values) in Views (StructView, MapView,
// etc). The different types of ValueView are for different Kinds of values
// (bool, float, etc) -- which can have different Kinds of owners. The
// ValueViewBase class supports all the basic fields for managing the owner
// kinds.
type ValueView interface {
ki.Ki
// AsValueViewBase gives access to the basic data fields so that the
// interface doesn't need to provide accessors for them.
AsValueViewBase() *ValueViewBase
// SetStructValue sets the value, owner and field information for a struct field.
SetStructValue(val reflect.Value, owner interface{}, field *reflect.StructField, tmpSave ValueView)
// SetMapKey sets the key value and owner for a map key.
SetMapKey(val reflect.Value, owner interface{}, tmpSave ValueView)
// SetMapValue sets the value, owner and map key information for a map
// element -- needs pointer to ValueView representation of key to track
// current key value.
SetMapValue(val reflect.Value, owner interface{}, key interface{}, keyView ValueView, tmpSave ValueView)
// SetSliceValue sets the value, owner and index information for a slice element.
SetSliceValue(val reflect.Value, owner interface{}, idx int, tmpSave ValueView)
// SetStandaloneValue sets the value for a singleton standalone value
// (e.g., for arg values).
SetStandaloneValue(val reflect.Value)
// OwnerKind returns the reflect.Kind of the owner: Struct, Map, or Slice
// (or Invalid for standalone values such as args).
OwnerKind() reflect.Kind
// IsInactive returns whether the value is inactive -- e.g., Map owners
// have Inactive values, and some fields can be marked as Inactive using a
// struct tag.
IsInactive() bool
// WidgetType returns an appropriate type of widget to represent the
// current value.
WidgetType() reflect.Type
// UpdateWidget updates the widget representation to reflect the current
// value. Must first check for a nil widget -- can be called in a
// no-widget context (e.g., for single-argument values with actions).
UpdateWidget()
// ConfigWidget configures a widget of WidgetType for representing the
// value, including setting up the signal connections to set the value
// when the user edits it (values are always set immediately when the
// widget is updated).
ConfigWidget(widg gi.Node2D)
// HasAction returns true if this value has an associated action, such as
// pulling up a dialog or chooser for this value. Activate method will
// trigger this action.
HasAction() bool
// Activate triggers any action associated with this value, such as
// pulling up a dialog or chooser for this value. This is called by
// default for single-argument methods that have value representations
// with actions. The viewport provides a context for opening other
// windows, and the receiver and dlgFunc should receive the DialogSig for
// the relevant dialog, or a pass-on call thereof, including the
// DialogAccepted or Canceled signal, so that the caller can execute its
// own actions based on the user hitting Ok or Cancel.
Activate(vp *gi.Viewport2D, recv ki.Ki, dlgFunc ki.RecvFunc)
// Val returns the reflect.Value representation for this item.
Val() reflect.Value
// SetValue assigns given value to this item (if not Inactive), using
// Ki.SetField for Ki types and kit.SetRobust otherwise -- emits a ViewSig
// signal when set.
SetValue(val interface{}) bool
// SetTags sets tags for this valueview, for non-struct values, to
// influence interface for this value -- see
// https://github.com/goki/gi/wiki/Tags for valid options. Adds to
// existing tags if some are already set.
SetTags(tags map[string]string)
// SetTag sets given tag to given value for this valueview, for non-struct
// values, to influence interface for this value -- see
// https://github.com/goki/gi/wiki/Tags for valid options.
SetTag(tag, value string)
// Tag returns value for given tag -- looks first at tags set by
// SetTag(s) methods, and then at field tags if this is a field in a
// struct -- returns false if tag was not set.
Tag(tag string) (string, bool)
// AllTags returns all the tags for this value view, from structfield or set
// specifically using SetTag* methods
AllTags() map[string]string
// SaveTmp saves a temporary copy of a struct to a map -- map values must
// be explicitly re-saved and cannot be directly written to by the value
// elements -- each ValueView has a pointer to any parent ValueView that
// might need to be saved after SetValue -- SaveTmp called automatically
// in SetValue but other cases that use something different need to call
// it explicitly.
SaveTmp()
}
// note: could have a more efficient way to represent the different owner type
// data (Key vs. Field vs. Idx), instead of just having everything for
// everything. However, ValueView itself gets customized for different target
// value types, and those are orthogonal to the owner type, so need a separate
// ValueViewOwner class that encodes these options more efficiently -- but
// that introduces another struct alloc and pointer -- not clear if it is
// worth it..
// ValueViewBase provides the basis for implementations of the ValueView
// interface, representing values in the interface -- it implements a generic
// TextField representation of the string value, and provides the generic
// fallback for everything that doesn't provide a specific ValueViewer type.
type ValueViewBase struct {
ki.Node
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 -- data is the value that was set"`
Value reflect.Value `desc:"the reflect.Value representation of the value"`
OwnKind reflect.Kind `desc:"kind of owner that we have -- reflect.Struct, .Map, .Slice are supported"`
IsMapKey bool `desc:"for OwnKind = Map, this value represents the Key -- otherwise the Value"`
Owner interface{} `desc:"the object that owns this value, either a struct, slice, or map, if non-nil -- if a Ki Node, then SetField is used to set value, to provide proper updating"`
Field *reflect.StructField `desc:"if Owner is a struct, this is the reflect.StructField associated with the value"`
Tags map[string]string `desc:"set of tags that can be set to customize interface for different types of values -- only source for non-structfield values"`
Key interface{} `desc:"if Owner is a map, and this is a value, this is the key for this value in the map"`
KeyView ValueView `desc:"if Owner is a map, and this is a value, this is the value view representing the key -- its value has the *current* value of the key, which can be edited"`
Idx int `desc:"if Owner is a slice, this is the index for the value in the slice"`
WidgetTyp reflect.Type `desc:"type of widget to create -- cached during WidgetType method -- chosen based on the ValueView type and reflect.Value type -- see ValueViewer interface"`
Widget gi.Node2D `desc:"the widget used to display and edit the value in the interface -- this is created for us externally and we cache it during ConfigWidget"`
TmpSave ValueView `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"`
}
var KiT_ValueViewBase = kit.Types.AddType(&ValueViewBase{}, ValueViewBaseProps)
var ValueViewBaseProps = ki.Props{
"base-type": true,
}
func (vv *ValueViewBase) AsValueViewBase() *ValueViewBase {
return vv
}
func (vv *ValueViewBase) SetStructValue(val reflect.Value, owner interface{}, field *reflect.StructField, tmpSave ValueView) {
vv.OwnKind = reflect.Struct
vv.Value = val
vv.Owner = owner
vv.Field = field
vv.TmpSave = tmpSave
vv.SetName(field.Name)
}
func (vv *ValueViewBase) SetMapKey(key reflect.Value, owner interface{}, tmpSave ValueView) {
vv.OwnKind = reflect.Map
vv.IsMapKey = true
vv.Value = key
vv.Owner = owner
vv.TmpSave = tmpSave
vv.SetName(kit.ToString(key.Interface()))
}
func (vv *ValueViewBase) SetMapValue(val reflect.Value, owner interface{}, key interface{}, keyView ValueView, tmpSave ValueView) {
vv.OwnKind = reflect.Map
vv.Value = val
vv.Owner = owner
vv.Key = key
vv.KeyView = keyView
vv.TmpSave = tmpSave
vv.SetName(kit.ToString(key))
}
func (vv *ValueViewBase) SetSliceValue(val reflect.Value, owner interface{}, idx int, tmpSave ValueView) {
vv.OwnKind = reflect.Slice
vv.Value = val
vv.Owner = owner
vv.Idx = idx
vv.TmpSave = tmpSave
vv.SetName(fmt.Sprintf("%v", idx))
}
func (vv *ValueViewBase) SetStandaloneValue(val reflect.Value) {
vv.OwnKind = reflect.Invalid
vv.Value = val
}
// we have this one accessor b/c it is more useful for outside consumers vs. internal usage
func (vv *ValueViewBase) OwnerKind() reflect.Kind {
return vv.OwnKind
}
func (vv *ValueViewBase) IsInactive() bool {
if vv.OwnKind == reflect.Struct {
if _, ok := vv.Tag("inactive"); ok {
return true
}
}
npv := kit.NonPtrValue(vv.Value)
if npv.Kind() == reflect.Interface && kit.ValueIsZero(npv) {
return true
}
return false
}
func (vv *ValueViewBase) HasAction() bool {
return false
}
func (vv *ValueViewBase) Activate(vp *gi.Viewport2D, recv ki.Ki, fun ki.RecvFunc) {
}
func (vv *ValueViewBase) Val() reflect.Value {
return vv.Value
}
func (vv *ValueViewBase) SetValue(val interface{}) bool {
if vv.This().(ValueView).IsInactive() {
return false
}
rval := false
if vv.Owner != nil {
switch vv.OwnKind {
case reflect.Struct:
if kiv, ok := vv.Owner.(ki.Ki); ok {
rval = kiv.SetField(vv.Field.Name, val)
} else {
rval = kit.SetRobust(kit.PtrValue(vv.Value).Interface(), val)
}
case reflect.Map:
ov := kit.NonPtrValue(reflect.ValueOf(vv.Owner))
if vv.IsMapKey {
nv := kit.NonPtrValue(reflect.ValueOf(val)) // new key value
kv := kit.NonPtrValue(vv.Value)
cv := ov.MapIndex(kv) // get current value
curnv := ov.MapIndex(nv) // see if new value there already
if val != kv.Interface() && !kit.ValueIsZero(curnv) {
var vp *gi.Viewport2D
if vv.Widget != nil {
widg := vv.Widget.AsNode2D()
vp = widg.Viewport
}
// actually new key and current exists
gi.ChoiceDialog(vp,
gi.DlgOpts{Title: "Map Key Conflict", Prompt: fmt.Sprintf("The map key value: %v already exists in the map -- are you sure you want to overwrite the current value?", val)},
[]string{"Cancel Change", "Overwrite"},
vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
switch sig {
case 0:
if vp != nil {
vp.FullRender2DTree()
}
case 1:
cv := ov.MapIndex(kv) // get current value
ov.SetMapIndex(kv, reflect.Value{}) // delete old key
ov.SetMapIndex(nv, cv) // set new key to current value
vv.Value = nv // update value to new key
vv.This().(ValueView).SaveTmp()
vv.ViewSig.Emit(vv.This(), 0, nil)
if vp != nil {
vp.FullRender2DTree()
}
}
})
return false // abort this action right now
}
ov.SetMapIndex(kv, reflect.Value{}) // delete old key
ov.SetMapIndex(nv, cv) // set new key to current value
vv.Value = nv // update value to new key
rval = true
} else {
vv.Value = reflect.ValueOf(val)
if vv.KeyView != nil {
ck := kit.NonPtrValue(vv.KeyView.Val()) // current key value
ov.SetMapIndex(ck, vv.Value)
} else { // static, key not editable?
ov.SetMapIndex(kit.NonPtrValue(reflect.ValueOf(vv.Key)), vv.Value)
}
rval = true
}
case reflect.Slice:
rval = kit.SetRobust(kit.PtrValue(vv.Value).Interface(), val)
}
} else {
rval = kit.SetRobust(kit.PtrValue(vv.Value).Interface(), val)
}
if rval {
vv.This().(ValueView).SaveTmp()
}
// fmt.Printf("value view: %T sending for setting val %v\n", vv.This(), val)
vv.ViewSig.Emit(vv.This(), 0, nil)
return rval
}
func (vv *ValueViewBase) SaveTmp() {
if vv.TmpSave == nil {
return
}
if vv.TmpSave == vv.This().(ValueView) {
// if we are a map value, of a struct value, we save our value
if vv.Owner != nil && vv.OwnKind == reflect.Map && !vv.IsMapKey {
if kit.NonPtrValue(vv.Value).Kind() == reflect.Struct {
ov := kit.NonPtrValue(reflect.ValueOf(vv.Owner))
if vv.KeyView != nil {
ck := kit.NonPtrValue(vv.KeyView.Val())
ov.SetMapIndex(ck, kit.NonPtrValue(vv.Value))
// fmt.Printf("save tmp of struct value in key: %v\n", ck.Interface())
} else {
ov.SetMapIndex(kit.NonPtrValue(reflect.ValueOf(vv.Key)), kit.NonPtrValue(vv.Value))
// fmt.Printf("save tmp of struct value in key: %v\n", vv.Key)
}
}
}
} else {
vv.TmpSave.SaveTmp()
}
}
func (vv *ValueViewBase) CreateTempIfNotPtr() bool {
if vv.Value.Kind() != reflect.Ptr { // we create a temp variable -- SaveTmp will save it!
vv.TmpSave = vv.This().(ValueView) // we are it!
vtyp := reflect.TypeOf(vv.Value.Interface())
vtp := reflect.New(vtyp)
// fmt.Printf("vtyp: %v %v %v, vtp: %v %v %T\n", vtyp, vtyp.Name(), vtyp.String(), vtp, vtp.Type(), vtp.Interface())
kit.SetRobust(vtp.Interface(), vv.Value.Interface())
vv.Value = vtp // use this instead
return true
}
return false
}
func (vv *ValueViewBase) SetTags(tags map[string]string) {
if vv.Tags == nil {
vv.Tags = make(map[string]string, len(tags))
}
for tag, val := range tags {
vv.Tags[tag] = val
}
}
func (vv *ValueViewBase) SetTag(tag, value string) {
if vv.Tags == nil {
vv.Tags = make(map[string]string, 10)
}
vv.Tags[tag] = value
}
func (vv *ValueViewBase) Tag(tag string) (string, bool) {
if vv.Tags != nil {
if tv, ok := vv.Tags[tag]; ok {
return tv, ok
}
}
if !(vv.Owner != nil && vv.OwnKind == reflect.Struct) {
return "", false
}
return vv.Field.Tag.Lookup(tag)
}
func (vv *ValueViewBase) AllTags() map[string]string {
rvt := make(map[string]string)
if vv.Tags != nil {
for key, val := range vv.Tags {
rvt[key] = val
}
}
if !(vv.Owner != nil && vv.OwnKind == reflect.Struct) {
return rvt
}
smap := kit.StructTags(vv.Field.Tag)
for key, val := range smap {
rvt[key] = val
}
return rvt
}
// OwnerLabel returns some extra info about the owner of this value view
// which is useful to put in title of our object
func (vv *ValueViewBase) OwnerLabel() string {
switch vv.OwnKind {
case reflect.Struct:
olbl := gi.ToLabeler(vv.Owner)
if olbl != "" {
return olbl + "." + vv.Field.Name
}
return vv.Field.Name
case reflect.Map:
if vv.IsMapKey {
kv := kit.NonPtrValue(vv.Value)
return kit.ToString(kv.Interface())
} else {
if vv.KeyView != nil {
ck := kit.NonPtrValue(vv.KeyView.Val()) // current key value
return kit.ToString(ck.Interface())
} else {
return kit.ToString(vv.Key)
}
}
case reflect.Slice:
return kit.ToString(kit.PtrValue(vv.Value).Interface())
}
return ""
}
////////////////////////////////////////////////////////////////////////////////////////
// Base Widget Functions -- these are typically redefined in ValueView subtypes
func (vv *ValueViewBase) WidgetType() reflect.Type {
vv.WidgetTyp = gi.KiT_TextField
return vv.WidgetTyp
}
func (vv *ValueViewBase) UpdateWidget() {
if vv.Widget == nil {
return
}
tf := vv.Widget.(*gi.TextField)
npv := kit.NonPtrValue(vv.Value)
// fmt.Printf("vvb val: %v type: %v kind: %v\n", npv.Interface(), npv.Type().String(), npv.Kind())
if npv.Kind() == reflect.Interface && kit.ValueIsZero(npv) {
tf.SetText("nil")
} else {
txt := kit.ToString(vv.Value.Interface())
tf.SetText(txt)
}
}
func (vv *ValueViewBase) ConfigWidget(widg gi.Node2D) {
vv.Widget = widg
tf := vv.Widget.(*gi.TextField)
tf.SetStretchMaxWidth()
tf.Tooltip, _ = vv.Tag("desc")
tf.SetInactiveState(vv.This().(ValueView).IsInactive())
tf.SetProp("min-width", units.NewValue(16, units.Ch))
if widthtag, ok := vv.Tag("width"); ok {
width, ok := kit.ToFloat32(widthtag)
if ok {
tf.SetMinPrefWidth(units.NewValue(width, units.Ch))
}
}
if maxwidthtag, ok := vv.Tag("max-width"); ok {
width, ok := kit.ToFloat32(maxwidthtag)
if ok {
tf.SetProp("max-width", units.NewValue(width, units.Ch))
}
}
if completetag, ok := vv.Tag("complete"); ok {
in := []reflect.Value{reflect.ValueOf(tf)}
in = append(in, reflect.ValueOf(completetag)) // pass tag value - object may doing completion on multiple fields
cmpfv := reflect.ValueOf(vv.Owner).MethodByName("SetCompleter")
if kit.ValueIsZero(cmpfv) {
log.Printf("giv.ValueViewBase: programmer error -- SetCompleter method not found in type: %T\n", vv.Owner)
} else {
cmpfv.Call(in)
}
}
tf.TextFieldSig.ConnectOnly(vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.TextFieldDone) || sig == int64(gi.TextFieldDeFocused) {
vvv, _ := recv.Embed(KiT_ValueViewBase).(*ValueViewBase)
tf := send.(*gi.TextField)
if vvv.SetValue(tf.Text()) {
vvv.UpdateWidget() // always update after setting value..
}
}
})
vv.UpdateWidget()
}
////////////////////////////////////////////////////////////////////////////////////////
// ViewIFace
// giv.ViewIFace is THE implementation of the gi.ViewIFace interface
type ViewIFace struct {
}
func (vi *ViewIFace) CtxtMenuView(val interface{}, inactive bool, vp *gi.Viewport2D, menu *gi.Menu) bool {
return CtxtMenuView(val, inactive, vp, menu)
}
func (vi *ViewIFace) GoGiEditor(obj ki.Ki) {
GoGiEditorDialog(obj)
}
func (vi *ViewIFace) PrefsView(prefs *gi.Preferences) {
PrefsView(prefs)
}
func (vi *ViewIFace) KeyMapsView(maps *gi.KeyMaps) {
KeyMapsView(maps)
}
func (vi *ViewIFace) PrefsDetView(prefs *gi.PrefsDetailed) {
PrefsDetView(prefs)
}
func (vi *ViewIFace) HiStylesView(styles interface{}) {
HiStylesView(styles.(*histyle.Styles))
}
func (vi *ViewIFace) PrefsDetDefaults(pf *gi.PrefsDetailed) {
pf.TextViewClipHistMax = TextViewClipHistMax
pf.MapInlineLen = MapInlineLen
pf.StructInlineLen = StructInlineLen
pf.SliceInlineLen = SliceInlineLen
pf.MaxScopeLines = MaxScopeLines
}
func (vi *ViewIFace) PrefsDetApply(pf *gi.PrefsDetailed) {
TextViewClipHistMax = pf.TextViewClipHistMax
MapInlineLen = pf.MapInlineLen
StructInlineLen = pf.StructInlineLen
SliceInlineLen = pf.SliceInlineLen
MaxScopeLines = pf.MaxScopeLines
}
func (vi *ViewIFace) PrefsDbgView(prefs *gi.PrefsDebug) {
PrefsDbgView(prefs)
}
////////////////////////////////////////////////////////////////////////////////////////
// VersCtrlValueView
// VersCtrlSystems is a list of supported Version Control Systems -- use these
// names in commands to select commands for the current VCS for this project
// (i.e., use shortest version of name, typically three letters)
var VersCtrlSystems = []string{"Git", "SVN"}
// VersCtrlName is the name of a version control system
type VersCtrlName string
func VersCtrlNameProper(vc string) VersCtrlName {
vcl := strings.ToLower(vc)
for _, vcnp := range VersCtrlSystems {
vcnpl := strings.ToLower(vcnp)
if strings.Compare(vcl, vcnpl) == 0 {
return VersCtrlName(vcnp)
}
}
return ""
}
// ValueView registers VersCtrlValueView as the viewer of VersCtrlName
func (kn VersCtrlName) ValueView() ValueView {
vv := VersCtrlValueView{}
vv.Init(&vv)
return &vv
}
// VersCtrlValueView presents an action for displaying an VersCtrlName and selecting
// from StringPopup
type VersCtrlValueView struct {
ValueViewBase
}
var KiT_VersCtrlValueView = kit.Types.AddType(&VersCtrlValueView{}, nil)
func (vv *VersCtrlValueView) WidgetType() reflect.Type {
vv.WidgetTyp = gi.KiT_Action
return vv.WidgetTyp
}
func (vv *VersCtrlValueView) UpdateWidget() {
if vv.Widget == nil {
return
}
ac := vv.Widget.(*gi.Action)
txt := kit.ToString(vv.Value.Interface())
if txt == "" {
txt = "(none)"
}
ac.SetText(txt)
}
func (vv *VersCtrlValueView) ConfigWidget(widg gi.Node2D) {
vv.Widget = widg
ac := vv.Widget.(*gi.Action)
ac.SetProp("border-radius", units.NewValue(4, units.Px))
ac.ActionSig.ConnectOnly(vv.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
vvv, _ := recv.Embed(KiT_VersCtrlValueView).(*VersCtrlValueView)
ac := vvv.Widget.(*gi.Action)
vvv.Activate(ac.Viewport, nil, nil)
})
vv.UpdateWidget()
}
func (vv *VersCtrlValueView) HasAction() bool {
return true
}
func (vv *VersCtrlValueView) Activate(vp *gi.Viewport2D, dlgRecv ki.Ki, dlgFunc ki.RecvFunc) {
if vv.IsInactive() {
return
}
cur := kit.ToString(vv.Value.Interface())
var recv gi.Node2D
if vv.Widget != nil {
recv = vv.Widget
} else {
recv = vp.This().(gi.Node2D)
}
gi.StringsChooserPopup(VersCtrlSystems, cur, recv, func(recv, send ki.Ki, sig int64, data interface{}) {
ac := send.(*gi.Action)
vv.SetValue(ac.Text)
vv.UpdateWidget()
if dlgRecv != nil && dlgFunc != nil {
dlgFunc(dlgRecv, send, int64(gi.DialogAccepted), data)
}
})
}