Repository navigation
Expand file tree
/
Copy pathmain.d
More file actions
73 lines (59 loc) · 1.71 KB
/
Copy pathmain.d
File metadata and controls
73 lines (59 loc) · 1.71 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
// D-WebUI - Custom Web Server Example
//
// Start the bundled Python server first, it serves this folder on port 8080:
//
// python simple_web_server.py
//
// then run this example.
import std.stdio : writeln;
import webui;
void main()
{
auto win = newWindow();
// An empty element name binds every event of the window
win.bind("", &events);
// Bind an HTML element with a D callable
win.bind("my_backend_func", &myBackendFunc);
// Set the web-server/WebSocket port WebUI should use, which makes
// `webui.js` available at http://localhost:8081/webui.js
win.setPort(8081);
// Show the page served by the external web server
win.show("http://localhost:8080/");
// Wait until all windows get closed
wait();
// Free all memory resources (optional)
clean();
}
void events(Event* e)
{
switch (e.eventType)
{
case EventType.connected:
writeln("Connected.");
break;
case EventType.disconnected:
writeln("Disconnected.");
break;
case EventType.mouseClick:
writeln("Click.");
break;
case EventType.navigation:
{
// Because this window binds "", WebUI blocks every `href` click and
// sends it here instead, so links can be handled as needed.
auto url = e.getString();
writeln("Starting navigation to: ", url);
e.window.navigate(url);
break;
}
default:
break;
}
}
void myBackendFunc(long first, long second, long third)
{
// JavaScript: my_backend_func(123, 456, 789);
writeln("my_backend_func 1: ", first);
writeln("my_backend_func 2: ", second);
writeln("my_backend_func 3: ", third);
}