...
1 package tview
2
3 import (
4 "github.com/gdamore/tcell/v2"
5 )
6
7
8
9
10 type Button struct {
11 *Box
12
13
14 disabled bool
15
16
17 text string
18
19
20 style tcell.Style
21
22
23 activatedStyle tcell.Style
24
25
26 disabledStyle tcell.Style
27
28
29 selected func()
30
31
32
33
34 exit func(tcell.Key)
35 }
36
37
38 func NewButton(label string) *Button {
39 box := NewBox()
40 box.SetRect(0, 0, TaggedStringWidth(label)+4, 1)
41 return &Button{
42 Box: box,
43 text: label,
44 style: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.PrimaryTextColor),
45 activatedStyle: tcell.StyleDefault.Background(Styles.PrimaryTextColor).Foreground(Styles.InverseTextColor),
46 disabledStyle: tcell.StyleDefault.Background(Styles.ContrastBackgroundColor).Foreground(Styles.ContrastSecondaryTextColor),
47 }
48 }
49
50
51 func (b *Button) SetLabel(label string) *Button {
52 b.text = label
53 return b
54 }
55
56
57 func (b *Button) GetLabel() string {
58 return b.text
59 }
60
61
62 func (b *Button) SetLabelColor(color tcell.Color) *Button {
63 b.style = b.style.Foreground(color)
64 return b
65 }
66
67
68 func (b *Button) SetStyle(style tcell.Style) *Button {
69 b.style = style
70 return b
71 }
72
73
74
75 func (b *Button) SetLabelColorActivated(color tcell.Color) *Button {
76 b.activatedStyle = b.activatedStyle.Foreground(color)
77 return b
78 }
79
80
81
82 func (b *Button) SetBackgroundColorActivated(color tcell.Color) *Button {
83 b.activatedStyle = b.activatedStyle.Background(color)
84 return b
85 }
86
87
88 func (b *Button) SetActivatedStyle(style tcell.Style) *Button {
89 b.activatedStyle = style
90 return b
91 }
92
93
94 func (b *Button) SetDisabledStyle(style tcell.Style) *Button {
95 b.disabledStyle = style
96 return b
97 }
98
99
100
101
102
103
104 func (b *Button) SetDisabled(disabled bool) *Button {
105 b.disabled = disabled
106 return b
107 }
108
109
110 func (b *Button) IsDisabled() bool {
111 return b.disabled
112 }
113
114
115 func (b *Button) SetSelectedFunc(handler func()) *Button {
116 b.selected = handler
117 return b
118 }
119
120
121
122
123
124
125
126
127 func (b *Button) SetExitFunc(handler func(key tcell.Key)) *Button {
128 b.exit = handler
129 return b
130 }
131
132
133 func (b *Button) Draw(screen tcell.Screen) {
134
135 style := b.style
136 if b.disabled {
137 style = b.disabledStyle
138 }
139 _, backgroundColor, _ := style.Decompose()
140 if b.HasFocus() && !b.disabled {
141 style = b.activatedStyle
142 _, backgroundColor, _ = style.Decompose()
143
144
145 borderColor := b.GetBorderColor()
146 b.SetBorderColor(backgroundColor)
147 defer func() {
148 b.SetBorderColor(borderColor)
149 }()
150 }
151 b.SetBackgroundColor(backgroundColor)
152 b.Box.DrawForSubclass(screen, b)
153
154
155 x, y, width, height := b.GetInnerRect()
156 if width > 0 && height > 0 {
157 y = y + height/2
158 printWithStyle(screen, b.text, x, y, 0, width, AlignCenter, style, true)
159 }
160 }
161
162
163 func (b *Button) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
164 return b.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
165 if b.disabled {
166 return
167 }
168
169
170 switch key := event.Key(); key {
171 case tcell.KeyEnter:
172 if b.selected != nil {
173 b.selected()
174 }
175 case tcell.KeyBacktab, tcell.KeyTab, tcell.KeyEscape:
176 if b.exit != nil {
177 b.exit(key)
178 }
179 }
180 })
181 }
182
183
184 func (b *Button) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
185 return b.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
186 if b.disabled {
187 return false, nil
188 }
189
190 if !b.InRect(event.Position()) {
191 return false, nil
192 }
193
194
195 if action == MouseLeftDown {
196 setFocus(b)
197 consumed = true
198 } else if action == MouseLeftClick {
199 if b.selected != nil {
200 b.selected()
201 }
202 consumed = true
203 }
204
205 return
206 })
207 }
208
View as plain text