-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
372 lines (319 loc) · 7.9 KB
/
app.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2018 The GoKi Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// based on golang.org/x/exp/shiny:
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build darwin
// +build !3d
package macdriver
import (
"fmt"
"go/build"
"image"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"sync"
"github.com/goki/gi/oswin"
"github.com/goki/gi/oswin/clip"
"github.com/goki/gi/oswin/cursor"
"golang.org/x/mobile/gl"
)
var theApp = &appImpl{
windows: make(map[uintptr]*windowImpl),
winlist: make([]*windowImpl, 0),
screens: make([]*oswin.Screen, 0),
name: "GoGi",
quitCloseCnt: make(chan struct{}),
}
type appImpl struct {
texture struct {
program gl.Program
pos gl.Attrib
mvp gl.Uniform
uvp gl.Uniform
inUV gl.Attrib
sample gl.Uniform
quad gl.Buffer
}
fill struct {
program gl.Program
pos gl.Attrib
mvp gl.Uniform
color gl.Uniform
quad gl.Buffer
}
mu sync.Mutex
windows map[uintptr]*windowImpl
winlist []*windowImpl
screens []*oswin.Screen
ctxtwin *windowImpl
name string
about string
quitting bool // set to true when quitting and closing windows
quitCloseCnt chan struct{} // counts windows to make sure all are closed before done
quitReqFunc func()
quitCleanFunc func()
}
func (app *appImpl) NewImage(size image.Point) (retBuf oswin.Image, retErr error) {
m := image.NewRGBA(image.Rectangle{Max: size})
return &imageImpl{
buf: m.Pix,
rgba: *m,
size: size,
}, nil
}
func (app *appImpl) NewTexture(win oswin.Window, size image.Point) (oswin.Texture, error) {
// TODO: can we compile these programs eagerly instead of lazily?
w := win.(*windowImpl)
w.glctxMu.Lock()
defer w.glctxMu.Unlock()
glctx := w.glctx
if glctx == nil {
return nil, fmt.Errorf("macdriver: no GL context available")
}
if !glctx.IsProgram(app.texture.program) {
p, err := compileProgram(glctx, textureVertexSrc, textureFragmentSrc)
if err != nil {
return nil, err
}
app.texture.program = p
app.texture.pos = glctx.GetAttribLocation(p, "pos")
app.texture.mvp = glctx.GetUniformLocation(p, "mvp")
app.texture.uvp = glctx.GetUniformLocation(p, "uvp")
app.texture.inUV = glctx.GetAttribLocation(p, "inUV")
app.texture.sample = glctx.GetUniformLocation(p, "sample")
app.texture.quad = glctx.CreateBuffer()
glctx.BindBuffer(gl.ARRAY_BUFFER, app.texture.quad)
glctx.BufferData(gl.ARRAY_BUFFER, quadCoords, gl.STATIC_DRAW)
}
t := &textureImpl{
w: w,
id: glctx.CreateTexture(),
size: size,
}
glctx.BindTexture(gl.TEXTURE_2D, t.id)
glctx.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, size.X, size.Y, gl.RGBA, gl.UNSIGNED_BYTE, nil)
glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
glctx.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
w.AddTexture(t)
return t, nil
}
func (app *appImpl) NewWindow(opts *oswin.NewWindowOptions) (oswin.Window, error) {
if opts == nil {
opts = &oswin.NewWindowOptions{}
}
opts.Fixup()
// can also apply further tuning here..
id, err := newWindow(opts)
if err != nil {
return nil, err
}
w := &windowImpl{
app: app,
id: id,
publish: make(chan struct{}),
winClose: make(chan struct{}),
publishDone: make(chan oswin.PublishResult),
drawDone: make(chan struct{}),
}
initWindow(w)
if opts.Title != "" {
w.SetTitle(opts.Title)
}
app.mu.Lock()
app.windows[id] = w
app.winlist = append(app.winlist, w)
app.mu.Unlock()
// todo: could try to find alternative screen number here..
sc := app.Screen(0)
w.PhysDPI = sc.PhysicalDPI
w.LogDPI = sc.LogicalDPI
w.Scrn = sc
w.Flag = opts.Flags
showWindow(w)
return w, nil
}
func (app *appImpl) DeleteWin(id uintptr) {
app.mu.Lock()
defer app.mu.Unlock()
_, ok := app.windows[id]
if !ok {
return
}
for i, w := range app.winlist {
if w.id == id {
app.winlist = append(app.winlist[:i], app.winlist[i+1:]...)
break
}
}
delete(app.windows, id)
}
func (app *appImpl) NScreens() int {
return len(app.screens)
}
func (app *appImpl) Screen(scrN int) *oswin.Screen {
sz := len(app.screens)
if scrN < sz {
return app.screens[scrN]
}
return nil
}
func (app *appImpl) ScreenByName(name string) *oswin.Screen {
for _, sc := range app.screens {
if sc.Name == name {
return sc
}
}
return nil
}
func (app *appImpl) NWindows() int {
app.mu.Lock()
defer app.mu.Unlock()
return len(app.winlist)
}
func (app *appImpl) Window(win int) oswin.Window {
app.mu.Lock()
defer app.mu.Unlock()
sz := len(app.winlist)
if win < sz {
return app.winlist[win]
}
return nil
}
func (app *appImpl) WindowByName(name string) oswin.Window {
app.mu.Lock()
defer app.mu.Unlock()
for _, win := range app.winlist {
if win.Name() == name {
return win
}
}
return nil
}
func (app *appImpl) WindowInFocus() oswin.Window {
app.mu.Lock()
defer app.mu.Unlock()
for _, win := range app.winlist {
if win.IsFocus() {
return win
}
}
return nil
}
func (app *appImpl) ContextWindow() oswin.Window {
app.mu.Lock()
cw := app.ctxtwin
app.mu.Unlock()
return cw
}
func (app *appImpl) Name() string {
return app.name
}
func (app *appImpl) SetName(name string) {
app.name = name
}
func (app *appImpl) About() string {
return app.about
}
func (app *appImpl) SetAbout(about string) {
app.about = about
}
func (app *appImpl) Platform() oswin.Platforms {
return oswin.MacOS
}
func (app *appImpl) PrefsDir() string {
usr, err := user.Current()
if err != nil {
log.Print(err)
return "/tmp"
}
return filepath.Join(usr.HomeDir, "Library")
}
func (app *appImpl) GoGiPrefsDir() string {
pdir := filepath.Join(app.PrefsDir(), "GoGi")
os.MkdirAll(pdir, 0755)
return pdir
}
func (app *appImpl) AppPrefsDir() string {
pdir := filepath.Join(app.PrefsDir(), app.Name())
os.MkdirAll(pdir, 0755)
return pdir
}
// SrcDir tries to locate dir in GOPATH/src/ or GOROOT/src/pkg/ and returns its
// full path. GOPATH may contain a list of paths. From Robin Elkind github.com/mewkiz/pkg
func SrcDir(dir string) (absDir string, err error) {
for _, srcDir := range build.Default.SrcDirs() {
absDir = filepath.Join(srcDir, dir)
finfo, err := os.Stat(absDir)
if err == nil && finfo.IsDir() {
return absDir, nil
}
}
return "", fmt.Errorf("unable to locate directory (%q) in GOPATH/src/ (%q) or GOROOT/src/pkg/ (%q)", dir, os.Getenv("GOPATH"), os.Getenv("GOROOT"))
}
func (app *appImpl) FontPaths() []string {
return []string{"/System/Library/Fonts", "/Library/Fonts"}
}
func (app *appImpl) ClipBoard(win oswin.Window) clip.Board {
app.mu.Lock()
app.ctxtwin = win.(*windowImpl)
app.mu.Unlock()
return &theClip
}
func (app *appImpl) Cursor(win oswin.Window) cursor.Cursor {
app.mu.Lock()
app.ctxtwin = win.(*windowImpl)
app.mu.Unlock()
return &theCursor
}
func (app *appImpl) OpenURL(url string) {
cmd := exec.Command("open", url)
cmd.Run()
}
func (app *appImpl) SetQuitReqFunc(fun func()) {
app.quitReqFunc = fun
}
func (app *appImpl) SetQuitCleanFunc(fun func()) {
app.quitCleanFunc = fun
}
func (app *appImpl) QuitReq() {
if app.quitting {
return
}
if app.quitReqFunc != nil {
app.quitReqFunc()
} else {
app.Quit()
}
}
func (app *appImpl) IsQuitting() bool {
return app.quitting
}
func (app *appImpl) QuitClean() {
app.quitting = true
if app.quitCleanFunc != nil {
app.quitCleanFunc()
}
app.mu.Lock()
nwin := len(app.winlist)
for i := nwin - 1; i >= 0; i-- {
win := app.winlist[i]
go win.Close()
}
app.mu.Unlock()
for i := 0; i < nwin; i++ {
<-app.quitCloseCnt
// fmt.Printf("win closed: %v\n", i)
}
}
func (app *appImpl) Quit() {
app.QuitClean()
callStopDriver()
}