views: Remember bounds of devtools window.

This commit is contained in:
Cheng Zhao 2014-07-04 21:34:47 +08:00
parent 4fb4b2d7ba
commit 651ebdde65
4 changed files with 50 additions and 5 deletions

View file

@ -15,6 +15,7 @@
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h" #include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_client_host.h" #include "content/public/browser/devtools_client_host.h"
#include "content/public/browser/devtools_http_handler.h" #include "content/public/browser/devtools_http_handler.h"
@ -27,22 +28,45 @@ namespace brightray {
namespace { namespace {
const char kChromeUIDevToolsURL[] = "chrome-devtools://devtools/devtools.html?can_dock=true"; const char kChromeUIDevToolsURL[] = "chrome-devtools://devtools/devtools.html?can_dock=true";
const char kIsDockedPref[] = "brightray.devtools.isDocked"; const char kDevToolsBoundsPref[] = "brightray.devtools.bounds";
void RectToDictionary(const gfx::Rect& bounds, base::DictionaryValue* dict) {
dict->SetInteger("x", bounds.x());
dict->SetInteger("y", bounds.y());
dict->SetInteger("width", bounds.width());
dict->SetInteger("height", bounds.height());
} }
void DictionaryToRect(const base::DictionaryValue& dict, gfx::Rect* bounds) {
int x = 0, y = 0, width = 800, height = 600;
dict.GetInteger("x", &x);
dict.GetInteger("y", &y);
dict.GetInteger("width", &width);
dict.GetInteger("height", &height);
*bounds = gfx::Rect(x, y, width, height);
}
} // namespace
// Implemented separately on each platform. // Implemented separately on each platform.
InspectableWebContentsView* CreateInspectableContentsView( InspectableWebContentsView* CreateInspectableContentsView(
InspectableWebContentsImpl* inspectable_web_contents_impl); InspectableWebContentsImpl* inspectable_web_contents_impl);
void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) { void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(kIsDockedPref, true); scoped_ptr<base::DictionaryValue> bounds_dict(new base::DictionaryValue);
RectToDictionary(gfx::Rect(0, 0, 800, 600), bounds_dict.get());
registry->RegisterDictionaryPref(kDevToolsBoundsPref, bounds_dict.release());
} }
InspectableWebContentsImpl::InspectableWebContentsImpl( InspectableWebContentsImpl::InspectableWebContentsImpl(
content::WebContents* web_contents) content::WebContents* web_contents)
: web_contents_(web_contents), : web_contents_(web_contents),
delegate_(nullptr) { delegate_(nullptr) {
auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
auto bounds_dict = context->prefs()->GetDictionary(kDevToolsBoundsPref);
if (bounds_dict)
DictionaryToRect(*bounds_dict, &devtools_bounds_);
view_.reset(CreateInspectableContentsView(this)); view_.reset(CreateInspectableContentsView(this));
} }
@ -102,6 +126,18 @@ bool InspectableWebContentsImpl::IsDevToolsViewShowing() {
return devtools_web_contents_ && view_->IsDevToolsViewShowing(); return devtools_web_contents_ && view_->IsDevToolsViewShowing();
} }
gfx::Rect InspectableWebContentsImpl::GetDevToolsBounds() const {
return devtools_bounds_;
}
void InspectableWebContentsImpl::SaveDevToolsBounds(const gfx::Rect& bounds) {
auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
base::DictionaryValue bounds_dict;
RectToDictionary(bounds, &bounds_dict);
context->prefs()->Set(kDevToolsBoundsPref, bounds_dict);
devtools_bounds_ = bounds;
}
void InspectableWebContentsImpl::ActivateWindow() { void InspectableWebContentsImpl::ActivateWindow() {
} }

View file

