...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package views
16
17 import (
18 "sync"
19
20 "github.com/gdamore/tcell/v2"
21 )
22
23
24
25
26
27 type TextBar struct {
28 changed bool
29 style tcell.Style
30 left Text
31 right Text
32 center Text
33 view View
34 lview ViewPort
35 rview ViewPort
36 cview ViewPort
37 once sync.Once
38
39 WidgetWatchers
40 }
41
42
43
44 func (t *TextBar) SetCenter(s string, style tcell.Style) {
45 t.initialize()
46 if style == tcell.StyleDefault {
47 style = t.style
48 }
49 t.center.SetText(s)
50 t.center.SetStyle(style)
51 }
52
53
54 func (t *TextBar) SetLeft(s string, style tcell.Style) {
55 t.initialize()
56 if style == tcell.StyleDefault {
57 style = t.style
58 }
59 t.left.SetText(s)
60 t.left.SetStyle(style)
61 }
62
63
64 func (t *TextBar) SetRight(s string, style tcell.Style) {
65 t.initialize()
66 if style == tcell.StyleDefault {
67 style = t.style
68 }
69 t.right.SetText(s)
70 t.right.SetStyle(style)
71 }
72
73
74
75
76 func (t *TextBar) SetStyle(style tcell.Style) {
77 t.initialize()
78 t.style = style
79 }
80
81 func (t *TextBar) initialize() {
82 t.once.Do(func() {
83 t.center.SetView(&t.cview)
84 t.left.SetView(&t.lview)
85 t.right.SetView(&t.rview)
86 t.center.SetAlignment(VAlignTop | HAlignCenter)
87 t.left.SetAlignment(VAlignTop | HAlignLeft)
88 t.right.SetAlignment(VAlignTop | HAlignRight)
89 t.center.Watch(t)
90 t.left.Watch(t)
91 t.right.Watch(t)
92 })
93 }
94
95 func (t *TextBar) layout() {
96 w, _ := t.view.Size()
97 ww, wh := t.left.Size()
98 t.lview.Resize(0, 0, ww, wh)
99
100 ww, wh = t.center.Size()
101 t.cview.Resize((w-ww)/2, 0, ww, wh)
102
103 ww, wh = t.right.Size()
104 t.rview.Resize(w-ww, 0, ww, wh)
105
106 t.changed = false
107 }
108
109
110 func (t *TextBar) SetView(view View) {
111 t.initialize()
112 t.view = view
113 t.lview.SetView(view)
114 t.rview.SetView(view)
115 t.cview.SetView(view)
116 t.changed = true
117 }
118
119
120 func (t *TextBar) Draw() {
121
122 t.initialize()
123 if t.changed {
124 t.layout()
125 }
126 w, h := t.view.Size()
127 for y := 0; y < h; y++ {
128 for x := 0; x < w; x++ {
129 t.view.SetContent(x, y, ' ', nil, t.style)
130 }
131 }
132
133
134
135 t.right.Draw()
136 t.center.Draw()
137 t.left.Draw()
138 }
139
140
141
142 func (t *TextBar) Resize() {
143 t.initialize()
144 t.layout()
145
146 t.left.Resize()
147 t.center.Resize()
148 t.right.Resize()
149
150 t.PostEventWidgetResize(t)
151 }
152
153
154
155 func (t *TextBar) Size() (int, int) {
156 w, h := 0, 0
157
158 ww, wh := t.left.Size()
159 w += ww
160 if wh > h {
161 h = wh
162 }
163 ww, wh = t.center.Size()
164 w += ww
165 if wh > h {
166 h = wh
167 }
168 ww, wh = t.right.Size()
169 w += ww
170 if wh > h {
171 h = wh
172 }
173 return w, h
174 }
175
176
177
178
179 func (t *TextBar) HandleEvent(ev tcell.Event) bool {
180 switch ev.(type) {
181 case *EventWidgetContent:
182 t.changed = true
183 return true
184 }
185 return false
186 }
187
188
189 func NewTextBar() *TextBar {
190 t := &TextBar{}
191 t.initialize()
192 return t
193 }
194
View as plain text