forked from webui-dev/zig-webui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.zig
More file actions
70 lines (54 loc) · 1.96 KB
/
main.zig
File metadata and controls
70 lines (54 loc) · 1.96 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
//! Public Network Access Example
const std = @import("std");
const webui = @import("webui");
// embed the html
const private_html = @embedFile("private.html");
const public_html = @embedFile("public.html");
// two windows
var private_window: webui = undefined;
var public_window: webui = undefined;
fn app_exit(_: *webui.Event) void {
webui.exit();
}
fn public_window_events(e: *webui.Event) void {
if (e.event_type == .EVENT_CONNECTED) {
// New connection
private_window.run("document.getElementById(\"Logs\").value += \"New connection.\\n\";");
} else if (e.event_type == .EVENT_DISCONNECTED) {
// Disconnection
private_window.run("document.getElementById(\"Logs\").value += \"Disconnected.\\n\";");
}
}
fn private_window_events(e: *webui.Event) void {
if (e.event_type == .EVENT_CONNECTED) {
const public_win_url: [:0]const u8 = public_window.getUrl() catch return;
var buf = std.mem.zeroes([1024]u8);
const js = std.fmt.bufPrintZ(&buf, "document.getElementById('urlSpan').innerHTML = '{s}';", .{public_win_url}) catch unreachable;
private_window.run(js);
}
}
pub fn main() !void {
// Create windows
private_window = webui.newWindow();
public_window = webui.newWindow();
// App
webui.setTimeout(0); // Wait forever (never timeout)
// Public Window
// Make URL accessible from public networks
public_window.setPublic(true);
// Bind all events
_ = try public_window.bind("", public_window_events);
// Set public window HTML
try public_window.showBrowser(public_html, .NoBrowser);
// Main Private Window
// Run Js
_ = try private_window.bind("", private_window_events);
// Bind exit button
_ = try private_window.bind("Exit", app_exit);
// Show the window
_ = try private_window.show(private_html);
// Wait until all windows get closed
webui.wait();
// Free all memory resources (Optional)
webui.clean();
}