2013-11-18 00:12:50 +00:00
|
|
|
#include "browser/linux/inspectable_web_contents_view_linux.h"
|
2013-11-15 20:23:52 +00:00
|
|
|
#include <glib-object.h>
|
|
|
|
#include <gtk/gtk.h>
|
2013-11-07 20:02:35 +00:00
|
|
|
|
2013-11-15 20:23:52 +00:00
|
|
|
#include "base/strings/stringprintf.h"
|
2013-11-07 20:02:35 +00:00
|
|
|
#include "browser/browser_client.h"
|
|
|
|
#include "browser/inspectable_web_contents_impl.h"
|
|
|
|
|
|
|
|
#include "content/public/browser/web_contents_view.h"
|
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
InspectableWebContentsView* CreateInspectableContentsView(
|
|
|
|
InspectableWebContentsImpl* inspectable_web_contents) {
|
2013-11-07 20:02:35 +00:00
|
|
|
return new InspectableWebContentsViewLinux(inspectable_web_contents);
|
|
|
|
}
|
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
InspectableWebContentsViewLinux::InspectableWebContentsViewLinux(
|
|
|
|
InspectableWebContentsImpl* inspectable_web_contents)
|
|
|
|
: inspectable_web_contents_(inspectable_web_contents),
|
|
|
|
devtools_window_(NULL) {
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InspectableWebContentsViewLinux::~InspectableWebContentsViewLinux() {
|
2013-11-16 04:22:25 +00:00
|
|
|
if (devtools_window_) gtk_widget_destroy(devtools_window_);
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 20:23:52 +00:00
|
|
|
#if 0 // some utility functions to debug GTK window hierarchies
|
|
|
|
static void dump_one(GtkWidget *wat, int indent) {
|
|
|
|
GtkAllocation alloc;
|
|
|
|
gtk_widget_get_allocation(wat, &alloc);
|
2013-11-18 00:12:50 +00:00
|
|
|
fprintf(stderr, "%*s[%p] %s @%d,%d %dx%d",
|
2013-11-15 20:23:52 +00:00
|
|
|
indent, "", wat,
|
2013-11-18 00:21:50 +00:00
|
|
|
g_type_name_from_instance(reinterpret_cast<GTypeInstance*>(wat)),
|
2013-11-15 20:23:52 +00:00
|
|
|
alloc.x, alloc.y, alloc.width, alloc.height);
|
2013-11-18 00:12:50 +00:00
|
|
|
if (GTK_IS_WINDOW(wat)) {
|
|
|
|
fprintf(stderr, " - \"%s\"", gtk_window_get_title(GTK_WINDOW(wat)));
|
|
|
|
}
|
2013-11-15 20:23:52 +00:00
|
|
|
fputc('\n', stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_the_whole_tree(GtkWidget *wat, int indent) {
|
|
|
|
if (!wat) {
|
|
|
|
fprintf(stderr, "(nil)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dump_one(wat, indent);
|
|
|
|
GList *kids = gtk_container_get_children(GTK_CONTAINER(wat));
|
2013-11-18 00:12:50 +00:00
|
|
|
for (GList *p = kids; p; p = p->next) {
|
2013-11-15 20:23:52 +00:00
|
|
|
dump_the_whole_tree(GTK_WIDGET(p->data), indent+2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dump_parents(GtkWidget *wat) {
|
|
|
|
fprintf(stderr, "Parents:\n");
|
2013-11-18 00:21:50 +00:00
|
|
|
for (GtkWidget *p = gtk_widget_get_parent(wat);
|
|
|
|
p;
|
|
|
|
p = gtk_widget_get_parent(p)) {
|
2013-11-15 20:23:52 +00:00
|
|
|
dump_one(p, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-11-07 20:02:35 +00:00
|
|
|
gfx::NativeView InspectableWebContentsViewLinux::GetNativeView() const {
|
2013-11-18 00:12:50 +00:00
|
|
|
auto web_contents = inspectable_web_contents_->GetWebContents();
|
|
|
|
return web_contents->GetView()->GetNativeView();
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 20:23:52 +00:00
|
|
|
|
|
|
|
/* This code is a little bit hairy.
|
|
|
|
The dev tools can be in any one of five places:
|
|
|
|
1. Unassigned and invisible. This is the default state until someone asks
|
|
|
|
to 'inspect element' for the first time. In this case, devtools->parent is
|
|
|
|
NULL.
|
|
|
|
2. In an onscreen window, visible.
|
|
|
|
3. In the bottom half of a GtkVPaned.
|
|
|
|
4. In the right half of a GtkHPaned.
|
|
|
|
5. In an offscreen window, invisible. This is where they go once they have
|
|
|
|
been displayed and the user asks to "close" them. They can't be put back
|
|
|
|
into the unassigned state.
|
|
|
|
ShowDevTools() and is responsible for transitioning from any one of these
|
|
|
|
states to the three visible states, 2-4, as indicated by the contents of the
|
2013-11-16 04:22:25 +00:00
|
|
|
'dockside_' variable. The helper functions ShowDevToolsInWindow and
|
2013-11-15 20:23:52 +00:00
|
|
|
ShowDevToolsInPane focus on transitioning to states 2 and 3+4, respectively.
|
|
|
|
These helper functions are responsible for the entire transition, including
|
|
|
|
cleaning up any extraneous containers from the old state.
|
|
|
|
|
|
|
|
Hiding the dev tools is taken care of by CloseDevTools (from paned states
|
|
|
|
3+4 to invisible state 5) or by the "delete-event" signal on the
|
2013-11-16 04:22:25 +00:00
|
|
|
devtools_window_ (from window state 2 to 5).
|
2013-11-15 20:23:52 +00:00
|
|
|
|
|
|
|
Remember that GTK does reference counting, so a view with no refs and no
|
|
|
|
parent will be freed. Views that have a ref but no parents will lose their
|
|
|
|
dimensions. So it's best to move the devtools view from place to place with
|
|
|
|
gtk_widget_reparent whenever possible. Unfortunately, one cannot reparent
|
|
|
|
things into a GtkPaned, so fairly brittle use of g_object_[un]ref and
|
|
|
|
gtk_container_remove happens.
|
|
|
|
*/
|
|
|
|
|
2013-11-07 20:02:35 +00:00
|
|
|
void InspectableWebContentsViewLinux::ShowDevTools() {
|
2013-11-18 00:12:50 +00:00
|
|
|
auto devtools_web_contents =
|
|
|
|
inspectable_web_contents()->devtools_web_contents();
|
|
|
|
GtkWidget *devtools = devtools_web_contents->GetView()->GetNativeView();
|
2013-11-15 20:23:52 +00:00
|
|
|
GtkWidget *parent = gtk_widget_get_parent(devtools);
|
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
DLOG(INFO) << base::StringPrintf(
|
2013-11-18 00:21:50 +00:00
|
|
|
"InspectableWebContentsViewLinux::ShowDevTools - " \
|
|
|
|
"parent=%s@%p window=%p dockside=\"%s\"",
|
|
|
|
g_type_name_from_instance(reinterpret_cast<GTypeInstance*>(parent)),
|
2013-11-18 00:12:50 +00:00
|
|
|
parent,
|
|
|
|
devtools_window_,
|
|
|
|
dockside_.c_str());
|
2013-11-15 20:23:52 +00:00
|
|
|
|
|
|
|
if (!parent || GTK_IS_PANED(parent)) {
|
2013-11-16 04:22:25 +00:00
|
|
|
if (dockside_ == "undocked") ShowDevToolsInWindow();
|
|
|
|
else if (dockside_ == "bottom") ShowDevToolsInPane(true);
|
|
|
|
else if (dockside_ == "right") ShowDevToolsInPane(false);
|
2013-11-18 00:12:50 +00:00
|
|
|
} else {
|
2013-11-16 04:22:25 +00:00
|
|
|
DCHECK(parent == devtools_window_);
|
|
|
|
if (dockside_ == "undocked") gtk_widget_show_all(parent);
|
|
|
|
else if (dockside_ == "bottom") ShowDevToolsInPane(true);
|
|
|
|
else if (dockside_ == "right") ShowDevToolsInPane(false);
|
2013-11-15 20:23:52 +00:00
|
|
|
}
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewLinux::CloseDevTools() {
|
2013-11-18 00:12:50 +00:00
|
|
|
auto devtools_web_contents =
|
|
|
|
inspectable_web_contents()->devtools_web_contents();
|
|
|
|
GtkWidget *devtools = devtools_web_contents->GetView()->GetNativeView();
|
2013-11-15 20:23:52 +00:00
|
|
|
GtkWidget *parent = gtk_widget_get_parent(devtools);
|
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
DLOG(INFO) << base::StringPrintf(
|
2013-11-18 00:21:50 +00:00
|
|
|
"InspectableWebContentsViewLinux::CloseDevTools - " \
|
|
|
|
"parent=%s@%p window=%p dockside=\"%s\"",
|
|
|
|
g_type_name_from_instance(reinterpret_cast<GTypeInstance*>(parent)),
|
2013-11-18 00:12:50 +00:00
|
|
|
parent,
|
|
|
|
devtools_window_,
|
|
|
|
dockside_.c_str());
|
2013-11-15 20:23:52 +00:00
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
if (!parent)
|
2013-11-15 20:23:52 +00:00
|
|
|
return; // Not visible -> nothing to do
|
2013-11-18 00:12:50 +00:00
|
|
|
|
|
|
|
if (GTK_IS_PANED(parent)) {
|
2013-11-15 20:23:52 +00:00
|
|
|
GtkWidget *browser = GetBrowserWindow();
|
|
|
|
GtkWidget *view = GetNativeView();
|
|
|
|
|
2013-11-16 04:22:25 +00:00
|
|
|
if (!devtools_window_) MakeDevToolsWindow();
|
|
|
|
gtk_widget_reparent(devtools, devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
g_object_ref(parent);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(browser), parent);
|
|
|
|
gtk_widget_reparent(view, browser);
|
|
|
|
g_object_unref(parent);
|
2013-11-18 00:12:50 +00:00
|
|
|
} else {
|
2013-11-16 04:22:25 +00:00
|
|
|
DCHECK(parent == devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
gtk_widget_hide(parent);
|
|
|
|
}
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InspectableWebContentsViewLinux::SetDockSide(const std::string& side) {
|
2013-11-18 00:12:50 +00:00
|
|
|
DLOG(INFO) <<
|
|
|
|
"InspectableWebContentsViewLinux::SetDockSide: \"" << side << "\"";
|
|
|
|
if (side != "undocked" && side != "bottom" && side != "right")
|
|
|
|
return false; // unsupported display location
|
|
|
|
if (dockside_ == side)
|
|
|
|
return true; // no change from current location
|
2013-11-15 20:23:52 +00:00
|
|
|
|
2013-11-16 04:22:25 +00:00
|
|
|
dockside_ = side;
|
2013-11-15 20:23:52 +00:00
|
|
|
|
|
|
|
// If devtools already has a parent, then we're being asked to move it.
|
2013-11-18 00:12:50 +00:00
|
|
|
auto devtools_web_contents =
|
|
|
|
inspectable_web_contents()->devtools_web_contents();
|
|
|
|
GtkWidget *devtools = devtools_web_contents->GetView()->GetNativeView();
|
2013-11-15 20:23:52 +00:00
|
|
|
if (gtk_widget_get_parent(devtools)) {
|
|
|
|
ShowDevTools();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewLinux::ShowDevToolsInWindow() {
|
2013-11-18 00:12:50 +00:00
|
|
|
auto devtools_web_contents =
|
|
|
|
inspectable_web_contents()->devtools_web_contents();
|
|
|
|
GtkWidget *devtools = devtools_web_contents->GetView()->GetNativeView();
|
2013-11-15 20:23:52 +00:00
|
|
|
GtkWidget *parent = gtk_widget_get_parent(devtools);
|
|
|
|
|
2013-11-18 00:12:50 +00:00
|
|
|
if (!devtools_window_)
|
|
|
|
MakeDevToolsWindow();
|
2013-11-15 20:23:52 +00:00
|
|
|
if (!parent) {
|
2013-11-16 04:22:25 +00:00
|
|
|
gtk_container_add(GTK_CONTAINER(devtools_window_), devtools);
|
2013-11-18 00:12:50 +00:00
|
|
|
} else if (parent != devtools_window_) {
|
2013-11-15 20:23:52 +00:00
|
|
|
DCHECK(GTK_IS_PANED(parent));
|
2013-11-16 04:22:25 +00:00
|
|
|
gtk_widget_reparent(devtools, devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
|
|
|
|
// Remove the pane.
|
|
|
|
GtkWidget *view = GetNativeView();
|
|
|
|
GtkWidget *browser = GetBrowserWindow();
|
|
|
|
g_object_ref(view);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(parent), view);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(browser), parent);
|
|
|
|
gtk_container_add(GTK_CONTAINER(browser), view);
|
|
|
|
g_object_unref(view);
|
|
|
|
}
|
2013-11-16 04:22:25 +00:00
|
|
|
gtk_widget_show_all(devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewLinux::MakeDevToolsWindow() {
|
2013-11-16 04:22:25 +00:00
|
|
|
DCHECK(!devtools_window_);
|
|
|
|
devtools_window_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);
|
|
|
|
gtk_window_set_title(GTK_WINDOW(devtools_window_), "Developer Tools");
|
|
|
|
gtk_window_set_default_size(GTK_WINDOW(devtools_window_), 800, 600);
|
2013-11-18 00:12:50 +00:00
|
|
|
g_signal_connect(GTK_OBJECT(devtools_window_),
|
|
|
|
"delete-event",
|
|
|
|
G_CALLBACK(gtk_widget_hide_on_delete),
|
|
|
|
this);
|
2013-11-15 20:23:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewLinux::ShowDevToolsInPane(bool on_bottom) {
|
2013-11-18 00:12:50 +00:00
|
|
|
auto devtools_web_contents =
|
|
|
|
inspectable_web_contents()->devtools_web_contents();
|
|
|
|
GtkWidget *devtools = devtools_web_contents->GetView()->GetNativeView();
|
2013-11-15 20:23:52 +00:00
|
|
|
GtkWidget *parent = gtk_widget_get_parent(devtools);
|
|
|
|
GtkWidget *pane = on_bottom ? gtk_vpaned_new() : gtk_hpaned_new();
|
|
|
|
GtkWidget *view = GetNativeView();
|
|
|
|
GtkWidget *browser = GetBrowserWindow();
|
|
|
|
|
|
|
|
GtkAllocation alloc;
|
|
|
|
gtk_widget_get_allocation(browser, &alloc);
|
2013-11-18 00:12:50 +00:00
|
|
|
gtk_paned_set_position(GTK_PANED(pane),
|
|
|
|
on_bottom ? alloc.height * 2 / 3 : alloc.width / 2);
|
2013-11-15 20:23:52 +00:00
|
|
|
if (!parent) {
|
|
|
|
g_object_ref(view);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(browser), view);
|
|
|
|
gtk_paned_add1(GTK_PANED(pane), view);
|
|
|
|
gtk_paned_add2(GTK_PANED(pane), devtools);
|
|
|
|
g_object_unref(view);
|
2013-11-18 00:12:50 +00:00
|
|
|
} else if (GTK_IS_PANED(parent)) {
|
2013-11-15 20:23:52 +00:00
|
|
|
g_object_ref(view);
|
|
|
|
g_object_ref(devtools);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(parent), view);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(parent), devtools);
|
|
|
|
gtk_paned_add1(GTK_PANED(pane), view);
|
|
|
|
gtk_paned_add2(GTK_PANED(pane), devtools);
|
|
|
|
g_object_unref(view);
|
|
|
|
g_object_unref(devtools);
|
|
|
|
gtk_container_remove(GTK_CONTAINER(browser), parent);
|
2013-11-18 00:12:50 +00:00
|
|
|
} else {
|
2013-11-16 04:22:25 +00:00
|
|
|
DCHECK(parent == devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
g_object_ref(view);
|
2013-11-16 04:22:25 +00:00
|
|
|
gtk_container_remove(GTK_CONTAINER(devtools_window_), devtools);
|
2013-11-15 20:23:52 +00:00
|
|
|
gtk_container_remove(GTK_CONTAINER(browser), view);
|
|
|
|
gtk_paned_add1(GTK_PANED(pane), view);
|
|
|
|
gtk_paned_add2(GTK_PANED(pane), devtools);
|
|
|
|
g_object_unref(view);
|
2013-11-16 04:22:25 +00:00
|
|
|
gtk_widget_hide(devtools_window_);
|
2013-11-15 20:23:52 +00:00
|
|
|
}
|
|
|
|
gtk_container_add(GTK_CONTAINER(browser), pane);
|
|
|
|
gtk_widget_show_all(pane);
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *InspectableWebContentsViewLinux::GetBrowserWindow() {
|
|
|
|
GtkWidget *view = GetNativeView();
|
|
|
|
GtkWidget *parent = gtk_widget_get_parent(view);
|
2013-11-18 00:12:50 +00:00
|
|
|
GtkWidget *browser =
|
|
|
|
GTK_IS_PANED(parent) ? gtk_widget_get_parent(parent) : parent;
|
2013-11-15 20:23:52 +00:00
|
|
|
DCHECK(GTK_IS_WINDOW(browser));
|
|
|
|
return browser;
|
2013-11-07 20:02:35 +00:00
|
|
|
}
|
|
|
|
|
2013-11-17 23:20:17 +00:00
|
|
|
} // namespace brightray
|