CallFunc calls the function associated with this button, prompting the user for any arguments.
()
| 327 | // CallFunc calls the function associated with this button, |
| 328 | // prompting the user for any arguments. |
| 329 | func (fb *FuncButton) CallFunc() { |
| 330 | if !fb.reflectFunc.IsValid() { |
| 331 | return |
| 332 | } |
| 333 | ctx := fb.goodContext() |
| 334 | if len(fb.Args) == 0 { |
| 335 | if !fb.Confirm { |
| 336 | fb.callFuncShowReturns() |
| 337 | return |
| 338 | } |
| 339 | fb.confirmDialog() |
| 340 | return |
| 341 | } |
| 342 | d := NewBody().AddTitle(fb.Text).AddText(fb.Tooltip) |
| 343 | str := funcArgsToStruct(fb.Args) |
| 344 | sv := NewForm(d).SetStruct(str.Addr().Interface()) |
| 345 | |
| 346 | accept := func() { |
| 347 | for i := range fb.Args { |
| 348 | fb.Args[i].Value = str.Field(i).Interface() |
| 349 | } |
| 350 | fb.callFuncShowReturns() |
| 351 | } |
| 352 | |
| 353 | // If there is a single value button, automatically |
| 354 | // open its dialog instead of this one |
| 355 | if len(fb.Args) == 1 { |
| 356 | sv.UpdateWidget() // need to update first |
| 357 | bt := AsButton(sv.Child(1)) |
| 358 | if bt != nil { |
| 359 | bt.OnFinal(events.Change, func(e events.Event) { |
| 360 | // the dialog for the argument has been accepted, so we call the function |
| 361 | accept() |
| 362 | }) |
| 363 | bt.Scene = fb.Scene // we must use this scene for context |
| 364 | bt.Send(events.Click) |
| 365 | return |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | d.AddBottomBar(func(parent Widget) { |
| 370 | d.AddCancel(parent) |
| 371 | d.AddOK(parent).SetText(fb.Text).OnClick(func(e events.Event) { |
| 372 | d.Close() // note: the other Close event happens too late! |
| 373 | accept() |
| 374 | }) |
| 375 | }) |
| 376 | d.RunDialog(ctx) |
| 377 | } |
| 378 | |
| 379 | // funcArgsToStruct converts a slice of [FuncArg] objects |
| 380 | // to a new non-pointer struct [reflect.Value]. |
no test coverage detected