Skip to content

Commit c3cd347

Browse files
committed
Fix window.close() issue for main window when popup browser is opened.
Issue #221.
1 parent 16448d1 commit c3cd347

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/client_handler.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "client_handler.h"
66
#include "settings.h"
7+
#include "gtk.h"
78

89
#include "include/cef_app.h"
910
#include "include/wrapper/cef_helpers.h"
@@ -167,6 +168,21 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
167168
browser->GetHost()->GetRequestContext()->GetDefaultCookieManager(NULL)
168169
->FlushStore(NULL);
169170

171+
// Currently if main window is closed app other popups will be
172+
// closed too and app terminates. However when "window.close" is
173+
// executed in main window (and a popup browser lives) then
174+
// it will close browser, but it won't destroy main window,
175+
// leaving a gray main window (and a popup running in background).
176+
// So to resolve this, detect that browser embedded in main window
177+
// is closing and destroy its GTK window, so that app terminates.
178+
if (!browser->IsPopup() && browser_list_.size() > 1) {
179+
GtkWidget* main_window = get_main_window();
180+
if (main_window) {
181+
LOG(INFO) << "Force destroy GTK window in OnBeforeClose";
182+
gtk_widget_destroy(main_window);
183+
}
184+
}
185+
170186
// Remove from the list of existing browsers.
171187
BrowserList::iterator bit = browser_list_.begin();
172188
for (; bit != browser_list_.end(); ++bit) {

src/gtk.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void window_size_allocate_signal(GtkWidget* widget, GtkAllocation *alloc,
7979
if (!xid) {
8080
return;
8181
}
82-
::Window xchild = find_child_browser(cef_get_xdisplay(), xid);
82+
::Window xchild = find_child_browser(xid);
8383
// LOG(INFO) << "window_size_allocate_signal() xchild=" << xchild;
8484
if (!xchild) {
8585
return;
@@ -97,7 +97,7 @@ void window_focus_in_signal(GtkWidget* widget, gpointer data) {
9797
// LOG(INFO) << "window_focus_in_signal";
9898
ClientHandler *handler = ClientHandler::GetInstance();
9999
::Window window_xid = get_window_xid(widget);
100-
::Window browser_xid = find_child_browser(cef_get_xdisplay(), window_xid);
100+
::Window browser_xid = find_child_browser(window_xid);
101101
CefRefPtr<CefBrowser> browser = handler->FindBrowserByXid(browser_xid);
102102
if (browser_xid && browser.get()) {
103103
// LOG(INFO) << "window_focus_in_signal: Focus browser";
@@ -109,7 +109,7 @@ void window_focus_out_signal(GtkWidget* widget, gpointer data) {
109109
// LOG(INFO) << "window_focus_out_signal";
110110
ClientHandler *handler = ClientHandler::GetInstance();
111111
::Window window_xid = get_window_xid(widget);
112-
::Window browser_xid = find_child_browser(cef_get_xdisplay(), window_xid);
112+
::Window browser_xid = find_child_browser(window_xid);
113113
CefRefPtr<CefBrowser> browser = handler->FindBrowserByXid(browser_xid);
114114
if (browser_xid && browser.get()) {
115115
// LOG(INFO) << "window_focus_out_signal: Unfocus browser";
@@ -144,13 +144,14 @@ void set_window_icon(GtkWindow* window, const char* icon) {
144144
}
145145
}
146146

147-
::Window find_child_browser(::Display* display, ::Window window) {
147+
::Window find_child_browser(::Window window) {
148148
::Window root;
149149
::Window parent;
150150
::Window* children;
151151
::Window child_window = 0L;
152152
unsigned int nchildren;
153-
if (XQueryTree(display, window, &root, &parent, &children, &nchildren)) {
153+
if (XQueryTree(cef_get_xdisplay(), window, &root, &parent, &children,
154+
&nchildren)) {
154155
if (children && nchildren > 1) {
155156
child_window = children[1]; // sic!
156157
XFree(children);

src/gtk.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ void window_focus_in_signal(GtkWidget* widget, gpointer data);
2121
void window_focus_out_signal(GtkWidget* widget, gpointer data);
2222
void window_destroy_signal(GtkWidget* widget, gpointer data);
2323
void set_window_icon(GtkWindow* window, const char* icon);
24-
::Window find_child_browser(::Display* display, ::Window window);
24+
::Window find_child_browser(::Window window);
2525
void fix_default_x11_visual(GtkWidget* widget);

src/www/window-close.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<style type="text/css">@import url("style.css");</style>
2+
<a href="index.php">Go back to index</a>
3+
| <a href="<?php echo $_SERVER["REQUEST_URI"];?>">Refresh</a>
4+
5+
6+
<h1>Test window.close</h1>
7+
8+
<a href="javascript:void(window.close())">window.close()</a>

0 commit comments

Comments
 (0)