Skip to content

Commit 05b2ac1

Browse files
committed
With this code change, the call to bind(<window>, "", <callback>),
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 <hans@dijkewijk.nl>
1 parent 2a9c91c commit 05b2ac1

2 files changed

Lines changed: 46 additions & 29 deletions

File tree

include/webui.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -530,19 +530,6 @@ WEBUI_EXPORT void webui_set_browser_folder(const char* path);
530530
*/
531531
WEBUI_EXPORT bool webui_set_default_root_folder(const char* path);
532532

533-
/**
534-
* @brief Set a callback to catch the navigation event of the WebView window.
535-
* Must return `false` to prevent the navigation event, `true` otherwise.
536-
*
537-
* @example
538-
* bool myNavigationEvent(size_t window) {
539-
* // Prevent WebView window navigation event
540-
* return false;
541-
* }
542-
* webui_set_navigation_handler_wv(myWindow, myNavigationEvent);
543-
*/
544-
WEBUI_EXPORT void webui_set_navigation_handler_wv(size_t window, bool (*navigate_handler)(size_t window));
545-
546533
/**
547534
* @brief Set a callback to catch the close event of the WebView window.
548535
* Must return `false` to prevent the close event, `true` otherwise.

src/webui.c

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ typedef struct webui_event_inf_t {
269269
bool navigate;
270270
bool size;
271271
bool position;
272+
bool in_show;
272273
unsigned int width;
273274
unsigned int height;
274275
unsigned int x;
@@ -825,21 +826,6 @@ void webui_run(size_t window, const char* script) {
825826
_webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len);
826827
}
827828

828-
void webui_set_navigation_handler_wv(size_t window, bool (*navigate_handler)(size_t window)) {
829-
#ifdef WEBUI_LOG
830-
_webui_log_info("[User]webui_set_navigation_handler_wv(%zu, %p)", window, navigate_handler);
831-
#endif
832-
833-
// Dereference
834-
if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL)
835-
return;
836-
837-
_webui_window_t* win = _webui.wins[window];
838-
839-
// Set the navigation handler
840-
win->navigation_handler_wv = navigate_handler;
841-
}
842-
843829
void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) {
844830

845831
// Initialization
@@ -1954,6 +1940,26 @@ void webui_set_context(size_t window, const char* element, void* context) {
19541940
#endif
19551941
}
19561942

1943+
1944+
#if __linux__
1945+
static bool _webui_may_navigate_gtk_wv(size_t window)
1946+
{
1947+
if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL)
1948+
return true;
1949+
1950+
_webui_window_t* win = _webui.wins[window];
1951+
if (win->webView) {
1952+
bool in_show = win->webView->in_show;
1953+
if (in_show) {
1954+
win->webView->in_show = false;
1955+
}
1956+
return in_show;
1957+
}
1958+
1959+
return true;
1960+
}
1961+
#endif
1962+
19571963
size_t webui_bind(size_t window, const char* element, void(*func)(webui_event_t* e)) {
19581964

19591965
#ifdef WEBUI_LOG
@@ -1986,7 +1992,18 @@ size_t webui_bind(size_t window, const char* element, void(*func)(webui_event_t*
19861992
#ifdef WEBUI_LOG
19871993
_webui_log_info("[User] webui_bind() -> Save bind (all events) index %zu, address 0x%p\n", index, func);
19881994
#endif
1995+
1996+
#if __linux__
1997+
if (win->navigation_handler_wv == NULL) {
1998+
win->navigation_handler_wv = _webui_may_navigate_gtk_wv;
1999+
}
2000+
#endif
19892001
}
2002+
#if __linux__
2003+
else {
2004+
win->navigation_handler_wv = NULL;
2005+
}
2006+
#endif
19902007
return index;
19912008
} else {
19922009
// Non-empty Element ID Binding (New / Update)
@@ -8150,6 +8167,12 @@ static const char* _webui_get_local_ip(void) {
81508167
#endif
81518168
}
81528169

8170+
#if __linux__
8171+
#define IN_SHOW(yes) if (win->webView) win->webView->in_show = true;
8172+
#else
8173+
#define IN_SHOW(yes)
8174+
#endif
8175+
81538176
static bool _webui_show_window(_webui_window_t* win, struct mg_connection* client, const char* content, int type, size_t browser) {
81548177

81558178
#ifdef WEBUI_LOG
@@ -8163,6 +8186,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
81638186
_webui_log_debug("[Core]\t\t_webui_show_window(FILE, [%zu])\n", browser);
81648187
#endif
81658188

8189+
IN_SHOW(true)
8190+
81668191
#ifdef WEBUI_TLS
81678192
// TLS
81688193
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
81938218
_webui_free_mem((void*)ssl_cert);
81948219
_webui_free_mem((void*)ssl_key);
81958220
WEBUI_ASSERT("Generating self-signed TLS certificate failed");
8221+
IN_SHOW(false)
81968222
return false;
81978223
}
81988224

@@ -8377,6 +8403,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
83778403
_webui_free_mem((void*)win->url);
83788404
_webui_free_port(win->server_port);
83798405
win->server_port = 0;
8406+
IN_SHOW(false)
83808407
return false;
83818408
}
83828409
}
@@ -12087,13 +12114,16 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
1208712114
_webui_wv_event_on_close), (void *)win, NULL, 0);
1208812115
g_signal_connect_data(win->webView->gtk_win, "destroy", G_CALLBACK(
1208912116
_webui_wv_event_closed), (void *)win, NULL, 0);
12090-
12117+
g_signal_connect_data(win->webView->gtk_wv, "decide-policy", G_CALLBACK(
12118+
_webui_wv_event_decision), (void *)win, NULL, 0);
12119+
1209112120
// Linux GTK WebView Auto JS Inject
1209212121
if (_webui.config.show_auto_js_inject) {
1209312122
// ...
1209412123
}
1209512124

1209612125
// Show
12126+
IN_SHOW(true)
1209712127
webkit_web_view_load_uri(win->webView->gtk_wv, win->webView->url);
1209812128
gtk_widget_show_all(win->webView->gtk_win);
1209912129
win->webView->open = true;

0 commit comments

Comments
 (0)