This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
99 lines (86 loc) · 2.58 KB
/
main.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
package main
import (
_ "embed"
"encoding/json"
"log"
"os"
"strings"
"github.com/webview/webview"
)
const html = `
<html lang="en">
<head>
<meta charset="UTF-8">
<!-- Use minimum-scale=1 to enable GPU rasterization -->
<meta
name='viewport'
content='user-scalable=0, initial-scale=1, minimum-scale=1, width=device-width, height=device-height'
/>
<style>
body {
margin: 0;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",
Ubuntu,Cantarell,Oxygen-Sans,"Helvetica Neue",Arial,Roboto,sans-serif;
}
* { margin: 0; padding: 0; }
</style>
</head>
<body><div id="app"></div><script>initiateReact()</script></body>
</html>
`
//go:embed dist/main.js
var js string
var w webview.WebView
const REQ_URL string = "http://localhost:7292" // TODO: Add some config around this.
// ParseToJsString takes a string and escapes slashes and double-quotes,
// and converts it to a string that can be passed to JavaScript.
func ParseToJsString(s string) string {
return "\"" + strings.ReplaceAll(strings.ReplaceAll(s, "\\", "\\\\"), "\"", "\\\"") + "\""
}
// SetToken sets the value of the config.Token variable in both Go and React.
func SetToken(value string) {
config.Token = value
w.Eval("setTokenReact(" + ParseToJsString(value) + ")")
SaveConfig()
}
// SetCache sets the value of the config.Cache variable in both Go and React.
func SetCache(cache []Todo) {
value, err := json.Marshal(cache)
if err != nil {
log.Println("failed to serialise []Todo to JSON to send to webview")
return
}
config.Cache = cache
w.Eval("setCacheReact(JSON.parse(" + ParseToJsString(string(value)) + "))")
SaveConfig()
}
func main() {
jsLogger := log.New(os.Stderr, "webview: ", log.LstdFlags)
log.SetPrefix("native: ")
ReadConfig()
SaveConfig()
debug := true
w = webview.New(debug)
defer w.Destroy()
w.SetTitle("Cerulean")
w.SetSize(325, 525, webview.HintMin)
// Bind variables.
w.Bind("setTokenGo", func(newToken string) { config.Token = newToken; SaveConfig() })
w.Bind("setCacheGo", func(newToken string) { config.Token = newToken; SaveConfig() })
// Bind a function to initiate React via webview.Eval.
w.Bind("initiateReact", func() {
value, err := json.Marshal(config.Cache)
if err != nil {
log.Println("failed to serialise []Todo to JSON to send to webview")
}
w.Eval("window.cache = " + ParseToJsString(string(value)))
w.Eval("window.token = " + ParseToJsString(config.Token))
w.Eval("window.reqUrl = " + ParseToJsString(REQ_URL))
w.Eval(js)
})
w.Bind("log", func(output interface{}) {
jsLogger.Println(output)
})
w.Navigate("data:text/html," + html)
w.Run()
}