/
funcbutton.go
490 lines (448 loc) · 14.9 KB
/
funcbutton.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
// Copyright (c) 2023, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package views
import (
"log/slog"
"reflect"
"strconv"
"strings"
"unicode"
"cogentcore.org/core/abilities"
"cogentcore.org/core/core"
"cogentcore.org/core/cursors"
"cogentcore.org/core/events"
"cogentcore.org/core/icons"
"cogentcore.org/core/keymap"
"cogentcore.org/core/reflectx"
"cogentcore.org/core/strcase"
"cogentcore.org/core/styles"
"cogentcore.org/core/tree"
"cogentcore.org/core/types"
)
// CallFunc calls the given function in the context of the given widget,
// popping up a dialog to prompt for any arguments and show the return
// values of the function. It is a helper function that uses [NewSoloFuncButton]
// under the hood.
func CallFunc(ctx core.Widget, fun any) {
NewSoloFuncButton(ctx, fun).CallFunc()
}
// NewSoloFuncButton returns a standalone FuncButton with the given context
// for popping up any dialog elements.
func NewSoloFuncButton(ctx core.Widget, fun any) *FuncButton {
parent := tree.NewRoot[*core.WidgetBase]("solo-func-button-parent")
fb := NewFuncButton(parent, fun)
fb.SetContext(ctx)
return fb
}
// FuncButton is a button that is set up to call a function when it
// is pressed, using a dialog to prompt the user for any arguments.
// Also, it automatically sets various properties of the button like
// the name, text, tooltip, and icon based on the properties of the
// function, using [reflect] and [types]. The function must be registered
// with [types] to get documentation information, but that is not
// required; add a `//types:add` comment directive and run `core generate`
// if you want tooltips. If the function is a method, both the method and
// its receiver type must be added to [types] to get documentation.
type FuncButton struct { //core:no-new
core.Button
// Func is the [types.Func] associated with this button.
// This function can also be a method, but it must be
// converted to a [types.Func] first. It should typically
// be set using [FuncButton.SetFunc].
Func *types.Func `set:"-"`
// ReflectFunc is the [reflect.Value] of the function or
// method associated with this button. It should typically
// bet set using [FuncButton.SetFunc].
ReflectFunc reflect.Value `set:"-"`
// Args are the [Value] objects associated with the
// arguments of the function. They are automatically set in
// [SetFunc], but they can be customized to configure
// default values and other options.
Args []Value `set:"-"`
// Returns are the [Value] objects associated with the
// return values of the function. They are automatically
// set in [SetFunc], but they can be customized to configure
// default values and other options. The [reflect.Value]s of
// the [Value] objects are not set until the function is
// called, and are thus not typically applicable to access.
Returns []Value `set:"-"`
// Confirm is whether to prompt the user for confirmation
// before calling the function.
Confirm bool
// ShowReturn is whether to display the return values of
// the function (and a success message if there are none).
// The way that the return values are shown is determined
// by ShowReturnAsDialog. Non-nil error return values will
// always be shown, even if ShowReturn is set to false.
ShowReturn bool
// ShowReturnAsDialog, if and only if ShowReturn is true,
// indicates to show the return values of the function in
// a dialog, instead of in a snackbar, as they are by default.
// If there are multiple return values from the function, or if
// one of them is a complex type (pointer, struct, slice,
// array, map), then ShowReturnAsDialog will
// automatically be set to true.
ShowReturnAsDialog bool
// NewWindow makes the ReturnDialog a NewWindow dialog
// (if supported by platform).
NewWindow bool
// WarnUnadded is whether to log warnings when a function that
// has not been added to [types] is used. It is on by default and
// must be set before [FuncButton.SetFunc] is called for it to
// have any effect.
WarnUnadded bool `default:"true"`
// Context is used for opening Dialogs if non-nil.
Context core.Widget
// AfterFunc is an optional function called after the func button
// function is executed
AfterFunc func()
}
// NewFuncButton adds a new [FuncButton] with the given function
// to the given parent.
func NewFuncButton(parent tree.Node, fun any) *FuncButton {
return parent.NewChild(FuncButtonType).(*FuncButton).SetFunc(fun)
}
func (fb *FuncButton) OnInit() {
fb.Button.OnInit()
fb.WarnUnadded = true
fb.Style(func(s *styles.Style) {
if fb.IsReadOnly() {
s.SetAbilities(false, abilities.Hoverable, abilities.Clickable, abilities.Activatable)
s.Cursor = cursors.None
}
})
fb.OnClick(func(e events.Event) {
if !fb.IsReadOnly() {
fb.CallFunc()
}
})
}
// SetText sets the [FuncButton.Text] and updates the tooltip to correspond to the new name.
func (fb *FuncButton) SetText(v string) *FuncButton {
ptext := fb.Text
fb.Text = v
if fb.Func != nil && fb.Text != ptext && ptext != "" {
fb.Func.Doc = types.FormatDoc(fb.Func.Doc, ptext, fb.Text)
fb.SetTooltip(fb.Func.Doc)
}
return fb
}
// SetFunc sets the function associated with the FuncButton to the
// given function or method value. For documentation information for
// the function to be obtained, it must be added to [types]. SetFunc is
// automatically called by [NewFuncButton].
func (fb *FuncButton) SetFunc(fun any) *FuncButton {
fnm := types.FuncName(fun)
// the "-fm" suffix indicates that it is a method
if strings.HasSuffix(fnm, "-fm") {
fnm = strings.TrimSuffix(fnm, "-fm")
// the last dot separates the function name
li := strings.LastIndex(fnm, ".")
metnm := fnm[li+1:]
typnm := fnm[:li]
// get rid of any parentheses and pointer receivers
// that may surround the type name
typnm = strings.ReplaceAll(typnm, "(*", "")
typnm = strings.TrimSuffix(typnm, ")")
gtyp := types.TypeByName(typnm)
var met *types.Method
if gtyp == nil {
if fb.WarnUnadded {
slog.Warn("views.FuncButton.SetFunc called with a method whose receiver type has not been added to types", "function", fnm)
}
met = &types.Method{Name: metnm}
} else {
for _, m := range gtyp.Methods {
if m.Name == metnm {
met = &m
break
}
}
if met == nil {
if fb.WarnUnadded {
slog.Warn("views.FuncButton.SetFunc called with a method that has not been added to types (even though the receiver type was, you still need to add the method itself)", "function", fnm)
}
met = &types.Method{Name: metnm}
}
}
return fb.SetMethodImpl(met, reflect.ValueOf(fun))
}
if isAnonymousFunction(fnm) {
f := &types.Func{Name: fnm, Doc: "Anonymous function " + fnm}
return fb.SetFuncImpl(f, reflect.ValueOf(fun))
}
f := types.FuncByName(fnm)
if f == nil {
if fb.WarnUnadded {
slog.Warn("views.FuncButton.SetFunc called with a function that has not been added to types", "function", fnm)
}
f = &types.Func{Name: fnm}
}
return fb.SetFuncImpl(f, reflect.ValueOf(fun))
}
func isAnonymousFunction(fnm string) bool {
// FuncName.funcN indicates that a function was defined anonymously
return len(fnm) > 0 && unicode.IsDigit(rune(fnm[len(fnm)-1])) && strings.Contains(fnm, ".func")
}
// SetFuncImpl is the underlying implementation of [FuncButton.SetFunc].
// It should typically not be used by end-user code.
func (fb *FuncButton) SetFuncImpl(gfun *types.Func, rfun reflect.Value) *FuncButton {
fb.Func = gfun
fb.ReflectFunc = rfun
fb.SetArgs()
fb.SetReturns()
snm := fb.Func.Name
// get name without package
li := strings.LastIndex(snm, ".")
isAnonymous := isAnonymousFunction(snm)
if isAnonymous {
snm = strings.TrimRightFunc(snm, func(r rune) bool {
return unicode.IsDigit(r) || r == '.'
})
snm = strings.TrimSuffix(snm, ".func")
// we cut at the second to last period (we want to keep the
// receiver / package for anonymous functions)
li = strings.LastIndex(snm[:strings.LastIndex(snm, ".")], ".")
}
if li >= 0 {
snm = snm[li+1:] // must also get rid of "."
// if we are a global function, we may have gone too far with the second to last period,
// so we go after the last slash if there still is one
if strings.Contains(snm, "/") {
snm = snm[strings.LastIndex(snm, "/")+1:]
}
}
snm = strings.Map(func(r rune) rune {
if r == '(' || r == ')' || r == '*' {
return -1
}
return r
}, snm)
// func name is not guaranteed to make it unique so we ensure it is (-1 because [tree.New] adds 1 first)
fb.SetName(snm + "-" + strconv.FormatUint(fb.Parent().NumLifetimeChildren()-1, 10))
txt := strcase.ToSentence(snm)
fb.SetText(txt)
// doc formatting interferes with anonymous functions
if !isAnonymous {
fb.Func.Doc = types.FormatDoc(fb.Func.Doc, snm, txt)
}
fb.SetTooltip(fb.Func.Doc)
// we default to the icon with the same name as
// the function, if it exists
ic := icons.Icon(strcase.ToSnake(snm))
if ic.IsValid() {
fb.SetIcon(ic)
}
return fb
}
func (fb *FuncButton) GoodContext() core.Widget {
ctx := fb.Context
if fb.Context == nil {
if fb.This() == nil {
return nil
}
ctx = fb.This().(core.Widget)
}
return ctx
}
func (fb *FuncButton) CallFuncShowReturns() {
if fb.AfterFunc != nil {
defer fb.AfterFunc()
}
if len(fb.Args) == 0 {
rets := fb.ReflectFunc.Call(nil)
fb.ShowReturnsDialog(rets)
return
}
rargs := make([]reflect.Value, len(fb.Args))
for i, arg := range fb.Args {
rargs[i] = arg.Val().Elem()
}
rets := fb.ReflectFunc.Call(rargs)
fb.ShowReturnsDialog(rets)
}
// ConfirmDialog runs the confirm dialog
func (fb *FuncButton) ConfirmDialog() {
ctx := fb.GoodContext()
d := core.NewBody().AddTitle(fb.Text + "?").AddText("Are you sure you want to run " + fb.Text + "? " + fb.Tooltip)
d.AddBottomBar(func(parent core.Widget) {
d.AddCancel(parent)
d.AddOK(parent).SetText(fb.Text).OnClick(func(e events.Event) {
fb.CallFuncShowReturns()
})
})
d.NewDialog(ctx).Run()
}
// CallFunc calls the function or method associated with this button,
// prompting the user for any arguments.
func (fb *FuncButton) CallFunc() {
ctx := fb.GoodContext()
if len(fb.Args) == 0 {
if !fb.Confirm {
fb.CallFuncShowReturns()
return
}
fb.ConfirmDialog()
return
}
// go directly to the dialog if there is one
if len(fb.Args) == 1 && OpenDialog(fb.Args[0], ctx, func() {
fb.CallFuncShowReturns()
}, func() {
makeTmpWidget(fb.Args[0])
}) {
return
}
d := core.NewBody().AddTitle(fb.Text).AddText(fb.Tooltip)
NewArgView(d).SetArgs(fb.Args...)
d.AddBottomBar(func(parent core.Widget) {
d.AddCancel(parent)
d.AddOK(parent).SetText(fb.Text).OnClick(func(e events.Event) {
d.Scene.Send(events.Close, e) // note: the other Close event happens too late!
fb.CallFuncShowReturns()
})
})
d.NewDialog(ctx).Run()
}
// SetMethodImpl is the underlying implementation of [FuncButton.SetFunc] for methods.
// It should typically not be used by end-user code.
func (fb *FuncButton) SetMethodImpl(gmet *types.Method, rmet reflect.Value) *FuncButton {
return fb.SetFuncImpl(&types.Func{
Name: gmet.Name,
Doc: gmet.Doc,
Directives: gmet.Directives,
Args: gmet.Args,
Returns: gmet.Returns,
}, rmet)
}
// ShowReturnsDialog runs a dialog displaying the given function return
// values for the function associated with the function button. It does
// nothing if [FuncButton.ShowReturn] is dialog
func (fb *FuncButton) ShowReturnsDialog(rets []reflect.Value) {
if !fb.ShowReturn {
for _, ret := range rets {
if err, ok := ret.Interface().(error); ok && err != nil {
core.ErrorSnackbar(fb, err, fb.Text+" failed")
return
}
}
return
}
ctx := fb.GoodContext()
if ctx == nil {
return
}
fb.SetReturnValues(rets)
main := "Result of " + fb.Text
if len(rets) == 0 {
main = fb.Text + " succeeded"
}
if !fb.ShowReturnAsDialog {
txt := main
if len(fb.Returns) > 0 {
txt += ": "
for i, ret := range fb.Returns {
txt += reflectx.NonPointerValue(ret.Val()).String()
if i < len(fb.Returns)-1 {
txt += ", "
}
}
}
core.NewBody().AddSnackbarText(txt).NewSnackbar(ctx).Run()
return
}
// go directly to the dialog if there is one
if len(fb.Returns) == 1 && OpenDialog(fb.Returns[0], ctx, nil, func() {
makeTmpWidget(fb.Returns[0])
}) {
return
}
d := core.NewBody().AddTitle(main).AddText(fb.Tooltip).AddOKOnly()
NewArgView(d).SetArgs(fb.Returns...).SetReadOnly(true)
if fb.NewWindow {
d.NewDialog(ctx).SetNewWindow(true).Run()
} else {
d.NewDialog(ctx).Run()
}
}
// SetArgs sets the appropriate [Value] objects for the
// arguments of the function associated with the function button.
// It is called in [FuncButton.SetFunc] and should typically not
// be called by end-user code.
func (fb *FuncButton) SetArgs() {
narg := fb.ReflectFunc.Type().NumIn()
fb.Args = make([]Value, narg)
for i := range fb.Args {
atyp := fb.ReflectFunc.Type().In(i)
name := ""
doc := ""
if fb.Func.Args != nil && len(fb.Func.Args) > i {
name = fb.Func.Args[i]
} else {
name = reflectx.NonPointerType(atyp).Name()
}
label := strcase.ToSentence(name)
val := reflect.New(atyp)
view := ToValue(val.Interface(), "")
view.SetSoloValue(val)
view.SetName(name)
view.SetLabel(label)
view.SetDoc(doc)
fb.Args[i] = view
}
}
// SetReturns sets the appropriate [Value] objects for the
// return values of the function associated with the function
// button. It is called in [FuncButton.SetFunc] and should
// typically not be called by end-user code.
func (fb *FuncButton) SetReturns() {
nret := fb.ReflectFunc.Type().NumOut()
fb.Returns = make([]Value, nret)
hasComplex := false
for i := range fb.Returns {
rtyp := fb.ReflectFunc.Type().Out(i)
if !hasComplex {
k := rtyp.Kind()
if k == reflect.Pointer || k == reflect.Struct || k == reflect.Slice || k == reflect.Array || k == reflect.Map {
hasComplex = true
}
}
name := ""
doc := ""
if fb.Func.Returns != nil && len(fb.Func.Returns) > i {
name = fb.Func.Returns[i]
} else {
name = reflectx.NonPointerType(rtyp).Name()
}
label := strcase.ToSentence(name)
val := reflect.New(rtyp)
view := ToValue(val.Interface(), "")
view.SetSoloValue(val)
view.SetName(name)
view.SetLabel(label)
view.SetDoc(doc)
fb.Returns[i] = view
}
if nret > 1 || hasComplex {
fb.ShowReturnAsDialog = true
}
}
// SetReturnValues sets the [reflect.Value]s of the return
// value [Value] objects. It assumes that [FuncButton.SetReturns]
// has already been called. It is called in [FuncButton.CallFunc]
// and should typically not be called by end-user code.
func (fb *FuncButton) SetReturnValues(rets []reflect.Value) {
for i, ret := range fb.Returns {
ret.SetSoloValue(rets[i])
}
}
// SetKey sets the shortcut of the function button from the given [keymap.Functions]
func (fb *FuncButton) SetKey(kf keymap.Functions) *FuncButton {
fb.Button.SetKey(kf)
return fb
}
// makeTmpWidget makes a temporary widget in a temporary parent for the given value.
func makeTmpWidget(v Value) {
v.SetWidget(tree.NewRoot[*core.WidgetBase]("value-tmp-parent").NewChild(v.WidgetType(), "value-tmp-widget").(core.Widget))
}