-
Notifications
You must be signed in to change notification settings - Fork 4
/
resize.go
188 lines (155 loc) · 4.19 KB
/
resize.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
package main
import (
"flag"
"fmt"
"github.com/raff/ultralight-go"
)
func main() {
title := flag.String("title", "Hello from Go", "Window title")
width := flag.Uint("width", 600, "Window width")
height := flag.Uint("height", 400, "Window height")
full := flag.Bool("fullscreen", false, "Go full screen")
flag.Parse()
app := ultralight.NewApp()
defer app.Destroy()
win := app.NewWindow(*width, *height, *full, *title)
defer win.Destroy()
if flag.NArg() > 0 {
win.View().LoadURL(flag.Arg(0)) // should be a URL
} else {
win.View().LoadHTML(`<html>
<p>Resize the browser window to fire the <code>resize</code> event.</p>
<p>Window height: <span id="height"></span></p>
<p>Window width: <span id="width"></span></p>
<script>
const heightOutput = document.querySelector('#height');
const widthOutput = document.querySelector('#width');
function reportWindowSize() {
heightOutput.textContent = window.innerHeight;
widthOutput.textContent = window.innerWidth;
window.gopher("hello");
}
window.onresize = reportWindowSize;
</script>
</html>`)
}
win.OnResize(func(width, height uint) {
fmt.Println("resize", width, height)
win.Resize(width, height)
})
win.OnClose(func() {
fmt.Println("window is closing")
})
//view := win.View()
win.View().OnBeginLoading(func() {
fmt.Println("begin loading")
})
win.View().OnFinishLoading(func() {
view := win.View()
win.SetTitle(view.Title())
fmt.Println("finish loading", view.URL())
})
win.View().OnUpdateHistory(func() {
fmt.Println("update history")
})
win.View().OnDOMReady(func() {
fmt.Println("DOM ready")
fmt.Println("GlobalObject properties:", win.View().JSContext().GlobalObject().PropertyNames(), "\n")
if false {
// test EvaluateScript and various JSValue methods
values := []string{
"'hello'",
"42",
"true",
"undefined",
"null",
"{a: 1, b: 2}",
"[1,2,3]",
"new Date()",
"function f(x) { return x == null ? 42 : x }",
}
typeof := func(v *ultralight.JSValue) string {
t := "<unknown>"
switch v.Type() {
case ultralight.JSTypeUndefined:
t = "undefined"
case ultralight.JSTypeNull:
t = "null"
case ultralight.JSTypeBoolean:
t = "boolean"
case ultralight.JSTypeNumber:
t = "number"
case ultralight.JSTypeString:
t = "string"
case ultralight.JSTypeObject:
t = "object"
}
if v.IsUndefined() {
t += "/undefined"
}
if v.IsNull() {
t += "/null"
}
if v.IsBoolean() {
t += "/boolean"
}
if v.IsNumber() {
t += "/number"
}
if v.IsString() {
t += "/string"
}
if v.IsObject() {
t += "/object"
}
if v.IsArray() {
t += "/array"
}
if v.IsDate() {
t += "/date"
}
if v.IsFunction() {
t += "/function"
}
return t
}
for _, s := range values {
v := win.View().EvaluateScript("(" + s + ")")
fmt.Printf("%v : %v %q\n", s, typeof(v), v.String())
if v.IsFunction() {
fmt.Println("call", v.Object().Call(nil).String())
fmt.Println("call", v.Object().Call(nil, "hello").String())
fmt.Println("call", v.Object().Call(nil, 999).String())
}
}
}
//
// Call Go from Javascript
//
win.View().JSContext().GlobalObject().SetPropertyValue("gopher",
func(f, this *ultralight.JSObject, args ...*ultralight.JSValue) *ultralight.JSValue {
fmt.Println("calling all gophers!")
return nil
})
//
// Call Javascript from Go
//
f := win.View().EvaluateScript(`(function() {
console.log("hello jesters");
})`)
fmt.Println(f.String())
f.Object().Call(nil)
})
win.View().OnConsoleMessage(func(source ultralight.MessageSource, level ultralight.MessageLevel,
message string, line uint, col uint, sourceId string) {
fmt.Printf("CONSOLE source=%v level=%v id=%q line=%c col=%v %v\n",
source, level, sourceId, line, col, message)
})
/*
app.OnUpdate(func() {
fmt.Println("app should update")
})
*/
win.Focus()
app.Run()
}