-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathContext.go
148 lines (115 loc) · 2.75 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package giu
import (
"fmt"
"sync"
imgui "github.com/AllenDang/cimgui-go"
"gopkg.in/eapache/queue.v1"
)
// Context represents a giu context.
var Context *context
// Disposable should be implemented by all states stored in context.
// Dispose method is called when state is removed from context.
type Disposable interface {
Dispose()
}
type genericDisposable[T any] interface {
Disposable
*T
}
type state struct {
valid bool
data Disposable
}
type context struct {
backend imgui.Backend
// TODO: should be handled by mainthread tbh
// see https://github.com/faiface/mainthread/pull/4
isRunning bool
widgetIndexCounter int
// Indicate whether current application is running
isAlive bool
// States will used by custom widget to store data
state sync.Map
InputHandler InputHandler
FontAtlas *FontAtlas
textureLoadingQueue *queue.Queue
cssStylesheet cssStylesheet
}
func CreateContext(b imgui.Backend) *context {
result := context{
cssStylesheet: make(cssStylesheet),
backend: b,
}
result.FontAtlas = newFontAtlas()
// Create font
if len(result.FontAtlas.defaultFonts) == 0 {
io := result.IO()
io.Fonts().AddFontDefault()
// TODO
//fontAtlas, _, _, _ := io.Fonts().GetTextureDataAsRGBA32()
// r.SetFontTexture(fontAtlas)
} else {
result.FontAtlas.shouldRebuildFontAtlas = true
}
return &result
}
func (c *context) IO() imgui.IO {
return imgui.CurrentIO()
}
func (c *context) invalidAllState() {
c.state.Range(func(k, v any) bool {
if s, ok := v.(*state); ok {
s.valid = false
}
return true
})
}
func (c *context) cleanState() {
c.state.Range(func(k, v any) 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 SetState[T any, PT genericDisposable[T]](c *context, id string, data PT) {
c.state.Store(id, &state{valid: true, data: data})
}
func (c *context) SetState(id string, data Disposable) {
c.state.Store(id, &state{valid: true, data: data})
}
func GetState[T any, PT genericDisposable[T]](c *context, id string) PT {
if s, ok := c.load(id); ok {
s.valid = true
data, isOk := s.data.(PT)
Assert(isOk, "Context", "GetState", fmt.Sprintf("got state of unexpected type: expected %T, instead found %T", new(T), s.data))
return data
}
return nil
}
func (c *context) GetState(id string) any {
if s, ok := c.load(id); ok {
s.valid = true
return s.data
}
return nil
}
func (c *context) load(id any) (*state, bool) {
if v, ok := c.state.Load(id); ok {
if s, ok := v.(*state); ok {
return s, true
}
}
return nil, false
}
// Get widget index for current layout.
func (c *context) GetWidgetIndex() int {
i := c.widgetIndexCounter
c.widgetIndexCounter++
return i
}