-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
driver.go
446 lines (364 loc) · 10.1 KB
/
driver.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// +build darwin,amd64
// Package mac is the driver to be used for apps that run on MacOS.
// It is build on the top of Cocoa and Webkit.
package mac
/*
#include "driver.h"
#include "bridge.h"
*/
import "C"
import (
"crypto/md5"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/murlokswarm/app"
"github.com/murlokswarm/app/bridge"
"github.com/pkg/errors"
)
var (
driver *Driver
)
func init() {
app.Loggers = []app.Logger{
app.NewLogger(os.Stdout, os.Stderr, true, true),
}
}
// Driver is the app.Driver implementation for MacOS.
type Driver struct {
app.BaseDriver
// Menubar configuration
MenubarConfig MenuBarConfig
// Component url to load in the dock.
DockURL string
// The bundle configuration.
// Only applied when the app is build with goapp mac build -bundle.
Bundle Bundle
// The handler called when the app is running.
OnRun func()
// The handler called when the app is focused.
OnFocus func()
// The handler called when the app loses focus.
OnBlur func()
// The handler called when the app is reopened.
OnReopen func(hasVisibleWindows bool)
// The handler called when a file associated with the app is opened.
OnFilesOpen func(filenames []string)
// The handler called when the app URI is invoked.
OnURLOpen func(u *url.URL)
// The handler called when the quit button is clicked.
OnQuit func() bool
// The handler called when the app is about to exit.
OnExit func()
factory app.Factory
elements app.ElemDB
uichan chan func()
macRPC bridge.PlatformRPC
goRPC bridge.GoRPC
menubar app.Menu
dock app.DockTile
devID string
droppedFiles []string
}
// Name satisfies the app.Driver interface.
func (d *Driver) Name() string {
return "MacOS"
}
// Run satisfies the app.Driver interface.
func (d *Driver) Run(f app.Factory) error {
if bundle := os.Getenv("GOAPP_BUNDLE"); bundle == "true" {
data, err := json.MarshalIndent(d.Bundle, "", " ")
if err != nil {
os.Exit(1)
}
return ioutil.WriteFile("goapp-mac.json", data, 0777)
}
if driver != nil {
return errors.Errorf("driver is already running")
}
d.devID = generateDevID()
d.factory = f
elements := app.NewElemDB()
elements = app.ConcurrentElemDB(elements)
d.elements = elements
d.uichan = make(chan func(), 4096)
defer close(d.uichan)
d.macRPC.Handler = macCall
d.goRPC.Handle("driver.OnRun", d.onRun)
d.goRPC.Handle("driver.OnFocus", d.onFocus)
d.goRPC.Handle("driver.OnBlur", d.onBlur)
d.goRPC.Handle("driver.OnReopen", d.onReopen)
d.goRPC.Handle("driver.OnFilesOpen", d.onFilesOpen)
d.goRPC.Handle("driver.OnURLOpen", d.onURLOpen)
d.goRPC.Handle("driver.OnFileDrop", d.onFileDrop)
d.goRPC.Handle("driver.OnQuit", d.onQuit)
d.goRPC.Handle("driver.OnExit", d.onExit)
d.goRPC.Handle("windows.OnMove", handleWindow(onWindowMove))
d.goRPC.Handle("windows.OnResize", handleWindow(onWindowResize))
d.goRPC.Handle("windows.OnFocus", handleWindow(onWindowFocus))
d.goRPC.Handle("windows.OnBlur", handleWindow(onWindowBlur))
d.goRPC.Handle("windows.OnFullScreen", handleWindow(onWindowFullScreen))
d.goRPC.Handle("windows.OnExitFullScreen", handleWindow(onWindowExitFullScreen))
d.goRPC.Handle("windows.OnMinimize", handleWindow(onWindowMinimize))
d.goRPC.Handle("windows.OnDeminimize", handleWindow(onWindowDeminimize))
d.goRPC.Handle("windows.OnClose", handleWindow(onWindowClose))
d.goRPC.Handle("windows.OnCallback", handleWindow(onWindowCallback))
d.goRPC.Handle("windows.OnNavigate", handleWindow(onWindowNavigate))
d.goRPC.Handle("menus.OnClose", handleMenu(onMenuClose))
d.goRPC.Handle("menus.OnCallback", handleMenu(onMenuCallback))
d.goRPC.Handle("filePanels.OnSelect", handleFilePanel(onFilePanelSelect))
d.goRPC.Handle("saveFilePanels.OnSelect", handleSaveFilePanel(onSaveFilePanelSelect))
d.goRPC.Handle("notifications.OnReply", handleNotification(onNotificationReply))
driver = d
errC := make(chan error)
go func() {
errC <- d.macRPC.Call("driver.Run", nil, nil)
}()
for {
select {
case f := <-d.uichan:
f()
case err := <-errC:
return err
}
}
}
func (d *Driver) onRun(in map[string]interface{}) interface{} {
err := d.newMenuBar()
if err != nil {
panic(err)
}
if d.dock, err = newDockTile(app.MenuConfig{
DefaultURL: d.DockURL,
}); err != nil {
panic(err)
}
if d.OnRun != nil {
d.OnRun()
}
return nil
}
func (d *Driver) onFocus(in map[string]interface{}) interface{} {
if d.OnFocus != nil {
d.OnFocus()
}
return nil
}
func (d *Driver) onBlur(in map[string]interface{}) interface{} {
if d.OnBlur != nil {
d.OnBlur()
}
return nil
}
func (d *Driver) onReopen(in map[string]interface{}) interface{} {
if d.OnReopen != nil {
d.OnReopen(in["HasVisibleWindows"].(bool))
}
return nil
}
func (d *Driver) onFilesOpen(in map[string]interface{}) interface{} {
if d.OnFilesOpen != nil {
d.OnFilesOpen(bridge.Strings(in["Filenames"]))
}
return nil
}
func (d *Driver) onURLOpen(in map[string]interface{}) interface{} {
if d.OnURLOpen != nil {
u, err := url.Parse(in["URL"].(string))
if err != nil {
panic(errors.Wrap(err, "onURLOpen"))
}
d.OnURLOpen(u)
}
return nil
}
func (d *Driver) onFileDrop(in map[string]interface{}) interface{} {
d.droppedFiles = bridge.Strings(in["Filenames"])
return nil
}
// AppName satisfies the app.Driver interface.
func (d *Driver) AppName() string {
var out struct {
AppName string
}
if err := d.macRPC.Call("driver.Bundle", &out, nil); err != nil {
panic(err)
}
if len(out.AppName) != 0 {
return out.AppName
}
wd, err := os.Getwd()
if err != nil {
panic(errors.Wrap(err, "app name unreachable"))
}
return filepath.Base(wd)
}
// Resources satisfies the app.Driver interface.
func (d *Driver) Resources(path ...string) string {
var out struct {
Resources string
}
if err := d.macRPC.Call("driver.Bundle", &out, nil); err != nil {
panic(err)
}
wd, err := os.Getwd()
if err != nil {
panic(errors.Wrap(err, "resources unreachable"))
}
if filepath.Dir(out.Resources) == wd {
out.Resources = filepath.Join(wd, "resources")
}
resources := filepath.Join(path...)
return filepath.Join(out.Resources, resources)
}
// Storage satisfies the app.DriverWithStorage interface.
func (d *Driver) Storage(path ...string) string {
storage := filepath.Join(path...)
return filepath.Join(d.support(), "storage", storage)
}
func (d *Driver) support() string {
var out struct {
Support string
}
if err := d.macRPC.Call("driver.Bundle", &out, nil); err != nil {
panic(err)
}
// Set up the support directory in case of the app is not bundled.
if strings.HasSuffix(out.Support, "{appname}") {
wd, err := os.Getwd()
if err != nil {
panic(errors.Wrap(err, "support unreachable"))
}
appname := filepath.Base(wd) + "-" + d.devID
out.Support = strings.Replace(out.Support, "{appname}", appname, 1)
}
return out.Support
}
// NewWindow satisfies the app.DriverWithWindows interface.
func (d *Driver) NewWindow(c app.WindowConfig) (app.Window, error) {
return newWindow(c)
}
// NewContextMenu satisfies the app.Driver interface.
func (d *Driver) NewContextMenu(c app.MenuConfig) (app.Menu, error) {
c.Type = "context menu"
m, err := newMenu(c)
if err != nil {
return nil, err
}
err = d.macRPC.Call("driver.SetContextMenu", nil, m.ID())
return m, err
}
// Render satisfies the app.Driver interface.
func (d *Driver) Render(c app.Component) error {
e, err := d.elements.ElementByComponent(c)
if err != nil {
return err
}
return e.Render(c)
}
// ElementByComponent satisfies the app.Driver interface.
func (d *Driver) ElementByComponent(c app.Component) (app.ElementWithComponent, error) {
return d.elements.ElementByComponent(c)
}
// NewFilePanel satisfies the app.DriverWithFilePanels interface.
func (d *Driver) NewFilePanel(c app.FilePanelConfig) error {
return newFilePanel(c)
}
// NewSaveFilePanel satisfies the app.DriverWithFilePanels interface.
func (d *Driver) NewSaveFilePanel(c app.SaveFilePanelConfig) error {
return newSaveFilePanel(c)
}
// NewShare satisfies the app.DriverWithShare interface.
func (d *Driver) NewShare(v interface{}) error {
in := struct {
Share string
Type string
}{
Share: fmt.Sprint(v),
}
switch v.(type) {
case url.URL, *url.URL:
in.Type = "url"
default:
in.Type = "string"
}
return d.macRPC.Call("driver.Share", nil, in)
}
// NewNotification satisfies the app.DriverWithPopupNotifications
// interface.
func (d *Driver) NewNotification(config app.NotificationConfig) error {
return newNotification(config)
}
// MenuBar satisfies the app.DriverWithMenuBar interface.
func (d *Driver) MenuBar() (app.Menu, error) {
return d.menubar, nil
}
func (d *Driver) newMenuBar() error {
menubar, err := newMenu(app.MenuConfig{Type: "menubar"})
if err != nil {
return errors.Wrap(err, "creating the menu bar failed")
}
d.menubar = menubar
if len(d.MenubarConfig.URL) == 0 {
format := "mac.menubar?appurl=%s&editurl=%s&windowurl=%s&helpurl=%s"
for _, u := range d.MenubarConfig.CustomURLs {
format += "&custom=" + u
}
err = menubar.Load(
format,
d.MenubarConfig.AppURL,
d.MenubarConfig.EditURL,
d.MenubarConfig.WindowURL,
d.MenubarConfig.HelpURL,
)
} else {
err = menubar.Load(d.MenubarConfig.URL)
}
if err != nil {
return err
}
if err = d.macRPC.Call("driver.SetMenubar", nil, menubar.ID()); err != nil {
return errors.Wrap(err, "set menu bar")
}
return nil
}
// Dock satisfies the app.DriverWithDock interface.
func (d *Driver) Dock() (app.DockTile, error) {
return d.dock, nil
}
// CallOnUIGoroutine satisfies the app.Driver interface.
func (d *Driver) CallOnUIGoroutine(f func()) {
d.uichan <- f
}
// Close quits the app.
func (d *Driver) Close() error {
return d.macRPC.Call("driver.Quit", nil, nil)
}
func (d *Driver) onQuit(in map[string]interface{}) interface{} {
out := struct {
Quit bool
}{
Quit: true,
}
if d.OnQuit != nil {
out.Quit = d.OnQuit()
}
return out
}
func (d *Driver) onExit(in map[string]interface{}) interface{} {
if d.OnExit != nil {
d.OnExit()
}
return nil
}
func generateDevID() string {
h := md5.New()
wd, _ := os.Getwd()
io.WriteString(h, wd)
return fmt.Sprintf("%x", h.Sum(nil))
}