ShowNodeContextMenu displays the context menu for node actions.
()
| 17 | |
| 18 | // ShowNodeContextMenu displays the context menu for node actions. |
| 19 | func (a *App) ShowNodeContextMenu() { |
| 20 | node := a.nodeList.GetSelectedNode() |
| 21 | if node == nil { |
| 22 | return |
| 23 | } |
| 24 | |
| 25 | // Store last focused primitive |
| 26 | a.lastFocus = a.GetFocus() |
| 27 | |
| 28 | type menuEntry struct { |
| 29 | label string |
| 30 | shortcut rune |
| 31 | handler func() |
| 32 | } |
| 33 | |
| 34 | entries := []menuEntry{ |
| 35 | {label: nodeActionOpenShell, shortcut: 's', handler: func() { a.openNodeShell() }}, |
| 36 | {label: nodeActionOpenVNC, shortcut: 'v', handler: func() { a.openNodeVNC() }}, |
| 37 | {label: nodeActionCreateVM, shortcut: 'c', handler: func() { a.showVMCreateForm(node) }}, |
| 38 | {label: nodeActionCreateLXC, shortcut: 'l', handler: func() { a.showLXCCreateForm(node) }}, |
| 39 | } |
| 40 | |
| 41 | if a.pluginRegistry != nil { |
| 42 | for _, pluginAction := range a.pluginRegistry.NodeActionsForNode(node) { |
| 43 | pa := pluginAction |
| 44 | entries = append(entries, menuEntry{ |
| 45 | label: pa.Label, |
| 46 | shortcut: pa.Shortcut, |
| 47 | handler: func() { |
| 48 | if pa.Handler == nil { |
| 49 | return |
| 50 | } |
| 51 | |
| 52 | if err := pa.Handler(a.ctx, a, node); err != nil { |
| 53 | a.showMessageSafe(fmt.Sprintf("%s failed: %v", pa.Label, err)) |
| 54 | } |
| 55 | }, |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | entries = append(entries, menuEntry{label: nodeActionRefresh, shortcut: 'r', handler: func() { |
| 61 | a.refreshNodeData(node) |
| 62 | }}) |
| 63 | |
| 64 | menuItems := make([]string, len(entries)) |
| 65 | shortcuts := make([]rune, len(entries)) |
| 66 | for i, entry := range entries { |
| 67 | menuItems[i] = entry.label |
| 68 | shortcuts[i] = entry.shortcut |
| 69 | } |
| 70 | |
| 71 | menu := NewContextMenuWithShortcuts(" Node Actions ", menuItems, shortcuts, func(index int, action string) { |
| 72 | a.CloseContextMenu() |
| 73 | |
| 74 | if index >= 0 && index < len(entries) { |
| 75 | handler := entries[index].handler |
| 76 | if handler != nil { |
no test coverage detected