-
Notifications
You must be signed in to change notification settings - Fork 0
/
valueview.go
1116 lines (1010 loc) · 35.1 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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/gist"
"github.com/goki/gi/histyle"
"github.com/goki/gi/oswin/key"
"github.com/goki/gi/units"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
func init() {
gi.TheViewIFace = &ViewIFace{}
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.IconName(""))), func() ValueView {
vv := &IconValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.FontName(""))), func() ValueView {
vv := &FontValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.FileName(""))), func() ValueView {
vv := &FileValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.KeyMapName(""))), func() ValueView {
vv := &KeyMapValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.ColorName(""))), func() ValueView {
vv := &ColorNameValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(key.Chord(""))), func() ValueView {
vv := &KeyChordValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(gi.HiStyleName(""))), func() ValueView {
vv := &HiStyleValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(time.Time{})), func() ValueView {
vv := &TimeValueView{}
ki.InitNode(vv)
return vv
})
ValueViewMapAdd(kit.LongTypeName(reflect.TypeOf(FileTime{})), func() ValueView {
vv := &TimeValueView{}
ki.InitNode(vv)
return vv
})
}
// 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
////////////////////////////////////////////////////////////////////////////////////////
// ValueViewer -- an interface for selecting ValueView GUI representation of types
// 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{}
// ki.InitNode(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
}
////////////////////////////////////////////////////////////////////////////////////////
// ValueViewMap -- alternative way to connect value view with type
// ValueViewFunc is a function that returns a new initialized ValueView
// of an appropriate type as registered in the ValueViewMap
type ValueViewFunc func() ValueView
// The ValueViewMap is used to connect type names with corresponding ValueView
// representations of those types -- this can be used when it is not possible
// to use the ValueViewer interface (e.g., interface methods can only be
// defined within the package that defines the type -- so we need this for
// all types in gi which don't know about giv).
// You must use kit.LongTypeName (full package name + "." . type name) for
// the type name, as that is how it will be looked up.
var ValueViewMap map[string]ValueViewFunc
// ValueViewMapAdd adds a ValueViewFunc for a given type name.
// You must use kit.LongTypeName (full package name + "." . type name) for
// the type name, as that is how it will be looked up.
func ValueViewMapAdd(typeNm string, fun ValueViewFunc) {
if ValueViewMap == nil {
ValueViewMap = make(map[string]ValueViewFunc)
}
ValueViewMap[typeNm] = fun
}
// StructTagVal returns the value for given key in given struct tag string
// uses reflect.StructTag Lookup method -- just a wrapper for external
// use (e.g., in Python code)
func StructTagVal(key, tags string) string {
stag := reflect.StructTag(tags)
val, _ := stag.Lookup(key)
return val
}
// 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.
// gopy:interface=handle
func ToValueView(it interface{}, tags string) ValueView {
if it == nil {
vv := &ValueViewBase{}
ki.InitNode(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)
nptypnm := kit.LongTypeName(nptyp)
if vvf, has := ValueViewMap[nptypnm]; has {
vv := vvf()
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) {
if kit.Enums.IsBitFlag(nptyp) {
vv := &BitFlagView{}
ki.InitNode(vv)
return vv
} else {
vv := &EnumValueView{}
ki.InitNode(vv)
return vv
}
} else if _, ok := it.(fmt.Stringer); ok { // use stringer
vv := &ValueViewBase{}
ki.InitNode(vv)
return vv
} else {
vv := &IntValueView{}
ki.InitNode(vv)
return vv
}
case vk == reflect.Bool:
vv := &BoolValueView{}
ki.InitNode(vv)
return vv
case vk >= reflect.Float32 && vk <= reflect.Float64:
vv := &FloatValueView{} // handles step, min / max etc
ki.InitNode(vv)
return vv
case vk >= reflect.Complex64 && vk <= reflect.Complex128:
// todo: special edit with 2 fields..
vv := &ValueViewBase{}
ki.InitNode(vv)
return vv
case vk == reflect.Ptr:
if ki.IsKi(nptyp) {
vv := &KiPtrValueView{}
ki.InitNode(vv)
return vv
}
if kit.IfaceIsNil(it) {
vv := &NilValueView{}
ki.InitNode(vv)
return vv
}
v := reflect.ValueOf(it)
if !kit.ValueIsZero(v) {
// note: interfaces go here:
// fmt.Printf("vv indirecting on pointer: %v type: %v\n", it, nptyp.String())
return ToValueView(v.Elem().Interface(), tags)
}
case nptyp == ki.KiT_Signal:
vv := &NilValueView{}
ki.InitNode(vv)
return vv
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{}
ki.InitNode(vv)
return vv
}
if _, ok := it.([]rune); ok {
vv := &RuneSliceValueView{}
ki.InitNode(vv)
return vv
}
isstru := (kit.NonPtrType(eltyp).Kind() == reflect.Struct)
if !forceNoInline && (forceInline || (!isstru && sz <= SliceInlineLen && !ki.IsKi(eltyp))) {
vv := &SliceInlineValueView{}
ki.InitNode(vv)
return vv
} else {
vv := &SliceValueView{}
ki.InitNode(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{}
ki.InitNode(vv)
return vv
} else {
vv := &MapValueView{}
ki.InitNode(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 == gist.KiT_Color {
vv := &ColorValueView{}
ki.InitNode(vv)
return vv
}
nfld := kit.AllFieldsN(nptyp)
if nfld > 0 && !forceNoInline && (forceInline || nfld <= StructInlineLen) {
vv := &StructInlineValueView{}
ki.InitNode(vv)
return vv
} else {
vv := &StructValueView{}
ki.InitNode(vv)
return vv
}
case vk == reflect.Interface:
// note: we never get here -- all interfaces are captured by pointer kind above
// apparently (because the non-ptr vk indirection does that I guess?)
fmt.Printf("interface kind: %v %v %v\n", nptyp, nptyp.Name(), nptyp.String())
switch {
case nptyp == reflect.TypeOf((*reflect.Type)(nil)).Elem():
vv := &TypeValueView{}
ki.InitNode(vv)
return vv
}
}
// fallback.
vv := &ValueViewBase{}
ki.InitNode(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)
// gopy:interface=handle
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)
if pv, has := kit.Types.Prop(nptyp, "EnumType:"+field); has {
et := pv.(reflect.Type)
if kit.Enums.IsBitFlag(et) {
vv := &BitFlagView{}
vv.AltType = et
ki.InitNode(vv)
return vv
} else {
vv := &EnumValueView{}
vv.AltType = et
ki.InitNode(vv)
return vv
}
}
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, viewPath string)
// 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, viewPath string)
// SetSliceValue sets the value, owner and index information for a slice element.
SetSliceValue(val reflect.Value, owner interface{}, idx int, tmpSave ValueView, viewPath string)
// SetSoloValue sets the value for a singleton standalone value
// (e.g., for arg values).
SetSoloValue(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"`
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"`
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) Disconnect() {
vv.Node.Disconnect()
vv.ViewSig.DisconnectAll()
}
func (vv *ValueViewBase) AsValueViewBase() *ValueViewBase {
return vv
}
func (vv *ValueViewBase) SetStructValue(val reflect.Value, owner interface{}, field *reflect.StructField, tmpSave ValueView, viewPath string) {
vv.OwnKind = reflect.Struct
vv.Value = val
vv.Owner = owner
vv.Field = field
vv.TmpSave = tmpSave
vv.ViewPath = viewPath + "." + field.Name
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, viewPath string) {
vv.OwnKind = reflect.Map
vv.Value = val
vv.Owner = owner
vv.Key = key
vv.KeyView = keyView
vv.TmpSave = tmpSave
keystr := kit.ToString(key)
vv.ViewPath = viewPath + "." + keystr
vv.SetName(keystr)
}
func (vv *ValueViewBase) SetSliceValue(val reflect.Value, owner interface{}, idx int, tmpSave ValueView, viewPath string) {
vv.OwnKind = reflect.Slice
vv.Value = val
vv.Owner = owner
vv.Idx = idx
vv.TmpSave = tmpSave
idxstr := fmt.Sprintf("%v", idx)
vpath := "[" + idxstr + "]"
if vv.Owner != nil {
if lblr, ok := vv.Owner.(gi.SliceLabeler); ok {
slbl := lblr.ElemLabel(idx)
if slbl != "" {
vpath = "[" + slbl + "]"
}
}
}
vv.ViewPath = viewPath + vpath
vv.SetName(idxstr)
}
// SetSoloValue sets the value for a singleton standalone value
// (e.g., for arg values).
func (vv *ValueViewBase) SetSoloValue(val reflect.Value) {
vv.OwnKind = reflect.Invalid
vv.Value = val
}
// SetSoloValueIface sets the value for a singleton standalone value
// using an interface for the value -- you must pass a pointer.
// for now, this cannot be a method because gopy doesn't find the
// key comment below that tells it what to do with the interface
// gopy:interface=handle
func SetSoloValueIface(vv *ValueViewBase, val interface{}) {
vv.OwnKind = reflect.Invalid
vv.Value = reflect.ValueOf(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) == nil)
} 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.SetNeedsFullRender()
}
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.SetNeedsFullRender()
}
}
})
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 = kit.NonPtrValue(reflect.ValueOf(val))
if vv.KeyView != nil {
ck := kit.NonPtrValue(vv.KeyView.Val()) // current key value
kit.SetMapRobust(ov, ck, reflect.ValueOf(val))
} else { // static, key not editable?
kit.SetMapRobust(ov, kit.NonPtrValue(reflect.ValueOf(vv.Key)), vv.Value)
}
rval = true
}
case reflect.Slice:
rval = kit.SetRobust(kit.PtrValue(vv.Value).Interface(), val)
}
if updtr, ok := vv.Owner.(gi.Updater); ok {
// fmt.Printf("updating: %v\n", updtr)
updtr.Update()
}
} 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())
kit.SetMapRobust(ov, ck, kit.NonPtrValue(vv.Value))
} else {
kit.SetMapRobust(ov, 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 {
if vv.Owner == nil {
return ""
}
olbl, _ := gi.ToLabeler(vv.Owner)
switch vv.OwnKind {
case reflect.Struct:
if olbl != "" {
return olbl + "." + vv.Field.Name
}
return vv.Field.Name
case reflect.Map:
kystr := ""
if vv.IsMapKey {
kv := kit.NonPtrValue(vv.Value)
kystr = kit.ToString(kv.Interface())
} else {
if vv.KeyView != nil {
ck := kit.NonPtrValue(vv.KeyView.Val()) // current key value
kystr = kit.ToString(ck.Interface())
} else {
kystr = kit.ToString(vv.Key)
}
}
if kystr != "" {
return olbl + "[" + kystr + "]"
}
return olbl
case reflect.Slice:
if lblr, ok := vv.Owner.(gi.SliceLabeler); ok {
slbl := lblr.ElemLabel(vv.Idx)
if slbl != "" {
return fmt.Sprintf("%s[%s]", olbl, slbl)
}
}
return fmt.Sprintf("%s[%d]", olbl, vv.Idx)
}
return olbl
}
// Label returns a label for this item suitable for a window title etc.
// Based on the underlying value type name, owner label, and ViewPath.
// newPath returns just what should be added to a ViewPath
// also includes zero value check reported in the isZero bool, which
// can be used for not proceeding in case of non-value-based types.
func (vv *ValueViewBase) Label() (label, newPath string, isZero bool) {
lbl := ""
var npt reflect.Type
if kit.ValueIsZero(vv.Value) || kit.ValueIsZero(kit.NonPtrValue(vv.Value)) {
npt = kit.NonPtrType(vv.Value.Type())
isZero = true
} else {
opv := kit.OnePtrUnderlyingValue(vv.Value)
npt = kit.NonPtrType(opv.Type())
}
lbl += npt.String()
newPath = lbl
olbl := vv.OwnerLabel()
if olbl != "" {
lbl += ": " + olbl
}
if vv.ViewPath != "" {
lbl += " [" + vv.ViewPath + "]"
}
return lbl, newPath, isZero
}
////////////////////////////////////////////////////////////////////////////////////////
// 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
vv.StdConfigWidget(widg)
tf, ok := vv.Widget.(*gi.TextField)
if !ok {
return
}
tf.SetStretchMaxWidth()
tf.Tooltip, _ = vv.Tag("desc")
tf.SetInactiveState(vv.This().(ValueView).IsInactive())
tf.SetProp("min-width", units.NewCh(16))
if completetag, ok := vv.Tag("complete"); ok {
// todo: this does not seem to be up-to-date and should use Completer interface..
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()
}
// StdConfigWidget does all of the standard widget configuration tag options
func (vv *ValueViewBase) StdConfigWidget(widg gi.Node2D) {
nb := widg.AsNode2D()
if widthtag, ok := vv.Tag("width"); ok {
width, ok := kit.ToFloat32(widthtag)
if ok {
nb.SetMinPrefWidth(units.NewCh(width))
}
}
if maxwidthtag, ok := vv.Tag("max-width"); ok {
width, ok := kit.ToFloat32(maxwidthtag)
if ok {
nb.SetProp("max-width", units.NewCh(width))
}
}
if heighttag, ok := vv.Tag("height"); ok {
height, ok := kit.ToFloat32(heighttag)
if ok {
nb.SetMinPrefHeight(units.NewEm(height))
}
}
if maxheighttag, ok := vv.Tag("max-height"); ok {
height, ok := kit.ToFloat32(maxheighttag)
if ok {
nb.SetProp("max-height", units.NewEm(height))
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// 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(std bool) {
if std {
HiStylesView(&histyle.StdStyles)
} else {
HiStylesView(&histyle.CustomStyles)
}
}
func (vi *ViewIFace) HiStyleInit() {
histyle.Init()
}
func (vi *ViewIFace) SetHiStyleDefault(hsty gi.HiStyleName) {
histyle.StyleDefault = hsty
}