@@ -8578,7 +8578,9 @@ static bool _webui_show(_webui_window_t* win, struct mg_connection* client, cons
85788578 win -> is_showing = false;
85798579
85808580 // Bring the window to front after showing content
8581- webui_focus (win -> num );
8581+ if ((browser != NoBrowser ) && (!win -> hide )) {
8582+ webui_focus (win -> num );
8583+ }
85828584
85838585 return status ;
85848586}
@@ -8914,6 +8916,9 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
89148916 _webui_log_debug ("[Core]\t\t_webui_show_window(FILE, [%zu])\n" , browser );
89158917 #endif
89168918
8919+ // Stop any window exit signal as we are going to show a window
8920+ _webui_mutex_win_is_exit_now (win , WEBUI_MUTEX_SET_FALSE );
8921+
89178922 #if __linux__
89188923 GTK_WEBVIEW_SET_IN_SHOW (win , true)
89198924 #endif
@@ -8967,37 +8972,41 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
89678972 }
89688973 #endif
89698974
8970- _webui_mutex_win_is_exit_now (win , WEBUI_MUTEX_SET_FALSE );
8975+ // Status
8976+ bool is_window_connected = _webui_mutex_is_connected (win , WEBUI_MUTEX_GET_STATUS );
8977+ bool is_server_running = _webui_mutex_is_server_running (win , WEBUI_MUTEX_GET_STATUS );
8978+ bool is_this_ui_reload = (is_window_connected && is_server_running ); // User set new UI content on a running window
8979+ bool is_this_new_window = (!is_window_connected && !is_server_running ); // New window
8980+ bool is_this_empty_server = (!is_window_connected && is_server_running ); // Server exit, but not connected
8981+ bool is_last_was_no_browser = (win -> current_browser == NoBrowser ); // Server exit, but not connected, probably by `webui_get_url()`
89718982
8972- // Wait for server threads to stop
8973- if (!_webui_mutex_is_connected (win , WEBUI_MUTEX_GET_STATUS ) &&
8974- _webui_mutex_is_server_running (win , WEBUI_MUTEX_GET_STATUS )) {
8975- // This is not a UI reload. The server thread is still running,
8976- // wait for it to stop before starting a new one.
8977- _webui_timer_t timer ;
8978- _webui_timer_start (& timer );
8979- for (;;) {
8980- #ifdef WEBUI_LOG
8981- _webui_log_debug ("[Core]\t\t_webui_show_window() -> Waiting for server thread to stop...\n" );
8982- #endif
8983- _webui_sleep (100 );
8984- if (!_webui_mutex_is_server_running (win , WEBUI_MUTEX_GET_STATUS )) {
8985- #ifdef WEBUI_LOG
8986- _webui_log_debug ("[Core]\t\t_webui_show_window() -> Server thread stopped.\n" );
8987- #endif
8988- break ;
8989- }
8990- if (_webui_timer_is_end (& timer , 1500 )) {
8983+ // Old Server Cleanup
8984+ if (is_this_empty_server ) {
8985+ if (!is_last_was_no_browser ) {
8986+ // Wait for old server threads to stop if it's doing cleaning up
8987+ // in some OS like Linux + GTK WebView, the old server thread may
8988+ // not stop immediately after the old window is closed.
8989+ _webui_timer_t timer ;
8990+ _webui_timer_start (& timer );
8991+ for (;;) {
89918992 #ifdef WEBUI_LOG
8992- _webui_log_debug ("[Core]\t\t_webui_show_window() -> Server thread did not stop in time .\n" );
8993+ _webui_log_debug ("[Core]\t\t_webui_show_window() -> Waiting for old server thread to stop.. .\n" );
89938994 #endif
8994- break ;
8995+ _webui_sleep (100 );
8996+ if (!_webui_mutex_is_server_running (win , WEBUI_MUTEX_GET_STATUS )) {
8997+ #ifdef WEBUI_LOG
8998+ _webui_log_debug ("[Core]\t\t_webui_show_window() -> Old server thread stopped.\n" );
8999+ #endif
9000+ break ;
9001+ }
9002+ if (_webui_timer_is_end (& timer , 1500 )) {
9003+ #ifdef WEBUI_LOG
9004+ _webui_log_debug ("[Core]\t\t_webui_show_window() -> Old server thread did not stop in time.\n" );
9005+ #endif
9006+ break ;
9007+ }
89959008 }
89969009 }
8997- } else {
8998- #ifdef WEBUI_LOG
8999- _webui_log_debug ("[Core]\t\t_webui_show_window() -> This is UI reload, reusing the same server thread.\n" );
9000- #endif
90019010 }
90029011
90039012 // Initialization
@@ -9017,6 +9026,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
90179026 }
90189027 }
90199028
9029+ // Free previous content if any
90209030 if (win -> html != NULL )
90219031 _webui_free_mem ((void * )win -> html );
90229032 if (win -> url != NULL )
@@ -9027,6 +9037,8 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
90279037 if (win -> user_index_file_encoded != NULL )
90289038 _webui_free_mem ((void * )win -> user_index_file_encoded );
90299039 }
9040+
9041+ // Reset HTML and URL
90309042 win -> html = NULL ;
90319043 win -> url = NULL ;
90329044 if (!keep_user_index_file ) {
@@ -9040,16 +9052,17 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
90409052 // Use user's custom port
90419053 win -> server_port = win -> custom_server_port ;
90429054 }
9043- else if (_webui_mutex_is_connected ( win , WEBUI_MUTEX_GET_STATUS ) ) {
9044- // Window is already connected, This is a UI reload.
9055+ else if (is_this_ui_reload || is_this_empty_server ) {
9056+ // This is a UI reload or empty server .
90459057 // Let's reuse the same port if still valid
9046- if (win -> server_port == 0 )
9047- // For some reason the port is 0 , get a new free port
9058+ if (win -> server_port == 0 ) {
9059+ // Port is not valid , get a new port
90489060 win -> server_port = _webui_get_free_port ();
9061+ }
90499062 }
90509063 else {
9051- // Window is not connected
9052- // Get a new free port
9064+ // This is a new window.
9065+ // Get a new port
90539066 win -> server_port = _webui_get_free_port ();
90549067 }
90559068
@@ -9177,6 +9190,7 @@ static bool _webui_show_window(_webui_window_t* win, struct mg_connection* clien
91779190 #ifdef WEBUI_LOG
91789191 _webui_log_debug ("[Core]\t\t_webui_show_window() -> Starting server only mode (NoBrowser)\n" );
91799192 #endif
9193+ win -> current_browser = NoBrowser ;
91809194 runBrowser = true;
91819195 }
91829196 }
0 commit comments