Repository navigation
Expand file tree
/
Copy pathmain.d
More file actions
110 lines (88 loc) · 2.94 KB
/
Copy pathmain.d
File metadata and controls
110 lines (88 loc) · 2.94 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
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
// D-WebUI - Serve a Folder Example
import std.file : thisExePath;
import std.format : format;
import std.path : dirName;
import std.stdio : writeln;
import std.string : representation;
import webui;
// Two files are served from D instead of from disk, so embed them.
enum testTxt = import("test.txt");
enum dynamicTxt = import("dynamic.txt");
Window mainWindow;
Window secondWindow;
int count;
void main()
{
// Create new windows
mainWindow = newWindow(1);
secondWindow = newWindow(2);
// Bind HTML element IDs with D callables
mainWindow.bind("SwitchToSecondPage", (Event* e) { e.window.show("second.html"); });
mainWindow.bind("OpenNewWindow", () { secondWindow.show("second.html"); });
mainWindow.bind("Exit", () { exit(); });
secondWindow.bind("Exit", () { exit(); });
// An empty element name binds every event of the window
mainWindow.bind("", &events);
// Set the runtime used for the served `.js` and `.ts` files
mainWindow.setRuntime(Runtime.deno);
// Serve a couple of paths from D instead of from disk
mainWindow.setFileHandler(&filesHandler);
mainWindow.setSize(800, 800);
mainWindow.setPosition(200, 200);
// Serve the folder holding this executable, so the example works no matter
// which directory it was started from.
const root = dirName(thisExePath);
mainWindow.setRootFolder(root);
secondWindow.setRootFolder(root);
// Show the window
mainWindow.show("index.html");
// Wait until all windows get closed
wait();
// Free all memory resources (optional)
clean();
}
void events(Event* e)
{
// This function gets called on every event of the window.
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;
}
}
const(ubyte)[] filesHandler(string filename)
{
writeln("File: ", filename);
// A static file served straight from the binary.
if (filename == "/test.txt")
return testTxt.representation;
// A file generated on every request. The handler returns the full HTTP
// response, header included.
if (filename == "/dynamic.html")
{
count++;
auto page = format(dynamicTxt, count);
return format("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s",
page.length, page).representation;
}
// Anything else: let WebUI serve the file from the root folder.
return null;
}