Skip to content

Commit 4fab77a

Browse files
feat: Introduce Zig error handle (#92)
* Added some basic error handling. * Updated some of the error handling. * feat: update dependency webui to 9136468 * some fix * little fix --------- Co-authored-by: jinzhongjia <[email protected]>
1 parent 721495d commit 4fab77a

File tree

13 files changed

+278
-149
lines changed

13 files changed

+278
-149
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
.minimum_zig_version = "0.14.0",
66
.dependencies = .{
77
.webui = .{
8-
.hash = "webui-2.5.0-beta.4-pxqD5YoPNwCJ9uGbFj8HOnmOUW6QgvHZtLqmpZN5kfmw",
9-
.url = "https://github.com/webui-dev/webui/archive/9dd20b5c98b53c1e03f94e80415ddfec5c37a1fa.tar.gz",
8+
.hash = "webui-2.5.0-beta.4-pxqD5Y4GNwCmkPZG76ANjcJbSUoP7R4qu3FsZEPRcB9J",
9+
.url = "https://github.com/webui-dev/webui/archive/91364686ca444ddc66aac9de5f5ca2fc7f7afdfe.tar.gz",
1010
},
1111
},
1212
.paths = .{

examples/call_js_from_zig/main.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ pub fn main() !void {
88
var nwin = webui.newWindow();
99

1010
// Bind HTML elements with C functions
11-
_ = nwin.bind("my_function_count", my_function_count);
12-
_ = nwin.bind("my_function_exit", my_function_exit);
11+
_ = try nwin.bind("my_function_count", my_function_count);
12+
_ = try nwin.bind("my_function_exit", my_function_exit);
1313

1414
// Show the window
15-
_ = nwin.show(html);
15+
try nwin.show(html);
1616
// _ = nwin.showBrowser(html, .Chrome);
1717

1818
// Wait until all windows get closed
@@ -31,13 +31,13 @@ fn my_function_count(e: *webui.Event) void {
3131
const win = e.getWindow();
3232

3333
// Run JavaScript
34-
if (!win.script("return GetCount();", 0, &response)) {
34+
win.script("return GetCount();", 0, &response) catch {
3535
if (!win.isShown()) {
3636
std.debug.print("window closed\n", .{});
3737
} else {
3838
std.debug.print("js error:{s}\n", .{response});
3939
}
40-
}
40+
};
4141

4242
const res_buf = response[0..std.mem.len(@as([*:0]u8, @ptrCast(&response)))];
4343

examples/call_zig_from_js/main.zig

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,28 @@ const html = @embedFile("index.html");
88
pub fn main() !void {
99
var nwin = webui.newWindow();
1010

11-
_ = nwin.bind("my_function_string", my_function_string);
12-
_ = nwin.bind("my_function_integer", my_function_integer);
13-
_ = nwin.bind("my_function_boolean", my_function_boolean);
14-
_ = nwin.bind("my_function_with_response", my_function_with_response);
15-
_ = nwin.bind("my_function_raw_binary", my_function_raw_binary);
11+
_ = try nwin.binding("my_function_string", getString);
12+
// _ = try nwin.bind("my_function_string", my_function_string);
13+
_ = try nwin.binding("my_function_integer", getInteger);
14+
// _ = try nwin.bind("my_function_integer", my_function_integer);
15+
_ = try nwin.bind("my_function_boolean", my_function_boolean);
16+
_ = try nwin.bind("my_function_with_response", my_function_with_response);
17+
_ = try nwin.bind("my_function_raw_binary", my_function_raw_binary);
1618

17-
_ = nwin.show(html);
19+
try nwin.show(html);
1820

1921
webui.wait();
2022

2123
webui.clean();
2224
}
2325

26+
fn getString(str1: [:0]const u8, str2: [:0]const u8) void {
27+
// Hello
28+
std.debug.print("my_function_string 1: {s}\n", .{str1});
29+
// World
30+
std.debug.print("my_function_string 2: {s}\n", .{str2});
31+
}
32+
2433
fn my_function_string(e: *webui.Event) void {
2534
// JavaScript:
2635
// my_function_string('Hello', 'World`);
@@ -35,6 +44,12 @@ fn my_function_string(e: *webui.Event) void {
3544
std.debug.print("my_function_string 2: {s}\n", .{str_2});
3645
}
3746

47+
fn getInteger(n1: i64, n2: i64, n3: i64, f1: f64) void {
48+
std.debug.print("number is {},{},{},{}", .{
49+
n1, n2, n3, f1,
50+
});
51+
}
52+
3853
fn my_function_integer(e: *webui.Event) void {
3954
// JavaScript:
4055
// my_function_integer(123, 456, 789, 12345.6789);
@@ -95,11 +110,11 @@ fn my_function_raw_binary(e: *webui.Event) void {
95110

96111
// Or e.getStringAt(0);
97112
const raw_1 = e.getString();
98-
const raw_2 = e.getStringAt(1);
113+
const raw_2 = e.getRawAt(1);
99114

100115
// Or e.getSizeAt(0);
101-
const len_1 = e.getSize();
102-
const len_2 = e.getSizeAt(1);
116+
const len_1 = e.getSize() catch return;
117+
const len_2 = e.getSizeAt(1) catch return;
103118

104119
// Print raw_1
105120
std.debug.print("my_function_raw_binary 1 ({} bytes): ", .{len_1});

examples/custom_spa_server_on_free_port/main.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Custom web Server - Free Port - Example
2+
// Note: if you want to run this example, you nedd a python, zig will wrap a child process to launch python server
23
const std = @import("std");
34
const webui = @import("webui");
45

@@ -12,9 +13,9 @@ pub fn main() !void {
1213
var nwin = webui.newWindow();
1314

1415
// Bind all events
15-
_ = nwin.bind("", events);
16+
_ = try nwin.bind("", events);
1617
// Bind a JS call to a Zig fn
17-
_ = nwin.bind("gotoPage", goto_page);
18+
_ = try nwin.bind("gotoPage", goto_page);
1819

1920
// The `webui.js` script will be available at:
2021
//
@@ -27,7 +28,7 @@ pub fn main() !void {
2728
const webui_port: u64 = webui.getFreePort();
2829
std.debug.print("Free Port for webui.js: {d} \n", .{webui_port});
2930
// now use the port:
30-
_ = nwin.setPort(webui_port);
31+
try nwin.setPort(webui_port);
3132

3233
const backend_port = webui.getFreePort();
3334
std.debug.print("Free Port for custom web server: {d} \n", .{backend_port});
@@ -45,7 +46,7 @@ pub fn main() !void {
4546
// Show a new window served by our custom web server (spawned above):
4647
var buf: [64]u8 = undefined;
4748
home_url = try std.fmt.bufPrintZ(&buf, "http://localhost:{d}/index.html", .{backend_port});
48-
_ = nwin.show(home_url);
49+
try nwin.show(home_url);
4950

5051
// Wait until all windows get closed
5152
webui.wait();

examples/custom_web_server/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ pub fn main() !void {
77
var nwin = webui.newWindow();
88

99
// Bind all events
10-
_ = nwin.bind("", events);
10+
_ = try nwin.bind("", events);
1111

1212
// Bind HTML elements with C functions
13-
_ = nwin.bind("my_backend_func", my_backend_func);
13+
_ = try nwin.bind("my_backend_func", my_backend_func);
1414

1515
// Set the web-server/WebSocket port that WebUI should
1616
// use. This means `webui.js` will be available at:
1717
// http://localhost:MY_PORT_NUMBER/webui.js
18-
_ = nwin.setPort(8081);
18+
try nwin.setPort(8081);
1919

2020
// Show a new window and show our custom web server
2121
// Assuming the custom web server is running on port
2222
// 8080...
23-
_ = nwin.show("http://localhost:8080/");
23+
try nwin.show("http://localhost:8080/");
2424

2525
// Wait until all windows get closed
2626
webui.wait();

examples/frameless/main.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ pub fn main() !void {
2525
// create a new window
2626
var nwin = webui.newWindow();
2727

28-
_ = nwin.bind("minimize", minimize);
29-
_ = nwin.bind("maximize", maximize);
30-
_ = nwin.bind("close", close);
28+
_ = try nwin.bind("minimize", minimize);
29+
_ = try nwin.bind("maximize", maximize);
30+
_ = try nwin.bind("close", close);
3131

3232
nwin.setSize(800, 600);
3333
nwin.setFrameless(true);
3434
nwin.setTransparent(true);
3535
nwin.setResizable(true);
3636
nwin.setCenter();
3737

38-
_ = nwin.showWv(html);
38+
try nwin.showWv(html);
3939

4040
// wait the window exit
4141
webui.wait();

examples/minimal/main.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn main() !void {
66
var nwin = webui.newWindow();
77

88
// show the content
9-
_ = nwin.show("<html><head><script src=\"/webui.js\"></script></head> Hello World ! </html>");
9+
try nwin.show("<html><head><script src=\"/webui.js\"></script></head> Hello World ! </html>");
1010

1111
// wait the window exit
1212
webui.wait();

examples/public_network_access/main.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn public_window_events(e: *webui.Event) void {
2626

2727
fn private_window_events(e: *webui.Event) void {
2828
if (e.event_type == .EVENT_CONNECTED) {
29-
const public_win_url: [:0]const u8 = public_window.getUrl();
29+
const public_win_url: [:0]const u8 = public_window.getUrl() catch return;
3030
var buf = std.mem.zeroes([1024]u8);
3131
const js = std.fmt.bufPrintZ(&buf, "document.getElementById('urlSpan').innerHTML = '{s}';", .{public_win_url}) catch unreachable;
3232
private_window.run(js);
@@ -36,7 +36,7 @@ fn private_window_events(e: *webui.Event) void {
3636
pub fn main() !void {
3737
// Create windows
3838
private_window = webui.newWindow();
39-
public_window = webui.newWindow();
39+
public_window = webui.newWindow();
4040

4141
// App
4242
webui.setTimeout(0); // Wait forever (never timeout)
@@ -46,21 +46,21 @@ pub fn main() !void {
4646
public_window.setPublic(true);
4747

4848
// Bind all events
49-
_ = public_window.bind("", public_window_events);
49+
_ = try public_window.bind("", public_window_events);
5050

5151
// Set public window HTML
52-
_ = public_window.showBrowser(public_html, .NoBrowser);
52+
try public_window.showBrowser(public_html, .NoBrowser);
5353

5454
// Main Private Window
5555

5656
// Run Js
57-
_ = private_window.bind("", private_window_events);
57+
_ = try private_window.bind("", private_window_events);
5858

5959
// Bind exit button
60-
_ = private_window.bind("Exit", app_exit);
60+
_ = try private_window.bind("Exit", app_exit);
6161

6262
// Show the window
63-
_ = private_window.show(private_html);
63+
_ = try private_window.show(private_html);
6464

6565
// Wait until all windows get closed
6666
webui.wait();

examples/serve_a_folder/main.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ var MySecondWindow: webui = undefined;
99

1010
pub fn main() !void {
1111
// Create new windows
12-
MyWindow = webui.newWindowWithId(1);
13-
MySecondWindow = webui.newWindowWithId(2);
12+
MyWindow = try webui.newWindowWithId(1);
13+
MySecondWindow = try webui.newWindowWithId(2);
1414

1515
// Bind HTML element IDs with a C functions
16-
_ = MyWindow.bind("SwitchToSecondPage", switch_second_window);
17-
_ = MyWindow.bind("OpenNewWindow", show_second_window);
18-
_ = MyWindow.bind("Exit", exit_app);
19-
_ = MySecondWindow.bind("Exit", exit_app);
16+
_ = try MyWindow.bind("SwitchToSecondPage", switch_second_window);
17+
_ = try MyWindow.bind("OpenNewWindow", show_second_window);
18+
_ = try MyWindow.bind("Exit", exit_app);
19+
_ = try MySecondWindow.bind("Exit", exit_app);
2020

2121
// Bind events
22-
_ = MyWindow.bind("", events);
22+
_ = try MyWindow.bind("", events);
2323

2424
// Set the `.ts` and `.js` runtime
2525
// webui_set_runtime(MyWindow, NodeJS);
@@ -38,7 +38,7 @@ pub fn main() !void {
3838
// Show a new window
3939
// webui_set_root_folder(MyWindow, "_MY_PATH_HERE_");
4040
// webui_show_browser(MyWindow, "index.html", Chrome);
41-
_ = MyWindow.show("index.html");
41+
try MyWindow.show("index.html");
4242

4343
// Wait until all windows get closed
4444
webui.wait();
@@ -85,7 +85,7 @@ fn switch_second_window(e: *webui.Event) void {
8585
// time the user clicks on "SwitchToSecondPage"
8686

8787
// Switch to `/second.html` in the same opened window.
88-
_ = e.getWindow().show("second.html");
88+
e.getWindow().show("second.html") catch return;
8989
}
9090

9191
fn show_second_window(_: *webui.Event) void {
@@ -94,7 +94,7 @@ fn show_second_window(_: *webui.Event) void {
9494

9595
// Show a new window, and navigate to `/second.html`
9696
// if it's already open, then switch in the same window
97-
_ = MySecondWindow.show("second.html");
97+
MySecondWindow.show("second.html") catch return;
9898
}
9999

100100
var count: i32 = 0;

examples/text_editor/main.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ pub fn main() !void {
1414
var mainW = webui.newWindow();
1515

1616
// Set the root folder for the UI
17-
_ = mainW.setRootFolder("ui");
17+
try mainW.setRootFolder("ui");
1818

1919
// Bind HTML elements with the specified ID to C functions
20-
_ = mainW.bind("close_app", close);
20+
_ = try mainW.bind("close_app", close);
2121

2222
// Show the window, this will select the best browser to show
2323
// and you can use showBrowser to select which browser will be used
24-
_ = mainW.show("index.html");
24+
try mainW.show("index.html");
2525

2626
// Wait until all windows get closed
2727
webui.wait();

0 commit comments

Comments
 (0)