This repository was archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbutton.go
138 lines (114 loc) · 2.56 KB
/
button.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
136
137
138
package component
import (
"fmt"
"github.com/jroimartin/gocui"
)
// Button button struct
type Button struct {
*gocui.Gui
label string
handlers Handlers
ctype ComponentType
*Position
*Attributes
}
// NewButton new button
func NewButton(gui *gocui.Gui, label string, x, y, width int) *Button {
if len(label) >= width {
width = len(label) + 1
}
b := &Button{
Gui: gui,
label: label,
Position: &Position{
x,
y,
x + width + 2,
y + 2,
},
Attributes: &Attributes{
textColor: gocui.ColorWhite | gocui.AttrBold,
textBgColor: gocui.ColorBlue,
hilightColor: gocui.ColorBlue | gocui.AttrBold,
hilightBgColor: gocui.ColorWhite,
},
handlers: make(Handlers),
ctype: TypeButton,
}
return b
}
// AddHandler add handler
func (b *Button) AddHandler(key Key, handler Handler) *Button {
b.handlers[key] = handler
return b
}
// SetTextColor add button fg and bg color
func (b *Button) SetTextColor(fgColor, bgColor gocui.Attribute) *Button {
b.textColor = fgColor
b.textBgColor = bgColor
return b
}
// SetHilightColor add button fg and bg color
func (b *Button) SetHilightColor(fgColor, bgColor gocui.Attribute) *Button {
b.hilightColor = fgColor
b.hilightBgColor = bgColor
return b
}
// GetLabel get button label
func (b *Button) GetLabel() string {
return b.label
}
// GetPosition get button position
func (b *Button) GetPosition() *Position {
return b.Position
}
// Focus focus to button
func (b *Button) Focus() {
b.Gui.Cursor = false
v, _ := b.Gui.SetCurrentView(b.label)
v.Highlight = true
}
// UnFocus un focus
func (b *Button) UnFocus() {
v, _ := b.Gui.View(b.label)
v.Highlight = false
}
// GetType get component type
func (b *Button) GetType() ComponentType {
return b.ctype
}
// Draw draw button
func (b *Button) Draw() {
if v, err := b.Gui.SetView(b.label, b.X, b.Y, b.W, b.H); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
v.Frame = false
v.FgColor = b.textColor
v.BgColor = b.textBgColor
v.SelFgColor = b.hilightColor
v.SelBgColor = b.hilightBgColor
fmt.Fprint(v, fmt.Sprintf(" %s ", b.label))
}
if b.handlers != nil {
for key, handler := range b.handlers {
if err := b.Gui.SetKeybinding(b.label, key, gocui.ModNone, handler); err != nil {
panic(err)
}
}
}
}
// Close close button
func (b *Button) Close() {
if err := b.DeleteView(b.label); err != nil {
if err != gocui.ErrUnknownView {
panic(err)
}
}
if b.handlers != nil {
b.DeleteKeybindings(b.label)
}
}
func (b *Button) AddHandlerOnly(key Key, handler Handler) {
b.handlers[key] = handler
}