Use new devtools resizing strategy from Chrome 37.

This commit is contained in:
Cheng Zhao 2014-09-01 19:15:07 +08:00
parent f8c8c415eb
commit 4107089363
6 changed files with 35 additions and 90 deletions

View file

@ -6,80 +6,48 @@
#include <algorithm> #include <algorithm>
DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy() { DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy()
} : hide_inspected_contents_(false) {
DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
const gfx::Insets& insets, const gfx::Size& min_size)
: insets_(insets),
min_size_(min_size) {
} }
DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy( DevToolsContentsResizingStrategy::DevToolsContentsResizingStrategy(
const gfx::Rect& bounds) const gfx::Rect& bounds)
: bounds_(bounds) { : bounds_(bounds),
hide_inspected_contents_(bounds_.IsEmpty() && !bounds_.x() &&
!bounds_.y()) {
} }
void DevToolsContentsResizingStrategy::CopyFrom( void DevToolsContentsResizingStrategy::CopyFrom(
const DevToolsContentsResizingStrategy& strategy) { const DevToolsContentsResizingStrategy& strategy) {
insets_ = strategy.insets();
min_size_ = strategy.min_size();
bounds_ = strategy.bounds(); bounds_ = strategy.bounds();
hide_inspected_contents_ = strategy.hide_inspected_contents();
} }
bool DevToolsContentsResizingStrategy::Equals( bool DevToolsContentsResizingStrategy::Equals(
const DevToolsContentsResizingStrategy& strategy) { const DevToolsContentsResizingStrategy& strategy) {
return insets_ == strategy.insets() && min_size_ == strategy.min_size() && return bounds_ == strategy.bounds() &&
bounds_ == strategy.bounds(); hide_inspected_contents_ == strategy.hide_inspected_contents();
} }
void ApplyDevToolsContentsResizingStrategy( void ApplyDevToolsContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy, const DevToolsContentsResizingStrategy& strategy,
const gfx::Size& container_size, 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_devtools_bounds,
gfx::Rect* new_contents_bounds) { gfx::Rect* new_contents_bounds) {
new_devtools_bounds->SetRect( new_devtools_bounds->SetRect(
0, 0, container_size.width(), container_size.height()); 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(); const gfx::Rect& bounds = strategy.bounds();
if (bounds.size().IsEmpty() && !strategy.hide_inspected_contents()) {
new_contents_bounds->SetRect(
0, 0, container_size.width(), container_size.height());
return;
}
if (!bounds.size().IsEmpty()) {
int left = std::min(bounds.x(), container_size.width()); int left = std::min(bounds.x(), container_size.width());
int top = std::min(bounds.y(), container_size.height()); int top = std::min(bounds.y(), container_size.height());
int width = std::min(bounds.width(), container_size.width() - left); int width = std::min(bounds.width(), container_size.width() - left);
int height = std::min(bounds.height(), container_size.height() - top); int height = std::min(bounds.height(), container_size.height() - top);
new_contents_bounds->SetRect(left, top, width, height); new_contents_bounds->SetRect(left, top, width, 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));
new_contents_bounds->SetRect(left, top, width, height);
} }

View file

