2018-02-28 01:30:59 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-11-05 10:19:46 +01:00
|
|
|
"git.sr.ht/~rjarry/aerc/config"
|
2020-07-27 10:03:55 +02:00
|
|
|
|
2020-11-30 23:07:03 +01:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2018-02-28 01:30:59 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Stack struct {
|
2018-06-12 02:04:21 +02:00
|
|
|
children []Drawable
|
2018-06-12 01:23:09 +02:00
|
|
|
onInvalidate []func(d Drawable)
|
2020-07-27 10:03:55 +02:00
|
|
|
uiConfig config.UIConfig
|
2018-02-28 01:30:59 +01:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:03:55 +02:00
|
|
|
func NewStack(uiConfig config.UIConfig) *Stack {
|
|
|
|
return &Stack{uiConfig: uiConfig}
|
2018-02-28 01:30:59 +01:00
|
|
|
}
|
|
|
|
|
2019-01-20 21:06:44 +01:00
|
|
|
func (stack *Stack) Children() []Drawable {
|
|
|
|
return stack.children
|
|
|
|
}
|
|
|
|
|
2018-06-12 02:04:21 +02:00
|
|
|
func (stack *Stack) OnInvalidate(onInvalidate func(d Drawable)) {
|
2018-06-12 01:23:09 +02:00
|
|
|
stack.onInvalidate = append(stack.onInvalidate, onInvalidate)
|
2018-02-28 01:30:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (stack *Stack) Invalidate() {
|
2018-06-12 01:23:09 +02:00
|
|
|
for _, fn := range stack.onInvalidate {
|
|
|
|
fn(stack)
|
2018-02-28 01:30:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (stack *Stack) Draw(ctx *Context) {
|
|
|
|
if len(stack.children) > 0 {
|
|
|
|
stack.Peek().Draw(ctx)
|
|
|
|
} else {
|
2020-07-27 10:03:55 +02:00
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ',
|
|
|
|
stack.uiConfig.GetStyle(config.STYLE_STACK))
|
2018-02-28 01:30:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-06 00:32:36 +02:00
|
|
|
func (stack *Stack) MouseEvent(localX int, localY int, event tcell.Event) {
|
|
|
|
if len(stack.children) > 0 {
|
2022-07-31 14:32:48 +02:00
|
|
|
if element, ok := stack.Peek().(Mouseable); ok {
|
2019-09-06 00:32:36 +02:00
|
|
|
element.MouseEvent(localX, localY, event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:30:59 +01:00
|
|
|
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()
|
|
|
|
}
|