/
column.go
138 lines (128 loc) · 3 KB
/
column.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
package dos
import (
"github.com/gdamore/tcell/v2"
)
// A Column orders its children vertically.
type Column struct {
Children []Widget
HorizontalAlign Alignment
FocusedIndex int // Index of child that receives focus
OnKeyEvent func(column *Column, ev *tcell.EventKey) bool
focused bool
}
func (c *Column) FocusNext() {
if len(c.Children) < 2 {
return
}
c.SetFocused(false) // Unfocus focused child
c.FocusedIndex++
if c.FocusedIndex >= len(c.Children) {
c.FocusedIndex = 0
}
c.SetFocused(c.focused)
}
func (c *Column) FocusPrevious() {
if len(c.Children) < 2 {
return
}
c.SetFocused(false) // Unfocus focused child
c.FocusedIndex--
if c.FocusedIndex < 0 {
c.FocusedIndex = len(c.Children) - 1
}
c.SetFocused(c.focused)
}
func (c *Column) GetChildRects(rect Rect) []Rect {
if childLen := len(c.Children); childLen > 0 {
rects := make([]Rect, childLen)
childHeightSum := 0
childMaxHeight := rect.H / childLen
for i := 0; i < childLen; i++ {
w, h := c.Children[i].DisplaySize(rect.W, childMaxHeight)
var x int
switch c.HorizontalAlign {
case AlignCenter:
x = rect.X + rect.W/2 - w/2
case AlignRight:
x = rect.X + rect.W - w
default:
x = rect.X
}
rects[i] = Rect{x, 0, w, h}
childHeightSum += h
}
if childHeightSum < rect.H {
qualifyingChildren := 0
for i := 0; i < childLen; i++ {
if rects[i].H < childMaxHeight {
qualifyingChildren++
}
}
if qualifyingChildren != 0 {
growAmount := (rect.H - childHeightSum) / qualifyingChildren
for i := 0; i < childLen; i++ {
if rects[i].H == childMaxHeight {
rects[i].H += growAmount
}
}
}
}
// Stack rects one on top of the other
childHeightSum = 0
for i := 0; i < childLen; i++ {
rects[i].Y = rect.Y + childHeightSum
childHeightSum += rects[i].H
}
return rects
}
return nil
}
func (c *Column) HandleMouse(currentRect Rect, ev *tcell.EventMouse) bool {
rects := c.GetChildRects(currentRect)
for i := range c.Children {
if c.Children[i].HandleMouse(rects[i], ev) {
c.SetFocused(false) // Unfocus any prior-focused child
c.FocusedIndex = i
return true
}
}
return false
}
func (c *Column) HandleKey(ev *tcell.EventKey) bool {
if c.OnKeyEvent != nil && c.OnKeyEvent(c, ev) {
return true
}
for i := range c.Children {
if c.Children[i].HandleKey(ev) {
return true
}
}
return false
}
func (c *Column) SetFocused(b bool) {
c.focused = b
if c.FocusedIndex < len(c.Children) {
c.Children[c.FocusedIndex].SetFocused(b)
}
}
func (c *Column) DisplaySize(boundsW, boundsH int) (w, h int) {
rects := c.GetChildRects(Rect{0, 0, boundsW, boundsH})
if rects == nil {
return 0, 0
}
height := 0
width := 0
for i := range rects {
height += rects[i].H // combined height
if rects[i].W > width {
width = rects[i].W // only the maximum width
}
}
return width, height
}
func (c *Column) Draw(rect Rect, s tcell.Screen) {
rects := c.GetChildRects(rect)
for i := range rects {
c.Children[i].Draw(rects[i], s)
}
}