From 05b2ac129d6ca641567db77ce03860373c9a8932 Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Tue, 21 Oct 2025 15:02:47 +0200 Subject: [PATCH 1/2] With this code change, the call to bind(, "", ), will also catch the navigation event for GtkWebKit (linux WebView). So no need anymore for an navigation handler, unless one would want to implement this as a feature for all webview / browser variants. Signed-off-by: Hans Dijkema --- include/webui.h | 13 ----------- src/webui.c | 62 ++++++++++++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/include/webui.h b/include/webui.h index 490e02b2c..8aad94a7d 100644 --- a/include/webui.h +++ b/include/webui.h @@ -530,19 +530,6 @@ WEBUI_EXPORT void webui_set_browser_folder(const char* path); */ WEBUI_EXPORT bool webui_set_default_root_folder(const char* path); -/** - * @brief Set a callback to catch the navigation event of the WebView window. - * Must return `false` to prevent the navigation event, `true` otherwise. - * - * @example - * bool myNavigationEvent(size_t window) { - * // Prevent WebView window navigation event - * return false; - * } - * webui_set_navigation_handler_wv(myWindow, myNavigationEvent); - */ -WEBUI_EXPORT void webui_set_navigation_handler_wv(size_t window, bool (*navigate_handler)(size_t window)); - /** * @brief Set a callback to catch the close event of the WebView window. * Must return `false` to prevent the close event, `true` otherwise. diff --git a/src/webui.c b/src/webui.c index c37eb055f..6671486a9 100644 --- a/src/webui.c +++ b/src/webui.c @@ -269,6 +269,7 @@ typedef struct webui_event_inf_t { bool navigate; bool size; bool position; + bool in_show; unsigned int width; unsigned int height; unsigned int x; @@ -825,21 +826,6 @@ void webui_run(size_t window, const char* script) { _webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len); } -void webui_set_navigation_handler_wv(size_t window, bool (*navigate_handler)(size_t window)) { - #ifdef WEBUI_LOG - _webui_log_info("[User]webui_set_navigation_handler_wv(%zu, %p)", window, navigate_handler); - #endif - - // Dereference - if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL) - return; - - _webui_window_t* win = _webui.wins[window]; - - // Set the navigation handler - win->navigation_handler_wv = navigate_handler; -} - void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) { // Initialization @@ -1954,6 +1940,26 @@ void webui_set_context(size_t window, const char* element, void* context) { #endif } + +#if __linux__ +static bool _webui_may_navigate_gtk_wv(size_t window) +{ + if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL) + return true; + + _webui_window_t* win = _webui.wins[window]; + if (win->webView) { + bool in_show = win->webView->in_show; + if (in_show) { + win->webView->in_show = false; + } + return in_show; + } + + return true; +} +#endif + size_t webui_bind(size_t window, const char* element, void(*func)(webui_event_t* e)) { #ifdef WEBUI_LOG @@ -1986,7 +1992,18 @@ size_t webui_bind(size_t window, const char* element, void(*func)(webui_event_t* #ifdef WEBUI_LOG _webui_log_info("[User] webui_bind() -> Save bind (all events) index %zu, address 0x%p\n", index, func); #endif + +#if __linux__ + if (win->navigation_handler_wv == NULL) { + win->navigation_handler_wv = _webui_may_navigate_gtk_wv; + } +#endif } +#if __linux__ + else { + win->navigation_handler_wv = NULL; + } +#endif return index; } else { // Non-empty Element ID Binding (New / Update) @@ -8150,6 +8167,12 @@ static const char* _webui_get_local_ip(void) { #endif } +#if __linux__ +#define IN_SHOW(yes) if (win->webView) win->webView->in_show = true; +#else +#define IN_SHOW(yes) +#endif + static bool _webui_show_window(_webui_window_t* win, struct mg_connection* client, const char* content, int type, size_t browser) { #ifdef WEBUI_LOG @@ -8163,6 +8186,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien _webui_log_debug("[Core]\t\t_webui_show_window(FILE, [%zu])\n", browser); #endif + IN_SHOW(true) + #ifdef WEBUI_TLS // TLS if (_webui_is_empty(_webui.ssl_cert) || _webui_is_empty(_webui.ssl_key)) { @@ -8193,6 +8218,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien _webui_free_mem((void*)ssl_cert); _webui_free_mem((void*)ssl_key); WEBUI_ASSERT("Generating self-signed TLS certificate failed"); + IN_SHOW(false) return false; } @@ -8377,6 +8403,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien _webui_free_mem((void*)win->url); _webui_free_port(win->server_port); win->server_port = 0; + IN_SHOW(false) return false; } } @@ -12087,13 +12114,16 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { _webui_wv_event_on_close), (void *)win, NULL, 0); g_signal_connect_data(win->webView->gtk_win, "destroy", G_CALLBACK( _webui_wv_event_closed), (void *)win, NULL, 0); - + g_signal_connect_data(win->webView->gtk_wv, "decide-policy", G_CALLBACK( + _webui_wv_event_decision), (void *)win, NULL, 0); + // Linux GTK WebView Auto JS Inject if (_webui.config.show_auto_js_inject) { // ... } // Show + IN_SHOW(true) webkit_web_view_load_uri(win->webView->gtk_wv, win->webView->url); gtk_widget_show_all(win->webView->gtk_win); win->webView->open = true; From 14b2bbd8579e8e25cc5d81c1ba6473b0380ce5e6 Mon Sep 17 00:00:00 2001 From: Hans Dijkema Date: Tue, 21 Oct 2025 16:05:46 +0200 Subject: [PATCH 2/2] Fix on communicating the type of requested navigation. Signed-off-by: Hans Dijkema --- src/webui.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/webui.c b/src/webui.c index 6671486a9..f2691f108 100644 --- a/src/webui.c +++ b/src/webui.c @@ -11903,9 +11903,9 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) { snprintf(buf, 20, "%d", navigation_type); size_t nt_s = strlen(buf) + 1; char *type = (char *) _webui_malloc(nt_s); - strncpy(uri, buf, nt_s - 1); - uri[nt_s] = '\0'; - + strncpy(type, buf, nt_s - 1); + type[nt_s] = '\0'; + // Event Info webui_event_inf_t* event_inf = NULL; size_t event_num = _webui_new_event_inf(win, &event_inf);