save devtools zoom level preference

This commit is contained in:
Robo 2015-07-27 20:04:21 +05:30
parent bd6019ba0d
commit a56a0505fa
2 changed files with 32 additions and 12 deletions

View file

@ -43,6 +43,7 @@ const char kChromeUIDevToolsURL[] = "chrome-devtools://devtools/devtools.html?"
"textColor=rgba(0,0,0,1)&" "textColor=rgba(0,0,0,1)&"
"experiments=true"; "experiments=true";
const char kDevToolsBoundsPref[] = "brightray.devtools.bounds"; const char kDevToolsBoundsPref[] = "brightray.devtools.bounds";
const char kDevToolsZoomPref[] = "brightray.devtools.zoom";
const char kFrontendHostId[] = "id"; const char kFrontendHostId[] = "id";
const char kFrontendHostMethod[] = "method"; const char kFrontendHostMethod[] = "method";
@ -71,11 +72,8 @@ void DictionaryToRect(const base::DictionaryValue& dict, gfx::Rect* bounds) {
*bounds = gfx::Rect(x, y, width, height); *bounds = gfx::Rect(x, y, width, height);
} }
double GetZoomLevelForWebContents(content::WebContents* web_contents) { void SetZoomLevelForWebContents(content::WebContents* web_contents,
return content::HostZoomMap::GetZoomLevel(web_contents); double level) {
}
void SetZoomLevelForWebContents(content::WebContents* web_contents, double level) {
content::HostZoomMap::SetZoomLevel(web_contents, level); content::HostZoomMap::SetZoomLevel(web_contents, level);
} }
@ -156,6 +154,7 @@ void InspectableWebContentsImpl::RegisterPrefs(PrefRegistrySimple* registry) {
auto bounds_dict = make_scoped_ptr(new base::DictionaryValue); auto bounds_dict = make_scoped_ptr(new base::DictionaryValue);
RectToDictionary(gfx::Rect(0, 0, 800, 600), bounds_dict.get()); RectToDictionary(gfx::Rect(0, 0, 800, 600), bounds_dict.get());
registry->RegisterDictionaryPref(kDevToolsBoundsPref, bounds_dict.release()); registry->RegisterDictionaryPref(kDevToolsBoundsPref, bounds_dict.release());
registry->RegisterDoublePref(kDevToolsZoomPref, 0.);
} }
InspectableWebContentsImpl::InspectableWebContentsImpl( InspectableWebContentsImpl::InspectableWebContentsImpl(
@ -166,7 +165,8 @@ InspectableWebContentsImpl::InspectableWebContentsImpl(
web_contents_(web_contents), web_contents_(web_contents),
weak_factory_(this) { weak_factory_(this) {
auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext()); auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
auto bounds_dict = context->prefs()->GetDictionary(kDevToolsBoundsPref); pref_service_ = context->prefs();
auto bounds_dict = pref_service_->GetDictionary(kDevToolsBoundsPref);
if (bounds_dict) if (bounds_dict)
DictionaryToRect(*bounds_dict, &devtools_bounds_); DictionaryToRect(*bounds_dict, &devtools_bounds_);
@ -282,14 +282,24 @@ gfx::Rect InspectableWebContentsImpl::GetDevToolsBounds() const {
} }
void InspectableWebContentsImpl::SaveDevToolsBounds(const gfx::Rect& bounds) { void InspectableWebContentsImpl::SaveDevToolsBounds(const gfx::Rect& bounds) {
auto context = static_cast<BrowserContext*>(web_contents_->GetBrowserContext());
base::DictionaryValue bounds_dict; base::DictionaryValue bounds_dict;
RectToDictionary(bounds, &bounds_dict); RectToDictionary(bounds, &bounds_dict);
context->prefs()->Set(kDevToolsBoundsPref, bounds_dict); pref_service_->Set(kDevToolsBoundsPref, bounds_dict);
devtools_bounds_ = bounds; devtools_bounds_ = bounds;
} }
double InspectableWebContentsImpl::GetDevToolsZoomLevel() const {
return pref_service_->GetDouble(kDevToolsZoomPref);
}
void InspectableWebContentsImpl::UpdateDevToolsZoomLevel(double level) {
pref_service_->SetDouble(kDevToolsZoomPref, level);
}
void InspectableWebContentsImpl::ActivateWindow() { void InspectableWebContentsImpl::ActivateWindow() {
// Set the zoom level.
SetZoomLevelForWebContents(GetDevToolsWebContents(),
GetDevToolsZoomLevel());
} }
void InspectableWebContentsImpl::CloseWindow() { void InspectableWebContentsImpl::CloseWindow() {
@ -404,17 +414,20 @@ void InspectableWebContentsImpl::SetWhitelistedShortcuts(const std::string& mess
} }
void InspectableWebContentsImpl::ZoomIn() { void InspectableWebContentsImpl::ZoomIn() {
double level = GetZoomLevelForWebContents(GetDevToolsWebContents()); double new_level = GetNextZoomLevel(GetDevToolsZoomLevel(), false);
SetZoomLevelForWebContents(GetDevToolsWebContents(), GetNextZoomLevel(level, false)); SetZoomLevelForWebContents(GetDevToolsWebContents(), new_level);
UpdateDevToolsZoomLevel(new_level);
} }
void InspectableWebContentsImpl::ZoomOut() { void InspectableWebContentsImpl::ZoomOut() {
double level = GetZoomLevelForWebContents(GetDevToolsWebContents()); double new_level = GetNextZoomLevel(GetDevToolsZoomLevel(), true);
SetZoomLevelForWebContents(GetDevToolsWebContents(), GetNextZoomLevel(level, true)); SetZoomLevelForWebContents(GetDevToolsWebContents(), new_level);
UpdateDevToolsZoomLevel(new_level);
} }
void InspectableWebContentsImpl::ResetZoom() { void InspectableWebContentsImpl::ResetZoom() {
SetZoomLevelForWebContents(GetDevToolsWebContents(), 0.); SetZoomLevelForWebContents(GetDevToolsWebContents(), 0.);
UpdateDevToolsZoomLevel(0.);
} }
void InspectableWebContentsImpl::SetDevicesUpdatesEnabled(bool enabled) { void InspectableWebContentsImpl::SetDevicesUpdatesEnabled(bool enabled) {

View file

@ -19,6 +19,7 @@
#include "net/url_request/url_fetcher_delegate.h" #include "net/url_request/url_fetcher_delegate.h"
#include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/rect.h"
class PrefService;
class PrefRegistrySimple; class PrefRegistrySimple;
namespace content { namespace content {
@ -65,6 +66,10 @@ class InspectableWebContentsImpl :
gfx::Rect GetDevToolsBounds() const; gfx::Rect GetDevToolsBounds() const;
void SaveDevToolsBounds(const gfx::Rect& bounds); void SaveDevToolsBounds(const gfx::Rect& bounds);
// Return the last set zoom level of devtools window.
double GetDevToolsZoomLevel() const;
void UpdateDevToolsZoomLevel(double level);
private: private:
// DevToolsEmbedderMessageDispacher::Delegate // DevToolsEmbedderMessageDispacher::Delegate
void ActivateWindow() override; void ActivateWindow() override;
@ -160,6 +165,8 @@ class InspectableWebContentsImpl :
PendingRequestsMap pending_requests_; PendingRequestsMap pending_requests_;
InspectableWebContentsDelegate* delegate_; // weak references. InspectableWebContentsDelegate* delegate_; // weak references.
PrefService* pref_service_; // weak reference.
scoped_ptr<content::WebContents> web_contents_; scoped_ptr<content::WebContents> web_contents_;
scoped_ptr<content::WebContents> devtools_web_contents_; scoped_ptr<content::WebContents> devtools_web_contents_;
scoped_ptr<InspectableWebContentsView> view_; scoped_ptr<InspectableWebContentsView> view_;