diff --git a/build.zig.zon b/build.zig.zon index df638d0..d487d39 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,12 +1,12 @@ .{ .name = .zig_webui, - .version = "0.0.1", - .minimum_zig_version = "0.12.0", + .version = "2.5.0-beta.4", .fingerprint = 0x95965ed3cdfb8c33, + .minimum_zig_version = "0.14.0", .dependencies = .{ .webui = .{ - .url = "https://github.com/webui-dev/webui/archive/5d497e3b839a93736be82107b333b4c0390dea2d.tar.gz", - .hash = "webui-2.5.0-beta.3-pxqD5WZmNgCLmrqQW0U_NI4iq-z706dyreyXr-e7kFmZ", + .hash = "webui-2.5.0-beta.4-pxqD5WQONwB73V_0MKBVXgR7k6t6pb_B3KfAZqfbPf-7", + .url = "https://github.com/webui-dev/webui/archive/3c03d4f45cf6e65e678b0ca87f8ec28d952b9d78.tar.gz", }, }, .paths = .{ diff --git a/src/c.zig b/src/c.zig index c19e27d..8d228d3 100644 --- a/src/c.zig +++ b/src/c.zig @@ -187,6 +187,15 @@ pub extern fn webui_set_custom_parameters(window: usize, params: [*:0]const u8) /// @example webui_set_high_contrast(my_window, true); pub extern fn webui_set_high_contrast(window: usize, status: bool) callconv(.C) void; +/// @brief Sets whether the window frame is resizable or fixed. +/// Works only on WebView window. +/// +/// @param window The window number +/// @param status True or False +/// +/// @example webui_set_resizable(myWindow, true); +pub extern fn webui_set_resizable(window: usize, status: bool) callconv(.C) void; + /// @brief Get OS high contrast preference. /// /// @return Returns True if OS is using high contrast theme @@ -214,6 +223,20 @@ pub extern fn webui_wait() callconv(.C) void; /// @example webui_close(my_window); pub extern fn webui_close(window: usize) callconv(.C) void; +/// @brief Minimize a WebView window. +/// +/// @param window The window number +/// +/// @example webui_minimize(myWindow); +pub extern fn webui_minimize(window: usize) callconv(.C) void; + +/// @brief Maximize a WebView window. +/// +/// @param window The window number +/// +/// @example webui_maximize(myWindow); +pub extern fn webui_maximize(window: usize) callconv(.C) void; + /// @brief Close a specific client. /// /// @param e The event struct @@ -241,6 +264,13 @@ pub extern fn webui_exit() callconv(.C) void; /// @example webui_set_root_folder(my_window, "/home/Foo/Bar/"); pub extern fn webui_set_root_folder(window: usize, path: [*:0]const u8) callconv(.C) bool; +/// @brief Set custom browser folder path. +/// +/// @param path The browser folder path +/// +/// @example webui_set_browser_folder("/home/Foo/Bar/"); +pub extern fn webui_set_browser_folder(path: [*:0]const u8) callconv(.C) void; + /// @brief Set the web-server root folder path for all windows. Should be used /// before `webui_show()`. /// @@ -293,7 +323,7 @@ pub extern fn webui_set_file_handler_window( /// pub extern fn webui_interface_set_response_file_handler( window: usize, - response: ?*const anyopaque, + response: *const anyopaque, length: usize, ) callconv(.C) void; @@ -442,6 +472,14 @@ pub extern fn webui_set_minimum_size( /// @example webui_set_position(my_window, 100, 100); pub extern fn webui_set_position(window: usize, x: u32, y: u32) callconv(.C) void; +/// @brief Centers the window on the screen. Works better with +/// WebView. Call this function before `webui_show()` for better results. +/// +/// @param window The window number +/// +/// @example webui_set_center(myWindow); +pub extern fn webui_set_center(window: usize) callconv(.C) void; + /// @brief Set the web browser profile to use. An empty `name` and `path` means /// the default user profile. Need to be called before `webui_show()`. /// @@ -556,6 +594,16 @@ pub extern fn webui_get_parent_process_id(window: usize) callconv(.C) usize; /// @example const id: usize = webui_get_child_process_id(my_window); pub extern fn webui_get_child_process_id(window: usize) callconv(.C) usize; +/// @brief Gets Win32 window `HWND`. More reliable with WebView +/// than web browser window, as browser PIDs may change on launch. +/// +/// @param window The window number +/// +/// @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; + /// @brief Get the network port of a running window. /// This can be useful to determine the HTTP link of `webui.js` /// @@ -604,6 +652,22 @@ pub extern fn webui_set_config(option: Config, status: bool) callconv(.C) void; /// @example webui_set_event_blocking(my_window, true); pub extern fn webui_set_event_blocking(window: usize, status: bool) callconv(.C) void; +/// @brief Make a WebView window frameless. +/// +/// @param window The window number +/// @param status The frameless status `true` or `false` +/// +/// @example webui_set_frameless(myWindow, true); +pub extern fn webui_set_frameless(window: usize, status: bool) callconv(.C) void; + +/// @brief Make a WebView window transparent. +/// +/// @param window The window number +/// @param status The transparency status `true` or `false` +/// +/// @example webui_set_transparent(myWindow, true); +pub extern fn webui_set_transparent(window: usize, status: bool) callconv(.C) void; + /// @brief Get the HTTP mime type of a file. /// /// @return Returns the HTTP mime string @@ -1050,4 +1114,4 @@ pub extern fn webui_interface_script_client( timeout: usize, buffer: [*c]u8, buffer_length: usize, -) callconv(.C) void; +) callconv(.C) bool; diff --git a/src/webui.zig b/src/webui.zig index 0716c83..f280c38 100644 --- a/src/webui.zig +++ b/src/webui.zig @@ -11,6 +11,7 @@ const webui = @This(); const builtin = @import("builtin"); const std = @import("std"); +const windows = std.os.windows; const flags = @import("flags"); @@ -32,7 +33,7 @@ pub fn newWindowWithId(id: usize) webui { std.log.err("id {} is illegal", .{id}); if (comptime builtin.zig_version.minor == 11) { std.os.exit(1); - } else if (comptime builtin.zig_version.minor == 12) { + } else if (comptime builtin.zig_version.minor >= 12) { std.posix.exit(1); } } @@ -122,6 +123,12 @@ pub fn setHighContrast(self: webui, status: bool) void { c.webui_set_high_contrast(self.window_handle, status); } +/// Sets whether the window frame is resizable or fixed. +/// Works only on WebView window. +pub fn setResizable(self: webui, status: bool) void { + c.webui_set_resizable(self.window_handle, status); +} + pub fn isHighConstrast() bool { return c.webui_is_high_contrast(); } @@ -137,10 +144,22 @@ pub fn wait() void { c.webui_wait(); } +/// Close a specific window only. The window object will still exist. +/// All clients. pub fn close(self: webui) void { c.webui_close(self.window_handle); } +/// Minimize a WebView window. +pub fn minimize(self: webui) void { + c.webui_minimize(self.window_handle); +} + +/// Maximize a WebView window. +pub fn maximize(self: webui) void { + c.webui_maximize(self.window_handle); +} + /// Close a specific window and free all memory resources. pub fn destroy(self: webui) void { c.webui_destroy(self.window_handle); @@ -157,6 +176,11 @@ pub fn setRootFolder(self: webui, path: [:0]const u8) bool { return c.webui_set_root_folder(self.window_handle, path.ptr); } +/// Set custom browser folder path. +pub fn setBrowserFolder(self: webui, path: [:0]const u8) bool { + return c.webui_set_browser_folder(self.window_handle, path.ptr); +} + /// Set the web-server root folder path for all windows. /// Should be used before `show()`. pub fn setDefaultRootFolder(path: [:0]const u8) bool { @@ -229,6 +253,7 @@ pub fn setIcon(self: webui, icon: [:0]const u8, icon_type: [:0]const u8) void { /// Base64 encoding. Use this to safely send text based data to the UI. If /// it fails it will return NULL. +/// you need free the return memory with free function pub fn encode(str: [:0]const u8) ?[]u8 { const ptr = c.webui_encode(str.ptr); if (ptr == null) { @@ -241,6 +266,7 @@ pub fn encode(str: [:0]const u8) ?[]u8 { /// Base64 decoding. /// Use this to safely decode received Base64 text from the UI. /// If it fails it will return NULL. +/// you need free the return memory with free function pub fn decode(str: [:0]const u8) ?[]u8 { const ptr = c.webui_decode(str.ptr); if (ptr == null) { @@ -295,6 +321,12 @@ pub fn setPosition(self: webui, x: u32, y: u32) void { c.webui_set_position(self.window_handle, x, y); } +/// Centers the window on the screen. Works better with +/// WebView. Call this function before `webui_show()` for better results. +pub fn setCenter(self: webui) void { + c.webui_set_center(self.window_handle); +} + /// Set the web browser profile to use. /// An empty `name` and `path` means the default user profile. /// Need to be called before `show()`. @@ -356,6 +388,12 @@ pub fn getChildProcessId(self: webui) usize { return c.webui_get_child_process_id(self.window_handle); } +/// Gets Win32 window `HWND`. More reliable with WebView +/// than web browser window, as browser PIDs may change on launch. +pub fn win32GetHwnd(self: webui) windows.HWND { + const tmp_hwnd = c.webui_win32_get_hwnd(self.window_handle); + return @ptrCast(tmp_hwnd); +} /// Get the network port of a running window. /// This can be useful to determine the HTTP link of `webui.js` pub fn getPort(self: webui) usize { @@ -388,6 +426,16 @@ pub fn setEventBlocking(self: webui, status: bool) void { c.webui_set_event_blocking(self.window_handle, status); } +/// Make a WebView window frameless. +pub fn setFrameless(self: webui, status: bool) void { + c.webui_set_frameless(self.window_handle, status); +} + +/// Make a WebView window transparent. +pub fn setTransparent(self: webui, status: bool) void { + c.webui_set_transparent(self.window_handle, status); +} + /// Get the HTTP mime type of a file. pub fn getMimeType(file: [:0]const u8) [:0]const u8 { const res = c.webui_get_mime_type(file.ptr); @@ -603,8 +651,8 @@ pub fn interfaceRunClient(self: webui, event_number: usize, script_content: [:0] } // Run JavaScript and get the response back. Single client. -pub fn interfaceScriptClient(self: webui, event_number: usize, script_content: [:0]const u8, timeout: usize, buffer: []u8) void { - c.webui_interface_script_client(self.window_handle, event_number, script_content.ptr, timeout, buffer.ptr, buffer.len); +pub fn interfaceScriptClient(self: webui, event_number: usize, script_content: [:0]const u8, timeout: usize, buffer: []u8) bool { + return c.webui_interface_script_client(self.window_handle, event_number, script_content.ptr, timeout, buffer.ptr, buffer.len); } /// a very convenient function for binding callback. @@ -668,15 +716,15 @@ pub fn binding(self: webui, element: [:0]const u8, comptime callback: anytype) u param_tup[i] = e; }, .Bool => { - const res = getBoolAt(e, i); + const res = e.getBoolAt(i); param_tup[i] = res; }, .Int => { - const res = getIntAt(e, i); + const res = e.getIntAt(i); param_tup[i] = @intCast(res); }, .Float => { - const res = getFloatAt(e, i); + const res = e.getFloatAt(i); param_tup[i] = res; }, .Pointer => |pointer| { @@ -687,8 +735,8 @@ pub fn binding(self: webui, element: [:0]const u8, comptime callback: anytype) u ); @compileError(err_msg); } - const str_ptr = getStringAt(e, i); - const tmp_str_len = getSizeAt(e, i); + const str_ptr = e.getStringAt(i); + const tmp_str_len = e.getSizeAt(i); const str: []const u8 = str_ptr[0..tmp_str_len]; param_tup[i] = str; }, @@ -993,7 +1041,7 @@ pub const Event = extern struct { /// Get the first argument as float pub fn getFloat(e: *Event) f64 { - c.webui_get_float(e); + return c.webui_get_float(e); } /// Get an argument as string at a specific index