Skip to content

Commit 05c75c8

Browse files
authored
Merge pull request #654 from hdijkema/parent_window
Get a reference to a native window handle (WebView) on all supported platforms
2 parents 2a9c91c + d4cd6f4 commit 05c75c8

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

include/webui.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,20 @@ WEBUI_EXPORT size_t webui_get_child_process_id(size_t window);
901901
*/
902902
WEBUI_EXPORT void* webui_win32_get_hwnd(size_t window);
903903

904+
/**
905+
* @brief Get window `HWND`. More reliable with WebView
906+
* than web browser window, as browser PIDs may change on launch.
907+
*
908+
* @param window The window number
909+
*
910+
* @return Returns the window `hwnd` in Win32, `GtkWindow` in Linux.
911+
*
912+
* @example
913+
* HWND hwnd = webui_get_hwnd(myWindow); // Win32 (Work with WebView and web browser)
914+
* GtkWindow* window = webui_get_hwnd(myWindow); // Linux (Work with WebView only)
915+
*/
916+
WEBUI_EXPORT void* webui_get_hwnd(size_t window);
917+
904918
/**
905919
* @brief Get the network port of a running window.
906920
* This can be useful to determine the HTTP link of `webui.js`

src/webui.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,6 +2800,38 @@ void* webui_win32_get_hwnd(size_t window) {
28002800
return NULL;
28012801
}
28022802

2803+
void* webui_get_hwnd(size_t window) {
2804+
2805+
#ifdef WEBUI_LOG
2806+
_webui_log_info("[User] webui_get_hwnd([%zu])\n", window);
2807+
#endif
2808+
2809+
#ifdef _WIN32
2810+
return webui_win32_get_hwnd(size_t window);
2811+
#elif __linux__
2812+
2813+
// Initialization
2814+
_webui_init();
2815+
2816+
// Dereference
2817+
if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL)
2818+
return NULL;
2819+
_webui_window_t* win = _webui.wins[window];
2820+
2821+
if (_webui.is_webview) {
2822+
if (win->webView) {
2823+
return win->webView->gtk_win;
2824+
}
2825+
}
2826+
2827+
return NULL;
2828+
2829+
#else
2830+
// macOS
2831+
return NULL; // TODO: Return window handler
2832+
#endif
2833+
}
2834+
28032835
void webui_set_hide(size_t window, bool status) {
28042836

28052837
#ifdef WEBUI_LOG

0 commit comments

Comments
 (0)