-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider.go
112 lines (91 loc) · 2.17 KB
/
provider.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
package goui
/*
#include <stdlib.h>
#include "provider.h"
*/
import "C"
import (
"os"
"path"
"runtime"
"unsafe"
)
const defaultDir = "ui"
const defaultIndex = "index.html"
func BoolToCInt(b bool) (i C.int) {
if b {
i = 1
}
return
}
func convertSettings(settings Settings) C.WindowSettings {
//dir := path.Dir(settings.Url)
if settings.UIDir == "" {
settings.UIDir = defaultDir
}
if settings.Index == "" {
settings.Index = defaultIndex
}
if settings.Url == "" {
settings.Url = path.Join(settings.UIDir, settings.Index)
if runtime.GOOS == "linux" {
wd, _ := os.Getwd()
settings.Url = path.Join("file://", wd, settings.Url)
} else if runtime.GOOS == "android" {
settings.Url = path.Join("file:///android_asset/", settings.Url)
}
}
// windows needs WebDir and Index
// macOS and iOS need Url
return C.WindowSettings{C.CString(settings.Title),
C.CString(settings.UIDir),
//C.CString(abs),
C.CString(settings.Index),
C.CString(settings.Url),
C.int(settings.Left),
C.int(settings.Top),
C.int(settings.Width),
C.int(settings.Height),
BoolToCInt(settings.Resizable),
BoolToCInt(settings.Debug),
}
}
func convertMenuDef(def MenuDef) (cMenuDef C.MenuDef) {
cMenuDef = C.MenuDef{}
cMenuDef.title = C.CString(def.Title)
cMenuDef.action = C.CString(def.Action)
cMenuDef.key = C.CString(def.HotKey)
cMenuDef.menuType = C.MenuType(def.Type)
cMenuDef.children, cMenuDef.childrenCount = convertMenuDefs(def.Children)
return
}
func convertMenuDefs(defs []MenuDef) (array *C.MenuDef, count C.int) {
l := len(defs)
if l == 0 {
return
}
count = C.int(l)
array = C.allocMenuDefArray(count)
for i := 0; i < l; i++ {
cMenuDef := convertMenuDef(defs[i])
C.addChildMenu(array, cMenuDef, C.int(i))
}
return
}
func create(settings Settings, menuDefs []MenuDef) {
//C.Create((*C.WindowSettings)(unsafe.Pointer(settings)))
cs := convertSettings(settings)
cMenuDefs, count := convertMenuDefs(menuDefs)
cCreate(cs, cMenuDefs, count)
}
func activate() {
}
func invokeJS(js string, fromMainThread bool) {
cJs := C.CString(js)
Log("invoke:", js)
defer C.free(unsafe.Pointer(cJs))
cInvokeJS(cJs, BoolToCInt(fromMainThread))
}
func exit() {
cExit()
}