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
8 changes: 4 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -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 = .{
Expand Down
68 changes: 66 additions & 2 deletions src/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()`.
///
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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()`.
///
Expand Down Expand Up @@ -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`
///
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
66 changes: 57 additions & 9 deletions src/webui.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const webui = @This();

const builtin = @import("builtin");
const std = @import("std");
const windows = std.os.windows;

const flags = @import("flags");

Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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()`.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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| {
Expand All @@ -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;
},
Expand Down Expand Up @@ -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
Expand Down