-
Notifications
You must be signed in to change notification settings - Fork 3
/
goui.go
116 lines (98 loc) · 2.72 KB
/
goui.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
//
// Package goui provides a cross platform GUI solution for Go developers.
//
// It uses Cocoa/WebKit for macOS, MSHTML for Windows and Gtk/WebKit for Linux.
//
// It provides two way bindings between Go and Javascript.
//
//
package goui
import "C"
import (
"encoding/json"
)
/*type iWindow interface {
create(Settings)
exit()
activate()
invokeJS(string)
}*/
// Settings is to configure the window's appearance
type Settings struct {
Title string //Title of the application window
UIDir string //Directory of the UI/Web related files, default: "ui"
Index string //Index html file, default: "index.html"
Url string //Full url address if you don't use WebDir + Index
Left int
Top int
Width int
Height int
Resizable bool
Debug bool
}
type Widget interface {
Register()
}
//as goui designed to support only single-page application, it is reasonable to hold a window globally
// Create is to create a native window with a webview
//
func Create(settings Settings) (err error) {
return CreateWithMenu(settings, nil)
}
func CreateWithMenu(settings Settings, menuDefs []MenuDef) (err error) {
create(settings, menuDefs)
defer exit()
return
}
// Service is to add a backend service for frontend to invoke.
// params:
// url - the url act as an unique identifier of a service, for example, "user/login", "blog/get/:id".
// handler - the function that handle the client request.
func Service(url string, handler func(*Context)) {
route := new(route)
route.handler = handler
parseRoute(url, route)
}
func RegisterWidgets(widgets ...Widget) {
for _, w := range widgets {
w.Register()
}
}
type JSServiceOptions struct {
Url string `json:"url"`
Data interface{} `json:"data"`
Success string `json:"success"`
Error string `json:"error"`
}
// RequestJSService is to send a request to the front end from the main thread
func RequestJSService(options JSServiceOptions) (err error) {
ops, err := json.Marshal(options)
if err != nil {
return
}
invokeJS("goui.handleRequest("+string(ops)+")", true)
return
}
// RequestJSServiceFromBackground is to send a request to the front end from a background thread
func RequestJSServiceFromBackground(options JSServiceOptions) (err error) {
ops, err := json.Marshal(options)
if err != nil {
return
}
invokeJS("goui.handleRequest("+string(ops)+")", false)
return
}
func ActivateWindow() {
//window.activate()
}
//InvokeJavascriptFunc is for the backend to invoke frontend javascript directly.
//params:
// name - javascript function name
// params - the parameters
/*func InvokeJavascriptFunc(name string, params ...interface{}) {
js := fiputil.MkString(params,name + "(",",",")")
worker.invokeJS(js)
}
*/
func OpenDevTools() {
}