2017-05-18 23:11:12 +00:00
|
|
|
#include "brightray/browser/views/inspectable_web_contents_view_views.h"
|
2014-07-04 08:25:59 +00:00
|
|
|
|
2014-07-04 12:26:59 +00:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2017-05-18 23:11:12 +00:00
|
|
|
#include "brightray/browser/inspectable_web_contents_delegate.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents_impl.h"
|
|
|
|
#include "brightray/browser/inspectable_web_contents_view_delegate.h"
|
2016-08-04 07:35:09 +00:00
|
|
|
#include "ui/views/controls/label.h"
|
2014-07-04 08:25:59 +00:00
|
|
|
#include "ui/views/controls/webview/webview.h"
|
2014-07-04 12:26:59 +00:00
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
#include "ui/views/widget/widget_delegate.h"
|
|
|
|
#include "ui/views/window/client_view.h"
|
2014-07-04 08:25:59 +00:00
|
|
|
|
|
|
|
namespace brightray {
|
|
|
|
|
2014-07-04 12:26:59 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class DevToolsWindowDelegate : public views::ClientView,
|
|
|
|
public views::WidgetDelegate {
|
|
|
|
public:
|
|
|
|
DevToolsWindowDelegate(InspectableWebContentsViewViews* shell,
|
|
|
|
views::View* view,
|
|
|
|
views::Widget* widget)
|
2014-08-09 01:34:06 +00:00
|
|
|
: views::ClientView(widget, view),
|
2014-07-04 12:26:59 +00:00
|
|
|
shell_(shell),
|
|
|
|
view_(view),
|
2017-04-01 12:56:51 +00:00
|
|
|
widget_(widget) {
|
2014-07-09 03:01:19 +00:00
|
|
|
// A WidgetDelegate should be deleted on DeleteDelegate.
|
|
|
|
set_owned_by_client();
|
2014-09-09 07:28:48 +00:00
|
|
|
|
2015-06-25 04:29:34 +00:00
|
|
|
if (shell->GetDelegate())
|
|
|
|
icon_ = shell->GetDelegate()->GetDevToolsWindowIcon();
|
2014-07-09 03:01:19 +00:00
|
|
|
}
|
2018-04-17 23:03:51 +00:00
|
|
|
~DevToolsWindowDelegate() override {}
|
2014-07-04 12:26:59 +00:00
|
|
|
|
|
|
|
// views::WidgetDelegate:
|
2014-11-06 11:04:50 +00:00
|
|
|
void DeleteDelegate() override { delete this; }
|
|
|
|
views::View* GetInitiallyFocusedView() override { return view_; }
|
|
|
|
bool CanResize() const override { return true; }
|
|
|
|
bool CanMaximize() const override { return true; }
|
2015-01-05 06:32:00 +00:00
|
|
|
bool CanMinimize() const override { return true; }
|
2017-04-01 12:56:51 +00:00
|
|
|
base::string16 GetWindowTitle() const override { return shell_->GetTitle(); }
|
2014-11-06 11:04:50 +00:00
|
|
|
gfx::ImageSkia GetWindowAppIcon() override { return GetWindowIcon(); }
|
|
|
|
gfx::ImageSkia GetWindowIcon() override { return icon_; }
|
|
|
|
views::Widget* GetWidget() override { return widget_; }
|
|
|
|
const views::Widget* GetWidget() const override { return widget_; }
|
|
|
|
views::View* GetContentsView() override { return view_; }
|
2017-03-23 22:47:30 +00:00
|
|
|
views::ClientView* CreateClientView(views::Widget* widget) override {
|
|
|
|
return this;
|
|
|
|
}
|
2014-07-04 12:26:59 +00:00
|
|
|
|
|
|
|
// views::ClientView:
|
2014-11-06 11:04:50 +00:00
|
|
|
bool CanClose() override {
|
2014-07-04 12:26:59 +00:00
|
|
|
shell_->inspectable_web_contents()->CloseDevTools();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
InspectableWebContentsViewViews* shell_;
|
|
|
|
views::View* view_;
|
|
|
|
views::Widget* widget_;
|
2014-09-09 07:28:48 +00:00
|
|
|
gfx::ImageSkia icon_;
|
2014-07-04 12:26:59 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(DevToolsWindowDelegate);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-07-04 08:25:59 +00:00
|
|
|
InspectableWebContentsView* CreateInspectableContentsView(
|
|
|
|
InspectableWebContentsImpl* inspectable_web_contents) {
|
|
|
|
return new InspectableWebContentsViewViews(inspectable_web_contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
InspectableWebContentsViewViews::InspectableWebContentsViewViews(
|
|
|
|
InspectableWebContentsImpl* inspectable_web_contents)
|
|
|
|
: inspectable_web_contents_(inspectable_web_contents),
|
2015-08-07 09:00:49 +00:00
|
|
|
devtools_window_web_view_(nullptr),
|
2016-08-04 07:35:09 +00:00
|
|
|
contents_web_view_(nullptr),
|
2015-08-07 09:00:49 +00:00
|
|
|
devtools_web_view_(new views::WebView(nullptr)),
|
|
|
|
devtools_visible_(false),
|
2017-04-01 12:56:51 +00:00
|
|
|
devtools_window_delegate_(nullptr),
|
|
|
|
title_(base::ASCIIToUTF16("Developer Tools")) {
|
2014-07-04 08:25:59 +00:00
|
|
|
set_owned_by_client();
|
|
|
|
|
2018-08-17 15:21:36 +00:00
|
|
|
if (!inspectable_web_contents_->IsGuest() &&
|
|
|
|
inspectable_web_contents_->GetWebContents()->GetNativeView()) {
|
2016-08-04 07:35:09 +00:00
|
|
|
views::WebView* contents_web_view = new views::WebView(nullptr);
|
2017-03-23 22:47:30 +00:00
|
|
|
contents_web_view->SetWebContents(
|
|
|
|
inspectable_web_contents_->GetWebContents());
|
2016-08-04 07:35:09 +00:00
|
|
|
contents_web_view_ = contents_web_view;
|
|
|
|
} else {
|
2018-04-18 01:56:12 +00:00
|
|
|
contents_web_view_ =
|
|
|
|
new views::Label(base::ASCIIToUTF16("No content under offscreen mode"));
|
2016-08-04 07:35:09 +00:00
|
|
|
}
|
|
|
|
|
2014-07-04 08:25:59 +00:00
|
|
|
devtools_web_view_->SetVisible(false);
|
|
|
|
AddChildView(devtools_web_view_);
|
|
|
|
AddChildView(contents_web_view_);
|
|
|
|
}
|
|
|
|
|
|
|
|
InspectableWebContentsViewViews::~InspectableWebContentsViewViews() {
|
2014-07-04 13:34:47 +00:00
|
|
|
if (devtools_window_)
|
2017-03-23 22:47:30 +00:00
|
|
|
inspectable_web_contents()->SaveDevToolsBounds(
|
|
|
|
devtools_window_->GetWindowBoundsInScreen());
|
2014-07-04 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
views::View* InspectableWebContentsViewViews::GetView() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
views::View* InspectableWebContentsViewViews::GetWebView() {
|
|
|
|
return contents_web_view_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewViews::ShowDevTools() {
|
2014-07-04 12:26:59 +00:00
|
|
|
if (devtools_visible_)
|
2014-07-04 08:25:59 +00:00
|
|
|
return;
|
|
|
|
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_visible_ = true;
|
|
|
|
if (devtools_window_) {
|
2015-06-05 06:33:37 +00:00
|
|
|
devtools_window_web_view_->SetWebContents(
|
|
|
|
inspectable_web_contents_->GetDevToolsWebContents());
|
2017-03-23 22:47:30 +00:00
|
|
|
devtools_window_->SetBounds(
|
|
|
|
inspectable_web_contents()->GetDevToolsBounds());
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_window_->Show();
|
|
|
|
} else {
|
|
|
|
devtools_web_view_->SetVisible(true);
|
2015-06-05 06:33:37 +00:00
|
|
|
devtools_web_view_->SetWebContents(
|
|
|
|
inspectable_web_contents_->GetDevToolsWebContents());
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_web_view_->RequestFocus();
|
|
|
|
Layout();
|
|
|
|
}
|
2014-07-04 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewViews::CloseDevTools() {
|
2014-07-04 12:26:59 +00:00
|
|
|
if (!devtools_visible_)
|
2014-07-04 08:25:59 +00:00
|
|
|
return;
|
|
|
|
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_visible_ = false;
|
|
|
|
if (devtools_window_) {
|
2017-03-23 22:47:30 +00:00
|
|
|
inspectable_web_contents()->SaveDevToolsBounds(
|
|
|
|
devtools_window_->GetWindowBoundsInScreen());
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_window_.reset();
|
2015-08-07 09:00:49 +00:00
|
|
|
devtools_window_web_view_ = nullptr;
|
|
|
|
devtools_window_delegate_ = nullptr;
|
2014-07-04 12:26:59 +00:00
|
|
|
} else {
|
|
|
|
devtools_web_view_->SetVisible(false);
|
|
|
|
devtools_web_view_->SetWebContents(NULL);
|
|
|
|
Layout();
|
|
|
|
}
|
2014-07-04 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InspectableWebContentsViewViews::IsDevToolsViewShowing() {
|
2014-07-04 12:26:59 +00:00
|
|
|
return devtools_visible_;
|
2014-07-04 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 03:04:46 +00:00
|
|
|
bool InspectableWebContentsViewViews::IsDevToolsViewFocused() {
|
2016-07-12 21:03:02 +00:00
|
|
|
if (devtools_window_web_view_)
|
|
|
|
return devtools_window_web_view_->HasFocus();
|
|
|
|
else if (devtools_web_view_)
|
2015-09-15 03:04:46 +00:00
|
|
|
return devtools_web_view_->HasFocus();
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-07-04 08:25:59 +00:00
|
|
|
void InspectableWebContentsViewViews::SetIsDocked(bool docked) {
|
2014-07-04 12:26:59 +00:00
|
|
|
CloseDevTools();
|
|
|
|
|
|
|
|
if (!docked) {
|
|
|
|
devtools_window_.reset(new views::Widget);
|
|
|
|
devtools_window_web_view_ = new views::WebView(NULL);
|
2017-03-23 22:47:30 +00:00
|
|
|
devtools_window_delegate_ = new DevToolsWindowDelegate(
|
2018-04-18 01:56:12 +00:00
|
|
|
this, devtools_window_web_view_, devtools_window_.get());
|
2014-07-04 12:26:59 +00:00
|
|
|
|
|
|
|
views::Widget::InitParams params;
|
|
|
|
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
|
2017-03-31 19:27:06 +00:00
|
|
|
params.delegate = devtools_window_delegate_;
|
2014-07-12 13:26:29 +00:00
|
|
|
params.bounds = inspectable_web_contents()->GetDevToolsBounds();
|
2014-11-24 08:03:55 +00:00
|
|
|
|
|
|
|
#if defined(USE_X11)
|
|
|
|
params.wm_role_name = "devtools";
|
2015-06-25 04:29:34 +00:00
|
|
|
if (GetDelegate())
|
2017-03-23 22:47:30 +00:00
|
|
|
GetDelegate()->GetDevToolsWindowWMClass(¶ms.wm_class_name,
|
|
|
|
¶ms.wm_class_class);
|
2014-11-24 08:03:55 +00:00
|
|
|
#endif
|
|
|
|
|
2014-07-04 12:26:59 +00:00
|
|
|
devtools_window_->Init(params);
|
2014-09-09 07:28:48 +00:00
|
|
|
devtools_window_->UpdateWindowIcon();
|
2014-07-04 12:26:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ShowDevTools();
|
2014-07-04 08:25:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void InspectableWebContentsViewViews::SetContentsResizingStrategy(
|
|
|
|
const DevToolsContentsResizingStrategy& strategy) {
|
|
|
|
strategy_.CopyFrom(strategy);
|
|
|
|
Layout();
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:00:49 +00:00
|
|
|
void InspectableWebContentsViewViews::SetTitle(const base::string16& title) {
|
|
|
|
if (devtools_window_) {
|
2017-04-01 12:56:51 +00:00
|
|
|
title_ = title;
|
2015-08-07 09:00:49 +00:00
|
|
|
devtools_window_->UpdateWindowTitle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-04 08:25:59 +00:00
|
|
|
void InspectableWebContentsViewViews::Layout() {
|
|
|
|
if (!devtools_web_view_->visible()) {
|
|
|
|
contents_web_view_->SetBoundsRect(GetContentsBounds());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Size container_size(width(), height());
|
|
|
|
gfx::Rect new_devtools_bounds;
|
|
|
|
gfx::Rect new_contents_bounds;
|
2018-04-18 01:56:12 +00:00
|
|
|
ApplyDevToolsContentsResizingStrategy(
|
|
|
|
strategy_, container_size, &new_devtools_bounds, &new_contents_bounds);
|
2014-07-04 08:25:59 +00:00
|
|
|
|
|
|
|
// DevTools cares about the specific position, so we have to compensate RTL
|
|
|
|
// layout here.
|
|
|
|
new_devtools_bounds.set_x(GetMirroredXForRect(new_devtools_bounds));
|
|
|
|
new_contents_bounds.set_x(GetMirroredXForRect(new_contents_bounds));
|
|
|
|
|
|
|
|
devtools_web_view_->SetBoundsRect(new_devtools_bounds);
|
|
|
|
contents_web_view_->SetBoundsRect(new_contents_bounds);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace brightray
|