From c3cd347dff9c4d9623fc46d1312c393ce6f539a3 Mon Sep 17 00:00:00 2001 From: cztomczak Date: Sun, 3 Feb 2019 21:15:46 +0100 Subject: [PATCH] Fix window.close() issue for main window when popup browser is opened. Issue #221. --- src/client_handler.cpp | 16 ++++++++++++++++ src/gtk.cpp | 11 ++++++----- src/gtk.h | 2 +- src/www/window-close.php | 8 ++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 src/www/window-close.php diff --git a/src/client_handler.cpp b/src/client_handler.cpp index 7d6d09c..a0fbd4f 100644 --- a/src/client_handler.cpp +++ b/src/client_handler.cpp @@ -4,6 +4,7 @@ #include "client_handler.h" #include "settings.h" +#include "gtk.h" #include "include/cef_app.h" #include "include/wrapper/cef_helpers.h" @@ -167,6 +168,21 @@ void ClientHandler::OnBeforeClose(CefRefPtr browser) { browser->GetHost()->GetRequestContext()->GetDefaultCookieManager(NULL) ->FlushStore(NULL); + // Currently if main window is closed app other popups will be + // closed too and app terminates. However when "window.close" is + // executed in main window (and a popup browser lives) then + // it will close browser, but it won't destroy main window, + // leaving a gray main window (and a popup running in background). + // So to resolve this, detect that browser embedded in main window + // is closing and destroy its GTK window, so that app terminates. + if (!browser->IsPopup() && browser_list_.size() > 1) { + GtkWidget* main_window = get_main_window(); + if (main_window) { + LOG(INFO) << "Force destroy GTK window in OnBeforeClose"; + gtk_widget_destroy(main_window); + } + } + // Remove from the list of existing browsers. BrowserList::iterator bit = browser_list_.begin(); for (; bit != browser_list_.end(); ++bit) { diff --git a/src/gtk.cpp b/src/gtk.cpp index 599b8aa..9e7bafa 100644 --- a/src/gtk.cpp +++ b/src/gtk.cpp @@ -79,7 +79,7 @@ void window_size_allocate_signal(GtkWidget* widget, GtkAllocation *alloc, if (!xid) { return; } - ::Window xchild = find_child_browser(cef_get_xdisplay(), xid); + ::Window xchild = find_child_browser(xid); // LOG(INFO) << "window_size_allocate_signal() xchild=" << xchild; if (!xchild) { return; @@ -97,7 +97,7 @@ void window_focus_in_signal(GtkWidget* widget, gpointer data) { // LOG(INFO) << "window_focus_in_signal"; ClientHandler *handler = ClientHandler::GetInstance(); ::Window window_xid = get_window_xid(widget); - ::Window browser_xid = find_child_browser(cef_get_xdisplay(), window_xid); + ::Window browser_xid = find_child_browser(window_xid); CefRefPtr browser = handler->FindBrowserByXid(browser_xid); if (browser_xid && browser.get()) { // LOG(INFO) << "window_focus_in_signal: Focus browser"; @@ -109,7 +109,7 @@ void window_focus_out_signal(GtkWidget* widget, gpointer data) { // LOG(INFO) << "window_focus_out_signal"; ClientHandler *handler = ClientHandler::GetInstance(); ::Window window_xid = get_window_xid(widget); - ::Window browser_xid = find_child_browser(cef_get_xdisplay(), window_xid); + ::Window browser_xid = find_child_browser(window_xid); CefRefPtr browser = handler->FindBrowserByXid(browser_xid); if (browser_xid && browser.get()) { // LOG(INFO) << "window_focus_out_signal: Unfocus browser"; @@ -144,13 +144,14 @@ void set_window_icon(GtkWindow* window, const char* icon) { } } -::Window find_child_browser(::Display* display, ::Window window) { +::Window find_child_browser(::Window window) { ::Window root; ::Window parent; ::Window* children; ::Window child_window = 0L; unsigned int nchildren; - if (XQueryTree(display, window, &root, &parent, &children, &nchildren)) { + if (XQueryTree(cef_get_xdisplay(), window, &root, &parent, &children, + &nchildren)) { if (children && nchildren > 1) { child_window = children[1]; // sic! XFree(children); diff --git a/src/gtk.h b/src/gtk.h index f32abfe..792dd03 100644 --- a/src/gtk.h +++ b/src/gtk.h @@ -21,5 +21,5 @@ void window_focus_in_signal(GtkWidget* widget, gpointer data); void window_focus_out_signal(GtkWidget* widget, gpointer data); void window_destroy_signal(GtkWidget* widget, gpointer data); void set_window_icon(GtkWindow* window, const char* icon); -::Window find_child_browser(::Display* display, ::Window window); +::Window find_child_browser(::Window window); void fix_default_x11_visual(GtkWidget* widget); diff --git a/src/www/window-close.php b/src/www/window-close.php new file mode 100644 index 0000000..b6bc8a7 --- /dev/null +++ b/src/www/window-close.php @@ -0,0 +1,8 @@ + +Go back to index +| ">Refresh + + +

Test window.close

+ +window.close()