-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheckgroup.go
More file actions
139 lines (117 loc) · 3.05 KB
/
checkgroup.go
File metadata and controls
139 lines (117 loc) · 3.05 KB
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
package vtui
import (
"github.com/unxed/vtinput"
"unicode"
)
// CheckGroup is a cluster of checkboxes managed as a single widget.
type CheckGroup struct {
ScreenObject
Items []string
States []bool
focusIdx int
Columns int
colWidths []int
}
func (cg *CheckGroup) GetData() any {
var mask uint32
for i, s := range cg.States {
if s {
mask |= (1 << i)
}
}
return mask
}
func (cg *CheckGroup) SetData(val any) {
if mask, ok := val.(uint32); ok {
for i := range cg.States {
cg.States[i] = (mask & (1 << i)) != 0
}
}
}
func NewCheckGroup(x, y, cols int, items []string) *CheckGroup {
cg := &CheckGroup{Items: items, States: make([]bool, len(items))}
cg.canFocus = true
if cols < 1 { cols = 1 }
cg.Columns = cols
rows := (len(items) + cols - 1) / cols
cg.colWidths = calcGridColWidths(cols, items)
totalW := 0
for _, w := range cg.colWidths { totalW += w }
cg.SetPosition(x, y, x+totalW-1, y+rows-1)
return cg
}
func (cg *CheckGroup) Show(scr *ScreenBuf) {
cg.ScreenObject.Show(scr)
cg.DisplayObject(scr)
}
func (cg *CheckGroup) DisplayObject(scr *ScreenBuf) {
if !cg.IsVisible() { return }
attr := Palette[ColDialogText]
highAttr := Palette[ColDialogHighlightText]
selAttr := Palette[ColDialogSelectedButton]
selHighAttr := Palette[ColDialogHighlightSelectedButton]
for i, itm := range cg.Items {
curAttr, curHigh := attr, highAttr
if cg.IsFocused() && i == cg.focusIdx {
curAttr, curHigh = selAttr, selHighAttr
}
if cg.IsDisabled() {
curAttr, curHigh = DimColor(curAttr), DimColor(curHigh)
}
prefix := "[ ] "
if cg.States[i] { prefix = "[x] " }
row := i / cg.Columns
col := i % cg.Columns
cx := cg.X1
for c := 0; c < col; c++ { cx += cg.colWidths[c] }
p := NewPainter(scr)
p.DrawStringHighlighted(cx, cg.Y1+row, prefix+itm, curAttr, curHigh)
}
}
func (cg *CheckGroup) ProcessKey(e *vtinput.InputEvent) bool {
if !e.KeyDown {
return false
}
if cg.IsDisabled() { return false }
newIdx, moved := gridNav(cg.focusIdx, len(cg.Items), cg.Columns, e.VirtualKeyCode)
if moved {
cg.focusIdx = newIdx
return true
}
if handleGridBoundaryNav(e.VirtualKeyCode, cg.focusIdx, len(cg.Items)) {
return true
}
switch e.VirtualKeyCode {
case vtinput.VK_SPACE:
cg.States[cg.focusIdx] = !cg.States[cg.focusIdx]
return true
}
if e.Char != 0 {
hkChar := unicode.ToLower(e.Char)
xlatChar := unicode.ToLower(GlobalXlator.Translate(e.Char))
for i, itm := range cg.Items {
hk := ExtractHotkey(itm)
if hk != 0 && (hk == hkChar || hk == xlatChar) {
cg.focusIdx = i
cg.States[i] = !cg.States[i]
return true
}
}
}
return false
}
func (cg *CheckGroup) ProcessMouse(e *vtinput.InputEvent) bool {
if cg.IsDisabled() { return false }
if e.ButtonState == vtinput.FromLeft1stButtonPressed && e.KeyDown {
mx, my := int(e.MouseX), int(e.MouseY)
if cg.HitTest(mx, my) {
idx := getGridIndexFromMouse(cg.X1, cg.Y1, mx, my, cg.Columns, cg.colWidths)
if idx >= 0 && idx < len(cg.Items) {
cg.focusIdx = idx
cg.States[idx] = !cg.States[idx]
return true
}
}
}
return false
}