forked from HACKERALERT/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.go
89 lines (70 loc) · 1.43 KB
/
Context.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
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
78
79
80
81
82
83
84
85
86
87
88
89
package giu
import (
"sync"
"github.com/Picocrypt/imgui-go"
)
var Context context
type Disposable interface {
Dispose()
}
type state struct {
valid bool
data Disposable
}
type context struct {
renderer imgui.Renderer
platform imgui.Platform
widgetIndexCounter int
// Indicate whether current application is running
isAlive bool
// States will used by custom widget to store data
state sync.Map
}
func (c *context) GetRenderer() imgui.Renderer {
return c.renderer
}
func (c *context) GetPlatform() imgui.Platform {
return c.platform
}
func (c *context) IO() imgui.IO {
return imgui.CurrentIO()
}
func (c *context) invalidAllState() {
c.state.Range(func(k, v interface{}) bool {
if s, ok := v.(*state); ok {
s.valid = false
}
return true
})
}
func (c *context) cleanState() {
c.state.Range(func(k, v interface{}) bool {
if s, ok := v.(*state); ok {
if !s.valid {
c.state.Delete(k)
s.data.Dispose()
}
}
return true
})
// Reset widgetIndexCounter
c.widgetIndexCounter = 0
}
func (c *context) SetState(id string, data Disposable) {
c.state.Store(id, &state{valid: true, data: data})
}
func (c *context) GetState(id string) interface{} {
if v, ok := c.state.Load(id); ok {
if s, ok := v.(*state); ok {
s.valid = true
return s.data
}
}
return nil
}
// Get widget index for current layout
func (c *context) GetWidgetIndex() int {
i := c.widgetIndexCounter
c.widgetIndexCounter++
return i
}