-
Notifications
You must be signed in to change notification settings - Fork 488
/
menubutton.go
96 lines (79 loc) · 2.1 KB
/
menubutton.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package components
import (
"fmt"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// NewMenuButton creates new menu button.
func NewMenuButton(label string) *MenuButton {
button := &MenuButton{
Button: tview.NewButton(label),
label: label,
colors: [4]tcell.Color{
tcell.ColorBlack,
tcell.ColorBlack,
tcell.ColorBlack,
tcell.ColorBlack,
},
active: false,
}
button.updateColors()
return button
}
const (
menuButtonBgColor = iota
menuButtonLabelColor
menuButtonActiveBgColor
menuButtonActiveLabelColor
)
// MenuButton creates a new menu button.
type MenuButton struct {
*tview.Button
label string
colors [4]tcell.Color
active bool
}
// SetActiveColors sets active state colors.
// 1st value is bg color.
// 2nd value is label color.
func (b *MenuButton) SetActiveColors(colors ...tcell.Color) {
for i, color := range colors {
b.colors[i+2] = color
}
b.updateColors()
}
// SetInactiveColors sets inactive state colors.
// 1st value is bg color.
// 2nd value is label color.
func (b *MenuButton) SetInactiveColors(colors ...tcell.Color) {
for i, color := range colors {
b.colors[i] = color
}
b.updateColors()
}
// SetActive changes menu button active state.
func (b *MenuButton) SetActive(active bool) {
b.active = active
b.updateColors()
format := "%s"
if b.active {
format = "[::u]%s[::-]"
}
b.SetLabel(fmt.Sprintf(format, b.label))
}
func (b *MenuButton) updateColors() {
if b.active {
b.SetLabelColor(b.colors[menuButtonActiveLabelColor])
b.SetLabelColorActivated(b.colors[menuButtonActiveLabelColor])
b.SetBackgroundColor(b.colors[menuButtonActiveBgColor])
b.SetBackgroundColorActivated(b.colors[menuButtonActiveBgColor])
} else {
b.SetLabelColor(b.colors[menuButtonLabelColor])
b.SetLabelColorActivated(b.colors[menuButtonLabelColor])
b.SetBackgroundColor(b.colors[menuButtonBgColor])
b.SetBackgroundColorActivated(b.colors[menuButtonBgColor])
}
}