Skip to content

Commit

Permalink
Fix window.close() issue for main window when popup browser is opened.
Browse files Browse the repository at this point in the history
Issue #221.
  • Loading branch information
cztomczak committed Feb 3, 2019
1 parent 16448d1 commit c3cd347
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/client_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -167,6 +168,21 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> 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) {
Expand Down
11 changes: 6 additions & 5 deletions src/gtk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CefBrowser> browser = handler->FindBrowserByXid(browser_xid);
if (browser_xid && browser.get()) {
// LOG(INFO) << "window_focus_in_signal: Focus browser";
Expand All @@ -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<CefBrowser> browser = handler->FindBrowserByXid(browser_xid);
if (browser_xid && browser.get()) {
// LOG(INFO) << "window_focus_out_signal: Unfocus browser";
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/gtk.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
8 changes: 8 additions & 0 deletions src/www/window-close.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<style type="text/css">@import url("style.css");</style>
<a href="index.php">Go back to index</a>
| <a href="<?php echo $_SERVER["REQUEST_URI"];?>">Refresh</a>


<h1>Test window.close</h1>

<a href="javascript:void(window.close())">window.close()</a>

0 comments on commit c3cd347

Please sign in to comment.