Repository navigation
Expand file tree
/
Copy pathmain.d
More file actions
61 lines (49 loc) · 2.19 KB
/
Copy pathmain.d
File metadata and controls
61 lines (49 loc) · 2.19 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
// D-WebUI - Call D from JavaScript Example
import std.stdio : writef, writefln, writeln;
import webui;
// Embed the UI into the binary at compile time.
enum html = import("index.html");
void main()
{
auto win = newWindow();
// `bind` accepts plain D callables: the JavaScript arguments are converted
// to the parameter types in order, so handlers rarely need the raw event.
win.bind("my_function_string", (string first, string second) {
writefln("my_function_string 1: %s", first); // Hello
writefln("my_function_string 2: %s", second); // World
});
win.bind("my_function_integer", (long first, long second, long third, double fourth) {
writefln("my_function_integer 1: %d", first); // 123
writefln("my_function_integer 2: %d", second); // 456
writefln("my_function_integer 3: %d", third); // 789
writefln("my_function_integer 4: %f", fourth); // 12345.6789
});
win.bind("my_function_boolean", (bool first, bool second) {
writefln("my_function_boolean 1: %s", first); // true
writefln("my_function_boolean 2: %s", second); // false
});
// A non-void return value is sent back to the JavaScript promise.
win.bind("my_function_with_response", (long number, long times) {
long result = number * times;
writefln("my_function_with_response: %d * %d = %d", number, times, result);
return result;
});
// Binary arguments arrive as a slice borrowed from WebUI. It stays valid
// until the handler returns, so copy it with `.dup` to keep it around.
win.bind("my_function_raw_binary", (const(ubyte)[] small, const(ubyte)[] big) {
writef("my_function_raw_binary 1 (%d bytes):", small.length);
foreach (b; small)
writef(" 0x%X", b);
writeln();
// The big buffer is [0xA1, 0x00..., 0xA2]
const valid = big.length > 1 && big[0] == 0xA1 && big[$ - 1] == 0xA2;
writefln("my_function_raw_binary 2 big (%d bytes): valid data? %s",
big.length, valid ? "Yes" : "No");
});
// Show the window
win.show(html);
// Wait until all windows get closed
wait();
// Free all memory resources (optional)
clean();
}