-
Notifications
You must be signed in to change notification settings - Fork 0
/
hermes.go
226 lines (203 loc) · 7.53 KB
/
hermes.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
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
package hermes
import (
"bytes"
"log"
"regexp"
"strings"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/qml"
)
const (
// ModeSet sets the target's properties according to the provided values.
// e.g.: target="some_Qml_Text's_id"; jsondata="{"text": "Hello World !",
// "height": 100}"
ModeSet int = iota
// ModeAdd adds a new element specified in the jsondata string to the
// target element. e.g.: target="some_Qml_Row's_id"; jsondata="
// {"template": "Text{text: 'Hello World <name> !'}", "variables":
// {"name": "John Smith"}}". Only the template's root element will be
// accessible via its id (added to the idMap). The id can be hardcoded
// in the template or dynamically added using the variables section.
ModeAdd
// ModeAddFromFile is the same as ModeAdd, but reads the template from a
// given path. e.g.: target="some_Qml_Row's_id"; jsondata="{"template":
// "path/to/your/template/from/your/main.qml", "variables":
// {"name": "John Smith"}}" Only the template's root element will be
// accessible via its id (added to the idMap). The id can be hardcoded
// in the template or dynamically added using the variables section.
ModeAddFromFile
// ModeRemove deletes an element by its qml-id provided in target.
// The jsondata should be a empty string.
ModeRemove
// ModeRead sends the requested values of the given target to the
// event listener with the name provided in the jsondata. e.g.:
// target="some_Qml_ElementÄs_id"; jsondata="{"eventListener":
// "readProperties", "properties":["color","text"]}"
ModeRead
// ModeCustom follows your rules. You can define the JavaScript processing
// in the "hermes.qml" snippet.
ModeCustom
)
//go:generate qtmoc
// QmlBridge is the connection between go and qml
type QmlBridge struct {
core.QObject
_ func(mode int, target, jsondata string) `signal:"sendToQml"`
_ func(action, source, jsondata string) `slot:"sendToGo"`
}
// Controller holds a QmlBridge and the according event-listeners
type Controller struct {
qmlBridge *QmlBridge
eventListeners map[string]func(string, string)
}
// DoLog can be used to activate logging
var DoLog bool
var (
newlineRegex *regexp.Regexp
newlineWinRegex *regexp.Regexp
tabRegex *regexp.Regexp
)
func init() {
newlineWinRegex = regexp.MustCompile(`\r\n`)
newlineRegex = regexp.MustCompile(`\n`)
tabRegex = regexp.MustCompile(`\t`)
}
// NewBridgeController creates a new controller. This should be done
// before creating a qml window.
func NewBridgeController(engine *qml.QQmlApplicationEngine) *Controller {
var c = Controller{
qmlBridge: NewQmlBridge(nil),
eventListeners: make(map[string]func(string, string)),
}
engine.RootContext().SetContextProperty("hermes", c.qmlBridge)
c.qmlBridge.ConnectSendToGo(c.interpretQmlCommand)
return &c
}
// AddEventListener registers a function to be called, when the qml-code
// sends a message with the given event string. The first string passed
// to the event listener is the source value of the Qml function "sendToGo".
// The second one is the jsondata string passed to the same function. Note
// that if there were any newlines in the jsondata, they were escaped to
// ensure the json.Unmarshal function won't fail. In case there were any,
// you can restore them by applying the following regex replacement operation:
// regexp.MustCompile(`\\n`).ReplaceAllString(regexp.MustCompile(`\\r\\n`)
// .ReplaceAllString(your_json_field_containing_line_breaks, "\r\n"), "\n")
func (c *Controller) AddEventListener(event string, action func(string, string)) {
c.eventListeners[event] = action
}
// RemoveEventListener removes an action, that was previously added
// using AddEventListener()
func (c *Controller) RemoveEventListener(event string) {
delete(c.eventListeners, event)
}
func (c *Controller) interpretQmlCommand(action, source, jsondata string) {
if DoLog {
log.Println("qml to go: " + string(action) + " | " + source + " | " + jsondata)
}
jsondata = newlineRegex.ReplaceAllString(newlineWinRegex.ReplaceAllString(jsondata, "\\r\\n"), "\\n")
if c.eventListeners[action] != nil {
c.eventListeners[action](source, jsondata)
} else {
log.Println("event listener " + action + " not registered !")
}
}
// SendToQml sends a message to the qml-code. Read the mode-constants'
// comments for further information on how target and jsondata have
// to look like.
func (c *Controller) SendToQml(mode int, target, jsondata string) {
if DoLog {
log.Println("go to qml: " + string(mode) + " | " + target + " | " + jsondata)
}
c.qmlBridge.SendToQml(mode, target, newlineRegex.ReplaceAllString(newlineWinRegex.ReplaceAllString(jsondata, "\\r\\n"), "\\n"))
}
// SetInQml is shorthand for SendToQml(ModeSet, ...)
func (c *Controller) SetInQml(target, jsondata string) {
c.SendToQml(ModeSet, target, jsondata)
}
// AddToQml is shorthand for SendToQml(ModeAdd, ...)
func (c *Controller) AddToQml(target, jsondata string) {
c.SendToQml(ModeAdd, target, jsondata)
}
// AddToQmlFromFile is shorthand for SendToQml(ModeAddFromFile, ...)
func (c *Controller) AddToQmlFromFile(target, jsondata string) {
c.SendToQml(ModeAddFromFile, target, jsondata)
}
// RemoveFromQml is shorthand for SendToQml(ModeRemove, ...)
func (c *Controller) RemoveFromQml(target string) {
c.SendToQml(ModeRemove, target, "")
}
// ReadQml is shorthand for SendToQml(ModeRead, ...)
func (c *Controller) ReadQml(target, jsondata string) {
c.SendToQml(ModeRead, target, jsondata)
}
// BuildSetModeJSON helps building trivial JSON strings.
// Every odd argument is a property-name, every even one
// the previous property's value
func BuildSetModeJSON(data ...string) string {
buff := bytes.NewBuffer([]byte{})
if len(data)%2 != 0 {
buff.WriteString("{}")
} else {
buff.WriteString("{")
for i, d := range data {
buff.WriteString(`"`)
buff.WriteString(strings.Replace(tabRegex.ReplaceAllString(d, "\\t"), `"`, `\"`, -1))
if i%2 == 0 {
buff.WriteString(`":`)
} else if i+1 == len(data) {
buff.WriteString(`"}`)
} else {
buff.WriteString(`",`)
}
}
}
return buff.String()
}
// BuildAddModeJSON helps building trivial JSON strings. The template
// is the template string or filepath. Every odd data argument is a
// variable-name, every even one the previous variable's value
func BuildAddModeJSON(template string, data ...string) string {
buff := bytes.NewBuffer([]byte{})
buff.WriteString(`{"template":"`)
buff.WriteString(template)
buff.WriteString(`"`)
if len(data) != 0 && len(data)%2 == 0 {
buff.WriteString(`, "variables": {`)
for i, d := range data {
buff.WriteString(`"`)
buff.WriteString(strings.Replace(tabRegex.ReplaceAllString(d, "\\t"), `"`, `\"`, -1))
if i%2 == 0 {
buff.WriteString(`":`)
} else if i+1 == len(data) {
buff.WriteString(`"}`)
} else {
buff.WriteString(`",`)
}
}
}
buff.WriteString("}")
return buff.String()
}
// BuildReadModeJSON helps building trivial JSON strings. The eventListener
// is the event string provided at registration, the properties are the
// target's wanted qml properties.
func BuildReadModeJSON(eventListener string, properties ...string) string {
buff := bytes.NewBuffer([]byte{})
buff.WriteString(`{"eventListener":"`)
buff.WriteString(eventListener)
buff.WriteString(`"`)
if len(properties) != 0 {
buff.WriteString(`, "properties":[`)
for i, p := range properties {
buff.WriteString(`"`)
buff.WriteString(strings.Replace(tabRegex.ReplaceAllString(p, "\\t"), `"`, `\"`, -1))
buff.WriteString(`"`)
if i+1 != len(properties) {
buff.WriteString(",")
}
}
buff.WriteString(`]`)
}
buff.WriteString("}")
return buff.String()
}