@ -14,6 +14,7 @@
#include "content/public/browser/devtools_frontend_host_delegate.h" #include "content/public/browser/devtools_frontend_host_delegate.h"
#include "content/public/browser/web_contents_delegate.h" #include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "ui/gfx/rect.h"
class PrefRegistrySimple; class PrefRegistrySimple;
@ -46,6 +47,10 @@ class InspectableWebContentsImpl :
virtual void CloseDevTools() OVERRIDE; virtual void CloseDevTools() OVERRIDE;
virtual bool IsDevToolsViewShowing() OVERRIDE; virtual bool IsDevToolsViewShowing() OVERRIDE;
// Return the last position and size of devtools window.
gfx::Rect GetDevToolsBounds() const;
void SaveDevToolsBounds(const gfx::Rect& bounds);
virtual void SetDelegate(InspectableWebContentsDelegate* delegate) { virtual void SetDelegate(InspectableWebContentsDelegate* delegate) {
delegate_ = delegate; delegate_ = delegate;
} }
@ -106,6 +111,7 @@ class InspectableWebContentsImpl :
scoped_refptr<content::DevToolsAgentHost> agent_host_; scoped_refptr<content::DevToolsAgentHost> agent_host_;
DevToolsContentsResizingStrategy contents_resizing_strategy_; DevToolsContentsResizingStrategy contents_resizing_strategy_;
gfx::Rect devtools_bounds_;
scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_; scoped_ptr<DevToolsEmbedderMessageDispatcher> embedder_message_dispatcher_;

View file

@ -95,7 +95,7 @@ using namespace brightray;
backing:NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer:YES]); defer:YES]);
[devtools_window_ setDelegate:self]; [devtools_window_ setDelegate:self];
[devtools_window_ setFrameAutosaveName:@"brightray.developer.tools"]; [devtools_window_ setFrameAutosaveName:@"brightray.devtools"];
[devtools_window_ setTitle:@"Developer Tools"]; [devtools_window_ setTitle:@"Developer Tools"];
[devtools_window_ setReleasedWhenClosed:NO]; [devtools_window_ setReleasedWhenClosed:NO];
[devtools_window_ setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge]; [devtools_window_ setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];

View file

@ -29,7 +29,7 @@ class DevToolsWindowDelegate : public views::ClientView,
// views::WidgetDelegate: // views::WidgetDelegate:
virtual views::View* GetInitiallyFocusedView() OVERRIDE { return view_; } virtual views::View* GetInitiallyFocusedView() OVERRIDE { return view_; }
virtual bool CanResize() const OVERRIDE { return true; } virtual bool CanResize() const OVERRIDE { return true; }
virtual bool CanMaximize() const OVERRIDE { return true; } virtual bool CanMaximize() const OVERRIDE { return false; }
virtual base::string16 GetWindowTitle() const OVERRIDE { return title_; } virtual base::string16 GetWindowTitle() const OVERRIDE { return title_; }
virtual views::Widget* GetWidget() OVERRIDE { return widget_; } virtual views::Widget* GetWidget() OVERRIDE { return widget_; }
virtual const views::Widget* GetWidget() const OVERRIDE { return widget_; } virtual const views::Widget* GetWidget() const OVERRIDE { return widget_; }
@ -74,6 +74,8 @@ InspectableWebContentsViewViews::InspectableWebContentsViewViews(
} }
InspectableWebContentsViewViews::~InspectableWebContentsViewViews() { InspectableWebContentsViewViews::~InspectableWebContentsViewViews() {
if (devtools_window_)
inspectable_web_contents()->SaveDevToolsBounds(devtools_window_->GetWindowBoundsInScreen());
} }
views::View* InspectableWebContentsViewViews::GetView() { views::View* InspectableWebContentsViewViews::GetView() {
@ -95,7 +97,7 @@ void InspectableWebContentsViewViews::ShowDevTools() {
devtools_visible_ = true; devtools_visible_ = true;
if (devtools_window_) { if (devtools_window_) {
devtools_window_web_view_->SetWebContents(inspectable_web_contents_->devtools_web_contents()); devtools_window_web_view_->SetWebContents(inspectable_web_contents_->devtools_web_contents());
devtools_window_->CenterWindow(gfx::Size(800, 600)); devtools_window_->SetBounds(inspectable_web_contents()->GetDevToolsBounds());
devtools_window_->Show(); devtools_window_->Show();
} else { } else {
devtools_web_view_->SetVisible(true); devtools_web_view_->SetVisible(true);
@ -111,6 +113,7 @@ void InspectableWebContentsViewViews::CloseDevTools() {
devtools_visible_ = false; devtools_visible_ = false;
if (devtools_window_) { if (devtools_window_) {
inspectable_web_contents()->SaveDevToolsBounds(devtools_window_->GetWindowBoundsInScreen());
devtools_window_.reset(); devtools_window_.reset();
devtools_window_web_view_ = NULL; devtools_window_web_view_ = NULL;
} else { } else {