Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
.minimum_zig_version = "0.14.0",
.dependencies = .{
.webui = .{
.hash = "webui-2.5.0-beta.4-pxqD5YoPNwCJ9uGbFj8HOnmOUW6QgvHZtLqmpZN5kfmw",
.url = "https://github.com/webui-dev/webui/archive/9dd20b5c98b53c1e03f94e80415ddfec5c37a1fa.tar.gz",
.hash = "webui-2.5.0-beta.4-pxqD5Y4GNwCmkPZG76ANjcJbSUoP7R4qu3FsZEPRcB9J",
.url = "https://github.com/webui-dev/webui/archive/91364686ca444ddc66aac9de5f5ca2fc7f7afdfe.tar.gz",
},
},
.paths = .{
Expand Down
10 changes: 5 additions & 5 deletions examples/call_js_from_zig/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ pub fn main() !void {
var nwin = webui.newWindow();

// Bind HTML elements with C functions
_ = nwin.bind("my_function_count", my_function_count);
_ = nwin.bind("my_function_exit", my_function_exit);
_ = try nwin.bind("my_function_count", my_function_count);
_ = try nwin.bind("my_function_exit", my_function_exit);

// Show the window
_ = nwin.show(html);
try nwin.show(html);
// _ = nwin.showBrowser(html, .Chrome);

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

// Run JavaScript
if (!win.script("return GetCount();", 0, &response)) {
win.script("return GetCount();", 0, &response) catch {
if (!win.isShown()) {
std.debug.print("window closed\n", .{});
} else {
std.debug.print("js error:{s}\n", .{response});
}
}
};

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

Expand Down
33 changes: 24 additions & 9 deletions examples/call_zig_from_js/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,28 @@ const html = @embedFile("index.html");
pub fn main() !void {
var nwin = webui.newWindow();

_ = nwin.bind("my_function_string", my_function_string);
_ = nwin.bind("my_function_integer", my_function_integer);
_ = nwin.bind("my_function_boolean", my_function_boolean);
_ = nwin.bind("my_function_with_response", my_function_with_response);
_ = nwin.bind("my_function_raw_binary", my_function_raw_binary);
_ = try nwin.binding("my_function_string", getString);
// _ = try nwin.bind("my_function_string", my_function_string);
_ = try nwin.binding("my_function_integer", getInteger);
// _ = try nwin.bind("my_function_integer", my_function_integer);
_ = try nwin.bind("my_function_boolean", my_function_boolean);
_ = try nwin.bind("my_function_with_response", my_function_with_response);
_ = try nwin.bind("my_function_raw_binary", my_function_raw_binary);

_ = nwin.show(html);
try nwin.show(html);

webui.wait();

webui.clean();
}

fn getString(str1: [:0]const u8, str2: [:0]const u8) void {
// Hello
std.debug.print("my_function_string 1: {s}\n", .{str1});
// World
std.debug.print("my_function_string 2: {s}\n", .{str2});
}

fn my_function_string(e: *webui.Event) void {
// JavaScript:
// my_function_string('Hello', 'World`);
Expand All @@ -35,6 +44,12 @@ fn my_function_string(e: *webui.Event) void {
std.debug.print("my_function_string 2: {s}\n", .{str_2});
}

fn getInteger(n1: i64, n2: i64, n3: i64, f1: f64) void {
std.debug.print("number is {},{},{},{}", .{
n1, n2, n3, f1,
});
}

fn my_function_integer(e: *webui.Event) void {
// JavaScript:
// my_function_integer(123, 456, 789, 12345.6789);
Expand Down Expand Up @@ -95,11 +110,11 @@ fn my_function_raw_binary(e: *webui.Event) void {

// Or e.getStringAt(0);
const raw_1 = e.getString();
const raw_2 = e.getStringAt(1);
const raw_2 = e.getRawAt(1);

// Or e.getSizeAt(0);
const len_1 = e.getSize();
const len_2 = e.getSizeAt(1);
const len_1 = e.getSize() catch return;
const len_2 = e.getSizeAt(1) catch return;

// Print raw_1
std.debug.print("my_function_raw_binary 1 ({} bytes): ", .{len_1});
Expand Down
9 changes: 5 additions & 4 deletions examples/custom_spa_server_on_free_port/main.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Custom web Server - Free Port - Example
// Note: if you want to run this example, you nedd a python, zig will wrap a child process to launch python server
const std = @import("std");
const webui = @import("webui");

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

// Bind all events
_ = nwin.bind("", events);
_ = try nwin.bind("", events);
// Bind a JS call to a Zig fn
_ = nwin.bind("gotoPage", goto_page);
_ = try nwin.bind("gotoPage", goto_page);

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

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

// Wait until all windows get closed
webui.wait();
Expand Down
8 changes: 4 additions & 4 deletions examples/custom_web_server/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ pub fn main() !void {
var nwin = webui.newWindow();

// Bind all events
_ = nwin.bind("", events);
_ = try nwin.bind("", events);

// Bind HTML elements with C functions
_ = nwin.bind("my_backend_func", my_backend_func);
_ = try nwin.bind("my_backend_func", my_backend_func);

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

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

// Wait until all windows get closed
webui.wait();
Expand Down
8 changes: 4 additions & 4 deletions examples/frameless/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ pub fn main() !void {
// create a new window
var nwin = webui.newWindow();

_ = nwin.bind("minimize", minimize);
_ = nwin.bind("maximize", maximize);
_ = nwin.bind("close", close);
_ = try nwin.bind("minimize", minimize);
_ = try nwin.bind("maximize", maximize);
_ = try nwin.bind("close", close);

nwin.setSize(800, 600);
nwin.setFrameless(true);
nwin.setTransparent(true);
nwin.setResizable(true);
nwin.setCenter();

_ = nwin.showWv(html);
try nwin.showWv(html);

// wait the window exit
webui.wait();
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn main() !void {
var nwin = webui.newWindow();

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

// wait the window exit
webui.wait();
Expand Down
14 changes: 7 additions & 7 deletions examples/public_network_access/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn public_window_events(e: *webui.Event) void {

fn private_window_events(e: *webui.Event) void {
if (e.event_type == .EVENT_CONNECTED) {
const public_win_url: [:0]const u8 = public_window.getUrl();
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);
Expand All @@ -36,7 +36,7 @@ fn private_window_events(e: *webui.Event) void {
pub fn main() !void {
// Create windows
private_window = webui.newWindow();
public_window = webui.newWindow();
public_window = webui.newWindow();

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

// Bind all events
_ = public_window.bind("", public_window_events);
_ = try public_window.bind("", public_window_events);

// Set public window HTML
_ = public_window.showBrowser(public_html, .NoBrowser);
try public_window.showBrowser(public_html, .NoBrowser);

// Main Private Window

// Run Js
_ = private_window.bind("", private_window_events);
_ = try private_window.bind("", private_window_events);

// Bind exit button
_ = private_window.bind("Exit", app_exit);
_ = try private_window.bind("Exit", app_exit);

// Show the window
_ = private_window.show(private_html);
_ = try private_window.show(private_html);

// Wait until all windows get closed
webui.wait();
Expand Down
20 changes: 10 additions & 10 deletions examples/serve_a_folder/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ var MySecondWindow: webui = undefined;

pub fn main() !void {
// Create new windows
MyWindow = webui.newWindowWithId(1);
MySecondWindow = webui.newWindowWithId(2);
MyWindow = try webui.newWindowWithId(1);
MySecondWindow = try webui.newWindowWithId(2);

// Bind HTML element IDs with a C functions
_ = MyWindow.bind("SwitchToSecondPage", switch_second_window);
_ = MyWindow.bind("OpenNewWindow", show_second_window);
_ = MyWindow.bind("Exit", exit_app);
_ = MySecondWindow.bind("Exit", exit_app);
_ = try MyWindow.bind("SwitchToSecondPage", switch_second_window);
_ = try MyWindow.bind("OpenNewWindow", show_second_window);
_ = try MyWindow.bind("Exit", exit_app);
_ = try MySecondWindow.bind("Exit", exit_app);

// Bind events
_ = MyWindow.bind("", events);
_ = try MyWindow.bind("", events);

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

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

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

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

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

var count: i32 = 0;
Expand Down
6 changes: 3 additions & 3 deletions examples/text_editor/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub fn main() !void {
var mainW = webui.newWindow();

// Set the root folder for the UI
_ = mainW.setRootFolder("ui");
try mainW.setRootFolder("ui");

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

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

// Wait until all windows get closed
webui.wait();
Expand Down
10 changes: 5 additions & 5 deletions examples/web_app_multi_client/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ pub fn main() !void {
var win = webui.newWindow();

// Bind HTML with a Zig functions
_ = win.bind("save", save);
_ = win.bind("saveAll", saveAll);
_ = win.bind("exit_app", exit_app);
_ = try win.bind("save", save);
_ = try win.bind("saveAll", saveAll);
_ = try win.bind("exit_app", exit_app);

// Bind all events
_ = win.bind("", events);
_ = try win.bind("", events);

// Start server only
const url = win.startServer("index.html");
const url = try win.startServer("index.html");

// Open a new page in the default native web browser
webui.openUrl(@as([*c]const u8, @ptrCast(url.ptr))[0..url.len :0]);
Expand Down
12 changes: 11 additions & 1 deletion src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ pub extern fn webui_get_child_process_id(window: usize) callconv(.C) usize;
/// @return Returns the window `hwnd` as `void*`
///
/// @example HWND hwnd = webui_win32_get_hwnd(myWindow);
pub extern fn webui_win32_get_hwnd(window: usize) callconv(.C) *anyopaque;
pub extern fn webui_win32_get_hwnd(window: usize) callconv(.C) ?*anyopaque;

/// @brief Get the network port of a running window.
/// This can be useful to determine the HTTP link of `webui.js`
Expand Down Expand Up @@ -895,6 +895,16 @@ pub extern fn webui_return_string(e: *Event, s: [*:0]const u8) callconv(.C) void
/// @example webui_return_bool(e, true);
pub extern fn webui_return_bool(e: *Event, b: bool) callconv(.C) void;

/// @brief Get the last WebUI error code.
///
/// @example int error_num = webui_get_last_error_number();
pub extern fn webui_get_last_error_number() callconv(.C) c_int;

/// @brief Get the last WebUI error message.
///
/// @example const char* error_msg = webui_get_last_error_message();
pub extern fn webui_get_last_error_message() callconv(.C) [*:0]const u8;

// -- Wrapper's Interface -------------

/// @brief Bind a specific HTML element click event with a function. Empty element means all events.
Expand Down
Loading