MCPcopy
hub / github.com/g3n/engine / buildMenu

Function buildMenu

gui/builder_panel.go:362–429  ·  view source on GitHub ↗

buildMenu builds a gui object of type: Menu or MenuBar from the specified panel descriptor.

(b *Builder, am map[string]interface{})

Source from the content-addressed store, hash-verified

360// buildMenu builds a gui object of type: Menu or MenuBar from the
361// specified panel descriptor.
362func buildMenu(b *Builder, am map[string]interface{}) (IPanel, error) {
363
364 // Builds menu bar or menu
365 var menu *Menu
366 if am[AttribType].(string) == TypeMenuBar {
367 menu = NewMenuBar()
368 } else {
369 menu = NewMenu()
370 }
371
372 // Only sets attribs for top level menus
373 if pi := am[AttribParentInternal]; pi != nil {
374 par := pi.(map[string]interface{})
375 ptype := ""
376 if ti := par[AttribType]; ti != nil {
377 ptype = ti.(string)
378 }
379 if ptype != TypeMenu && ptype != TypeMenuBar {
380 err := b.SetAttribs(am, menu)
381 if err != nil {
382 return nil, err
383 }
384 }
385 }
386
387 // Builds and adds menu items
388 if am[AttribItems] != nil {
389 items := am[AttribItems].([]map[string]interface{})
390 for i := 0; i < len(items); i++ {
391 // Get the item optional type and text
392 item := items[i]
393 itype := ""
394 itext := ""
395 if iv := item[AttribType]; iv != nil {
396 itype = iv.(string)
397 }
398 if iv := item[AttribText]; iv != nil {
399 itext = iv.(string)
400 }
401 // Item is another menu
402 if itype == TypeMenu {
403 subm, err := buildMenu(b, item)
404 if err != nil {
405 return nil, err
406 }
407 menu.AddMenu(itext, subm.(*Menu))
408 continue
409 }
410 // Item is a separator
411 if itext == TypeSeparator {
412 menu.AddSeparator()
413 continue
414 }
415 // Item must be a menu option
416 mi := menu.AddOption(itext)
417 // Set item optional icon(s)
418 if icon := item[AttribIcon]; icon != nil {
419 mi.SetIcon(icon.(string))

Callers

nothing calls this directly

Calls 10

AddMenuMethod · 0.95
AddSeparatorMethod · 0.95
AddOptionMethod · 0.95
ModifierKeyTypeAlias · 0.92
KeyTypeAlias · 0.92
NewMenuBarFunction · 0.85
NewMenuFunction · 0.85
SetAttribsMethod · 0.80
SetShortcutMethod · 0.80
SetIconMethod · 0.45

Tested by

no test coverage detected