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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
// Copyright 2016 The Tcell Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package views
import (
"github.com/gdamore/tcell"
)
// BoxLayout is a container Widget that lays out its child widgets in
// either a horizontal row or a vertical column.
type BoxLayout struct {
view View
orient Orientation
style tcell.Style // backing style
cells []*boxLayoutCell
width int
height int
changed bool
WidgetWatchers
}
type boxLayoutCell struct {
widget Widget
fill float64 // fill factor - 0.0 means no expansion
pad int // count of padding spaces (stretch)
frac float64 // calculated residual spacing, used internally
view *ViewPort
}
func (b *BoxLayout) hLayout() {
w, h := b.view.Size()
totf := 0.0
for _, c := range b.cells {
x, y := c.widget.Size()
totf += c.fill
b.width += x
if y > b.height {
b.height = y
}
c.pad = 0
c.frac = 0
}
extra := w - b.width
if extra < 0 {
extra = 0
}
resid := extra
if totf == 0 {
resid = 0
}
for _, c := range b.cells {
if c.fill > 0 {
c.frac = float64(extra) * c.fill / totf
c.pad = int(c.frac)
c.frac -= float64(c.pad)
resid -= c.pad
}
}
// Distribute any left over padding. We try to give it to the
// the cells with the highest residual fraction. It should be
// the case that no single cell gets more than one more cell.
for resid > 0 {
var best *boxLayoutCell
for _, c := range b.cells {
if c.fill == 0 {
continue
}
if best == nil || c.frac > best.frac {
best = c
}
}
best.pad++
best.frac = 0
resid--
}
x, y, xinc := 0, 0, 0
for _, c := range b.cells {
cw, _ := c.widget.Size()
xinc = cw + c.pad
cw += c.pad
c.view.Resize(x, y, cw, h)
c.widget.Resize()
x += xinc
}
}
func (b *BoxLayout) vLayout() {
w, h := b.view.Size()
totf := 0.0
for _, c := range b.cells {
x, y := c.widget.Size()
b.height += y
totf += c.fill
if x > b.width {
b.width = x
}
c.pad = 0
c.frac = 0
}
extra := h - b.height
if extra < 0 {
extra = 0
}
resid := extra
if totf == 0 {
resid = 0
}
for _, c := range b.cells {
if c.fill > 0 {
c.frac = float64(extra) * c.fill / totf
c.pad = int(c.frac)
c.frac -= float64(c.pad)
resid -= c.pad
}
}
// Distribute any left over padding. We try to give it to the
// the cells with the highest residual fraction. It should be
// the case that no single cell gets more than one more cell.
for resid > 0 {
var best *boxLayoutCell
for _, c := range b.cells {
if c.fill == 0 {
continue
}
if best == nil || c.frac > best.frac {
best = c
}
}
best.pad++
best.frac = 0
resid--
}
x, y, yinc := 0, 0, 0
for _, c := range b.cells {
_, ch := c.widget.Size()
yinc = ch + c.pad
ch += c.pad
c.view.Resize(x, y, w, ch)
c.widget.Resize()
y += yinc
}
}
func (b *BoxLayout) layout() {
if b.view == nil {
return
}
b.width, b.height = 0, 0
switch b.orient {
case Horizontal:
b.hLayout()
case Vertical:
b.vLayout()
default:
panic("Bad orientation")
}
b.changed = false
}
// Resize adjusts the layout when the underlying View changes size.
func (b *BoxLayout) Resize() {
b.layout()
// Now also let the children know we resized.
for i := range b.cells {
b.cells[i].widget.Resize()
}
b.PostEventWidgetResize(b)
}
// Draw is called to update the displayed content.
func (b *BoxLayout) Draw() {
if b.view == nil {
return
}
if b.changed {
b.layout()
}
b.view.Fill(' ', b.style)
for i := range b.cells {
b.cells[i].widget.Draw()
}
}
// Size returns the preferred size in character cells (width, height).
func (b *BoxLayout) Size() (int, int) {
return b.width, b.height
}
// SetView sets the View object used for the text bar.
func (b *BoxLayout) SetView(view View) {
b.changed = true
b.view = view
for _, c := range b.cells {
c.view.SetView(view)
}
}
// HandleEvent implements a tcell.EventHandler. The only events
// we care about are Widget change events from our children. We
// watch for those so that if the child changes, we can arrange
// to update our layout.
func (b *BoxLayout) HandleEvent(ev tcell.Event) bool {
switch ev.(type) {
case *EventWidgetContent:
// This can only have come from one of our children.
b.changed = true
b.PostEventWidgetContent(b)
return true
}
for _, c := range b.cells {
if c.widget.HandleEvent(ev) {
return true
}
}
return false
}
// AddWidget adds a widget to the end of the BoxLayout.
func (b *BoxLayout) AddWidget(widget Widget, fill float64) {
c := &boxLayoutCell{
widget: widget,
fill: fill,
view: NewViewPort(b.view, 0, 0, 0, 0),
}
widget.SetView(c.view)
b.cells = append(b.cells, c)
b.changed = true
widget.Watch(b)
b.layout()
b.PostEventWidgetContent(b)
}
// InsertWidget inserts a widget at the given offset. Offset 0 is the
// front. If the index is longer than the number of widgets, then it
// just gets appended to the end.
func (b *BoxLayout) InsertWidget(index int, widget Widget, fill float64) {
c := &boxLayoutCell{
widget: widget,
fill: fill,
view: NewViewPort(b.view, 0, 0, 0, 0),
}
c.widget.SetView(c.view)
if index < 0 {
index = 0
}
if index > len(b.cells) {
index = len(b.cells)
}
b.cells = append(b.cells, c)
copy(b.cells[index+1:], b.cells[index:])
b.cells[index] = c
widget.Watch(b)
b.layout()
b.PostEventWidgetContent(b)
}
// RemoveWidget removes a Widget from the layout.
func (b *BoxLayout) RemoveWidget(widget Widget) {
changed := false
for i := 0; i < len(b.cells); i++ {
if b.cells[i].widget == widget {
b.cells = append(b.cells[:i], b.cells[i+1:]...)
changed = true
}
}
if !changed {
return
}
b.changed = true
widget.Unwatch(b)
b.layout()
b.PostEventWidgetContent(b)
}
// Widgets returns the list of Widgets for this BoxLayout.
func (b *BoxLayout) Widgets() []Widget {
w := make([]Widget, 0, len(b.cells))
for _, c := range b.cells {
w = append(w, c.widget)
}
return w
}
// SetOrientation sets the orientation as either Horizontal or Vertical.
func (b *BoxLayout) SetOrientation(orient Orientation) {
if b.orient != orient {
b.orient = orient
b.changed = true
b.PostEventWidgetContent(b)
}
}
// SetStyle sets the style used.
func (b *BoxLayout) SetStyle(style tcell.Style) {
b.style = style
b.PostEventWidgetContent(b)
}
// NewBoxLayout creates an empty BoxLayout.
func NewBoxLayout(orient Orientation) *BoxLayout {
return &BoxLayout{orient: orient}
}
|