-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheckbox.go
79 lines (69 loc) · 1.42 KB
/
checkbox.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
package gonsole
import "github.com/nsf/termbox-go"
type Checkbox struct {
BasicControl
// custom
Text string
Checked bool
// internal
label *Label
}
func NewCheckbox(id string) *Checkbox {
// auxiliary label
label := NewLabel("__lbl_" + id)
checkbox := &Checkbox{
label: label,
}
checkbox.Init(id)
checkbox.SetFocussable(true)
return checkbox
}
func (c *Checkbox) Repaint() {
if !c.Dirty() {
return
}
c.BasicControl.Repaint()
// Box
var icon rune
if c.Checked {
icon = '☑'
} else {
icon = '☐'
}
contentBox := c.ContentBox()
foreground := c.Foreground
if c.Focussed() && !c.HasBorder() {
foreground = termbox.ColorYellow
}
termbox.SetCell(contentBox.Left, contentBox.Top, icon, foreground, c.Background)
// Label
label := c.label
label.Text = c.Text
label.Position = c.ContentBox().Minus(Sides{Left: 2}).Position()
// make sure the label is repainted too
label.Pollute()
label.Repaint()
}
func (chk *Checkbox) ParseEvent(ev *termbox.Event) bool {
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyEnter:
fallthrough
case termbox.KeySpace:
// change state
chk.Checked = !chk.Checked
// events
if chk.Checked {
chk.SubmitEvent(&Event{"checked", chk, nil})
} else {
chk.SubmitEvent(&Event{"unchecked", chk, nil})
}
chk.SubmitEvent(&Event{"changed", chk, nil})
return true
}
case termbox.EventError:
panic(ev.Err)
}
return false
}