summary refs log tree commit diff stats
path: root/scripts
Commit message (Expand)AuthorAgeFilesLines
* added scripts/ranger (simple link to ranger.py)hut2010-06-181-0/+1
='#n21'>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 66 67 68 69 70 71 72 73 74 75 76 77
' href='/akspecs/aerc/commit/lib/ui/stack.go?h=0.1.1&id=1c41b63f08d9b46ed558b0b3470cd2a2a9dfaf97'>1c41b63 ^
4675648 ^





a0c2b1c ^



6728a11 ^
1c41b63 ^
4675648 ^


1c41b63 ^

4675648 ^






80e891a ^
4675648 ^



































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
66
67
68
69
70
71
72




             
                                  


                   
                               
                                       





                        



                                           
                                                                 
                                                                     


                                  

                                               






                                        
                                                                                  



































                                                                        
package ui

import (
	"fmt"

	"github.com/gdamore/tcell"
)

type Stack struct {
	children     []Drawable
	onInvalidate []func(d Drawable)
}

func NewStack() *Stack {
	return &Stack{}
}

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

func (stack *Stack) OnInvalidate(onInvalidate func(d Drawable)) {
	stack.onInvalidate = append(stack.onInvalidate, onInvalidate)
}

func (stack *Stack) Invalidate() {
	for _, fn := range stack.onInvalidate {
		fn(stack)
	}
}

func (stack *Stack) Draw(ctx *Context) {
	if len(stack.children) > 0 {
		stack.Peek().Draw(ctx)
	} else {
		ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
	}
}

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