forked from webview/webview
/
main.go
125 lines (116 loc) · 2.63 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
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
package main
//go:generate go get -u github.com/jteeuwen/go-bindata/...
//go:generate go-bindata -pkg $GOPACKAGE -o assets.go -prefix assets/ assets/
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"mime"
"net"
"net/http"
"path/filepath"
"github.com/zserge/webview"
)
func startServer() string {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal(err)
}
go func() {
defer ln.Close()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if len(path) > 0 && path[0] == '/' {
path = path[1:]
}
if path == "" {
path = "index.html"
}
if bs, err := Asset(path); err != nil {
w.WriteHeader(http.StatusNotFound)
} else {
w.Header().Add("Content-Type", mime.TypeByExtension(filepath.Ext(path)))
io.Copy(w, bytes.NewBuffer(bs))
}
})
log.Fatal(http.Serve(ln, nil))
}()
return "http://" + ln.Addr().String()
}
// Task is a data model type, it contains information about task name and status (done/not done).
type Task struct {
Name string `json:"name"`
Done bool `json:"done"`
}
// Tasks is a global data model, to keep things simple.
var Tasks = []Task{}
func render(w webview.WebView, tasks []Task) {
b, err := json.Marshal(tasks)
if err == nil {
w.Eval(fmt.Sprintf("rpc.render(%s)", string(b)))
}
}
func handleRPC(w webview.WebView, data string) {
cmd := struct {
Name string `json:"cmd"`
}{}
if err := json.Unmarshal([]byte(data), &cmd); err != nil {
log.Println(err)
return
}
switch cmd.Name {
case "init":
render(w, Tasks)
case "log":
logInfo := struct {
Text string `json:"text"`
}{}
if err := json.Unmarshal([]byte(data), &logInfo); err != nil {
log.Println(err)
} else {
log.Println(logInfo.Text)
}
case "addTask":
task := Task{}
if err := json.Unmarshal([]byte(data), &task); err != nil {
log.Println(err)
} else if len(task.Name) > 0 {
Tasks = append(Tasks, task)
render(w, Tasks)
}
case "markTask":
taskInfo := struct {
Index int `json:"index"`
Done bool `json:"done"`
}{}
if err := json.Unmarshal([]byte(data), &taskInfo); err != nil {
log.Println(err)
} else if taskInfo.Index >= 0 && taskInfo.Index < len(Tasks) {
Tasks[taskInfo.Index].Done = taskInfo.Done
render(w, Tasks)
}
case "clearDoneTasks":
newTasks := []Task{}
for _, task := range Tasks {
if !task.Done {
newTasks = append(newTasks, task)
}
}
Tasks = newTasks
render(w, Tasks)
}
}
func main() {
url := startServer()
w := webview.New(webview.Settings{
Width: 320,
Height: 480,
Title: "Todo App",
URL: url,
ExternalInvokeCallback: handleRPC,
})
defer w.Exit()
w.Run()
}