forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_sizehint_test.go
151 lines (125 loc) · 2.93 KB
/
label_sizehint_test.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
139
140
141
142
143
144
145
146
147
148
149
package tui
import (
"image"
"testing"
)
var TailBoxTests = []struct {
Test string
Setup func() Widget
Want string
} {
{
Test: "draw small labels",
Setup: func() Widget {
return NewTailBox(
NewLabel("hello mom"),
NewLabel("hello dad"),
)
},
Want: `
hello mom
hello dad
`,
},
{
Test: "draw unwrapped labels",
Setup: func() Widget {
l1, l2 := NewLabel("hello muddah"), NewLabel("hello faddah")
return NewTailBox(l1, l2)
},
Want: `
hello mudd
hello fadd
`,
},
{
Test: "draw wrapped labels",
Setup: func() Widget {
l1, l2 := NewLabel("hello muddah"), NewLabel("hello faddah")
l1.SetWordWrap(true)
l2.SetWordWrap(true)
return NewTailBox(l1, l2)
},
Want: `
hello
muddah
hello
faddah
`,
},
}
func TestTailBox(t *testing.T) {
for _, tt := range TailBoxTests {
tt := tt
t.Run(tt.Test, func(t *testing.T) {
surface := NewTestSurface(10, 5)
p := NewPainter(surface, NewTheme())
p.Repaint(tt.Setup())
if surface.String() != tt.Want {
t.Errorf("unexpected contents: got = \n%s\nwant = \n%s", surface.String(), tt.Want)
}
})
}
}
// TailBox is a container Widget that may not show all its
// While Box attempts to show every contained Widget - sometimes shrinking
// those Widgets to do so- TailBox prioritizes completely displaying its last
// Widget, then the next-to-last widget, etc.
// It is vertically-aligned, i.e. all the contained Widgets have the same width.
type TailBox struct {
WidgetBase
sz image.Point
contents []Widget
}
var _ Widget = &TailBox{}
func NewTailBox(w ...Widget) *TailBox {
return &TailBox{
contents: w,
}
}
func (t *TailBox) Append(w Widget) {
t.contents = append(t.contents, w)
t.doLayout(t.Size())
}
func (t *TailBox) SetContents(w ...Widget) {
t.contents = w
t.doLayout(t.Size())
}
func (t *TailBox) Draw(p *Painter) {
p.WithMask(image.Rect(0, 0, t.sz.X, t.sz.Y), func(p *Painter) {
// Draw background
p.FillRect(0, 0, t.sz.X, t.sz.Y)
// Draw from the bottom up.
space := t.sz.Y
p.Translate(0, space)
defer p.Restore()
for i := len(t.contents) - 1; i >= 0 && space > 0; i-- {
w := t.contents[i]
space -= w.Size().Y
p.Translate(0, -w.Size().Y)
defer p.Restore()
w.Draw(p)
}
})
}
// Resize recalculates the layout of the box's contents.
func (t *TailBox) Resize(size image.Point) {
t.WidgetBase.Resize(size)
defer func() {
t.sz = size
}()
// If it's just a height change, Draw should do the right thing already.
if size.X != t.sz.X {
t.doLayout(size)
}
}
func (t *TailBox) doLayout(size image.Point) {
for _, w := range t.contents {
hint := w.SizeHint()
// Set the width to the container width, and height to the requested height
w.Resize(image.Pt(size.X, hint.Y))
// ...and then resize again, now that the Y-hint has been refreshed by the X-value.
hint = w.SizeHint()
w.Resize(image.Pt(size.X, hint.Y))
}
}