-
Notifications
You must be signed in to change notification settings - Fork 0
/
methview.go
1012 lines (922 loc) · 30.3 KB
/
methview.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"
"github.com/fatih/camelcase"
"github.com/goki/gi/gi"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/key"
"github.com/goki/ki/bitflag"
"github.com/goki/ki/ki"
"github.com/goki/ki/kit"
)
// these are special menus that we ignore
var specialMenus = map[string]struct{}{
"AppMenu": {}, "Copy Cut Paste": {}, "Copy Cut Paste Dupe": {}, "Windows": {},
}
// MainMenuView configures the given MenuBar according to the "MainMenu"
// properties registered on the type for given value element, through the
// kit.AddType method. See https://github.com/goki/gi/wiki/Views for full
// details on formats and options for configuring the menu. Returns false if
// there is no main menu defined for this type, or on errors (which are
// programmer errors sent to log).
// gopy:interface=handle
func MainMenuView(val interface{}, win *gi.Window, mbar *gi.MenuBar) bool {
tpp, vtyp, ok := MethViewTypeProps(val)
if !ok {
return false
}
mp, ok := ki.SliceTypeProps(tpp, "MainMenu")
if !ok {
return false
}
if win == nil {
return false
}
if mbar == nil {
mbar = win.AddMainMenu()
}
mnms := make([]string, len(mp))
for mmi, mm := range mp {
if mm.Name == "AppMenu" {
mnms[mmi] = oswin.TheApp.Name()
} else {
mnms[mmi] = mm.Name
}
}
mbar.ConfigMenus(mnms)
rval := true
for mmi, mm := range mp {
ma := mbar.Child(mmi).(*gi.Action)
if mm.Name == "AppMenu" {
ma.Menu.AddAppMenu(win)
continue
}
if mm.Name == "Edit" {
if ms, ok := mm.Value.(string); ok {
if ms == "Copy Cut Paste" {
ma.Menu.AddCopyCutPaste(win)
} else if ms == "Copy Cut Paste Dupe" {
ma.Menu.AddCopyCutPasteDupe(win)
} else {
MethViewErr(vtyp, fmt.Sprintf("Unrecognized Edit menu special string: %v -- `Copy Cut Paste` is standard", ms))
}
continue
}
}
if mm.Name == "Window" {
if ms, ok := mm.Value.(string); ok {
if ms == "Windows" {
// automatic
} else {
MethViewErr(vtyp, fmt.Sprintf("Unrecognized Window menu special string: %v -- `Windows` is standard", ms))
}
continue
}
}
rv := ActionsView(val, vtyp, win.Viewport, ma, mm.Value)
if !rv {
rval = false
}
}
win.MainMenuUpdated()
return rval
}
// HasToolBarView returns true if given val has a ToolBar type property
// registered -- call this to check before then calling ToolBarView.
func HasToolBarView(val interface{}) bool {
tpp, _, ok := MethViewTypeProps(val)
if !ok {
return false
}
_, ok = ki.SliceTypeProps(tpp, "ToolBar")
if !ok {
return false
}
return true
}
// ToolBarView configures ToolBar according to the "ToolBar" properties
// registered on the type for given value element, through the kit.AddType
// method. See https://github.com/goki/gi/wiki/Views for full details on
// formats and options for configuring the menu. Returns false if there is no
// toolbar defined for this type, or on errors (which are programmer errors
// sent to log).
func ToolBarView(val interface{}, vp *gi.Viewport2D, tb *gi.ToolBar) bool {
tpp, vtyp, ok := MethViewTypeProps(val)
if !ok {
return false
}
tp, ok := ki.SliceTypeProps(tpp, "ToolBar")
if !ok {
return false
}
if vp == nil {
vp = tb.ParentViewport()
if vp == nil {
MethViewErr(vtyp, "Viewport is nil in ToolBarView config -- must set viewport in widget prior to calling this method!")
}
return false
}
rval := true
for _, te := range tp {
if strings.HasPrefix(te.Name, "sep-") {
sep := tb.AddNewChild(gi.KiT_Separator, te.Name).(*gi.Separator)
sep.Horiz = false
continue
}
var ac *gi.Action
if aci := tb.ChildByName(te.Name, 0); aci != nil { // allows overriding of defaults etc
ac = aci.(*gi.Action)
// fmt.Printf("ToolBar action override: %v\n", ac.Nm)
ac.ActionSig.DisconnectAll()
} else {
ac = tb.AddNewChild(gi.KiT_Action, te.Name).(*gi.Action)
}
rv := ActionsView(val, vtyp, vp, ac, te.Value)
if !rv {
rval = false
}
}
return rval
}
// CtxtMenuView configures a popup context menu according to the "CtxtMenu"
// properties registered on the type for given value element, through the
// kit.AddType method. See https://github.com/goki/gi/wiki/Views for full
// details on formats and options for configuring the menu. It looks first
// for "CtxtMenuActive" or "CtxtMenuInactive" depending on inactive flag
// (which applies to the gui view), so you can have different menus in those
// cases, and then falls back on "CtxtMenu". Returns false if there is no
// context menu defined for this type, or on errors (which are programmer
// errors sent to log).
func CtxtMenuView(val interface{}, inactive bool, vp *gi.Viewport2D, menu *gi.Menu) bool {
tpp, vtyp, ok := MethViewTypeProps(val)
if !ok {
return false
}
var tp ki.PropSlice
got := false
if inactive {
tp, got = ki.SliceTypeProps(tpp, "CtxtMenuInactive")
} else {
tp, got = ki.SliceTypeProps(tpp, "CtxtMenuActive")
}
if !got {
tp, got = ki.SliceTypeProps(tpp, "CtxtMenu")
}
if !got {
return false
}
if vp == nil {
MethViewErr(vtyp, "Viewport is nil in CtxtMenuView config -- must set viewport in widget prior to calling this method!")
return false
}
rval := true
for _, te := range tp {
if strings.HasPrefix(te.Name, "sep-") {
menu.AddSeparator(te.Name)
continue
}
ac := menu.AddAction(gi.ActOpts{Label: te.Name}, nil, nil)
rv := ActionsView(val, vtyp, vp, ac, te.Value)
if !rv {
rval = false
}
}
return rval
}
//////////////////////////////////////////////////////////////////////////////////
// CallMethod -- auto gui
// CallMethod calls given method on given object val, using GUI interface to
// prompt for args. This only works for methods that have been configured
// either on the CallMethods list or any of the ToolBar, MainMenu, or CtxtMenu
// lists (in that order). List of available methods is cached in type
// properties after first call.
// gopy:interface=handle
func CallMethod(val interface{}, method string, vp *gi.Viewport2D) bool {
tpp, vtyp, ok := MethViewTypeProps(val)
if !ok {
MethViewErr(vtyp, fmt.Sprintf("Type: %v properties not found for CallMethod -- need to register type using kit.AddType\n", vtyp.String()))
return false
}
cmp, ok := ki.SubTypeProps(tpp, MethodViewCallMethsProp)
if !ok {
cmp = MethViewCompileMeths(val, vp)
}
acp, has := cmp[method]
if !has {
MethViewErr(vtyp, fmt.Sprintf("Method: %v not found among all different methods registered on type properties -- add to CallMethods to make available for CallMethod\n", method))
return false
}
ac, ok := acp.(*gi.Action)
if !ok {
MethViewErr(vtyp, fmt.Sprintf("Method: %v not a gi.Action -- should be!\n", method))
return false
}
MethViewSetActionData(ac, val, vp)
ac.Trigger()
return true
}
// MethViewSetActionData sets the MethViewData associated with the given action
// with values updated from the given val and viewport
func MethViewSetActionData(ac *gi.Action, val interface{}, vp *gi.Viewport2D) {
if ac.Data == nil {
fmt.Printf("giv.MethView no MethViewData on action: %v\n", ac.Nm)
return
}
md := ac.Data.(*MethViewData)
md.Val = val
md.ValVal = reflect.ValueOf(val)
md.Vp = vp
md.MethVal = md.ValVal.MethodByName(md.Method)
if len(ac.ActionSig.Cons) == 0 {
fmt.Printf("giv.MethView CallMethod had no connections: %v\n", ac.Nm)
ac.ActionSig.Connect(vp.This(), MethViewCall)
}
}
var compileMethsOrder = []string{"CallMethods", "ToolBar", "MainMenu", "CtxtMenuActive", "CtxtMenu", "CtxtMenuInactive"}
// MethViewCompileMeths gets all methods either on the CallMethods list or any
// of the ToolBar, MainMenu, or CtxtMenu lists (in that order). Returns
// property list of them, which are just names -> Actions
func MethViewCompileMeths(val interface{}, vp *gi.Viewport2D) ki.Props {
tpp, vtyp, ok := MethViewTypeProps(val)
if !ok {
return nil
}
var cmp ki.Props = make(ki.Props)
for _, lst := range compileMethsOrder {
tp, got := ki.SliceTypeProps(tpp, lst)
if !got {
continue
}
MethViewCompileActions(cmp, val, vtyp, vp, "", tp)
}
kit.SetTypeProp(tpp, MethodViewCallMethsProp, cmp)
return cmp
}
// MethViewCompileActions processes properties for parent action pa for
// overall object val of given type -- could have a sub-menu of further
// actions or might just be a single action
func MethViewCompileActions(cmp ki.Props, val interface{}, vtyp reflect.Type, vp *gi.Viewport2D, pnm string, pp interface{}) bool {
rval := true
if pv, ok := pp.(ki.PropSlice); ok {
for _, mm := range pv {
_, isspec := specialMenus[mm.Name]
if strings.HasPrefix(mm.Name, "sep-") || isspec {
continue
} else {
rv := MethViewCompileActions(cmp, val, vtyp, vp, mm.Name, mm.Value)
if !rv {
rval = false
}
}
}
} else {
_, isspec := specialMenus[pnm]
if strings.HasPrefix(pnm, "sep-") || isspec {
return rval
}
if _, has := cmp[pnm]; has {
return rval
}
ac := &gi.Action{}
ac.InitName(ac, pnm)
ac.Text = strings.Replace(strings.Join(camelcase.Split(ac.Nm), " "), " ", " ", -1)
cmp[pnm] = ac
rv := false
switch pv := pp.(type) {
case ki.BlankProp:
rv = ActionView(val, vtyp, vp, ac, nil)
case ki.Props:
rv = ActionView(val, vtyp, vp, ac, pv)
}
if !rv {
rval = false
}
}
return rval
}
//////////////////////////////////////////////////////////////////////////////////
// Utils
// MethViewErr is error logging function for MethView system, showing the type info
func MethViewErr(vtyp reflect.Type, msg string) {
if vtyp != nil {
log.Printf("giv.MethodView for type: %v: debug error: %v\n", vtyp.String(), msg)
} else {
log.Printf("giv.MethodView debug error: %v\n", msg)
}
}
// MethViewTypeProps gets props, typ of val, returns false if not found or
// other err
func MethViewTypeProps(val interface{}) (ki.Props, reflect.Type, bool) {
if kit.IfaceIsNil(val) {
return nil, nil, false
}
vtyp := reflect.TypeOf(val)
tpp := kit.Types.Properties(kit.NonPtrType(vtyp), false)
if tpp == nil {
return nil, vtyp, false
}
return *tpp, vtyp, true
}
// HasMainMenuView returns true if given val has a MainMenu type property
// registered -- call this to check before then calling MainMenuView
func HasMainMenuView(val interface{}) bool {
tpp, _, ok := MethViewTypeProps(val)
if !ok {
return false
}
_, ok = ki.SliceTypeProps(tpp, "MainMenu")
if !ok {
return false
}
return true
}
// MethViewNoUpdateAfterProp returns true if given val has a top-level "MethViewNoUpdateAfter"
// type property registered -- some types generically want that and it is much easier to
// just specify once instead of every time..
func MethViewNoUpdateAfterProp(val interface{}) bool {
tpp, _, ok := MethViewTypeProps(val)
if !ok {
return false
}
_, nua := kit.TypeProp(tpp, "MethViewNoUpdateAfter")
return nua
}
// This is the name of the property that holds cached map of compiled callable methods
var MethodViewCallMethsProp = "__MethViewCallMeths"
//////////////////////////////////////////////////////////////////////////////////
// ActionsView
// ActionsView processes properties for parent action pa for overall object
// val of given type -- could have a sub-menu of further actions or might just
// be a single action
func ActionsView(val interface{}, vtyp reflect.Type, vp *gi.Viewport2D, pa *gi.Action, pp interface{}) bool {
pa.Text = strings.Replace(strings.Join(camelcase.Split(pa.Nm), " "), " ", " ", -1)
rval := true
switch pv := pp.(type) {
case ki.PropSlice:
for _, mm := range pv {
if strings.HasPrefix(mm.Name, "sep-") {
pa.Menu.AddSeparator(mm.Name)
} else {
nac := &gi.Action{}
nac.InitName(nac, mm.Name)
nac.SetAsMenu()
pa.Menu = append(pa.Menu, nac.This().(gi.Node2D))
rv := ActionsView(val, vtyp, vp, nac, mm.Value)
if !rv {
rval = false
}
}
}
case ki.BlankProp:
rv := ActionView(val, vtyp, vp, pa, nil)
if !rv {
rval = false
}
case ki.Props:
rv := ActionView(val, vtyp, vp, pa, pv)
if !rv {
rval = false
}
}
return rval
}
// ActionView configures given action with given props
func ActionView(val interface{}, vtyp reflect.Type, vp *gi.Viewport2D, ac *gi.Action, props ki.Props) bool {
// special action names
switch ac.Nm {
case "Close Window":
ac.Shortcut = gi.ShortcutForFun(gi.KeyFunWinClose)
ac.ActionSig.Connect(vp.Win.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
vp.Win.CloseReq()
})
return true
}
// other special cases based on props
nometh := false // set to true if doesn't have an actual method to call, e.g., keyfun
for pk := range props {
switch pk {
case "keyfun":
nometh = true
}
}
methNm := ac.Nm
methTyp, hasmeth := vtyp.MethodByName(methNm)
if !nometh && !hasmeth {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v -- not found in type", methNm))
return false
}
valval := reflect.ValueOf(val)
methVal := valval.MethodByName(methNm)
if !nometh && (kit.ValueIsZero(methVal) || methVal.IsNil()) {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v -- method value not valid", methNm))
return false
}
rval := true
md := &MethViewData{Val: val, ValVal: valval, Vp: vp, Method: methNm, MethVal: methVal, MethTyp: methTyp}
ac.Data = md
if MethViewNoUpdateAfterProp(val) {
bitflag.Set32((*int32)(&md.Flags), int(MethViewNoUpdateAfter))
}
if props == nil {
ac.ActionSig.Connect(vp.This(), MethViewCall)
return true
}
for pk, pv := range props {
switch pk {
case "shortcut":
if kf, ok := pv.(gi.KeyFuns); ok {
ac.Shortcut = gi.ShortcutForFun(kf)
} else {
ac.Shortcut = key.Chord(kit.ToString(pv)).OSShortcut()
}
case "shortcut-func":
if sf, ok := pv.(ShortcutFunc); ok {
ac.Shortcut = sf(md.Val, ac)
} else if sf, ok := pv.(func(it interface{}, act *gi.Action) key.Chord); ok {
ac.Shortcut = sf(md.Val, ac)
} else {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v, shortcut-func must be of type ShortcutFunc", methNm))
}
case "keyfun":
if kf, ok := pv.(gi.KeyFuns); ok {
ac.Shortcut = gi.ShortcutForFun(kf)
md.KeyFun = kf
bitflag.Set32((*int32)(&md.Flags), int(MethViewKeyFun))
}
case "label":
ac.Text = kit.ToString(pv)
case "label-func":
if sf, ok := pv.(LabelFunc); ok {
str := sf(md.Val, ac)
ac.Text = str
} else if sf, ok := pv.(func(it interface{}, act *gi.Action) string); ok {
ac.Text = sf(md.Val, ac)
} else {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v, label-func must be of type LabelFunc", methNm))
}
case "icon":
ac.Icon = gi.IconName(kit.ToString(pv))
case "desc":
md.Desc = kit.ToString(pv)
ac.Tooltip = md.Desc
case "confirm":
bitflag.Set32((*int32)(&md.Flags), int(MethViewConfirm))
case "show-return":
bitflag.Set32((*int32)(&md.Flags), int(MethViewShowReturn))
case "no-update-after":
bitflag.Set32((*int32)(&md.Flags), int(MethViewNoUpdateAfter))
case "update-after": // if MethViewNoUpdateAfterProp was set above
bitflag.Clear32((*int32)(&md.Flags), int(MethViewNoUpdateAfter))
case "updtfunc":
if uf, ok := pv.(ActionUpdateFunc); ok {
md.UpdateFunc = uf
ac.UpdateFunc = MethViewUpdateFunc
} else if uf, ok := pv.(func(it interface{}, act *gi.Action)); ok {
md.UpdateFunc = ActionUpdateFunc(uf)
ac.UpdateFunc = MethViewUpdateFunc
} else {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v, updtfunc must be of type ActionUpdateFunc", methNm))
}
case "submenu":
ac.MakeMenuFunc = MethViewSubMenuFunc
if pvs, ok := pv.(string); ok { // field name
md.SubMenuField = pvs
} else {
md.SubMenuSlice = pv
}
bitflag.Set32((*int32)(&md.Flags), int(MethViewHasSubMenu))
case "submenu-func":
if sf, ok := pv.(SubMenuFunc); ok {
ac.MakeMenuFunc = MethViewSubMenuFunc
md.SubMenuFunc = sf
bitflag.Set32((*int32)(&md.Flags), int(MethViewHasSubMenu))
} else if sf, ok := pv.(func(it interface{}, vp *gi.Viewport2D) []string); ok {
ac.MakeMenuFunc = MethViewSubMenuFunc
md.SubMenuFunc = SubMenuFunc(sf)
bitflag.Set32((*int32)(&md.Flags), int(MethViewHasSubMenu))
} else {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v, submenu-func must be of type SubMenuFunc", methNm))
}
case "Args":
argv, ok := pv.(ki.PropSlice)
if !ok {
MethViewErr(vtyp, fmt.Sprintf("ActionView for Method: %v, Args property must be of type ki.PropSlice, containing names and other properties for each arg", methNm))
rval = false
} else {
if ActionViewArgsValidate(md, vtyp, methTyp, argv) {
md.ArgProps = argv
} else {
rval = false
}
}
}
}
if !rval {
return false
}
if !bitflag.Has32((int32)(md.Flags), int(MethViewHasSubMenu)) {
ac.ActionSig.Connect(vp.This(), MethViewCall)
}
return true
}
// ActionViewArgsValidate validates the Args properties relative to number of args on type
func ActionViewArgsValidate(md *MethViewData, vtyp reflect.Type, meth reflect.Method, argprops ki.PropSlice) bool {
mtyp := meth.Type
narg := mtyp.NumIn()
apsz := len(argprops)
if narg-1 != apsz {
MethViewErr(vtyp, fmt.Sprintf("Method: %v takes %v args (beyond the receiver), but Args properties only has %v", meth.Name, narg-1, apsz))
return false
}
if bitflag.Has32((int32)(md.Flags), int(MethViewHasSubMenu)) && apsz != 1 {
MethViewErr(vtyp, fmt.Sprintf("Method: %v has a submenu of values to use as the one arg for it, but it takes %v args (beyond the receiver) -- should only take 1", meth.Name, narg-1))
return false
}
return true
}
//////////////////////////////////////////////////////////////////////////////////
// Method Callbacks -- called when Action fires
// MethViewFlags define bitflags for method view action options
type MethViewFlags int32
const (
// MethViewConfirm confirms action before proceeding
MethViewConfirm MethViewFlags = iota
// MethViewShowReturn shows the return value from the method
MethViewShowReturn
// MethViewNoUpdateAfter means do not update window after method runs (default is to do so)
MethViewNoUpdateAfter
// MethViewHasSubMenu means that this action has a submenu option --
// argument values will be selected from the auto-generated submenu
MethViewHasSubMenu
// MethViewHasSubMenuVal means that this action was called using a submenu
// and the SubMenuVal has the selected value
MethViewHasSubMenuVal
// MethViewKeyFun means this action's only function is to emit the key fun
MethViewKeyFun
MethViewFlagsN
)
//go:generate stringer -type=MethViewFlags
var KiT_MethViewFlags = kit.Enums.AddEnumAltLower(MethViewFlagsN, kit.BitFlag, nil, "MethView")
// SubMenuFunc is a function that returns a string slice of submenu items
// used in MethView submenu-func option
// first argument is the object on which the method is defined (receiver)
type SubMenuFunc func(it interface{}, vp *gi.Viewport2D) []string
// ShortcutFunc is a function that returns a key.Chord string for a shortcut
// used in MethView shortcut-func option
// first argument is the object on which the method is defined (receiver)
type ShortcutFunc func(it interface{}, act *gi.Action) key.Chord
// LabelFunc is a function that returns a string to set a label
// first argument is the object on which the method is defined (receiver)
type LabelFunc func(it interface{}, act *gi.Action) string
// ActionUpdateFunc is a function that updates method active / inactive status
// first argument is the object on which the method is defined (receiver)
type ActionUpdateFunc func(it interface{}, act *gi.Action)
// MethViewData is set to the Action.Data field for all MethView actions,
// containing info needed to actually call the Method on value Val.
type MethViewData struct {
Val interface{}
ValVal reflect.Value
Vp *gi.Viewport2D
Method string
MethVal reflect.Value
MethTyp reflect.Method
ArgProps ki.PropSlice `desc:"names and other properties of args, in one-to-one with method args"`
SpecProps ki.Props `desc:"props for special action types, e.g., FileView"`
Desc string `desc:"prompt shown in arg dialog or confirm prompt dialog"`
UpdateFunc ActionUpdateFunc `desc:"update function defined in properties -- called by our wrapper update function"`
SubMenuSlice interface{} `desc:"value for submenu generation as a literal slice of items of appropriate type for method being called"`
SubMenuField string `desc:"value for submenu generation as name of field on obj"`
SubMenuFunc SubMenuFunc `desc:"function that will generate submenu items, as []string slice"`
SubMenuVal interface{} `desc:"value that the user selected from submenu for this action -- this should be assigned to the first (only) arg of the method"`
KeyFun gi.KeyFuns `desc:"key function that we emit, if MethViewKeyFun type"`
Flags MethViewFlags
}
func (md *MethViewData) MethName() string {
typnm := kit.ShortTypeName(md.ValVal.Type())
methnm := typnm + ":" + md.Method
return methnm
}
// MethViewCall is the receiver func for MethView actions that call a method
// -- it uses the MethViewData to call the target method.
func MethViewCall(recv, send ki.Ki, sig int64, data interface{}) {
ac := send.(*gi.Action)
md := ac.Data.(*MethViewData)
if md.ArgProps == nil { // no args -- just call
MethViewCallNoArgPrompt(ac, md, nil)
return
}
// need to prompt for args
ads, args, nprompt, ok := MethViewArgData(md)
if !ok {
return
}
if nprompt == 0 {
MethViewCallNoArgPrompt(ac, md, args)
return
}
// check for single arg with action -- do action directly
if len(ads) == 1 {
ad := &ads[0]
if ad.Desc == "" {
ad.Desc = md.Desc
}
if ad.Desc != "" {
ad.View.SetTag("desc", ad.Desc)
}
if ad.View.HasAction() {
ad.View.Activate(md.Vp, ad.View, func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
MethViewCallMeth(md, args)
}
})
return
}
}
ArgViewDialog(md.Vp, ads, DlgOpts{Title: ac.Text, Prompt: md.Desc},
md.Vp.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
// ddlg := send.Embed(gi.KiT_Dialog).(*gi.Dialog)
MethViewCallMeth(md, args)
}
})
}
// MethViewCallNoArgPrompt calls the method in case where there is no
// prompting otherwise of the user for arg values -- checks for Confirm case
// or otherwise directly calls method
func MethViewCallNoArgPrompt(ac *gi.Action, md *MethViewData, args []reflect.Value) {
if bitflag.Has32(int32(md.Flags), int(MethViewKeyFun)) {
if md.Vp != nil && md.Vp.Win != nil {
md.Vp.Win.EventMgr.SendKeyFunEvent(md.KeyFun, false)
}
return
}
if bitflag.Has32(int32(md.Flags), int(MethViewConfirm)) {
gi.PromptDialog(md.Vp, gi.DlgOpts{Title: ac.Text, Prompt: md.Desc}, gi.AddOk, gi.AddCancel,
md.Vp.This(), func(recv, send ki.Ki, sig int64, data interface{}) {
if sig == int64(gi.DialogAccepted) {
MethViewCallMeth(md, args)
}
})
} else {
MethViewCallMeth(md, args)
}
}
// MethViewCallMeth calls the method with given args, and processes the
// results as specified in the MethViewData.
func MethViewCallMeth(md *MethViewData, args []reflect.Value) {
rv := md.MethVal.Call(args)
methnm := md.MethName()
mtyp := md.MethTyp.Type
narg := mtyp.NumIn() - 1
for ai := 0; ai < narg; ai++ {
ap := md.ArgProps[ai]
argnm := methnm + "." + ap.Name
MethArgHist[argnm] = args[ai].Interface()
}
if !bitflag.Has32(int32(md.Flags), int(MethViewNoUpdateAfter)) {
md.Vp.SetNeedsFullRender() // always update after all methods -- almost always want that
}
if bitflag.Has32(int32(md.Flags), int(MethViewShowReturn)) {
if len(rv) >= 1 {
MethViewShowValue(md.Vp, rv[0], md.Method+" Result", "")
}
}
}
// MethViewShowValue displays a value in a dialog window (e.g., for MethViewShowReturn)
func MethViewShowValue(vp *gi.Viewport2D, val reflect.Value, title, prompt string) {
if kit.ValueIsZero(val) {
return
}
npv := kit.NonPtrValue(val)
if kit.ValueIsZero(npv) {
return
}
tk := npv.Type().Kind()
switch tk {
case reflect.Struct:
StructViewDialog(vp, val.Interface(), DlgOpts{Title: title, Prompt: prompt, Ok: true, Cancel: true}, nil, nil)
case reflect.Slice:
if bs, ok := npv.Interface().([]byte); ok {
TextViewDialog(vp, bs, DlgOpts{Title: title, Prompt: prompt, Ok: true})
} else if bs, ok := val.Interface().([]byte); ok {
TextViewDialog(vp, bs, DlgOpts{Title: title, Prompt: prompt, Ok: true})
} else {
SliceViewDialog(vp, val.Interface(), DlgOpts{Title: title, Prompt: prompt, Ok: true, Cancel: true}, nil, nil, nil)
}
case reflect.Map:
MapViewDialog(vp, val.Interface(), DlgOpts{Title: title, Prompt: prompt, Ok: true, Cancel: true}, nil, nil)
default:
TextViewDialog(vp, []byte(npv.String()), DlgOpts{Title: title, Prompt: prompt, Ok: true})
}
}
// ArgData contains the relevant data for each arg, including the
// reflect.Value, name, optional description, and default value
type ArgData struct {
Val reflect.Value
Name string
Desc string
View ValueView
Default interface{}
Flags ArgDataFlags
}
// ArgDataFlags define bitflags for method view action options
type ArgDataFlags int32
const (
// ArgDataHasDef means that there was a Default value set
ArgDataHasDef ArgDataFlags = iota
// ArgDataValSet means that there is a fixed value for this arg, given in
// the config props and set in the Default, so it does not need to be
// prompted for
ArgDataValSet
ArgDataFlagsN
)
//go:generate stringer -type=ArgDataFlags
var KiT_ArgDataFlags = kit.Enums.AddEnumAltLower(ArgDataFlagsN, kit.BitFlag, nil, "ArgData")
func (ad *ArgData) HasDef() bool {
return bitflag.Has32(int32(ad.Flags), int(ArgDataHasDef))
}
func (ad *ArgData) SetHasDef() {
bitflag.Set32((*int32)(&ad.Flags), int(ArgDataHasDef))
}
func (ad *ArgData) HasValSet() bool {
return bitflag.Has32(int32(ad.Flags), int(ArgDataValSet))
}
// MethArgHist stores the history of method arg values -- used for setting defaults
// for next time the method is called. Key is type:method name
var MethArgHist = map[string]interface{}{}
// MethViewArgData gets the arg data for the method args, returns false if
// errors -- nprompt is the number of args that require prompting from the
// user (minus any cases with value: set directly)
func MethViewArgData(md *MethViewData) (ads []ArgData, args []reflect.Value, nprompt int, ok bool) {
mtyp := md.MethTyp.Type
narg := mtyp.NumIn() - 1
ads = make([]ArgData, narg)
args = make([]reflect.Value, narg)
nprompt = 0
ok = true
methnm := md.MethName()
for ai := 0; ai < narg; ai++ {
ad := &ads[ai]
atyp := mtyp.In(1 + ai)
av := reflect.New(atyp)
ad.Val = av
args[ai] = av.Elem()
aps := &md.ArgProps[ai]
ad.Name = aps.Name
argnm := methnm + "." + ad.Name
if def, has := MethArgHist[argnm]; has {
ad.Default = def
ad.SetHasDef()
}
ad.View = ToValueView(ad.Val.Interface(), "")
ad.View.SetSoloValue(ad.Val)
ad.View.SetName(ad.Name)
nprompt++ // assume prompt
switch apv := aps.Value.(type) {
case ki.BlankProp:
case ki.Props:
for pk, pv := range apv {
switch pk {
case "desc":
ad.Desc = kit.ToString(pv)
ad.View.SetTag("desc", ad.Desc)
case "default":
ad.Default = pv
ad.SetHasDef()
case "value":
ad.Default = pv
ad.SetHasDef()
bitflag.Set32((*int32)(&ad.Flags), int(ArgDataValSet))
nprompt--
case "default-field":
field := pv.(string)
if flv, ok := MethViewFieldValue(md.ValVal, field); ok {
ad.Default = flv.Interface()
ad.SetHasDef()
}
default:
ad.View.SetTag(pk, kit.ToString(pv))
}
}
}
if bitflag.Has32((int32)(md.Flags), int(MethViewHasSubMenuVal)) {
ad.Default = md.SubMenuVal
ad.SetHasDef()
bitflag.Set32((*int32)(&ad.Flags), int(ArgDataValSet))
nprompt--
}
if ad.HasDef() {
ad.View.SetValue(ad.Default)
}
}
return
}
// MethViewArgDefaultVal returns the default value of the given argument index
func MethViewArgDefaultVal(md *MethViewData, ai int) (interface{}, bool) {
aps := &md.ArgProps[ai]
var def interface{}
got := false
switch apv := aps.Value.(type) {
case ki.BlankProp:
case ki.Props:
for pk, pv := range apv {
switch pk {
case "default":
def = pv
got = true
case "value":
def = pv
got = true
case "default-field":
field := pv.(string)
if flv, ok := MethViewFieldValue(md.ValVal, field); ok {
def = flv.Interface()
got = true
}
}
}
}
return def, got
}
// MethViewFieldValue returns a reflect.Value for the given field name,
// checking safely (false if not found)
func MethViewFieldValue(vval reflect.Value, field string) (*reflect.Value, bool) {
fv, ok := kit.FieldValueByPath(kit.NonPtrValue(vval).Interface(), field)
if !ok {
log.Printf("giv.MethViewFieldValue: Could not find field %v in type: %v\n", field, vval.Type().String())
return nil, false
}
return &fv, true
}
// MethViewUpdateFunc is general Action.UpdateFunc that then calls any
// MethViewData.UpdateFunc from its data
func MethViewUpdateFunc(act *gi.Action) {
md := act.Data.(*MethViewData)
if md.UpdateFunc != nil && md.Val != nil {
md.UpdateFunc(md.Val, act)
}
}
// MethViewSubMenuFunc is a MakeMenuFunc for items that have submenus
func MethViewSubMenuFunc(aki ki.Ki, m *gi.Menu) {
ac := aki.(*gi.Action)
md := ac.Data.(*MethViewData)
smd := md.SubMenuSlice
if md.SubMenuFunc != nil {
smd = md.SubMenuFunc(md.Val, md.Vp)
} else if md.SubMenuField != "" {
if flv, ok := MethViewFieldValue(md.ValVal, md.SubMenuField); ok {
smd = flv.Interface()
}
}
if smd == nil {
return
}
sltp := kit.NonPtrType(reflect.TypeOf(smd))
if sltp.Kind() != reflect.Slice && sltp.Kind() != reflect.Array {
log.Printf("giv.MethViewSubMenuFunc: submenu data must be a slice or array, not: %v\n", sltp.String())
return
}
def, gotDef := MethViewArgDefaultVal(md, 0) // assume first
defstr := ""
if gotDef {
defstr = kit.ToString(def)
}
mv := reflect.ValueOf(smd)
mvnp := kit.NonPtrValue(mv)
sz := mvnp.Len()
*m = make(gi.Menu, sz)
for i := 0; i < sz; i++ {
val := mvnp.Index(i)
vi := val.Interface()
nm := kit.ToString(val)
if nm == gi.MenuTextSeparator {
sp := &gi.Separator{}
sp.InitName(sp, "sep")
sp.Horiz = true
(*m)[i] = sp
continue
}
nac := &gi.Action{}
nac.InitName(nac, nm)
nac.Text = nm
nac.SetAsMenu()
nac.ActionSig.Connect(md.Vp.This(), MethViewCall)
nd := *md // copy