Add stack UI container

This commit is contained in:
Drew DeVault 2018-02-27 19:30:59 -05:00
parent 384fe0d826
commit 46756487fb
3 changed files with 84 additions and 8 deletions

View File

@ -71,8 +71,11 @@ func main() {
fill('.'), libui.BORDER_RIGHT)).At(1, 0).Span(2, 1) fill('.'), libui.BORDER_RIGHT)).At(1, 0).Span(2, 1)
grid.AddChild(tabs.TabStrip).At(0, 1) grid.AddChild(tabs.TabStrip).At(0, 1)
grid.AddChild(tabs.TabContent).At(1, 1) grid.AddChild(tabs.TabContent).At(1, 1)
statusbar := libui.NewStack()
exline := widgets.NewExLine() exline := widgets.NewExLine()
grid.AddChild(exline).At(2, 1) statusbar.Push(exline)
grid.AddChild(statusbar).At(2, 1)
ui, err := libui.Initialize(conf, grid) ui, err := libui.Initialize(conf, grid)
if err != nil { if err != nil {

View File

@ -10,7 +10,7 @@ type Grid struct {
rowLayout []gridLayout rowLayout []gridLayout
columns []GridSpec columns []GridSpec
columnLayout []gridLayout columnLayout []gridLayout
Cells []*GridCell cells []*GridCell
onInvalidate func(d Drawable) onInvalidate func(d Drawable)
invalid bool invalid bool
} }
@ -76,7 +76,7 @@ func (grid *Grid) Draw(ctx *Context) {
if invalid { if invalid {
grid.reflow(ctx) grid.reflow(ctx)
} }
for _, cell := range grid.Cells { for _, cell := range grid.cells {
if !cell.invalid && !invalid { if !cell.invalid && !invalid {
continue continue
} }
@ -140,7 +140,7 @@ func (grid *Grid) invalidateLayout() {
func (grid *Grid) Invalidate() { func (grid *Grid) Invalidate() {
grid.invalidateLayout() grid.invalidateLayout()
for _, cell := range grid.Cells { for _, cell := range grid.cells {
cell.Content.Invalidate() cell.Content.Invalidate()
} }
} }
@ -156,7 +156,7 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
Content: content, Content: content,
invalid: true, invalid: true,
} }
grid.Cells = append(grid.Cells, cell) grid.cells = append(grid.cells, cell)
cell.Content.OnInvalidate(grid.cellInvalidated) cell.Content.OnInvalidate(grid.cellInvalidated)
cell.invalid = true cell.invalid = true
grid.invalidateLayout() grid.invalidateLayout()
@ -164,9 +164,9 @@ func (grid *Grid) AddChild(content Drawable) *GridCell {
} }
func (grid *Grid) RemoveChild(cell *GridCell) { func (grid *Grid) RemoveChild(cell *GridCell) {
for i, _cell := range grid.Cells { for i, _cell := range grid.cells {
if _cell == cell { if _cell == cell {
grid.Cells = append(grid.Cells[:i], grid.Cells[i+1:]...) grid.cells = append(grid.cells[:i], grid.cells[i+1:]...)
break break
} }
} }
@ -175,7 +175,7 @@ func (grid *Grid) RemoveChild(cell *GridCell) {
func (grid *Grid) cellInvalidated(drawable Drawable) { func (grid *Grid) cellInvalidated(drawable Drawable) {
var cell *GridCell var cell *GridCell
for _, cell = range grid.Cells { for _, cell = range grid.cells {
if cell.Content == drawable { if cell.Content == drawable {
break break
} }

73
lib/ui/stack.go Normal file
View File

@ -0,0 +1,73 @@
package ui
import (
"fmt"
tb "github.com/nsf/termbox-go"
)
type Stack struct {
children []Drawable
onInvalidate func(d Drawable)
}
func NewStack() *Stack {
return &Stack{}
}
func (stack *Stack) OnInvalidate(onInvalidate func (d Drawable)) {
stack.onInvalidate = onInvalidate
}
func (stack *Stack) Invalidate() {
if stack.onInvalidate != nil {
stack.onInvalidate(stack)
}
}
func (stack *Stack) Draw(ctx *Context) {
if len(stack.children) > 0 {
stack.Peek().Draw(ctx)
} else {
cell := tb.Cell{
Fg: tb.ColorDefault,
Bg: tb.ColorDefault,
Ch: ' ',
}
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), cell)
}
}
func (stack *Stack) Push(d Drawable) {
if len(stack.children) != 0 {
stack.Peek().OnInvalidate(nil)
}
stack.children = append(stack.children, d)
d.OnInvalidate(stack.invalidateFromChild)
stack.Invalidate()
}
func (stack *Stack) Pop() Drawable {
if len(stack.children) == 0 {
panic(fmt.Errorf("Tried to pop from an empty UI stack"))
}
d := stack.children[len(stack.children)-1]
stack.children = stack.children[:len(stack.children)-1]
stack.Invalidate()
d.OnInvalidate(nil)
if len(stack.children) != 0 {
stack.Peek().OnInvalidate(stack.invalidateFromChild)
}
return d
}
func (stack *Stack) Peek() Drawable {
if len(stack.children) == 0 {
panic(fmt.Errorf("Tried to peek from an empty stack"))
}
return stack.children[len(stack.children)-1]
}
func (stack *Stack) invalidateFromChild(d Drawable) {
stack.Invalidate()
}