From 410708936377097310d8ae2e9a228618cf095e4b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 1 Sep 2014 19:15:07 +0800 Subject: [PATCH] Use new devtools resizing strategy from Chrome 37. --- .../devtools_contents_resizing_strategy.cc | 62 +++++-------------- .../devtools_contents_resizing_strategy.h | 20 ++---- .../devtools_embedder_message_dispatcher.cc | 31 +++------- .../devtools_embedder_message_dispatcher.h | 4 +- .../browser/inspectable_web_contents_impl.cc | 5 +- .../browser/inspectable_web_contents_impl.h | 3 +- 6 files changed, 35 insertions(+), 90 deletions(-) diff --git a/brightray/browser/devtools_contents_resizing_strategy.cc b/brightray/browser/devtools_contents_resizing_strategy.cc index 15d7a5253f13..30ee028669e3 100644 --- a/brightray/browser/devtools_contents_resizing_strategy.cc +++ b/brightray/browser/devtools_contents_resizing_strategy.cc @@ -6,80 +6,48 @@ #include -DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy() { -} - -DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy( - const gfx::Insets& insets, const gfx::Size& min_size) - : insets_(insets), - min_size_(min_size) { +DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy() + : hide_inspected_contents_(false) { } DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy( const gfx::Rect& bounds) - : bounds_(bounds) { + : bounds_(bounds), + hide_inspected_contents_(bounds_.IsEmpty() && !bounds_.x() && + !bounds_.y()) { } void DevToolsContentsResizingStrategy::CopyFrom( const DevToolsContentsResizingStrategy& strategy) { - insets_ = strategy.insets(); - min_size_ = strategy.min_size(); bounds_ = strategy.bounds(); + hide_inspected_contents_ = strategy.hide_inspected_contents(); } bool DevToolsContentsResizingStrategy::Equals( const DevToolsContentsResizingStrategy& strategy) { - return insets_ == strategy.insets() && min_size_ == strategy.min_size() && - bounds_ == strategy.bounds(); + return bounds_ == strategy.bounds() && + hide_inspected_contents_ == strategy.hide_inspected_contents(); } void ApplyDevToolsContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy, const gfx::Size& container_size, - const gfx::Rect& old_devtools_bounds, - const gfx::Rect& old_contents_bounds, gfx::Rect* new_devtools_bounds, gfx::Rect* new_contents_bounds) { new_devtools_bounds->SetRect( 0, 0, container_size.width(), container_size.height()); - const gfx::Insets& insets = strategy.insets(); - const gfx::Size& min_size = strategy.min_size(); const gfx::Rect& bounds = strategy.bounds(); - - if (!bounds.size().IsEmpty()) { - int left = std::min(bounds.x(), container_size.width()); - int top = std::min(bounds.y(), container_size.height()); - int width = std::min(bounds.width(), container_size.width() - left); - int height = std::min(bounds.height(), container_size.height() - top); - new_contents_bounds->SetRect(left, top, width, height); + if (bounds.size().IsEmpty() && !strategy.hide_inspected_contents()) { + new_contents_bounds->SetRect( + 0, 0, container_size.width(), container_size.height()); return; } - int width = std::max(0, container_size.width() - insets.width()); - int left = insets.left(); - if (width < min_size.width() && insets.width() > 0) { - int min_width = std::min(min_size.width(), container_size.width()); - int insets_width = container_size.width() - min_width; - int insets_decrease = insets.width() - insets_width; - // Decrease both left and right insets proportionally. - left -= insets_decrease * insets.left() / insets.width(); - width = min_width; - } - left = std::max(0, std::min(container_size.width(), left)); - - int height = std::max(0, container_size.height() - insets.height()); - int top = insets.top(); - if (height < min_size.height() && insets.height() > 0) { - int min_height = std::min(min_size.height(), container_size.height()); - int insets_height = container_size.height() - min_height; - int insets_decrease = insets.height() - insets_height; - // Decrease both top and bottom insets proportionally. - top -= insets_decrease * insets.top() / insets.height(); - height = min_height; - } - top = std::max(0, std::min(container_size.height(), top)); - + int left = std::min(bounds.x(), container_size.width()); + int top = std::min(bounds.y(), container_size.height()); + int width = std::min(bounds.width(), container_size.width() - left); + int height = std::min(bounds.height(), container_size.height() - top); new_contents_bounds->SetRect(left, top, width, height); } diff --git a/brightray/browser/devtools_contents_resizing_strategy.h b/brightray/browser/devtools_contents_resizing_strategy.h index 86ea260c71a2..806d78a7156f 100644 --- a/brightray/browser/devtools_contents_resizing_strategy.h +++ b/brightray/browser/devtools_contents_resizing_strategy.h @@ -15,28 +15,22 @@ class DevToolsContentsResizingStrategy { public: DevToolsContentsResizingStrategy(); - DevToolsContentsResizingStrategy( - const gfx::Insets& insets, - const gfx::Size& min_size); - explicit DevToolsContentsResizingStrategy(const gfx::Rect& bounds); + explicit DevToolsContentsResizingStrategy( + const gfx::Rect& bounds); void CopyFrom(const DevToolsContentsResizingStrategy& strategy); bool Equals(const DevToolsContentsResizingStrategy& strategy); - const gfx::Insets& insets() const { return insets_; } - const gfx::Size& min_size() const { return min_size_; } const gfx::Rect& bounds() const { return bounds_; } + bool hide_inspected_contents() const { return hide_inspected_contents_; } private: - // Insets of contents inside DevTools. - gfx::Insets insets_; - - // Minimum size of contents. - gfx::Size min_size_; - // Contents bounds. When non-empty, used instead of insets. gfx::Rect bounds_; + // Determines whether inspected contents is visible. + bool hide_inspected_contents_; + DISALLOW_COPY_AND_ASSIGN(DevToolsContentsResizingStrategy); }; @@ -48,8 +42,6 @@ class DevToolsContentsResizingStrategy { void ApplyDevToolsContentsResizingStrategy( const DevToolsContentsResizingStrategy& strategy, const gfx::Size& container_size, - const gfx::Rect& old_devtools_bounds, - const gfx::Rect& old_contents_bounds, gfx::Rect* new_devtools_bounds, gfx::Rect* new_contents_bounds); diff --git a/brightray/browser/devtools_embedder_message_dispatcher.cc b/brightray/browser/devtools_embedder_message_dispatcher.cc index c4e87fcf7e6e..bb64846320b4 100644 --- a/brightray/browser/devtools_embedder_message_dispatcher.cc +++ b/brightray/browser/devtools_embedder_message_dispatcher.cc @@ -23,33 +23,20 @@ bool GetValue(const base::ListValue& list, int pos, bool& value) { return list.GetBoolean(pos, &value); } -bool GetValue(const base::ListValue& list, int pos, gfx::Insets& insets) { - const base::DictionaryValue* dict; - if (!list.GetDictionary(pos, &dict)) - return false; - int top = 0; - int left = 0; - int bottom = 0; - int right = 0; - if (!dict->GetInteger("top", &top) || - !dict->GetInteger("left", &left) || - !dict->GetInteger("bottom", &bottom) || - !dict->GetInteger("right", &right)) - return false; - insets.Set(top, left, bottom, right); - return true; -} - -bool GetValue(const base::ListValue& list, int pos, gfx::Size& size) { +bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) { const base::DictionaryValue* dict; if (!list.GetDictionary(pos, &dict)) return false; + int x = 0; + int y = 0; int width = 0; int height = 0; - if (!dict->GetInteger("width", &width) || + if (!dict->GetInteger("x", &x) || + !dict->GetInteger("y", &y) || + !dict->GetInteger("width", &width) || !dict->GetInteger("height", &height)) return false; - size.SetSize(width, height); + rect.SetRect(x, y, width, height); return true; } @@ -191,8 +178,8 @@ DevToolsEmbedderMessageDispatcher::DevToolsEmbedderMessageDispatcher( RegisterHandler("closeWindow", BindToListParser(base::Bind(&Delegate::CloseWindow, base::Unretained(delegate)))); - RegisterHandler("setContentsResizingStrategy", - BindToListParser(base::Bind(&Delegate::SetContentsResizingStrategy, + RegisterHandler("setInspectedPageBounds", + BindToListParser(base::Bind(&Delegate::SetInspectedPageBounds, base::Unretained(delegate)))); RegisterHandler("inspectElementCompleted", BindToListParser(base::Bind(&Delegate::InspectElementCompleted, diff --git a/brightray/browser/devtools_embedder_message_dispatcher.h b/brightray/browser/devtools_embedder_message_dispatcher.h index 0448f86aa16c..1b0bf9e3a022 100644 --- a/brightray/browser/devtools_embedder_message_dispatcher.h +++ b/brightray/browser/devtools_embedder_message_dispatcher.h @@ -10,6 +10,7 @@ #include "base/callback.h" #include "ui/gfx/insets.h" +#include "ui/gfx/rect.h" #include "ui/gfx/size.h" namespace base { @@ -32,8 +33,7 @@ class DevToolsEmbedderMessageDispatcher { virtual void ActivateWindow() = 0; virtual void CloseWindow() = 0; - virtual void SetContentsResizingStrategy( - const gfx::Insets& insets, const gfx::Size& min_size) = 0; + virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0; virtual void InspectElementCompleted() = 0; virtual void MoveWindow(int x, int y) = 0; virtual void SetIsDocked(bool docked) = 0; diff --git a/brightray/browser/inspectable_web_contents_impl.cc b/brightray/browser/inspectable_web_contents_impl.cc index c512d2eb2e9d..94b1069ebfab 100644 --- a/brightray/browser/inspectable_web_contents_impl.cc +++ b/brightray/browser/inspectable_web_contents_impl.cc @@ -212,9 +212,8 @@ void InspectableWebContentsImpl::CloseWindow() { devtools_web_contents()->DispatchBeforeUnload(false); } -void InspectableWebContentsImpl::SetContentsResizingStrategy( - const gfx::Insets& insets, const gfx::Size& min_size) { - DevToolsContentsResizingStrategy strategy(insets, min_size); +void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) { + DevToolsContentsResizingStrategy strategy(rect); if (contents_resizing_strategy_.Equals(strategy)) return; diff --git a/brightray/browser/inspectable_web_contents_impl.h b/brightray/browser/inspectable_web_contents_impl.h index 8bbf70273b28..bae3cf865f1d 100644 --- a/brightray/browser/inspectable_web_contents_impl.h +++ b/brightray/browser/inspectable_web_contents_impl.h @@ -64,8 +64,7 @@ class InspectableWebContentsImpl : virtual void ActivateWindow() OVERRIDE; virtual void CloseWindow() OVERRIDE; - virtual void SetContentsResizingStrategy( - const gfx::Insets& insets, const gfx::Size& min_size) OVERRIDE; + virtual void SetInspectedPageBounds(const gfx::Rect& rect) OVERRIDE; virtual void InspectElementCompleted() OVERRIDE; virtual void MoveWindow(int x, int y) OVERRIDE; virtual void SetIsDocked(bool docked) OVERRIDE;