-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.go
98 lines (84 loc) · 1.6 KB
/
window.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
package app
import (
"strings"
"github.com/StevenZack/openurl"
"github.com/gofaith/webview"
)
type Window struct {
title string
debug bool
server *bridgeServer
head []Widget
body []Widget
binders []binder
}
var instance *Window
func NewWindow() *Window {
if instance != nil {
panic("You can only create one window")
}
instance = &Window{}
return instance
}
func (w *Window) Debug(debug bool) *Window {
w.debug = debug
return w
}
func (w *Window) IsRunning() bool {
return w.server != nil
}
func (w *Window) addBinder(key string, value interface{}) {
for i, v := range w.binders {
if v.key == key {
w.binders[i].value = value
return
}
}
w.binders = append(w.binders, binder{key: key, value: value})
}
func (w *Window) Run() {
//compose with bootstrap
doc := html().Head(
Head(
append(w.head, Style(_bootstrapCSS))...,
),
).Body(
Body(append(w.body, Script().Text(_bootstrapJS))...),
)
// webview
//render
buf := new(strings.Builder)
buf.WriteString("<!DOCTYPE html>")
buf.WriteString(doc.Render())
openurl.OpenApp("data:text/html," + buf.String())
//bind
for _, v := range w.binders {
e := w.w.Bind(v.key, v.value)
if e != nil {
panic(e)
}
}
}
func (w *Window) SizeFixed() *Window {
w.hint = webview.HintFixed
return w
}
func (w *Window) Title(s string) *Window {
w.title = s
if w.w != nil {
w.w.SetTitle(s)
}
return w
}
func (w *Window) Assign(v **Window) *Window {
*v = w
return w
}
func (w *Window) Head(widgets ...Widget) *Window {
w.head = widgets
return w
}
func (w *Window) Body(widgets ...Widget) *Window {
w.body = widgets
return w
}