forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 6
/
widget_menu.go
135 lines (106 loc) · 2.95 KB
/
widget_menu.go
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
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type MenuWidget struct {
Root *tview.Flex
buttonsLeft *tview.Flex
buttonsRight *tview.Flex
activeButton string
buttons map[string]*tview.Button
buttonStyle tcell.Style
quitActiveStyle tcell.Style
// external references
ui *Ui
}
const (
PAGE_BROWSER = iota
PAGE_QUEUE
PAGE_PLAYLISTS
PAGE_SEARCH
PAGE_LOG
)
var buttonOrder = []string{PageBrowser, PageQueue, PagePlaylists, PageSearch, PageLog}
func (ui *Ui) createMenuWidget() (m *MenuWidget) {
m = &MenuWidget{
activeButton: buttonOrder[PAGE_BROWSER],
buttons: make(map[string]*tview.Button),
buttonStyle: tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite),
quitActiveStyle: tcell.StyleDefault.Background(tcell.ColorWhite).Foreground(tcell.ColorRed),
ui: ui,
}
// page buttons on the left
m.buttonsLeft = tview.NewFlex().
SetDirection(tview.FlexColumn)
m.createPageButtons()
m.updatePageButtons()
// help and quit button on the right
quitButton := tview.NewButton("Q: quit").
SetStyle(m.buttonStyle).
SetActivatedStyle(m.quitActiveStyle).
SetSelectedFunc(func() {
ui.Quit()
})
helpButton := tview.NewButton("?: help").
SetStyle(m.buttonStyle).
SetActivatedStyle(m.buttonStyle).
SetSelectedFunc(func() {
ui.ShowHelp()
})
m.buttonsRight = tview.NewFlex().
SetDirection(tview.FlexColumn)
m.buttonsRight.AddItem(nil, 0, 1, false) // fill space to right-align the buttons
m.buttonsRight.AddItem(helpButton, 9, 0, false)
m.buttonsRight.AddItem(quitButton, 9, 0, false)
m.Root = tview.NewFlex().SetDirection(tview.FlexColumn).
AddItem(m.buttonsLeft, 0, 4, false).
AddItem(m.buttonsRight, 0, 2, false)
// clear background
m.Root.Box = tview.NewBox()
return
}
func (m *MenuWidget) createPageButtons() {
for i, page := range buttonOrder {
button := tview.NewButton(page)
button.SetStyle(m.buttonStyle)
// HACK because I couldn't find a way to un-focus a button after switching with 1,2,3,4 keys:
button.SetActivatedStyle(m.buttonStyle)
// create copy for our function
buttonPage := page
button.SetSelectedFunc(func() {
m.ui.ShowPage(buttonPage)
})
m.buttons[page] = button
// add button
m.buttonsLeft.AddItem(button, 15, 0, false)
// add spacer
if i < len(buttonOrder)-1 {
m.buttonsLeft.AddItem(nil, 1, 0, false)
}
}
}
func (m *MenuWidget) updatePageButtons() {
for i, page := range buttonOrder {
var text string
if page == m.activeButton {
text = fmt.Sprintf("%d: [::b]%s[::-]", i+1, page)
} else {
text = fmt.Sprintf("%d: %s", i+1, page)
}
m.buttons[page].SetLabel(text)
}
}
func (m *MenuWidget) SetActivePage(name string) {
if _, ok := m.buttons[name]; !ok {
return // invalid button name
}
m.activeButton = name
m.updatePageButtons()
}
func (m *MenuWidget) GetActivePage() string {
return m.activeButton
}