-
Notifications
You must be signed in to change notification settings - Fork 50
/
button.go
197 lines (173 loc) · 4.63 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
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
package clui
import (
xs "github.com/huandu/xstrings"
term "github.com/nsf/termbox-go"
"sync/atomic"
"time"
)
/*
Button is a simpe push button control. Every time a user clicks a Button, it
emits OnClick event. Event has only one valid field Sender.
Button can be clicked with mouse or using space on keyboard while the Button is active.
*/
type Button struct {
BaseControl
shadowColor term.Attribute
pressed int32
shadowType ButtonShadow
onClick func(Event)
}
/*
NewButton creates a new Button.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
width and heigth - are minimal size of the control.
title - button title.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateButton(parent Control, width, height int, title string, scale int) *Button {
b := new(Button)
b.BaseControl = NewBaseControl()
b.parent = parent
b.align = AlignCenter
if height == AutoSize {
height = 4
}
if width == AutoSize {
width = xs.Len(title) + 2 + 1
}
if height < 4 {
height = 4
}
if width < 6 {
width = 6
}
b.SetTitle(title)
b.SetSize(width, height)
b.SetConstraints(width, height)
b.SetScale(scale)
if parent != nil {
parent.AddChild(b)
}
return b
}
// Repaint draws the control on its View surface
func (b *Button) Draw() {
if b.hidden {
return
}
b.mtx.RLock()
defer b.mtx.RUnlock()
PushAttributes()
defer PopAttributes()
x, y := b.Pos()
w, h := b.Size()
fg, bg := b.fg, b.bg
shadow := RealColor(b.shadowColor, b.Style(), ColorButtonShadow)
if b.disabled {
fg, bg = RealColor(fg, b.Style(), ColorButtonDisabledText), RealColor(bg, b.Style(), ColorButtonDisabledBack)
} else if b.Active() {
fg, bg = RealColor(b.fgActive, b.Style(), ColorButtonActiveText), RealColor(b.bgActive, b.Style(), ColorButtonActiveBack)
} else {
fg, bg = RealColor(fg, b.Style(), ColorButtonText), RealColor(bg, b.Style(), ColorButtonBack)
}
dy := int((h - 1) / 2)
SetTextColor(fg)
shift, text := AlignColorizedText(b.title, w-1, b.align)
if b.isPressed() == 0 {
switch b.shadowType {
case ShadowFull:
SetBackColor(shadow)
FillRect(x+1, y+h-1, w-1, 1, ' ')
FillRect(x+w-1, y+1, 1, h-1, ' ')
case ShadowHalf:
parts := []rune(SysObject(ObjButton))
var bottomCh, rightCh rune
if len(parts) < 2 {
bottomCh, rightCh = '▀', '█'
} else {
bottomCh, rightCh = parts[0], parts[1]
}
SetTextColor(shadow)
FillRect(x+1, y+h-1, w-1, 1, bottomCh)
FillRect(x+w-1, y+1, 1, h-2, rightCh)
}
SetTextColor(fg)
SetBackColor(bg)
FillRect(x, y, w-1, h-1, ' ')
DrawText(x+shift, y+dy, text)
} else {
SetBackColor(bg)
FillRect(x+1, y+1, w-1, h-1, ' ')
DrawText(x+1+shift, y+1+dy, b.title)
}
}
func (b *Button) isPressed() int32 {
return atomic.LoadInt32(&b.pressed)
}
func (b *Button) setPressed(pressed int32) {
atomic.StoreInt32(&b.pressed, pressed)
}
/*
ProcessEvent processes all events come from the control parent. If a control
processes an event it should return true. If the method returns false it means
that the control do not want or cannot process the event and the caller sends
the event to the control parent
*/
func (b *Button) ProcessEvent(event Event) bool {
if !b.Enabled() {
return false
}
if event.Type == EventKey {
if event.Key == term.KeySpace && b.isPressed() == 0 {
b.setPressed(1)
ev := Event{Type: EventRedraw}
go func() {
PutEvent(ev)
time.Sleep(100 * time.Millisecond)
b.setPressed(0)
PutEvent(ev)
}()
if b.onClick != nil {
b.onClick(event)
}
return true
} else if event.Key == term.KeyEsc && b.isPressed() != 0 {
b.setPressed(0)
ReleaseEvents()
return true
}
} else if event.Type == EventMouse {
if event.Key == term.MouseLeft {
b.setPressed(1)
GrabEvents(b)
return true
} else if event.Key == term.MouseRelease && b.isPressed() != 0 {
ReleaseEvents()
if event.X >= b.x && event.Y >= b.y && event.X < b.x+b.width && event.Y < b.y+b.height {
if b.onClick != nil {
b.onClick(event)
}
}
b.setPressed(0)
return true
}
}
return false
}
// OnClick sets the callback that is called when one clicks button
// with mouse or pressing space on keyboard while the button is active
func (b *Button) OnClick(fn func(Event)) {
b.onClick = fn
}
// ShadowType returns type of a show the button drops
func (b *Button) ShadowType() ButtonShadow {
return b.shadowType
}
// SetShadowType changes the shadow the button drops
func (b *Button) SetShadowType(sh ButtonShadow) {
b.mtx.Lock()
b.shadowType = sh
b.mtx.Unlock()
}