aerc/lib/ui/stack.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

2018-02-28 01:30:59 +01:00
package ui
import (
"fmt"
"git.sr.ht/~rjarry/aerc/config"
"github.com/gdamore/tcell/v2"
2018-02-28 01:30:59 +01:00
)
type Stack struct {
children []Drawable
uiConfig config.UIConfig
2018-02-28 01:30:59 +01:00
}
func NewStack(uiConfig config.UIConfig) *Stack {
return &Stack{uiConfig: uiConfig}
2018-02-28 01:30:59 +01:00
}
func (stack *Stack) Children() []Drawable {
return stack.children
}
2018-02-28 01:30:59 +01:00
func (stack *Stack) Invalidate() {
Invalidate()
2018-02-28 01:30:59 +01:00
}
func (stack *Stack) Draw(ctx *Context) {
if len(stack.children) > 0 {
stack.Peek().Draw(ctx)
} else {
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ',
stack.uiConfig.GetStyle(config.STYLE_STACK))
2018-02-28 01:30:59 +01:00
}
}
func (stack *Stack) MouseEvent(localX int, localY int, event tcell.Event) {
if len(stack.children) > 0 {
if element, ok := stack.Peek().(Mouseable); ok {
element.MouseEvent(localX, localY, event)
}
}
}
2018-02-28 01:30:59 +01:00
func (stack *Stack) Push(d Drawable) {
stack.children = append(stack.children, d)
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()
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]
}