Flex layout can have nil elements.

This commit is contained in:
Oliver 2018-02-06 12:30:52 +01:00
parent d448b35bc5
commit acf9158c77
1 changed files with 18 additions and 11 deletions

13
flex.go
View File

@ -12,7 +12,7 @@ const (
// flexItem holds layout options for one item. // flexItem holds layout options for one item.
type flexItem struct { type flexItem struct {
Item Primitive // The item to be positioned. Item Primitive // The item to be positioned. May be nil for an empty item.
FixedSize int // The item's fixed size which may not be changed, 0 if it has no fixed size. 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. Proportion int // The item's proportion.
Focus bool // Whether or not this item attracts the layout's focus. Focus bool // Whether or not this item attracts the layout's focus.
@ -72,6 +72,9 @@ func (f *Flex) SetFullScreen(fullScreen bool) *Flex {
// If "focus" is set to true, the item will receive focus when the Flex // 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 // primitive receives focus. If multiple items have the "focus" flag set to
// true, the first one will receive focus. // true, the first one will receive focus.
//
// You can provide a nil value for the primitive. This will still consume screen
// space but nothing will be drawn.
func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) *Flex { func (f *Flex) AddItem(item Primitive, fixedSize, proportion int, focus bool) *Flex {
f.items = append(f.items, flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus}) f.items = append(f.items, flexItem{Item: item, FixedSize: fixedSize, Proportion: proportion, Focus: focus})
return f return f
@ -116,13 +119,16 @@ func (f *Flex) Draw(screen tcell.Screen) {
distSize -= size distSize -= size
proportionSum -= item.Proportion proportionSum -= item.Proportion
} }
if item.Item != nil {
if f.direction == FlexColumn { if f.direction == FlexColumn {
item.Item.SetRect(pos, y, size, height) item.Item.SetRect(pos, y, size, height)
} else { } else {
item.Item.SetRect(x, pos, width, size) item.Item.SetRect(x, pos, width, size)
} }
}
pos += size pos += size
if item.Item != nil {
if item.Item.GetFocusable().HasFocus() { if item.Item.GetFocusable().HasFocus() {
defer item.Item.Draw(screen) defer item.Item.Draw(screen)
} else { } else {
@ -130,11 +136,12 @@ func (f *Flex) Draw(screen tcell.Screen) {
} }
} }
} }
}
// Focus is called when this primitive receives focus. // Focus is called when this primitive receives focus.
func (f *Flex) Focus(delegate func(p Primitive)) { func (f *Flex) Focus(delegate func(p Primitive)) {
for _, item := range f.items { for _, item := range f.items {
if item.Focus { if item.Item != nil && item.Focus {
delegate(item.Item) delegate(item.Item)
return return
} }
@ -144,7 +151,7 @@ func (f *Flex) Focus(delegate func(p Primitive)) {
// HasFocus returns whether or not this primitive has focus. // HasFocus returns whether or not this primitive has focus.
func (f *Flex) HasFocus() bool { func (f *Flex) HasFocus() bool {
for _, item := range f.items { for _, item := range f.items {
if item.Item.GetFocusable().HasFocus() { if item.Item != nil && item.Item.GetFocusable().HasFocus() {
return true return true
} }
} }