forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Context.go
171 lines (133 loc) · 3.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package giu
import (
"fmt"
"sync"
"github.com/AllenDang/imgui-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 {
// TODO: should be handled by mainthread tbh
// see https://github.com/faiface/mainthread/pull/4
isRunning bool
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
InputHandler InputHandler
FontAtlas *FontAtlas
textureLoadingQueue *queue.Queue
cssStylesheet cssStylesheet
m *sync.Mutex
}
func CreateContext(p imgui.Platform, r imgui.Renderer) *context {
result := context{
platform: p,
renderer: r,
cssStylesheet: make(cssStylesheet),
m: &sync.Mutex{},
}
result.FontAtlas = newFontAtlas()
// Create font
if len(result.FontAtlas.defaultFonts) == 0 {
io := result.IO()
io.Fonts().AddFontDefault()
fontAtlas := io.Fonts().TextureDataRGBA32()
r.SetFontTexture(fontAtlas)
} else {
result.FontAtlas.shouldRebuildFontAtlas = true
}
return &result
}
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 any) bool {
if s, ok := v.(*state); ok {
c.m.Lock()
s.valid = false
c.m.Unlock()
}
return true
})
}
func (c *context) cleanState() {
c.state.Range(func(k, v any) bool {
if s, ok := v.(*state); ok {
c.m.Lock()
valid := s.valid
c.m.Unlock()
if !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 {
c.m.Lock()
s.valid = true
c.m.Unlock()
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 {
c.m.Lock()
s.valid = true
c.m.Unlock()
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
}