-
Notifications
You must be signed in to change notification settings - Fork 1
/
dialog.go
116 lines (105 loc) · 3.52 KB
/
dialog.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 main
import (
ui "github.com/malivvan/webkitgtk"
)
type DialogAPI struct {
app *ui.App
}
type MessageDialog struct {
Title string `json:"title"`
Message string `json:"message"`
Actions []string `json:"actions"`
}
type OpenDialog struct {
Title string `json:"title"`
Multiple bool `json:"multiple,omitempty"`
}
type SaveDialog struct {
Title string `json:"title"`
}
func (a *DialogAPI) Ask(req MessageDialog) (int, error) {
return <-a.app.Dialog().Ask(req.Title, req.Message, req.Actions...).Show(), nil
}
func (a *DialogAPI) Info(req MessageDialog) (int, error) {
return <-a.app.Dialog().Info(req.Title, req.Message).Show(), nil
}
func (a *DialogAPI) Warn(req MessageDialog) (int, error) {
return <-a.app.Dialog().Warn(req.Title, req.Message).Show(), nil
}
func (a *DialogAPI) Fail(req MessageDialog) (int, error) {
return <-a.app.Dialog().Fail(req.Title, req.Message).Show(), nil
}
func (a *DialogAPI) Open(req OpenDialog) ([]string, error) {
return <-a.app.Dialog().Open(req.Title).AllowsMultipleSelection(req.Multiple).Show(), nil
}
func (a *DialogAPI) Save(req SaveDialog) (string, error) {
return <-a.app.Dialog().Save(req.Title).Show(), nil
}
func main() {
app := ui.New(ui.AppOptions{
ID: "com.github.malivvan.webkitgtk.examples.dialog",
Name: "WebKitGTK Dialog Example",
})
app.Open(ui.WindowOptions{
Title: "dialog",
Width: 400,
Height: 220,
HTML: `<doctype html>
<html>
<head>
<style>
input, textarea {
width: 100%;
}
textarea {
resize: none;
}
</style>
<body>
<input id="title" type="text" value="title"></input><br>
<input id="message" type="text" value="message"></input><br>
<button id="ask">ask</button>
<button id="info">info</button>
<button id="warn">warn</button>
<button id="fail">fail</button>
<button id="file">open file</button>
<button id="files">open files</button>
<button id="save">save</button>
<br>
<textarea id="out" rows="8"></textarea>
<script>
let title = document.querySelector("#title");
let message = document.querySelector("#message");
let output = document.querySelector("#out");
document.querySelector("#ask").addEventListener("click", () => {
dialog.ask({title: title.value, message: message.value, actions: ["ok", "cancel"]}).then(resp => output.value = resp);
});
document.querySelector("#info").addEventListener("click", () => {
dialog.info({title: title.value, message: message.value}).then(resp => output.value = resp);
});
document.querySelector("#warn").addEventListener("click", () => {
dialog.warn({title: title.value, message: message.value}).then(resp => output.value = resp);
});
document.querySelector("#fail").addEventListener("click", () => {
dialog.fail({title: title.value, message: message.value}).then(resp => output.value = resp);
});
document.querySelector("#file").addEventListener("click", () => {
dialog.open({title: title.value}).then(resp => output.value = resp.join("\n"));
});
document.querySelector("#files").addEventListener("click", () => {
dialog.open({title: title.value, multiple: true}).then(resp => output.value = resp.join("\n"));
});
document.querySelector("#save").addEventListener("click", () => {
dialog.save({title: title.value}).then(resp => output.value = resp);
});
</script>
</body>
</html>`,
Define: map[string]interface{}{
"dialog": &DialogAPI{app: app},
},
})
if err := app.Run(); err != nil {
panic(err)
}
}