-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdemo.go
More file actions
319 lines (287 loc) · 6.5 KB
/
demo.go
File metadata and controls
319 lines (287 loc) · 6.5 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"archive/zip"
"bytes"
"embed"
"io"
"log"
"math/rand"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"github.com/forbe/gohl"
)
//go:embed embed.zip
var embedZip embed.FS
var (
embedReader *zip.Reader
embedOnce sync.Once
)
var W *gohl.Window
const WM_TRAYMSG = 0x0400 + 1
func main() {
initEmbedZip()
gohl.RegisterResourceLoader("embed", embedLoader)
W = gohl.NewWindow(gohl.WindowConfig{
Title: "联机助手",
Width: 360,
Height: 480,
Frameless: true,
Resize: false,
Border: true,
Rounded: true,
CornerRadius: 10,
Icon: gohl.LoadIconFromResource(2),
Center: true,
Handler: gohl.NotifyHandler{
OnDocumentComplete: func() uintptr {
log.Printf("OnDocumentComplete: %v", W.GetHwnd())
//弹窗
demoBtn := W.GetElementById("demo-btn")
if demoBtn != nil {
demoBtn.OnClick = func(elem *gohl.Element) bool {
showModal("default-modal", "提示", "你好,欢迎来到中国!", func(submit bool) bool {
return true
}, nil)
return true
}
}
//弹窗
demoBtn2 := W.GetElementById("demo-btn2")
if demoBtn2 != nil {
demoBtn2.OnClick = func(elem *gohl.Element) bool {
W.UpdateUI(gohl.U{
ID: "demo-img",
Action: "attr",
Value: map[string]interface{}{"src": "http://bd.kuaishoua.com/787b568727c8f5ce.png"},
}, gohl.U{
ID: "demo-tips",
Action: "text",
Value: "你好,欢迎来到中国!",
})
showNotification("已更新UI", 2*time.Second)
return true
}
}
return W.GetHwnd()
},
},
})
//窗体最小化之前调用,返回false不最小化
W.OnMinimize = func() bool {
log.Println("最小化窗口")
return true
}
//点击按钮触发
W.OnButtonClick = func(elem *gohl.Element) bool {
role, _ := elem.Attr("role")
id, _ := elem.Attr("id")
switch role {
case "show-modal":
if id == "show-modal" {
html := `<div id="join-code-container"><input id="join-code" type="text" /><br /></div>`
showModal("default-modal", "请输入联机码", html, func(submit bool) bool {
return true
}, nil)
return true
}
}
return true
}
W.OnButtonStateChanged = func(elem *gohl.Element, checked bool) bool {
log.Printf("OnButtonStateChanged: %v, %v", elem, checked)
return true
}
W.OnHyperlinkClick = func(elem *gohl.Element) bool {
href, ok := elem.Attr("@href")
if ok {
log.Println("点击了超链接: " + href)
exec.Command("cmd", "/c", "start", href).Start()
}
return true
}
if FileExists("app.html") {
W.LoadFile("app.html").Run()
} else {
html := loadEmbedRes("embed://app.html")
W.SetHtml(html).Run()
}
}
func showLoading(show bool) {
W.UpdateUI(gohl.U{ID: "loading", Action: "show", Value: show})
}
func showModal(id, title, body string, cbk func(submit bool) bool, onRender func(btnOk, btnCancel, btnClose *gohl.Element)) {
root := W.GetRootElement()
if root == nil {
return
}
overlay := root.GetElementById(id)
if overlay == nil {
return
}
titleEl := overlay.GetElementByAttr("role", "modal-title")
bodyEl := overlay.GetElementByAttr("role", "modal-body")
btnCancelEl := overlay.GetElementByAttr("role", "modal-cancel")
btnOkEl := overlay.GetElementByAttr("role", "modal-ok")
btnCloseEl := overlay.GetElementByAttr("role", "modal-close")
if btnOkEl != nil {
btnOkEl.Show()
}
if btnCancelEl != nil {
btnCancelEl.Show()
}
if btnCloseEl != nil {
btnCloseEl.Show()
}
if titleEl != nil {
titleEl.SetText(title)
}
if bodyEl != nil {
if body != "" {
bodyEl.SetHtml(body)
}
}
if overlay != nil {
overlay.Show()
}
if onRender != nil {
if btnOkEl != nil && btnCancelEl != nil && btnCloseEl != nil {
onRender(btnOkEl, btnCancelEl, btnCloseEl)
}
}
if btnCloseEl != nil {
btnCloseEl.OnClick = func(elem *gohl.Element) bool {
if cbk != nil {
cbk(false)
}
overlay.Hide()
return true
}
}
if btnCancelEl != nil {
btnCancelEl.OnClick = func(elem *gohl.Element) bool {
if cbk != nil {
cbk(false)
}
overlay.Hide()
return true
}
}
if btnOkEl != nil {
btnOkEl.OnClick = func(elem *gohl.Element) bool {
if cbk != nil {
if cbk(true) {
overlay.Hide()
}
}
return true
}
}
}
func showNotification(text string, dismissTime time.Duration) {
W.UpdateUI(
gohl.U{
ID: "notification",
Action: "show",
Value: true,
},
gohl.U{
ID: "notification-text",
Action: "text",
Value: text,
},
)
time.AfterFunc(dismissTime, func() {
hideNotification()
})
}
func hideNotification() {
W.UpdateUI(gohl.U{
ID: "notification",
Action: "show",
Value: false,
})
}
func initEmbedZip() {
embedOnce.Do(func() {
data, err := embedZip.ReadFile("embed.zip")
if err != nil {
log.Printf("Failed to read embed.zip: %v", err)
return
}
reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
log.Printf("Failed to parse embed.zip: %v", err)
return
}
embedReader = reader
})
}
func loadEmbedRes(uri string) string {
uri = strings.TrimPrefix(uri, "embed://")
uri = strings.TrimSuffix(uri, "/")
if embedReader == nil {
return ""
}
for _, file := range embedReader.File {
if file.Name == uri {
rc, err := file.Open()
if err != nil {
return ""
}
defer rc.Close()
data, err := io.ReadAll(rc)
if err != nil {
return ""
}
return string(data)
}
}
return ""
}
func embedLoader(uri string) ([]byte, uint32, bool) {
data := loadEmbedRes(uri)
if data == "" {
return nil, 0, false
}
filename := strings.TrimPrefix(uri, "embed://")
return []byte(data), gohl.GetResourceDataType(filename), true
}
func alert(msg string) {
showModal("default-modal", "提示", msg, func(submmit bool) bool {
return true
}, func(btnOk, btnCancel, btnClose *gohl.Element) {
btnOk.Show()
btnCancel.Hide()
btnClose.Show()
})
}
func showNewWindow(uri string, width, height int) {
w := gohl.NewWindow(gohl.WindowConfig{
Title: "联机助手",
Width: width,
Height: height,
Resize: false,
Border: true,
Frameless: false,
Rounded: true,
CornerRadius: 10,
ClassName: "HTMLayoutWindow" + strconv.Itoa(rand.Intn(1000000)),
Icon: gohl.LoadIconFromResource(2),
Center: true,
})
w.SetHtml(loadEmbedRes(uri)).Run()
}
func FileExists(path string) bool {
info, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return !info.IsDir()
}
func init() {
rand.Seed(time.Now().UnixNano())
}