forked from jojomi/gonsole
-
Notifications
You must be signed in to change notification settings - Fork 4
/
button.go
65 lines (53 loc) · 1.14 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
package gonsole
import "github.com/nsf/termbox-go"
type Button struct {
BaseControl
text string
onClick func()
}
func NewButton(win AppWindow, parent Container, id string) *Button {
button := &Button{}
button.Init(win, parent, id, "button")
button.SetFocusable(true)
parent.AddControl(button)
return button
}
func (b *Button) Text() string {
return b.text
}
func (b *Button) SetText(text string) {
b.text = text
}
func (b *Button) OnClick(handler func()) {
b.onClick = handler
}
func (b *Button) Repaint() {
if !b.Dirty() {
return
}
b.BaseControl.Repaint()
t := b.Theme()
cb := b.ContentBox()
if b.Focused() {
DrawTextSimple(b.text, false, cb, t.ColorTermbox("focused.fg"), t.ColorTermbox("focused.bg"))
} else {
DrawTextSimple(b.text, false, cb, t.ColorTermbox("fg"), t.ColorTermbox("bg"))
}
}
func (b *Button) ParseEvent(ev *termbox.Event) (handled, repaint bool) {
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeySpace:
fallthrough
case termbox.KeyEnter:
if b.onClick != nil {
b.onClick()
}
return true, true
}
case termbox.EventError:
panic(ev.Err)
}
return false, false
}