@@ -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);
641642static bool _webui_wv_set_size (_webui_wv_linux_t * webView , int windowWidth , int windowHeight );
642643static bool _webui_wv_show (_webui_window_t * win , char * url );
643644static void _webui_wv_event_closed (void * widget , void * arg );
645+ static bool _webui_wv_event_on_close (void * widget , void * event , void * arg );
644646static int _webui_wv_exit_schedule (void * arg );
645647static bool _webui_wv_maximize (_webui_wv_linux_t * webView );
646648static 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+
778799void 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