2017-12-15 14:29:21 +00:00
|
|
|
package tview
|
|
|
|
|
2017-12-27 21:55:50 +00:00
|
|
|
import (
|
2020-10-18 12:15:57 +00:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2017-12-27 21:55:50 +00:00
|
|
|
)
|
2017-12-15 14:29:21 +00:00
|
|
|
|
|
|
|
// Configuration values.
|
|
|
|
const (
|
2021-10-29 09:03:57 +00:00
|
|
|
FlexRow = 0 // One item per row.
|
|
|
|
FlexColumn = 1 // One item per column.
|
|
|
|
FlexRowCSS = 1 // As defined in CSS, items distributed along a row.
|
|
|
|
FlexColumnCSS = 0 // As defined in CSS, items distributed within a column.
|
2017-12-15 14:29:21 +00:00
|
|
|
)
|
|
|
|
|
2017-12-16 21:48:26 +00:00
|
|
|
// flexItem holds layout options for one item.
|
|
|
|
type flexItem struct {
|
2018-02-06 11:30:52 +00:00
|
|
|
Item Primitive // The item to be positioned. May be nil for an empty item.
|
2017-12-26 20:49:11 +00:00
|
|
|
FixedSize int // The item's fixed size which may not be changed, 0 if it has no fixed size.
|
|
|
|
Proportion int // The item's proportion.
|
2017-12-27 21:27:42 +00:00
|
|
|
Focus bool // Whether or not this item attracts the layout's focus.
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:35:27 +00:00
|
|
|
// Flex is a basic implementation of the Flexbox layout. The contained
|
|
|
|
// primitives are arranged horizontally or vertically. The way they are
|
|
|
|
// distributed along that dimension depends on their layout settings, which is
|
|
|
|
// either a fixed length or a proportional length. See AddItem() for details.
|
2018-01-07 15:39:06 +00:00
|
|
|
//
|
|
|
|
// See https://github.com/rivo/tview/wiki/Flex for an example.
|
2017-12-15 14:29:21 +00:00
|
|
|
type Flex struct {
|
2017-12-20 19:54:49 +00:00
|
|
|
*Box
|
|
|
|
|
|
|
|
// The items to be positioned.
|
2018-06-09 20:16:38 +00:00
|
|
|
items []*flexItem
|
2017-12-20 19:54:49 +00:00
|
|
|
|
|
|
|
// FlexRow or FlexColumn.
|
|
|
|
direction int
|
|
|
|
|
2018-05-02 15:02:52 +00:00
|
|
|
// If set to true, Flex will use the entire screen as its available space
|
|
|
|
// instead its box dimensions.
|
2017-12-20 19:54:49 +00:00
|
|
|
fullScreen bool
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-15 17:35:27 +00:00
|
|
|
// NewFlex returns a new flexbox layout container with no primitives and its
|
|
|
|
// direction set to FlexColumn. To add primitives to this layout, see AddItem().
|
|
|
|
// To change the direction, see SetDirection().
|
2018-05-02 15:02:52 +00:00
|
|
|
//
|
2021-03-12 17:48:52 +00:00
|
|
|
// Note that Box, the superclass of Flex, will not clear its contents so that
|
|
|
|
// any nil flex items will leave their background unchanged. To clear a Flex's
|
|
|
|
// background before any items are drawn, set it to a box with the desired
|
|
|
|
// color:
|
2018-05-02 15:02:52 +00:00
|
|
|
//
|
2021-03-12 17:48:52 +00:00
|
|
|
// flex.Box = NewBox()
|
2017-12-20 19:54:49 +00:00
|
|
|
func NewFlex() *Flex {
|
|
|
|
f := &Flex{
|
|
|
|
direction: FlexColumn,
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2021-03-12 17:48:52 +00:00
|
|
|
f.Box = NewBox()
|
|
|
|
f.Box.dontClear = true
|
2017-12-20 19:54:49 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirection sets the direction in which the contained primitives are
|
2021-10-29 09:03:57 +00:00
|
|
|
// distributed. This can be either FlexColumn (default) or FlexRow. Note that
|
|
|
|
// these are the opposite of what you would expect coming from CSS. You may also
|
|
|
|
// use FlexColumnCSS or FlexRowCSS, to remain in line with the CSS definition.
|
2017-12-20 19:54:49 +00:00
|
|
|
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 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 20:49:11 +00:00
|
|
|
// AddItem adds a new item to the container. The "fixedSize" argument is a width
|
|
|
|
// or height that may not be changed by the layout algorithm. A value of 0 means
|
|
|
|
// that its size is flexible and may be changed. The "proportion" argument
|
|
|
|
// defines the relative size of the item compared to other flexible-size items.
|
|
|
|
// For example, items with a proportion of 2 will be twice as large as items
|
2018-04-19 15:49:26 +00:00
|
|
|
// with a proportion of 1. The proportion must be at least 1 if fixedSize == 0
|
|
|
|
// (ignored otherwise).
|
2017-12-27 21:27:42 +00:00
|
|
|
//
|
|
|
|
// If "focus" is set to true, the item will receive focus when the Flex
|
|
|
|
// primitive receives focus. If multiple items have the "focus" flag set to
|
|
|
|
// true, the first one will receive focus.
|
2018-02-06 11:30:52 +00:00
|
|
|
//
|
|
|
|
// You can provide a nil value for the primitive. This will still consume screen
|
|
|
|
// space but nothing will be drawn.
|
2017-12-27 21:27:42 +00:00
|
|
|
func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) *Flex {
|
2018-06-09 20:16:38 +00:00
|
|
|
f.items = append(f.items, &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus})
|
2017-12-15 14:29:21 +00:00
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:07:46 +00:00
|
|
|
// RemoveItem removes all items for the given primitive from the container,
|
|
|
|
// keeping the order of the remaining items intact.
|
|
|
|
func (f *Flex) RemoveItem(p Primitive) *Flex {
|
|
|
|
for index := len(f.items) - 1; index >= 0; index-- {
|
|
|
|
if f.items[index].Item == p {
|
|
|
|
f.items = append(f.items[:index], f.items[index+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2021-11-08 15:13:18 +00:00
|
|
|
// GetItemCount returns the number of items in this container.
|
|
|
|
func (f *Flex) GetItemCount() int {
|
|
|
|
return len(f.items)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetItem returns the primitive at the given index, starting with 0 for the
|
|
|
|
// first primitive in this container.
|
|
|
|
//
|
|
|
|
// This function will panic for out of range indices.
|
|
|
|
func (f *Flex) GetItem(index int) Primitive {
|
|
|
|
return f.items[index].Item
|
|
|
|
}
|
|
|
|
|
2020-01-27 14:37:07 +00:00
|
|
|
// Clear removes all items from the container.
|
2020-01-21 18:13:47 +00:00
|
|
|
func (f *Flex) Clear() *Flex {
|
|
|
|
f.items = nil
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2018-06-07 12:19:25 +00:00
|
|
|
// ResizeItem sets a new size for the item(s) with the given primitive. If there
|
|
|
|
// are multiple Flex items with the same primitive, they will all receive the
|
|
|
|
// same size. For details regarding the size parameters, see AddItem().
|
|
|
|
func (f *Flex) ResizeItem(p Primitive, fixedSize, proportion int) *Flex {
|
|
|
|
for _, item := range f.items {
|
|
|
|
if item.Item == p {
|
|
|
|
item.FixedSize = fixedSize
|
|
|
|
item.Proportion = proportion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Draw draws this primitive onto the screen.
|
|
|
|
func (f *Flex) Draw(screen tcell.Screen) {
|
2020-11-17 18:33:25 +00:00
|
|
|
f.Box.DrawForSubclass(screen, f)
|
2018-05-15 14:37:51 +00:00
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// Calculate size and position of the items.
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// Do we use the entire screen?
|
|
|
|
if f.fullScreen {
|
|
|
|
width, height := screen.Size()
|
2018-01-14 12:50:58 +00:00
|
|
|
f.SetRect(0, 0, width, height)
|
2017-12-20 19:54:49 +00:00
|
|
|
}
|
|
|
|
|
2017-12-15 14:29:21 +00:00
|
|
|
// How much space can we distribute?
|
2017-12-21 17:08:53 +00:00
|
|
|
x, y, width, height := f.GetInnerRect()
|
2017-12-26 20:49:11 +00:00
|
|
|
var proportionSum int
|
2017-12-21 17:08:53 +00:00
|
|
|
distSize := width
|
2017-12-16 21:48:26 +00:00
|
|
|
if f.direction == FlexRow {
|
2017-12-21 17:08:53 +00:00
|
|
|
distSize = height
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
for _, item := range f.items {
|
2017-12-15 14:29:21 +00:00
|
|
|
if item.FixedSize > 0 {
|
|
|
|
distSize -= item.FixedSize
|
|
|
|
} else {
|
2017-12-26 20:49:11 +00:00
|
|
|
proportionSum += item.Proportion
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate positions and draw items.
|
2017-12-21 17:08:53 +00:00
|
|
|
pos := x
|
2017-12-16 21:48:26 +00:00
|
|
|
if f.direction == FlexRow {
|
2017-12-21 17:08:53 +00:00
|
|
|
pos = y
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
for _, item := range f.items {
|
2017-12-15 14:29:21 +00:00
|
|
|
size := item.FixedSize
|
|
|
|
if size <= 0 {
|
2019-10-17 09:33:25 +00:00
|
|
|
if proportionSum > 0 {
|
|
|
|
size = distSize * item.Proportion / proportionSum
|
|
|
|
distSize -= size
|
|
|
|
proportionSum -= item.Proportion
|
|
|
|
} else {
|
|
|
|
size = 0
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2018-02-06 11:30:52 +00:00
|
|
|
if item.Item != nil {
|
|
|
|
if f.direction == FlexColumn {
|
|
|
|
item.Item.SetRect(pos, y, size, height)
|
|
|
|
} else {
|
|
|
|
item.Item.SetRect(x, pos, width, size)
|
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
pos += size
|
|
|
|
|
2018-02-06 11:30:52 +00:00
|
|
|
if item.Item != nil {
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.Item.HasFocus() {
|
2018-02-06 11:30:52 +00:00
|
|
|
defer item.Item.Draw(screen)
|
|
|
|
} else {
|
|
|
|
item.Item.Draw(screen)
|
|
|
|
}
|
2017-12-20 19:54:49 +00:00
|
|
|
}
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Focus is called when this primitive receives focus.
|
2017-12-18 19:04:52 +00:00
|
|
|
func (f *Flex) Focus(delegate func(p Primitive)) {
|
2017-12-27 21:27:42 +00:00
|
|
|
for _, item := range f.items {
|
2018-02-06 11:30:52 +00:00
|
|
|
if item.Item != nil && item.Focus {
|
2017-12-27 21:27:42 +00:00
|
|
|
delegate(item.Item)
|
|
|
|
return
|
|
|
|
}
|
2017-12-16 21:48:26 +00:00
|
|
|
}
|
2021-11-08 16:05:49 +00:00
|
|
|
f.Box.Focus(delegate)
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-20 19:54:49 +00:00
|
|
|
// HasFocus returns whether or not this primitive has focus.
|
|
|
|
func (f *Flex) HasFocus() bool {
|
|
|
|
for _, item := range f.items {
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.Item != nil && item.Item.HasFocus() {
|
2017-12-20 19:54:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-11-08 16:05:49 +00:00
|
|
|
return f.Box.HasFocus()
|
2017-12-15 14:29:21 +00:00
|
|
|
}
|
2019-11-04 05:34:46 +00:00
|
|
|
|
2020-01-24 20:40:34 +00:00
|
|
|
// MouseHandler returns the mouse handler for this primitive.
|
2020-02-14 02:09:09 +00:00
|
|
|
func (f *Flex) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
|
|
|
return f.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
|
2020-01-24 20:40:34 +00:00
|
|
|
if !f.InRect(event.Position()) {
|
2020-02-14 02:09:09 +00:00
|
|
|
return false, nil
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
// Pass mouse events along to the first child item that takes it.
|
2020-01-24 20:40:34 +00:00
|
|
|
for _, item := range f.items {
|
2020-04-04 20:19:02 +00:00
|
|
|
if item.Item == nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-02-14 02:09:09 +00:00
|
|
|
consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
|
2020-01-24 20:40:34 +00:00
|
|
|
if consumed {
|
2020-03-27 17:41:44 +00:00
|
|
|
return
|
2020-01-24 20:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:41:44 +00:00
|
|
|
|
|
|
|
return
|
2020-01-24 20:40:34 +00:00
|
|
|
})
|
2019-11-04 05:34:46 +00:00
|
|
|
}
|
2020-08-18 10:05:43 +00:00
|
|
|
|
|
|
|
// InputHandler returns the handler for this primitive.
|
|
|
|
func (f *Flex) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
return f.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
|
|
|
|
for _, item := range f.items {
|
2020-11-17 18:33:25 +00:00
|
|
|
if item.Item != nil && item.Item.HasFocus() {
|
2020-08-18 10:05:43 +00:00
|
|
|
if handler := item.Item.InputHandler(); handler != nil {
|
|
|
|
handler(event, setFocus)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|