-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrow.go
136 lines (126 loc) · 2.9 KB
/
row.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
package dos
import (
"github.com/gdamore/tcell/v2"
)
// A Row orders its children horizontally.
type Row struct {
Children []Widget
VerticalAlign Alignment
FocusedIndex int // Index of child that receives focus
OnKeyEvent func(row *Row, ev *tcell.EventKey) bool
focused bool
}
func (r *Row) FocusNext() {
if len(r.Children) < 2 {
return
}
r.SetFocused(false) // Unfocus focused child
r.FocusedIndex++
if r.FocusedIndex >= len(r.Children) {
r.FocusedIndex = 0
}
r.SetFocused(r.focused)
}
func (r *Row) FocusPrevious() {
if len(r.Children) < 2 {
return
}
r.SetFocused(false) // Unfocus focused child
r.FocusedIndex--
if r.FocusedIndex < 0 {
r.FocusedIndex = len(r.Children) - 1
}
r.SetFocused(r.focused)
}
func (r *Row) GetChildRects(rect Rect) []Rect {
if childLen := len(r.Children); childLen > 0 {
rects := make([]Rect, childLen)
childWidthSum := 0
childMaxWidth := rect.W / childLen
for i := 0; i < childLen; i++ {
w, h := r.Children[i].DisplaySize(childMaxWidth, rect.H)
var y int
switch r.VerticalAlign {
case AlignCenter:
y = rect.Y + rect.H/2 - h/2
case AlignRight:
y = rect.Y + rect.H - h
default:
y = rect.Y
}
rects[i] = Rect{0, y, w, h}
childWidthSum += w
}
if childWidthSum < rect.H {
qualifyingChildren := 0
for i := 0; i < childLen; i++ {
if rects[i].W < childMaxWidth {
qualifyingChildren++
}
}
growAmount := (rect.W - childWidthSum) / qualifyingChildren
for i := 0; i < childLen; i++ {
if rects[i].W == childMaxWidth {
rects[i].W += growAmount
}
}
}
// Stack rects one on top of the other
childWidthSum = 0
for i := 0; i < childLen; i++ {
rects[i].X = rect.X + childWidthSum
childWidthSum += rects[i].W
}
return rects
}
return nil
}
func (r *Row) HandleMouse(currentRect Rect, ev *tcell.EventMouse) bool {
rects := r.GetChildRects(currentRect)
for i := range r.Children {
if r.Children[i].HandleMouse(rects[i], ev) {
r.SetFocused(false) // Unfocus any prior-focused child
r.FocusedIndex = i
return true
}
}
return false
}
func (r *Row) HandleKey(ev *tcell.EventKey) bool {
if r.OnKeyEvent != nil && r.OnKeyEvent(r, ev) {
return true
}
for i := range r.Children {
if r.Children[i].HandleKey(ev) {
return true
}
}
return false
}
func (r *Row) SetFocused(b bool) {
r.focused = b
if r.FocusedIndex < len(r.Children) {
r.Children[r.FocusedIndex].SetFocused(b)
}
}
func (r *Row) DisplaySize(boundsW, boundsH int) (w, h int) {
rects := r.GetChildRects(Rect{0, 0, boundsW, boundsH})
if rects == nil {
return 0, 0
}
height := 0
width := 0
for i := range rects {
width += rects[i].W // combined width
if rects[i].H > height {
height = rects[i].H // only the maximum height
}
}
return width, height
}
func (r *Row) Draw(rect Rect, s tcell.Screen) {
rects := r.GetChildRects(rect)
for i := range rects {
r.Children[i].Draw(rects[i], s)
}
}