forked from noisetorch/NoiseTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.go
50 lines (38 loc) · 719 Bytes
/
views.go
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
package main
import (
"fmt"
"sync"
"github.com/aarzilli/nucular"
)
type ViewFunc func(ctx *ntcontext, w *nucular.Window)
type ViewStack struct {
stack [100]ViewFunc
sp int8
mu sync.Mutex
}
func NewViewStack() *ViewStack {
return &ViewStack{sp: -1}
}
func (v *ViewStack) Push(f ViewFunc) {
v.mu.Lock()
defer v.mu.Unlock()
v.stack[v.sp+1] = f
v.sp++
}
func (v *ViewStack) Pop() (ViewFunc, error) {
v.mu.Lock()
if v.sp <= 0 {
return nil, fmt.Errorf("Cannot pop root element from ViewStack")
}
defer (func() {
v.stack[v.sp] = nil
v.sp--
v.mu.Unlock()
})()
return v.stack[v.sp], nil
}
func (v *ViewStack) Peek() ViewFunc {
v.mu.Lock()
defer v.mu.Unlock()
return v.stack[v.sp]
}