Flex must know where to delegate focus.

This commit is contained in:
Oliver 2017-12-27 22:27:42 +01:00
parent 6751475a74
commit f6f32eec04
2 changed files with 18 additions and 9 deletions

View File

@ -7,12 +7,12 @@ import (
func main() {
app := tview.NewApplication()
flex := tview.NewFlex().
AddItem(tview.NewBox().SetBorder(true).SetTitle("Left (1/2 x width of Top)"), 0, 1).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Left (1/2 x width of Top)"), 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1), 0, 2).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1)
AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Right (20 cols)"), 20, 1, false)
if err := app.SetRoot(flex, true).SetFocus(flex).Run(); err != nil {
panic(err)
}

17
flex.go
View File

@ -13,6 +13,7 @@ type flexItem struct {
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.
Proportion int // The item's proportion.
Focus bool // Whether or not this item attracts the layout's focus.
}
// Flex is a basic implementation of a flexbox layout.
@ -61,8 +62,13 @@ func (f *Flex) SetFullScreen(fullScreen bool) *Flex {
// 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. Must be at least 1. Ignored if fixedSize > 0.
func (f *Flex) AddItem(item Primitive, fixedSize, proportion int) *Flex {
// with a proportion of 1. 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.
func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) *Flex {
f.items = append(f.items, flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion})
return f
}
@ -124,8 +130,11 @@ func (f *Flex) Draw(screen tcell.Screen) {
// Focus is called when this primitive receives focus.
func (f *Flex) Focus(delegate func(p Primitive)) {
if len(f.items) > 0 {
delegate(f.items[0].Item)
for _, item := range f.items {
if item.Focus {
delegate(item.Item)
return
}
}
}