Show displays the context menu as a modal.
()
| 42 | |
| 43 | // Show displays the context menu as a modal. |
| 44 | func (cm *ContextMenu) Show() *tview.List { |
| 45 | list := tview.NewList() |
| 46 | list.ShowSecondaryText(false) |
| 47 | list.SetBorder(true) |
| 48 | list.SetTitle(cm.title) |
| 49 | list.SetSelectedStyle(tcell.StyleDefault.Background(theme.Colors.Selection).Foreground(theme.Colors.Primary).Attributes(tcell.AttrReverse)) |
| 50 | |
| 51 | for i, action := range cm.menuItems { |
| 52 | var shortcut rune |
| 53 | |
| 54 | // If explicit shortcuts are provided, use them (even if 0) |
| 55 | if len(cm.shortcuts) > 0 { |
| 56 | if i < len(cm.shortcuts) { |
| 57 | shortcut = cm.shortcuts[i] |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // If shortcut is 0, tview will not display it. |
| 62 | list.AddItem(action, "", shortcut, nil) |
| 63 | } |
| 64 | |
| 65 | list.SetHighlightFullLine(true) |
| 66 | |
| 67 | list.SetSelectedFunc(func(index int, mainText string, secondaryText string, shortcut rune) { |
| 68 | // The parent App should handle closing the context menu |
| 69 | if cm.onAction != nil { |
| 70 | cm.onAction(index, mainText) |
| 71 | } |
| 72 | }) |
| 73 | |
| 74 | list.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { |
| 75 | if event.Key() == tcell.KeyEscape { |
| 76 | // The parent App should handle closing the context menu |
| 77 | return nil |
| 78 | } else if event.Key() == tcell.KeyRune { |
| 79 | switch event.Rune() { |
| 80 | case 'j': |
| 81 | return tcell.NewEventKey(tcell.KeyDown, 0, tcell.ModNone) |
| 82 | case 'k': |
| 83 | return tcell.NewEventKey(tcell.KeyUp, 0, tcell.ModNone) |
| 84 | case 'h': |
| 85 | // The parent App should handle closing the context menu |
| 86 | return nil |
| 87 | case 'l': |
| 88 | index := list.GetCurrentItem() |
| 89 | if index >= 0 && index < len(cm.menuItems) { |
| 90 | if cm.onAction != nil { |
| 91 | cm.onAction(index, cm.menuItems[index]) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return nil |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return event |
| 100 | }) |
| 101 |
nothing calls this directly
no test coverage detected