diff --git a/include/webui.h b/include/webui.h index 490e02b2c..9748954ca 100644 --- a/include/webui.h +++ b/include/webui.h @@ -901,6 +901,20 @@ WEBUI_EXPORT size_t webui_get_child_process_id(size_t window); */ WEBUI_EXPORT void* webui_win32_get_hwnd(size_t window); +/** + * @brief Get 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` in Win32, `GtkWindow` in Linux. + * + * @example + * HWND hwnd = webui_get_hwnd(myWindow); // Win32 (Work with WebView and web browser) + * GtkWindow* window = webui_get_hwnd(myWindow); // Linux (Work with WebView only) + */ +WEBUI_EXPORT void* webui_get_hwnd(size_t window); + /** * @brief Get the network port of a running window. * This can be useful to determine the HTTP link of `webui.js` diff --git a/src/webui.c b/src/webui.c index c37eb055f..e198454e5 100644 --- a/src/webui.c +++ b/src/webui.c @@ -2800,6 +2800,38 @@ void* webui_win32_get_hwnd(size_t window) { return NULL; } +void* webui_get_hwnd(size_t window) { + + #ifdef WEBUI_LOG + _webui_log_info("[User] webui_get_hwnd([%zu])\n", window); + #endif + + #ifdef _WIN32 + return webui_win32_get_hwnd(size_t window); + #elif __linux__ + + // Initialization + _webui_init(); + + // Dereference + if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL) + return NULL; + _webui_window_t* win = _webui.wins[window]; + + if (_webui.is_webview) { + if (win->webView) { + return win->webView->gtk_win; + } + } + + return NULL; + + #else + // macOS + return NULL; // TODO: Return window handler + #endif +} + void webui_set_hide(size_t window, bool status) { #ifdef WEBUI_LOG