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 @@ -4,8 +4,8 @@
.minimum_zig_version = "0.12.0",
.dependencies = .{
.webui = .{
.url = "https://github.com/webui-dev/webui/archive/2d3a68ebeac6741caad16af9752ce0045e4cdcdd.tar.gz",
.hash = "1220886276e230842be96daa45bf96b2f305d9eaba9e8c0c0a2886a0fc350a5c18b5",
.url = "https://github.com/webui-dev/webui/archive/3ff8742f3ecac116465564eef3b1c231d89fd1d9.tar.gz",
.hash = "122084515081be3d1bba85189fc9f3e9e3f35a207350ba1f9aa4c34c97ff52ecc063",
},
},
.paths = .{
Expand Down
62 changes: 57 additions & 5 deletions src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,45 @@ pub extern fn webui_bind(
func: *const fn (e: *Event) callconv(.C) void,
) callconv(.C) usize;

/// @brief Use this API after using `webui_bind()` to add any user data to it that can be
/// read later using `webui_get_context()`.
///
/// @param window The window number
/// @param element The HTML element / JavaScript object
/// @param context Any user data
///
/// @example
/// webui_bind(myWindow, "myFunction", myFunction);
///
/// webui_set_context(myWindow, "myFunction", myData);
///
/// void myFunction(webui_event_t* e) {
/// void* myData = webui_get_context(e);
/// }
pub extern fn webui_set_context(
window: usize,
element: [*:0]const u8,
context: *anyopaque,
) callconv(.C) void;

/// @brief Get user data that is set using `webui_set_context()`.
///
/// @param e The event struct
///
/// @return Returns user data pointer.
///
/// @example
/// webui_bind(myWindow, "myFunction", myFunction);
///
/// webui_set_context(myWindow, "myFunction", myData);
///
/// void myFunction(webui_event_t* e) {
/// void* myData = webui_get_context(e);
/// }
pub extern fn webui_get_context(
e: *Event,
) callconv(.C) *anyopaque;

/// @brief Get the recommended web browser ID to use. If you
/// are already using one, this function will return the same ID.
///
Expand Down Expand Up @@ -242,16 +281,16 @@ pub extern fn webui_set_file_handler_window(
) callconv(.C) ?*const anyopaque,
) callconv(.C) void;

///
/// @brief Use this API to set a file handler response if your backend need async
///
/// @brief Use this API to set a file handler response if your backend need async
/// response for `webui_set_file_handler()`.
///
///
/// @param window The window number
/// @param response The response buffer
/// @param length The response size
///
///
/// @example webui_interface_set_response_file_handler(myWindow, buffer, 1024);
///
///
pub extern fn webui_interface_set_response_file_handler(
window: usize,
response: ?*const anyopaque,
Expand Down Expand Up @@ -311,6 +350,19 @@ pub extern fn webui_decode(str: [*:0]const u8) callconv(.C) ?[*:0]u8;
/// @example webui_free(my_buffer);
pub extern fn webui_free(ptr: *anyopaque) callconv(.C) void;

/// @brief Copy raw data.
///
/// @param dest Destination memory pointer
/// @param src Source memory pointer
/// @param count Bytes to copy
///
/// @example webui_memcpy(myBuffer, myData, 64);
pub extern fn webui_memcpy(
dest: *anyopaque,
src: *anyopaque,
count: usize,
) callconv(.C) void;

/// @brief Safely allocate memory using the WebUI memory management system. It
/// can be safely freed using `webui_free()` at any time.
///
Expand Down
19 changes: 18 additions & 1 deletion src/webui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ pub fn bind(
return c.webui_bind(self.window_handle, element.ptr, tmp_struct.handle);
}

/// Use this API after using `bind()` to add any user data to it that can be
/// read later using `getContext()`
pub fn setContext(self: webui, element: [:0]const u8, context: *anyopaque) void {
c.webui_set_context(self.window_handle, element.ptr, context);
}

/// Get the recommended web browser ID to use. If you
/// are already using one, this function will return the same ID.
pub fn getBestBrowser(self: webui) Browser {
Expand Down Expand Up @@ -195,7 +201,7 @@ pub fn setFileHandlerWindow(self: webui, comptime handler: fn (window_handle: us
c.webui_set_file_handler_window(self.window_handle, tmp_struct.handle);
}

/// Use this API to set a file handler response if your backend need async
/// Use this API to set a file handler response if your backend need async
/// response for `setFileHandler()`.
pub fn interfaceSetResponseFileHandler(self: webui, response: []u8) void {
c.webui_interface_set_response_file_handler(
Expand Down Expand Up @@ -257,6 +263,12 @@ pub fn malloc(size: usize) ![]u8 {
return @as([*]u8, @ptrCast(ptr))[0..size];
}

/// Copy raw data
/// In general, you should not use this function
pub fn memcpy(dst: []u8, src: []const u8) void {
c.webui_memcpy(@ptrCast(dst.ptr), @ptrCast(src.ptr), src.len);
}

/// Safely send raw data to the UI. All clients.
pub fn sendRaw(self: webui, js_func: [:0]const u8, raw: []u8) void {
c.webui_send_raw(self.window_handle, js_func.ptr, @ptrCast(raw.ptr), raw.len);
Expand Down Expand Up @@ -1017,4 +1029,9 @@ pub const Event = extern struct {
pub fn getSize(e: *Event) usize {
return c.webui_get_size(e);
}

/// Get user data that is set using `SetContext()`.
pub fn getContext(e: *Event) *anyopaque {
return c.webui_get_context(e);
}
};