electron/brightray/browser/win/inspectable_web_contents_view_win.cc

211 lines
5.8 KiB
C++
Raw Normal View History

#include "browser/win/inspectable_web_contents_view_win.h"
#include "browser/browser_client.h"
#include "browser/inspectable_web_contents_impl.h"
#include "browser/win/devtools_window.h"
#include "content/public/browser/web_contents_view.h"
#include "ui/gfx/win/hwnd_util.h"
2014-06-10 02:29:39 +00:00
#include "ui/views/controls/single_split_view.h"
2014-06-10 01:58:28 +00:00
#include "ui/views/controls/webview/webview.h"
#include "ui/views/view.h"
namespace brightray {
namespace {
const int kWindowInset = 100;
}
2014-06-10 01:58:28 +00:00
class ContainerView : public views::View {
public:
explicit ContainerView(InspectableWebContentsViewWin* web_contents_view)
: container_view_created_(false),
2014-06-10 04:35:23 +00:00
dockside_("none"), // "none" is treated as "bottom".
2014-06-10 01:58:28 +00:00
web_view_(new views::WebView(NULL)),
web_contents_view_(web_contents_view) {
2014-06-10 02:03:35 +00:00
set_owned_by_client();
2014-06-10 02:29:39 +00:00
web_view_->set_owned_by_client();
2014-06-10 01:58:28 +00:00
web_view_->SetWebContents(
web_contents_view_->inspectable_web_contents()->GetWebContents());
}
2014-06-10 03:37:07 +00:00
views::View* GetWebView() const {
return web_view_.get();
}
2014-06-10 02:29:39 +00:00
void ShowDevTools() {
if (IsDevToolsViewShowing())
return;
RemoveChildView(web_view_.get());
devtools_view_ = new views::WebView(NULL);
devtools_view_->SetWebContents(web_contents_view_->
inspectable_web_contents()->devtools_web_contents());
split_view_.reset(new views::SingleSplitView(
web_view_.get(),
devtools_view_,
2014-06-10 04:03:20 +00:00
GetSplitViewOrientation(),
2014-06-10 02:29:39 +00:00
NULL));
2014-06-10 04:03:20 +00:00
split_view_->set_divider_offset(GetSplitVievDividerOffset());
2014-06-10 02:29:39 +00:00
AddChildView(split_view_.get());
Layout();
2014-06-10 04:03:20 +00:00
2014-06-10 03:37:07 +00:00
devtools_view_->RequestFocus();
2014-06-10 02:29:39 +00:00
}
void CloseDevTools() {
if (!IsDevToolsViewShowing())
return;
RemoveChildView(split_view_.get());
split_view_.reset();
AddChildView(web_view_.get());
Layout();
}
bool IsDevToolsViewShowing() {
return split_view_;
}
2014-06-10 04:35:23 +00:00
void SetDockSide(const std::string& side) {
2014-06-10 04:03:20 +00:00
if (dockside_ == side)
2014-06-10 04:35:23 +00:00
return; // no change from current location
2014-06-10 04:03:20 +00:00
dockside_ = side;
if (!IsDevToolsViewShowing())
2014-06-10 04:35:23 +00:00
return;
2014-06-10 04:03:20 +00:00
split_view_->set_orientation(GetSplitViewOrientation());
split_view_->set_divider_offset(GetSplitVievDividerOffset());
split_view_->Layout();
2014-06-10 04:35:23 +00:00
return;
2014-06-10 04:03:20 +00:00
}
2014-06-10 01:58:28 +00:00
private:
// views::View:
virtual void Layout() OVERRIDE {
2014-06-10 02:29:39 +00:00
if (split_view_)
split_view_->SetBounds(0, 0, width(), height());
else
web_view_->SetBounds(0, 0, width(), height());
2014-06-10 01:58:28 +00:00
}
virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE {
View::ViewHierarchyChanged(details);
// We're not using child == this because a Widget may not be
// available when this is added to the hierarchy.
if (details.is_add && GetWidget() && !container_view_created_) {
container_view_created_ = true;
2014-06-10 02:29:39 +00:00
AddChildView(web_view_.get());
2014-06-10 01:58:28 +00:00
}
}
2014-06-10 04:03:20 +00:00
views::SingleSplitView::Orientation GetSplitViewOrientation() const {
2014-06-10 04:35:23 +00:00
if (dockside_ == "right")
2014-06-10 04:03:20 +00:00
return views::SingleSplitView::HORIZONTAL_SPLIT;
2014-06-10 04:35:23 +00:00
else
return views::SingleSplitView::VERTICAL_SPLIT;
2014-06-10 04:03:20 +00:00
}
int GetSplitVievDividerOffset() const {
2014-06-10 04:35:23 +00:00
if (dockside_ == "right")
2014-06-10 04:03:20 +00:00
return width() * 2 / 3;
2014-06-10 04:35:23 +00:00
else
return height() * 2 / 3;
2014-06-10 04:03:20 +00:00
}
2014-06-10 01:58:28 +00:00
// True if the container view has already been created, or false otherwise.
bool container_view_created_;
2014-06-10 04:03:20 +00:00
std::string dockside_;
2014-06-10 02:29:39 +00:00
scoped_ptr<views::WebView> web_view_;
scoped_ptr<views::SingleSplitView> split_view_;
views::WebView* devtools_view_; // Owned by split_view_.
2014-06-10 01:58:28 +00:00
InspectableWebContentsViewWin* web_contents_view_;
};
InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsImpl* inspectable_web_contents) {
return new InspectableWebContentsViewWin(inspectable_web_contents);
}
InspectableWebContentsViewWin::InspectableWebContentsViewWin(
InspectableWebContentsImpl* inspectable_web_contents)
2014-06-10 01:58:28 +00:00
: inspectable_web_contents_(inspectable_web_contents),
2014-06-10 04:35:23 +00:00
undocked_(false),
2014-06-10 01:58:28 +00:00
container_(new ContainerView(this)) {
}
InspectableWebContentsViewWin::~InspectableWebContentsViewWin() {
if (devtools_window_)
DestroyWindow(devtools_window_->hwnd());
}
2014-06-10 01:58:28 +00:00
views::View* InspectableWebContentsViewWin::GetView() const {
2014-06-10 02:03:35 +00:00
return container_.get();
2014-06-10 01:58:28 +00:00
}
2014-06-10 03:37:07 +00:00
views::View* InspectableWebContentsViewWin::GetWebView() const {
return container_->GetWebView();
}
gfx::NativeView InspectableWebContentsViewWin::GetNativeView() const {
auto web_contents = inspectable_web_contents_->GetWebContents();
return web_contents->GetView()->GetNativeView();
}
void InspectableWebContentsViewWin::ShowDevTools() {
2014-06-10 04:35:23 +00:00
if (undocked_) {
if (!devtools_window_) {
devtools_window_ = DevToolsWindow::Create(this)->AsWeakPtr();
devtools_window_->Init(HWND_DESKTOP, gfx::Rect());
}
2014-06-10 04:35:23 +00:00
auto contents_view = inspectable_web_contents_->GetWebContents()->GetView();
auto size = contents_view->GetContainerSize();
size.Enlarge(-kWindowInset, -kWindowInset);
gfx::CenterAndSizeWindow(contents_view->GetNativeView(),
devtools_window_->hwnd(),
size);
2014-06-10 04:35:23 +00:00
ShowWindow(devtools_window_->hwnd(), SW_SHOWNORMAL);
} else {
container_->ShowDevTools();
}
}
void InspectableWebContentsViewWin::CloseDevTools() {
2014-06-10 04:35:23 +00:00
if (undocked_)
SendMessage(devtools_window_->hwnd(), WM_CLOSE, 0, 0);
else
container_->CloseDevTools();
}
bool InspectableWebContentsViewWin::IsDevToolsViewShowing() {
2014-06-10 02:29:39 +00:00
return container_->IsDevToolsViewShowing() || devtools_window_;
}
bool InspectableWebContentsViewWin::SetDockSide(const std::string& side) {
2014-06-10 04:35:23 +00:00
if (side == "undocked") {
undocked_ = true;
container_->CloseDevTools();
} else if (side == "right" || side == "bottom") {
undocked_ = false;
if (devtools_window_)
SendMessage(devtools_window_->hwnd(), WM_CLOSE, 0, 0);
container_->SetDockSide(side);
} else {
return false;
}
ShowDevTools();
return true;
}
} // namespace brightray