summaryrefslogtreecommitdiffstats
path: root/cmd/grv/context_menu_view.go
blob: aa3835b0bcd42ae89709f868c791d2c5a4d67c21 (plain)
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
package main

import (
	"fmt"
	"strings"
	"sync"

	log "github.com/Sirupsen/logrus"
)

// OnContextMenuEntrySelected is called when an entry is selected
type OnContextMenuEntrySelected func(entry ContextMenuEntry, entryIndex uint)

// ContextMenuEntry stores the display name and value of
// a context menu item
type ContextMenuEntry struct {
	DisplayName string
	Value       interface{}
}

// ContextMenuConfig is the configuration for the ContextMenuView
// It contains all context menu contextMenuConfig and an observer
type ContextMenuConfig struct {
	Entity   string
	Entries  []ContextMenuEntry
	OnSelect OnContextMenuEntrySelected
}

type contextMenuViewHandler func(*ContextMenuView, Action) error

// ContextMenuView is a view that displays a context menu
type ContextMenuView struct {
	*AbstractWindowView
	contextMenuConfig ContextMenuConfig
	activeViewPos     ViewPos
	lastViewDimension ViewDimension
	handlers          map[ActionType]contextMenuViewHandler
	lock              sync.Mutex
}

// NewContextMenuView creates a new instance
func NewContextMenuView(contextMenuConfig ContextMenuConfig, channels Channels, config Config, variables GRVVariableSetter) *ContextMenuView {
	contextMenuView := &ContextMenuView{
		contextMenuConfig: contextMenuConfig,
		activeViewPos:     NewViewPosition(),
		handlers: map[ActionType]contextMenuViewHandler{
			ActionSelect:   selectContextMenuEntry,
			ActionPrevLine: moveUpContextEntry,
			ActionNextLine: moveDownContextEntry,
		},
	}

	contextMenuView.AbstractWindowView = NewAbstractWindowView(contextMenuView, channels, config, variables, &contextMenuView.lock, "menu item")

	if contextMenuConfig.Entity == "" {
		contextMenuView.contextMenuConfig.Entity = "Action"
	}

	return contextMenuView
}

// ViewID returns the ViewID of the context menu view
func (contextMenuView *ContextMenuView) ViewID() ViewID {
	return ViewContextMenu
}

// Render generates the context menu view and writes it to the provided window
func (contextMenuView *ContextMenuView) Render(win RenderWindow) (err error) {
	contextMenuView.lock.Lock()
	defer contextMenuView.lock.Unlock()

	contextMenuView.lastViewDimension = win.ViewDimensions()

	winRows := win.Rows() - 2
	viewPos := contextMenuView.viewPos()

	viewRows := contextMenuView.rows()
	viewPos.DetermineViewStartRow(winRows, viewRows)

	viewRowIndex := viewPos.ViewStartRowIndex()
	startColumn := viewPos.ViewStartColumn()

	for rowIndex := uint(0); rowIndex < winRows && viewRowIndex < viewRows; rowIndex++ {
		entry := contextMenuView.contextMenuConfig.Entries[viewRowIndex]

		if err = win.SetRow(rowIndex+1, startColumn, CmpNone, " %v", entry.DisplayName); err != nil {
			return
		}

		viewRowIndex++
	}

	win.ApplyStyle(CmpContextMenuContent)

	if err = win.SetSelectedRow(viewPos.SelectedRowIndex()+1, ViewStateActive); err != nil {
		return
	}

	win.DrawBorderWithStyle(CmpContextMenuContent)
	entity := contextMenuView.contextMenuConfig.Entity

	if err = win.SetTitle(CmpContextMenuTitle, "Select %v", entity); err != nil {
		return
	}

	if err = win.SetFooter(CmpContextMenuTitle, "%v %v of %v", entity, viewPos.SelectedRowIndex()+1, viewRows); err != nil {
		return
	}

	return
}

// RenderHelpBar renders a help message for the context menu
func (contextMenuView *ContextMenuView) RenderHelpBar(lineBuilder *LineBuilder) (err error) {
	var quitKeyText string

	quitKeys := contextMenuView.config.KeyStrings(ActionRemoveView, ViewHierarchy{ViewContextMenu, ViewAll})
	if len(quitKeys) > 0 {
		quitKeyText = fmt.Sprintf("(Press %v to close menu)", quitKeys[len(quitKeys)-1].keystring)
	}

	entity := strings.ToLower(contextMenuView.contextMenuConfig.Entity)
	lineBuilder.AppendWithStyle(CmpHelpbarviewSpecial, "Select %v %v", entity, quitKeyText)
	return
}

func (contextMenuView *ContextMenuView) viewPos() ViewPos {
	return contextMenuView.activeViewPos
}

func (contextMenuView *ContextMenuView) rows() uint {
	return uint(len(contextMenuView.contextMenuConfig.Entries))
}

func (contextMenuView *ContextMenuView) viewDimension() ViewDimension {
	return contextMenuView.lastViewDimension
}

func (contextMenuView *ContextMenuView) onRowSelected(rowIndex uint) (err error) {
	return
}

func (contextMenuView *ContextMenuView) line(lineIndex uint) (line string) {
	if lineIndex < contextMenuView.rows() {
		line = contextMenuView.contextMenuConfig.Entries[lineIndex].DisplayName
	}

	return
}

// HandleAction handles the action if supported
func (contextMenuView *ContextMenuView) HandleAction(action Action) (err error) {
	contextMenuView.lock.Lock()
	defer contextMenuView.lock.Unlock()

	var handled bool
	if handler, ok := contextMenuView.handlers[action.ActionType]; ok {
		log.Debugf("Action handled by ContextMenuView")
		err = handler(contextMenuView, action)
	} else if handled, err = contextMenuView.AbstractWindowView.HandleAction(action); handled {
		log.Debugf("Action handled by AbstractWindowView")
	} else {
		log.Debugf("Action not handled")
	}

	return
}

func selectContextMenuEntry(contextMenuView *ContextMenuView, action Action) (err error) {
	viewPos := contextMenuView.viewPos()
	selectedIndex := viewPos.ActiveRowIndex()
	selectedEntry := contextMenuView.contextMenuConfig.Entries[selectedIndex]

	log.Debugf("Context menu entry selected: %v", selectedEntry)

	contextMenuView.channels.DoAction(Action{ActionType: ActionRemoveView})

	if contextMenuView.contextMenuConfig.OnSelect != nil {
		go contextMenuView.contextMenuConfig.OnSelect(selectedEntry, selectedIndex)
	}

	return
}

func moveUpContextEntry(contextMenuView *ContextMenuView, action Action) (err error) {
	viewPos := contextMenuView.viewPos()

	if viewPos.ActiveRowIndex() == 0 {
		_, err = contextMenuView.AbstractWindowView.HandleAction(Action{ActionType: ActionLastLine})
	} else {
		_, err = contextMenuView.AbstractWindowView.HandleAction(action)
	}

	return
}

func moveDownContextEntry(contextMenuView *ContextMenuView, action Action) (err error) {
	rows := contextMenuView.rows()
	viewPos := contextMenuView.viewPos()

	if viewPos.ActiveRowIndex() == rows-1 {
		_, err = contextMenuView.AbstractWindowView.HandleAction(Action{ActionType: ActionFirstLine})
	} else {
		_, err = contextMenuView.AbstractWindowView.HandleAction(action)
	}

	return
}