Skip to content

Commit 63e0a97

Browse files
authored
Merge pull request #643 from hdijkema/on-close
"On Close" events and handling the window close for WebView solutions
2 parents 8b5a276 + 1efae09 commit 63e0a97

2 files changed

Lines changed: 73 additions & 7 deletions

File tree

include/webui.h

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

522+
/**
523+
* @brief Set a callback to catch the close event of the WebView window.
524+
* Must return `false` to prevent the close event, `true` otherwise.
525+
*
526+
* @example
527+
* bool myCloseEvent(size_t window) {
528+
* // Prevent WebView window close event
529+
* return false;
530+
* }
531+
* webui_set_close_handler(myWindow, myCloseEvent);
532+
*/
533+
WEBUI_EXPORT void webui_set_close_handler_wv(size_t window, bool (*close_handler)(size_t window));
534+
522535
/**
523536
* @brief Set a custom handler to serve files. This custom handler should
524537
* return full HTTP header and body.

src/webui.c

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ typedef struct _webui_window_t {
373373
int x;
374374
int y;
375375
bool position_set;
376+
bool (*close_handler_wv)(size_t window);
376377
const void*(*files_handler)(const char* filename, int* length);
377378
const void*(*files_handler_window)(size_t window, const char* filename, int* length);
378379
const void* file_handler_async_response;
@@ -641,6 +642,7 @@ static bool _webui_wv_set_position(_webui_wv_linux_t* webView, int x, int y);
641642
static bool _webui_wv_set_size(_webui_wv_linux_t* webView, int windowWidth, int windowHeight);
642643
static bool _webui_wv_show(_webui_window_t* win, char* url);
643644
static void _webui_wv_event_closed(void *widget, void *arg);
645+
static bool _webui_wv_event_on_close(void *widget, void *event, void *arg);
644646
static int _webui_wv_exit_schedule(void* arg);
645647
static bool _webui_wv_maximize(_webui_wv_linux_t* webView);
646648
static bool _webui_wv_minimize(_webui_wv_linux_t* webView);
@@ -775,6 +777,25 @@ void webui_run(size_t window, const char* script) {
775777
_webui_send_all(win, 0, WEBUI_CMD_JS_QUICK, script, js_len);
776778
}
777779

780+
void webui_set_close_handler_wv(size_t window, bool(*close_handler)(size_t window)) {
781+
782+
// Initialization
783+
_webui_init();
784+
785+
// Dereference
786+
if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS) || _webui.wins[window] == NULL)
787+
return;
788+
789+
_webui_window_t* win = _webui.wins[window];
790+
791+
#ifdef WEBUI_LOG
792+
printf("[User]webui_set_close_handler(%zu, %p)", window, close_handler);
793+
#endif
794+
795+
// Set the close handler
796+
win->close_handler_wv = close_handler;
797+
}
798+
778799
void webui_set_file_handler(size_t window, const void*(*handler)(const char* filename, int* length)) {
779800

780801
if (handler == NULL)
@@ -11546,13 +11567,25 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
1154611567
}
1154711568
case WM_CLOSE: {
1154811569
if (win) {
11549-
// Stop the WebView thread, close the window
11550-
// and free resources.
11551-
if (win->webView) {
11552-
win->webView->stop = true;
11553-
_webui_webview_update(win);
11554-
}
11555-
_webui_wv_event_closed(win);
11570+
bool can_close = true;
11571+
if (win->close_handler_wv != NULL) {
11572+
can_close = win->close_handler_wv(win->num);
11573+
#ifdef WEBUI_LOG
11574+
webui_log_debug("[Core]\t\tClose handler installed for %zu, result = %d\n", win->num, can_close);
11575+
#endif
11576+
}
11577+
if (can_close) {
11578+
// Stop the WebView thread, close the window
11579+
// and free resources.
11580+
if (win->webView) {
11581+
win->webView->stop = true;
11582+
_webui_webview_update(win);
11583+
}
11584+
_webui_wv_event_closed(win);
11585+
} else {
11586+
// Do not close the window, no default processing
11587+
return 0;
11588+
}
1155611589
}
1155711590
break;
1155811591
}
@@ -11987,6 +12020,24 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
1198712020
}
1198812021
}
1198912022

12023+
// Delete Event (implement on close handling)
12024+
static bool _webui_wv_event_on_close(void *widget, void *evt, void *arg) {
12025+
#ifdef WEBUI_LOG
12026+
printf("[Core]\t\t_webui_wv_event_on_close()\n");
12027+
#endif
12028+
_webui_window_t* win = _webui_dereference_win_ptr(arg);
12029+
if (win) {
12030+
if (win->close_handler_wv) {
12031+
bool can_close = win->close_handler_wv(win->num);
12032+
#ifdef WEBUI_LOG
12033+
printf("[Core]\t\t_webui_wv_event_on_close() -> can_close = %d\n", can_close);
12034+
#endif
12035+
return !can_close;
12036+
}
12037+
}
12038+
return false;
12039+
}
12040+
1199012041
static bool _webui_wv_set_size(_webui_wv_linux_t* webView, int windowWidth, int windowHeight) {
1199112042
#ifdef WEBUI_LOG
1199212043
printf("[Core]\t\t_webui_wv_set_size(%d. %d)\n", windowWidth, windowHeight);
@@ -12125,6 +12176,8 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved) {
1212512176
// Events
1212612177
g_signal_connect_data(win->webView->gtk_wv, "notify::title", G_CALLBACK(
1212712178
_webui_wv_event_title), (void *)win, NULL, 0);
12179+
g_signal_connect_data(win->webView->gtk_win, "delete-event", G_CALLBACK(
12180+
_webui_wv_event_on_close), (void *)win, NULL, 0);
1212812181
g_signal_connect_data(win->webView->gtk_win, "destroy", G_CALLBACK(
1212912182
_webui_wv_event_closed), (void *)win, NULL, 0);
1213012183

0 commit comments

Comments
 (0)