2017-12-15 15:29:21 +01:00
|
|
|
package tview
|
|
|
|
|
|
|
|
import "github.com/gdamore/tcell"
|
|
|
|
|
|
|
|
// Configuration values.
|
|
|
|
const (
|
|
|
|
FlexRow = iota
|
|
|
|
FlexColumn
|
|
|
|
)
|
|
|
|
|
2017-12-16 22:48:26 +01:00
|
|
|
// flexItem holds layout options for one item.
|
|
|
|
type flexItem struct {
|
2017-12-15 15:29:21 +01:00
|
|
|
Item Primitive // The item to be positioned.
|
|
|
|
FixedSize int // The item's fixed size which may not be changed, 0 if it has no fixed size.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flex is a basic implementation of a flexbox layout.
|
|
|
|
type Flex struct {
|
2017-12-20 20:54:49 +01:00
|
|
|
*Box
|
|
|
|
|
|
|
|
// The items to be positioned.
|
|
|
|
items []flexItem
|
|
|
|
|
|
|
|
// FlexRow or FlexColumn.
|
|
|
|
direction int
|
|
|
|
|
|
|
|
// If set to true, will use the entire screen as its available space instead
|
|
|
|
// its box dimensions.
|
|
|
|
fullScreen bool
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlex returns a new flexbox layout container with the given primitives.
|
|
|
|
// The items all have no fixed size. If more control is needed, call AddItem().
|
|
|
|
// The direction argument must be FlexRow or FlexColumn.
|
2017-12-20 20:54:49 +01:00
|
|
|
func NewFlex() *Flex {
|
|
|
|
f := &Flex{
|
|
|
|
Box: NewBox(),
|
|
|
|
direction: FlexColumn,
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
2017-12-20 20:54:49 +01:00
|
|
|
f.focus = f
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirection sets the direction in which the contained primitives are
|
|
|
|
// distributed. This can be either FlexColumn (default) or FlexRow.
|
|
|
|
func (f *Flex) SetDirection(direction int) *Flex {
|
|
|
|
f.direction = direction
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetFullScreen sets the flag which, when true, causes the flex layout to use
|
|
|
|
// the entire screen space instead of whatever size it is currently assigned to.
|
|
|
|
func (f *Flex) SetFullScreen(fullScreen bool) *Flex {
|
|
|
|
f.fullScreen = fullScreen
|
|
|
|
return f
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddItem adds a new item to the container. fixedSize is a size that may not be
|
|
|
|
// changed. A value of 0 means that its size may be changed.
|
|
|
|
func (f *Flex) AddItem(item Primitive, fixedSize int) *Flex {
|
2017-12-16 22:48:26 +01:00
|
|
|
f.items = append(f.items, flexItem{Item: item, FixedSize: fixedSize})
|
2017-12-15 15:29:21 +01:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (f *Flex) Draw(screen tcell.Screen) {
|
|
|
|
// Calculate size and position of the items.
|
|
|
|
|
2017-12-20 20:54:49 +01:00
|
|
|
// Do we use the entire screen?
|
|
|
|
if f.fullScreen {
|
|
|
|
f.x = 0
|
|
|
|
f.y = 0
|
|
|
|
width, height := screen.Size()
|
|
|
|
f.width = width
|
|
|
|
f.height = height
|
|
|
|
}
|
|
|
|
|
2017-12-15 15:29:21 +01:00
|
|
|
// How much space can we distribute?
|
2017-12-21 18:08:53 +01:00
|
|
|
x, y, width, height := f.GetInnerRect()
|
2017-12-15 15:29:21 +01:00
|
|
|
var variables int
|
2017-12-21 18:08:53 +01:00
|
|
|
distSize := width
|
2017-12-16 22:48:26 +01:00
|
|
|
if f.direction == FlexRow {
|
2017-12-21 18:08:53 +01:00
|
|
|
distSize = height
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
2017-12-16 22:48:26 +01:00
|
|
|
for _, item := range f.items {
|
2017-12-15 15:29:21 +01:00
|
|
|
if item.FixedSize > 0 {
|
|
|
|
distSize -= item.FixedSize
|
|
|
|
} else {
|
|
|
|
variables++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate positions and draw items.
|
2017-12-21 18:08:53 +01:00
|
|
|
pos := x
|
2017-12-16 22:48:26 +01:00
|
|
|
if f.direction == FlexRow {
|
2017-12-21 18:08:53 +01:00
|
|
|
pos = y
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
2017-12-16 22:48:26 +01:00
|
|
|
for _, item := range f.items {
|
2017-12-15 15:29:21 +01:00
|
|
|
size := item.FixedSize
|
|
|
|
if size <= 0 {
|
|
|
|
size = distSize / variables
|
|
|
|
distSize -= size
|
|
|
|
variables--
|
|
|
|
}
|
2017-12-16 22:48:26 +01:00
|
|
|
if f.direction == FlexColumn {
|
2017-12-21 18:08:53 +01:00
|
|
|
item.Item.SetRect(pos, y, size, height)
|
2017-12-15 15:29:21 +01:00
|
|
|
} else {
|
2017-12-21 18:08:53 +01:00
|
|
|
item.Item.SetRect(x, pos, width, size)
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
|
|
|
pos += size
|
|
|
|
|
2017-12-20 20:54:49 +01:00
|
|
|
if item.Item.GetFocusable().HasFocus() {
|
|
|
|
defer item.Item.Draw(screen)
|
|
|
|
} else {
|
|
|
|
item.Item.Draw(screen)
|
|
|
|
}
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Focus is called when this primitive receives focus.
|
2017-12-18 20:04:52 +01:00
|
|
|
func (f *Flex) Focus(delegate func(p Primitive)) {
|
2017-12-16 22:48:26 +01:00
|
|
|
if len(f.items) > 0 {
|
2017-12-18 20:04:52 +01:00
|
|
|
delegate(f.items[0].Item)
|
2017-12-16 22:48:26 +01:00
|
|
|
}
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|
|
|
|
|
2017-12-20 20:54:49 +01:00
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (f *Flex) HasFocus() bool {
|
|
|
|
for _, item := range f.items {
|
|
|
|
if item.Item.GetFocusable().HasFocus() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2017-12-15 15:29:21 +01:00
|
|
|
}
|