aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ui/stack.go
blob: c0aca4e40217c010e17a0f6e8a0d0386b8402aec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package ui

import (
	"fmt"

	"git.sr.ht/~rjarry/aerc/config"

	"github.com/gdamore/tcell/v2"
)

type Stack struct {
	children []Drawable
	uiConfig config.UIConfig
}

func NewStack(uiConfig config.UIConfig) *Stack {
	return &Stack{uiConfig: uiConfig}
}

func (stack *Stack) Children() []Drawable {
	return stack.children
}

func (stack *Stack) Invalidate() {
	Invalidate()
}

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))
	}
}

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)
		}
	}
}

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]
}