From 85270f6e3b73cdd25783c63a7bf1438825df4dc6 Mon Sep 17 00:00:00 2001 From: Stephen Gutekanst Date: Mon, 18 Oct 2021 00:39:31 -0700 Subject: [PATCH] glfw: add Window.setIconifyCallback Signed-off-by: Stephen Gutekanst --- glfw/src/Window.zig | 77 +++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/glfw/src/Window.zig b/glfw/src/Window.zig index 96b07ad..fbe5746 100644 --- a/glfw/src/Window.zig +++ b/glfw/src/Window.zig @@ -41,6 +41,7 @@ pub const InternalUserPointer = struct { setCloseCallback: ?fn (window: Window) void, setRefreshCallback: ?fn (window: Window) void, setFocusCallback: ?fn (window: Window, focused: bool) void, + setIconifyCallback: ?fn (window: Window, iconified: bool) void, }; /// Resets all window hints to their default values. @@ -1158,27 +1159,42 @@ pub inline fn setFocusCallback(self: Window, callback: ?fn (window: Window, focu getError() catch {}; } -// TODO(window): +fn setIconifyCallbackWrapper(handle: ?*c.GLFWwindow, iconified: c_int) callconv(.C) void { + const window = from(handle.?) catch unreachable; + const internal = window.getInternal(); + internal.setIconifyCallback.?(window, if (iconified == c.GLFW_TRUE) true else false); +} -// /// Sets the iconify callback for the specified window. -// /// -// /// This function sets the iconification callback of the specified window, which -// /// is called when the window is iconified or restored. -// /// -// /// @param[in] window The window whose callback to set. -// /// @param[in] callback The new callback, or null to remove the currently set -// /// callback. -// /// -// /// @callback_param `window` the window which was iconified or restored. -// /// @callback_param `focused` `true` if the window was iconified, or `false` if it was restored. -// /// -// /// wayland: The wl_shell protocol has no concept of iconification, -// /// this callback will never be called when using this deprecated protocol. -// /// -// /// @thread_safety This function must only be called from the main thread. -// /// -// /// see also: window_iconify -// GLFWAPI GLFWwindowiconifyfun glfwSetWindowIconifyCallback(GLFWwindow* window, GLFWwindowiconifyfun callback); +/// Sets the iconify callback for the specified window. +/// +/// This function sets the iconification callback of the specified window, which +/// is called when the window is iconified or restored. +/// +/// @param[in] window The window whose callback to set. +/// @param[in] callback The new callback, or null to remove the currently set +/// callback. +/// +/// @callback_param `window` the window which was iconified or restored. +/// @callback_param `iconified` `true` if the window was iconified, or `false` if it was restored. +/// +/// wayland: The wl_shell protocol has no concept of iconification, +/// this callback will never be called when using this deprecated protocol. +/// +/// @thread_safety This function must only be called from the main thread. +/// +/// see also: window_iconify +pub inline fn setIconifyCallback(self: Window, callback: ?fn (window: Window, iconified: bool) void) void { + var internal = self.getInternal(); + internal.setIconifyCallback = callback; + _ = c.glfwSetWindowIconifyCallback(self.handle, if (callback != null) setIconifyCallbackWrapper else null); + + // The only error this could return would be glfw.Error.NotInitialized, which should + // definitely have occurred before calls to this. Returning an error here makes the API + // awkward to use, so we discard it instead. + getError() catch {}; +} + +// TODO(window): // /// Sets the maximize callback for the specified window. // /// @@ -1879,3 +1895,24 @@ test "setFocusCallback" { } }).callback); } + +test "setIconifyCallback" { + const glfw = @import("main.zig"); + try glfw.init(); + defer glfw.terminate(); + + const window = glfw.Window.create(640, 480, "Hello, Zig!", null, null) catch |err| { + // return without fail, because most of our CI environments are headless / we cannot open + // windows on them. + std.debug.print("note: failed to create window: {}\n", .{err}); + return; + }; + defer window.destroy(); + + window.setIconifyCallback((struct { + fn callback(_window: Window, iconified: bool) void { + _ = _window; + _ = iconified; + } + }).callback); +}