From 9a70c9bb44be2d780047aa175240c208521e2372 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 10 Jun 2014 12:35:23 +0800 Subject: [PATCH] Enable undocked devtools. --- .../win/inspectable_web_contents_view_win.cc | 79 +++++++++++-------- .../win/inspectable_web_contents_view_win.h | 2 +- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/brightray/browser/win/inspectable_web_contents_view_win.cc b/brightray/browser/win/inspectable_web_contents_view_win.cc index 02fe15c0f534..24a52b658f7d 100644 --- a/brightray/browser/win/inspectable_web_contents_view_win.cc +++ b/brightray/browser/win/inspectable_web_contents_view_win.cc @@ -22,7 +22,7 @@ class ContainerView : public views::View { public: explicit ContainerView(InspectableWebContentsViewWin* web_contents_view) : container_view_created_(false), - dockside_("bottom"), + dockside_("none"), // "none" is treated as "bottom". web_view_(new views::WebView(NULL)), web_contents_view_(web_contents_view) { set_owned_by_client(); @@ -69,20 +69,18 @@ class ContainerView : public views::View { return split_view_; } - bool SetDockSide(const std::string& side) { - if (side != "bottom" && side != "right") - return false; // unsupported display location + void SetDockSide(const std::string& side) { if (dockside_ == side) - return true; // no change from current location + return; // no change from current location dockside_ = side; if (!IsDevToolsViewShowing()) - return true; + return; split_view_->set_orientation(GetSplitViewOrientation()); split_view_->set_divider_offset(GetSplitVievDividerOffset()); split_view_->Layout(); - return true; + return; } private: @@ -106,17 +104,17 @@ class ContainerView : public views::View { } views::SingleSplitView::Orientation GetSplitViewOrientation() const { - if (dockside_ == "bottom") - return views::SingleSplitView::VERTICAL_SPLIT; - else + if (dockside_ == "right") return views::SingleSplitView::HORIZONTAL_SPLIT; + else + return views::SingleSplitView::VERTICAL_SPLIT; } int GetSplitVievDividerOffset() const { - if (dockside_ == "bottom") - return height() * 2 / 3; - else + if (dockside_ == "right") return width() * 2 / 3; + else + return height() * 2 / 3; } // True if the container view has already been created, or false otherwise. @@ -139,6 +137,7 @@ InspectableWebContentsView* CreateInspectableContentsView( InspectableWebContentsViewWin::InspectableWebContentsViewWin( InspectableWebContentsImpl* inspectable_web_contents) : inspectable_web_contents_(inspectable_web_contents), + undocked_(false), container_(new ContainerView(this)) { } @@ -161,31 +160,30 @@ gfx::NativeView InspectableWebContentsViewWin::GetNativeView() const { } void InspectableWebContentsViewWin::ShowDevTools() { - container_->ShowDevTools(); - return; + if (undocked_) { + if (!devtools_window_) { + devtools_window_ = DevToolsWindow::Create(this)->AsWeakPtr(); + devtools_window_->Init(HWND_DESKTOP, gfx::Rect()); + } - if (!devtools_window_) { - devtools_window_ = DevToolsWindow::Create(this)->AsWeakPtr(); - devtools_window_->Init(HWND_DESKTOP, gfx::Rect()); + 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); + + ShowWindow(devtools_window_->hwnd(), SW_SHOWNORMAL); + } else { + container_->ShowDevTools(); } - - 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); - - ShowWindow(devtools_window_->hwnd(), SW_SHOWNORMAL); } void InspectableWebContentsViewWin::CloseDevTools() { - container_->CloseDevTools(); - return; - - if (!devtools_window_) - return; - SendMessage(devtools_window_->hwnd(), WM_CLOSE, 0, 0); + if (undocked_) + SendMessage(devtools_window_->hwnd(), WM_CLOSE, 0, 0); + else + container_->CloseDevTools(); } bool InspectableWebContentsViewWin::IsDevToolsViewShowing() { @@ -193,7 +191,20 @@ bool InspectableWebContentsViewWin::IsDevToolsViewShowing() { } bool InspectableWebContentsViewWin::SetDockSide(const std::string& side) { - return container_->SetDockSide(side); + 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 diff --git a/brightray/browser/win/inspectable_web_contents_view_win.h b/brightray/browser/win/inspectable_web_contents_view_win.h index e7d288affda0..e55bbaac0902 100644 --- a/brightray/browser/win/inspectable_web_contents_view_win.h +++ b/brightray/browser/win/inspectable_web_contents_view_win.h @@ -40,8 +40,8 @@ class InspectableWebContentsViewWin : public InspectableWebContentsView { // Owns us. InspectableWebContentsImpl* inspectable_web_contents_; + bool undocked_; scoped_ptr container_; - base::WeakPtr devtools_window_; DISALLOW_COPY_AND_ASSIGN(InspectableWebContentsViewWin);