2016-05-24 15:50:38 +08:00
|
|
|
// Copyright 2016 The Tcell Authors
|
2015-10-31 00:33:45 +08:00
|
|
|
//
|
|
|
|
// 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 (
|
2020-08-26 07:20:58 +08:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2015-10-31 00:33:45 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
func (b *BoxLayout) hLayout() {
|
2015-10-31 00:33:45 +08:00
|
|
|
w, h := b.view.Size()
|
|
|
|
|
|
|
|
totf := 0.0
|
|
|
|
for _, c := range b.cells {
|
|
|
|
x, y := c.widget.Size()
|
|
|
|
totf += c.fill
|
2015-11-05 09:05:24 +08:00
|
|
|
b.width += x
|
|
|
|
if y > b.height {
|
|
|
|
b.height = y
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
2015-11-05 09:05:24 +08:00
|
|
|
c.pad = 0
|
|
|
|
c.frac = 0
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
extra := w - b.width
|
2015-10-31 00:33:45 +08:00
|
|
|
if extra < 0 {
|
|
|
|
extra = 0
|
|
|
|
}
|
|
|
|
resid := extra
|
|
|
|
if totf == 0 {
|
|
|
|
resid = 0
|
|
|
|
}
|
2015-11-02 06:44:23 +08:00
|
|
|
|
2015-10-31 00:33:45 +08:00
|
|
|
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 {
|
2015-11-05 03:24:00 +08:00
|
|
|
var best *boxLayoutCell
|
2015-10-31 00:33:45 +08:00
|
|
|
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--
|
|
|
|
}
|
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
x, y, xinc := 0, 0, 0
|
2015-10-31 00:33:45 +08:00
|
|
|
for _, c := range b.cells {
|
2015-11-05 09:05:24 +08:00
|
|
|
cw, _ := c.widget.Size()
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
xinc = cw + c.pad
|
|
|
|
cw += c.pad
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
c.view.Resize(x, y, cw, h)
|
2017-01-07 14:16:22 +08:00
|
|
|
c.widget.Resize()
|
2015-11-05 09:05:24 +08:00
|
|
|
x += xinc
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
func (b *BoxLayout) vLayout() {
|
|
|
|
w, h := b.view.Size()
|
2015-10-31 00:33:45 +08:00
|
|
|
|
2015-11-05 09:05:24 +08:00
|
|
|
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
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
2015-11-05 09:05:24 +08:00
|
|
|
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)
|
2017-01-07 14:16:22 +08:00
|
|
|
c.widget.Resize()
|
2015-10-31 00:33:45 +08:00
|
|
|
y += yinc
|
|
|
|
}
|
2015-11-05 09:05:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
b.changed = false
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Resize adjusts the layout when the underlying View changes size.
|
2015-10-31 00:33:45 +08:00
|
|
|
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()
|
|
|
|
}
|
2018-09-24 10:54:34 +08:00
|
|
|
b.view.Fill(' ', b.style)
|
2015-10-31 00:33:45 +08:00
|
|
|
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:
|
2015-11-02 06:44:23 +08:00
|
|
|
// This can only have come from one of our children.
|
2015-10-31 00:33:45 +08:00
|
|
|
b.changed = true
|
2016-05-24 15:50:38 +08:00
|
|
|
b.PostEventWidgetContent(b)
|
2015-10-31 00:33:45 +08:00
|
|
|
return true
|
|
|
|
}
|
2015-11-02 06:44:23 +08:00
|
|
|
for _, c := range b.cells {
|
|
|
|
if c.widget.HandleEvent(ev) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// AddWidget adds a widget to the end of the BoxLayout.
|
2015-10-31 00:33:45 +08:00
|
|
|
func (b *BoxLayout) AddWidget(widget Widget, fill float64) {
|
|
|
|
c := &boxLayoutCell{
|
|
|
|
widget: widget,
|
|
|
|
fill: fill,
|
|
|
|
view: NewViewPort(b.view, 0, 0, 0, 0),
|
|
|
|
}
|
2016-05-24 15:50:38 +08:00
|
|
|
widget.SetView(c.view)
|
2015-10-31 00:33:45 +08:00
|
|
|
b.cells = append(b.cells, c)
|
|
|
|
b.changed = true
|
|
|
|
widget.Watch(b)
|
2015-11-02 06:44:23 +08:00
|
|
|
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()
|
2015-10-31 00:33:45 +08:00
|
|
|
b.PostEventWidgetContent(b)
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// RemoveWidget removes a Widget from the layout.
|
2015-10-31 00:33:45 +08:00
|
|
|
func (b *BoxLayout) RemoveWidget(widget Widget) {
|
2017-09-15 14:13:49 +08:00
|
|
|
changed := false
|
2015-10-31 00:33:45 +08:00
|
|
|
for i := 0; i < len(b.cells); i++ {
|
|
|
|
if b.cells[i].widget == widget {
|
2015-11-02 06:44:23 +08:00
|
|
|
b.cells = append(b.cells[:i], b.cells[i+1:]...)
|
2017-09-15 14:13:49 +08:00
|
|
|
changed = true
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-15 14:13:49 +08:00
|
|
|
if !changed {
|
|
|
|
return
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
b.changed = true
|
|
|
|
widget.Unwatch(b)
|
2015-11-02 06:44:23 +08:00
|
|
|
b.layout()
|
2015-10-31 00:33:45 +08:00
|
|
|
b.PostEventWidgetContent(b)
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// Widgets returns the list of Widgets for this BoxLayout.
|
2015-11-02 06:44:23 +08:00
|
|
|
func (b *BoxLayout) Widgets() []Widget {
|
|
|
|
w := make([]Widget, 0, len(b.cells))
|
|
|
|
for _, c := range b.cells {
|
|
|
|
w = append(w, c.widget)
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
2015-11-05 03:24:00 +08:00
|
|
|
// SetOrientation sets the orientation as either Horizontal or Vertical.
|
2015-10-31 00:33:45 +08:00
|
|
|
func (b *BoxLayout) SetOrientation(orient Orientation) {
|
2015-11-02 06:44:23 +08:00
|
|
|
if b.orient != orient {
|
|
|
|
b.orient = orient
|
|
|
|
b.changed = true
|
|
|
|
b.PostEventWidgetContent(b)
|
|
|
|
}
|
2015-10-31 00:33:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
}
|