Debian Codegraph @

lib/ui/stack.go

raw
1 package ui
2
3 import (
4 "fmt"
5
6 "git.sr.ht/~rjarry/aerc/config"
7 "go.rockorager.dev/vaxis"
8 )
9
10 type Stack struct {
11 children []Drawable
12 uiConfig *config.UIConfig
13 }
14
15 func NewStack(uiConfig *config.UIConfig) *Stack {
16 return &Stack{uiConfig: uiConfig}
17 }
18
19 func (stack *Stack) Children() []Drawable {
20 return stack.children
21 }
22
23 func (stack *Stack) Invalidate() {
24 Invalidate()
25 }
26
27 func (stack *Stack) Draw(ctx *Context) {
28 if len(stack.children) > 0 {
29 stack.Peek().Draw(ctx)
30 } else {
31 ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ',
32 stack.uiConfig.GetStyle(config.STYLE_STACK))
33 }
34 }
35
36 func (stack *Stack) MouseEvent(localX int, localY int, event vaxis.Event) {
37 if len(stack.children) > 0 {
38 if element, ok := stack.Peek().(Mouseable); ok {
39 element.MouseEvent(localX, localY, event)
40 }
41 }
42 }
43
44 func (stack *Stack) Push(d Drawable) {
45 stack.children = append(stack.children, d)
46 stack.Invalidate()
47 }
48
49 func (stack *Stack) Pop() Drawable {
50 if len(stack.children) == 0 {
51 panic(fmt.Errorf("Tried to pop from an empty UI stack"))
52 }
53 d := stack.children[len(stack.children)-1]
54 stack.children = stack.children[:len(stack.children)-1]
55 stack.Invalidate()
56 return d
57 }
58
59 func (stack *Stack) Peek() Drawable {
60 if len(stack.children) == 0 {
61 panic(fmt.Errorf("Tried to peek from an empty stack"))
62 }
63 return stack.children[len(stack.children)-1]
64 }