cview/flex.go

274 lines
7.1 KiB
Go
Raw Normal View History

package cview
2017-12-27 21:55:50 +00:00
import (
2020-03-25 14:32:57 +00:00
"sync"
2020-08-30 15:36:03 +00:00
"github.com/gdamore/tcell/v2"
2017-12-27 21:55:50 +00:00
)
// Configuration values.
const (
FlexRow = iota
FlexColumn
)
// 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.
Focus bool // Whether or not this item attracts the layout's focus.
}
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.
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
// 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
2020-03-25 14:32:57 +00:00
sync.RWMutex
}
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().
//
// Note that Flex will have a transparent background by default so that any nil
// flex items will show primitives behind the Flex.
// To disable this transparency:
//
// flex.SetBackgroundTransparent(false)
2017-12-20 19:54:49 +00:00
func NewFlex() *Flex {
f := &Flex{
Box: NewBox(),
2017-12-20 19:54:49 +00:00
direction: FlexColumn,
}
f.SetBackgroundTransparent(true)
2017-12-20 19:54:49 +00:00
f.focus = f
return f
}
2020-09-16 11:26:56 +00:00
// GetDirection returns the direction in which the contained primitives are
// distributed. This can be either FlexColumn (default) or FlexRow.
func (f *Flex) GetDirection() int {
f.RLock()
defer f.RUnlock()
2020-09-16 11:26:56 +00:00
return f.direction
}
2017-12-20 19:54:49 +00:00
// 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) {
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
2017-12-20 19:54:49 +00:00
f.direction = direction
}
// 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) {
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
2017-12-20 19:54:49 +00:00
f.fullScreen = fullScreen
}
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
// with a proportion of 1. The proportion must be at least 1 if fixedSize == 0
// (ignored otherwise).
//
// 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
//
// A nil value for the primitive represents empty space.
func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) {
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
if item == nil {
item = NewBox()
item.SetVisible(false)
}
2018-06-09 20:16:38 +00:00
f.items = append(f.items, &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus})
}
2020-09-16 12:44:53 +00:00
// AddItemAtIndex adds an item to the flex at a given index.
// For more information see AddItem.
func (f *Flex) AddItemAtIndex(index int, item Primitive, fixedSize, proportion int, focus bool) {
2020-09-16 12:44:53 +00:00
f.Lock()
defer f.Unlock()
newItem := &flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus}
if index == 0 {
f.items = append([]*flexItem{newItem}, f.items...)
} else {
f.items = append(f.items[:index], append([]*flexItem{newItem}, f.items[index:]...)...)
}
}
// 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) {
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
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:]...)
}
}
}
// 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) {
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
for _, item := range f.items {
if item.Item == p {
item.FixedSize = fixedSize
item.Proportion = proportion
}
}
}
// Draw draws this primitive onto the screen.
func (f *Flex) Draw(screen tcell.Screen) {
if !f.GetVisible() {
return
}
f.Box.Draw(screen)
2020-03-25 14:32:57 +00:00
f.Lock()
defer f.Unlock()
// 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()
f.SetRect(0, 0, width, height)
2017-12-20 19:54:49 +00:00
}
// How much space can we distribute?
x, y, width, height := f.GetInnerRect()
2017-12-26 20:49:11 +00:00
var proportionSum int
distSize := width
if f.direction == FlexRow {
distSize = height
}
for _, item := range f.items {
if item.FixedSize > 0 {
distSize -= item.FixedSize
} else {
2017-12-26 20:49:11 +00:00
proportionSum += item.Proportion
}
}
// Calculate positions and draw items.
pos := x
if f.direction == FlexRow {
pos = y
}
for _, item := range f.items {
size := item.FixedSize
if size <= 0 {
if proportionSum > 0 {
size = distSize * item.Proportion / proportionSum
distSize -= size
proportionSum -= item.Proportion
} else {
size = 0
}
}
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)
}
}
pos += size
2018-02-06 11:30:52 +00:00
if item.Item != nil {
if item.Item.GetFocusable().HasFocus() {
defer item.Item.Draw(screen)
} else {
item.Item.Draw(screen)
}
2017-12-20 19:54:49 +00:00
}
}
}
// Focus is called when this primitive receives focus.
func (f *Flex) Focus(delegate func(p Primitive)) {
2020-03-25 14:32:57 +00:00
f.Lock()
for _, item := range f.items {
2018-02-06 11:30:52 +00:00
if item.Item != nil && item.Focus {
2020-03-25 14:32:57 +00:00
f.Unlock()
delegate(item.Item)
return
}
}
2020-03-25 14:32:57 +00:00
f.Unlock()
}
2017-12-20 19:54:49 +00:00
// HasFocus returns whether or not this primitive has focus.
func (f *Flex) HasFocus() bool {
f.RLock()
defer f.RUnlock()
2020-03-25 14:32:57 +00:00
2017-12-20 19:54:49 +00:00
for _, item := range f.items {
2018-02-06 11:30:52 +00:00
if item.Item != nil && item.Item.GetFocusable().HasFocus() {
2017-12-20 19:54:49 +00:00
return true
}
}
return false
}
2019-11-04 05:34:46 +00:00
// MouseHandler returns the mouse handler for this primitive.
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) {
if !f.InRect(event.Position()) {
return false, nil
}
2020-03-25 14:32:57 +00:00
// Pass mouse events along to the first child item that takes it.
for _, item := range f.items {
if item.Item == nil {
continue
}
consumed, capture = item.Item.MouseHandler()(action, event, setFocus)
if consumed {
return
}
}
return
})
2019-11-04 05:34:46 +00:00
}