Skip to content

Commit 0c45391

Browse files
authored
feat: add new api setContext, getContext, memcpy, and sync upstream commit 3ff8742 (#81)
* update dependency * add new apis
1 parent fe699dc commit 0c45391

File tree

3 files changed

+77
-8
lines changed

3 files changed

+77
-8
lines changed

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
.minimum_zig_version = "0.12.0",
55
.dependencies = .{
66
.webui = .{
7-
.url = "https://github.com/webui-dev/webui/archive/2d3a68ebeac6741caad16af9752ce0045e4cdcdd.tar.gz",
8-
.hash = "1220886276e230842be96daa45bf96b2f305d9eaba9e8c0c0a2886a0fc350a5c18b5",
7+
.url = "https://github.com/webui-dev/webui/archive/3ff8742f3ecac116465564eef3b1c231d89fd1d9.tar.gz",
8+
.hash = "122084515081be3d1bba85189fc9f3e9e3f35a207350ba1f9aa4c34c97ff52ecc063",
99
},
1010
},
1111
.paths = .{

src/c.zig

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,45 @@ pub extern fn webui_bind(
4747
func: *const fn (e: *Event) callconv(.C) void,
4848
) callconv(.C) usize;
4949

50+
/// @brief Use this API after using `webui_bind()` to add any user data to it that can be
51+
/// read later using `webui_get_context()`.
52+
///
53+
/// @param window The window number
54+
/// @param element The HTML element / JavaScript object
55+
/// @param context Any user data
56+
///
57+
/// @example
58+
/// webui_bind(myWindow, "myFunction", myFunction);
59+
///
60+
/// webui_set_context(myWindow, "myFunction", myData);
61+
///
62+
/// void myFunction(webui_event_t* e) {
63+
/// void* myData = webui_get_context(e);
64+
/// }
65+
pub extern fn webui_set_context(
66+
window: usize,
67+
element: [*:0]const u8,
68+
context: *anyopaque,
69+
) callconv(.C) void;
70+
71+
/// @brief Get user data that is set using `webui_set_context()`.
72+
///
73+
/// @param e The event struct
74+
///
75+
/// @return Returns user data pointer.
76+
///
77+
/// @example
78+
/// webui_bind(myWindow, "myFunction", myFunction);
79+
///
80+
/// webui_set_context(myWindow, "myFunction", myData);
81+
///
82+
/// void myFunction(webui_event_t* e) {
83+
/// void* myData = webui_get_context(e);
84+
/// }
85+
pub extern fn webui_get_context(
86+
e: *Event,
87+
) callconv(.C) *anyopaque;
88+
5089
/// @brief Get the recommended web browser ID to use. If you
5190
/// are already using one, this function will return the same ID.
5291
///
@@ -242,16 +281,16 @@ pub extern fn webui_set_file_handler_window(
242281
) callconv(.C) ?*const anyopaque,
243282
) callconv(.C) void;
244283

245-
///
246-
/// @brief Use this API to set a file handler response if your backend need async
284+
///
285+
/// @brief Use this API to set a file handler response if your backend need async
247286
/// response for `webui_set_file_handler()`.
248-
///
287+
///
249288
/// @param window The window number
250289
/// @param response The response buffer
251290
/// @param length The response size
252-
///
291+
///
253292
/// @example webui_interface_set_response_file_handler(myWindow, buffer, 1024);
254-
///
293+
///
255294
pub extern fn webui_interface_set_response_file_handler(
256295
window: usize,
257296
response: ?*const anyopaque,
@@ -311,6 +350,19 @@ pub extern fn webui_decode(str: [*:0]const u8) callconv(.C) ?[*:0]u8;
311350
/// @example webui_free(my_buffer);
312351
pub extern fn webui_free(ptr: *anyopaque) callconv(.C) void;
313352

353+
/// @brief Copy raw data.
354+
///
355+
/// @param dest Destination memory pointer
356+
/// @param src Source memory pointer
357+
/// @param count Bytes to copy
358+
///
359+
/// @example webui_memcpy(myBuffer, myData, 64);
360+
pub extern fn webui_memcpy(
361+
dest: *anyopaque,
362+
src: *anyopaque,
363+
count: usize,
364+
) callconv(.C) void;
365+
314366
/// @brief Safely allocate memory using the WebUI memory management system. It
315367
/// can be safely freed using `webui_free()` at any time.
316368
///

src/webui.zig

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ pub fn bind(
6565
return c.webui_bind(self.window_handle, element.ptr, tmp_struct.handle);
6666
}
6767

68+
/// Use this API after using `bind()` to add any user data to it that can be
69+
/// read later using `getContext()`
70+
pub fn setContext(self: webui, element: [:0]const u8, context: *anyopaque) void {
71+
c.webui_set_context(self.window_handle, element.ptr, context);
72+
}
73+
6874
/// Get the recommended web browser ID to use. If you
6975
/// are already using one, this function will return the same ID.
7076
pub fn getBestBrowser(self: webui) Browser {
@@ -195,7 +201,7 @@ pub fn setFileHandlerWindow(self: webui, comptime handler: fn (window_handle: us
195201
c.webui_set_file_handler_window(self.window_handle, tmp_struct.handle);
196202
}
197203

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

266+
/// Copy raw data
267+
/// In general, you should not use this function
268+
pub fn memcpy(dst: []u8, src: []const u8) void {
269+
c.webui_memcpy(@ptrCast(dst.ptr), @ptrCast(src.ptr), src.len);
270+
}
271+
260272
/// Safely send raw data to the UI. All clients.
261273
pub fn sendRaw(self: webui, js_func: [:0]const u8, raw: []u8) void {
262274
c.webui_send_raw(self.window_handle, js_func.ptr, @ptrCast(raw.ptr), raw.len);
@@ -1017,4 +1029,9 @@ pub const Event = extern struct {
10171029
pub fn getSize(e: *Event) usize {
10181030
return c.webui_get_size(e);
10191031
}
1032+
1033+
/// Get user data that is set using `SetContext()`.
1034+
pub fn getContext(e: *Event) *anyopaque {
1035+
return c.webui_get_context(e);
1036+
}
10201037
};

0 commit comments

Comments
 (0)