@ -15,28 +15,22 @@
class DevToolsContentsResizingStrategy { class DevToolsContentsResizingStrategy {
public: public:
DevToolsContentsResizingStrategy(); DevToolsContentsResizingStrategy();
DevToolsContentsResizingStrategy( explicit DevToolsContentsResizingStrategy(
const gfx::Insets& insets, const gfx::Rect& bounds);
const gfx::Size& min_size);
explicit DevToolsContentsResizingStrategy(const gfx::Rect& bounds);
void CopyFrom(const DevToolsContentsResizingStrategy& strategy); void CopyFrom(const DevToolsContentsResizingStrategy& strategy);
bool Equals(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_; } const gfx::Rect& bounds() const { return bounds_; }
bool hide_inspected_contents() const { return hide_inspected_contents_; }
private: 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. // Contents bounds. When non-empty, used instead of insets.
gfx::Rect bounds_; gfx::Rect bounds_;
// Determines whether inspected contents is visible.
bool hide_inspected_contents_;
DISALLOW_COPY_AND_ASSIGN(DevToolsContentsResizingStrategy); DISALLOW_COPY_AND_ASSIGN(DevToolsContentsResizingStrategy);
}; };
@ -48,8 +42,6 @@ class DevToolsContentsResizingStrategy {
void ApplyDevToolsContentsResizingStrategy( void ApplyDevToolsContentsResizingStrategy(
const DevToolsContentsResizingStrategy& strategy, const DevToolsContentsResizingStrategy& strategy,
const gfx::Size& container_size, 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_devtools_bounds,
gfx::Rect* new_contents_bounds); gfx::Rect* new_contents_bounds);

View file

@ -23,33 +23,20 @@ bool GetValue(const base::ListValue& list, int pos, bool& value) {
return list.GetBoolean(pos, &value); return list.GetBoolean(pos, &value);
} }
bool GetValue(const base::ListValue& list, int pos, gfx::Insets& insets) { bool GetValue(const base::ListValue& list, int pos, gfx::Rect& rect) {
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) {
const base::DictionaryValue* dict; const base::DictionaryValue* dict;
if (!list.GetDictionary(pos, &dict)) if (!list.GetDictionary(pos, &dict))
return false; return false;
int x = 0;
int y = 0;
int width = 0; int width = 0;
int height = 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)) !dict->GetInteger("height", &height))
return false; return false;
size.SetSize(width, height); rect.SetRect(x, y, width, height);
return true; return true;
} }
@ -191,8 +178,8 @@ DevToolsEmbedderMessageDispatcher::DevToolsEmbedderMessageDispatcher(
RegisterHandler("closeWindow", RegisterHandler("closeWindow",
BindToListParser(base::Bind(&Delegate::CloseWindow, BindToListParser(base::Bind(&Delegate::CloseWindow,
base::Unretained(delegate)))); base::Unretained(delegate))));
RegisterHandler("setContentsResizingStrategy", RegisterHandler("setInspectedPageBounds",
BindToListParser(base::Bind(&Delegate::SetContentsResizingStrategy, BindToListParser(base::Bind(&Delegate::SetInspectedPageBounds,
base::Unretained(delegate)))); base::Unretained(delegate))));
RegisterHandler("inspectElementCompleted", RegisterHandler("inspectElementCompleted",
BindToListParser(base::Bind(&Delegate::InspectElementCompleted, BindToListParser(base::Bind(&Delegate::InspectElementCompleted,

View file

@ -10,6 +10,7 @@
#include "base/callback.h" #include "base/callback.h"
#include "ui/gfx/insets.h" #include "ui/gfx/insets.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h" #include "ui/gfx/size.h"
namespace base { namespace base {
@ -32,8 +33,7 @@ class DevToolsEmbedderMessageDispatcher {
virtual void ActivateWindow() = 0; virtual void ActivateWindow() = 0;
virtual void CloseWindow() = 0; virtual void CloseWindow() = 0;
virtual void SetContentsResizingStrategy( virtual void SetInspectedPageBounds(const gfx::Rect& rect) = 0;
const gfx::Insets& insets, const gfx::Size& min_size) = 0;
virtual void InspectElementCompleted() = 0; virtual void InspectElementCompleted() = 0;
virtual void MoveWindow(int x, int y) = 0; virtual void MoveWindow(int x, int y) = 0;
virtual void SetIsDocked(bool docked) = 0; virtual void SetIsDocked(bool docked) = 0;

View file

@ -212,9 +212,8 @@ void InspectableWebContentsImpl::CloseWindow() {
devtools_web_contents()->DispatchBeforeUnload(false); devtools_web_contents()->DispatchBeforeUnload(false);
} }
void InspectableWebContentsImpl::SetContentsResizingStrategy( void InspectableWebContentsImpl::SetInspectedPageBounds(const gfx::Rect& rect) {
const gfx::Insets& insets, const gfx::Size& min_size) { DevToolsContentsResizingStrategy strategy(rect);
DevToolsContentsResizingStrategy strategy(insets, min_size);
if (contents_resizing_strategy_.Equals(strategy)) if (contents_resizing_strategy_.Equals(strategy))
return; return;

View file

@ -64,8 +64,7 @@ class InspectableWebContentsImpl :
virtual void ActivateWindow() OVERRIDE; virtual void ActivateWindow() OVERRIDE;
virtual void CloseWindow() OVERRIDE; virtual void CloseWindow() OVERRIDE;
virtual void SetContentsResizingStrategy( virtual void SetInspectedPageBounds(const gfx::Rect& rect) OVERRIDE;
const gfx::Insets& insets, const gfx::Size& min_size) OVERRIDE;
virtual void InspectElementCompleted() OVERRIDE; virtual void InspectElementCompleted() OVERRIDE;
virtual void MoveWindow(int x, int y) OVERRIDE; virtual void MoveWindow(int x, int y) OVERRIDE;
virtual void SetIsDocked(bool docked) OVERRIDE; virtual void SetIsDocked(bool docked) OVERRIDE;