Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(678)

Side by Side Diff: chrome/browser/ui/zoom/zoom_controller.cc

Issue 10736028: Refactor browser window zoom handling and enable zoom icon on all platforms. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/zoom/zoom_controller.h" 5 #include "chrome/browser/ui/zoom/zoom_controller.h"
6 6
7 #include "chrome/browser/profiles/profile.h" 7 #include "chrome/browser/profiles/profile.h"
8 #include "chrome/browser/ui/browser_finder.h" 8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/browser/ui/tab_contents/tab_contents.h" 9 #include "chrome/browser/ui/tab_contents/tab_contents.h"
10 #include "chrome/common/chrome_notification_types.h" 10 #include "chrome/common/chrome_notification_types.h"
11 #include "chrome/common/pref_names.h" 11 #include "chrome/common/pref_names.h"
12 #include "content/public/browser/notification_types.h" 12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/notification_details.h" 13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_service.h" 14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/page_zoom.h" 16 #include "content/public/common/page_zoom.h"
17 #include "grit/theme_resources.h"
17 18
18 ZoomController::ZoomController(TabContents* tab_contents) 19 ZoomController::ZoomController(TabContents* tab_contents)
19 : content::WebContentsObserver(tab_contents->web_contents()), 20 : content::WebContentsObserver(tab_contents->web_contents()),
20 zoom_icon_state_(NONE),
21 zoom_percent_(100), 21 zoom_percent_(100),
22 tab_contents_(tab_contents), 22 tab_contents_(tab_contents),
23 observer_(NULL) { 23 observer_(NULL) {
24 default_zoom_level_.Init(prefs::kDefaultZoomLevel, 24 default_zoom_level_.Init(prefs::kDefaultZoomLevel,
25 tab_contents->profile()->GetPrefs(), this); 25 tab_contents->profile()->GetPrefs(), this);
26 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED, 26 registrar_.Add(this, content::NOTIFICATION_ZOOM_LEVEL_CHANGED,
27 content::NotificationService::AllBrowserContextsAndSources()); 27 content::NotificationService::AllBrowserContextsAndSources());
28 28
29 UpdateState(false); 29 UpdateState(false);
30 } 30 }
31 31
32 ZoomController::~ZoomController() { 32 ZoomController::~ZoomController() {
33 default_zoom_level_.Destroy(); 33 default_zoom_level_.Destroy();
34 registrar_.RemoveAll(); 34 registrar_.RemoveAll();
35 } 35 }
36 36
37 bool ZoomController::IsAtDefaultZoom() const {
38 return content::ZoomValuesEqual(tab_contents_->web_contents()->GetZoomLevel(),
39 default_zoom_level_.GetValue());
40 }
41
42 int ZoomController::GetResourceForZoomLevel() const {
43 DCHECK(!IsAtDefaultZoom());
44 double zoom = tab_contents_->web_contents()->GetZoomLevel();
45 return zoom > default_zoom_level_.GetValue() ? IDR_ZOOM_PLUS : IDR_ZOOM_MINUS;
46 }
47
37 void ZoomController::DidNavigateMainFrame( 48 void ZoomController::DidNavigateMainFrame(
38 const content::LoadCommittedDetails& details, 49 const content::LoadCommittedDetails& details,
39 const content::FrameNavigateParams& params) { 50 const content::FrameNavigateParams& params) {
40 // If the main frame's content has changed, the new page may have a different 51 // If the main frame's content has changed, the new page may have a different
41 // zoom level from the old one. 52 // zoom level from the old one.
42 UpdateState(false); 53 UpdateState(false);
43 } 54 }
44 55
45 void ZoomController::Observe(int type, 56 void ZoomController::Observe(int type,
46 const content::NotificationSource& source, 57 const content::NotificationSource& source,
47 const content::NotificationDetails& details) { 58 const content::NotificationDetails& details) {
48 switch (type) { 59 switch (type) {
49 case chrome::NOTIFICATION_PREF_CHANGED: { 60 case chrome::NOTIFICATION_PREF_CHANGED: {
50 std::string* pref_name = content::Details<std::string>(details).ptr(); 61 std::string* pref_name = content::Details<std::string>(details).ptr();
51 DCHECK(pref_name && *pref_name == prefs::kDefaultZoomLevel); 62 DCHECK(pref_name && *pref_name == prefs::kDefaultZoomLevel);
52 UpdateState(false); 63 UpdateState(false);
53 break; 64 break;
54 } 65 }
55 case content::NOTIFICATION_ZOOM_LEVEL_CHANGED: 66 case content::NOTIFICATION_ZOOM_LEVEL_CHANGED:
56 UpdateState(!content::Details<std::string>(details)->empty()); 67 UpdateState(!content::Details<std::string>(details)->empty());
57 break; 68 break;
58 default: 69 default:
59 NOTREACHED(); 70 NOTREACHED();
60 } 71 }
61 } 72 }
62 73
63 void ZoomController::UpdateState(bool can_show_bubble) { 74 void ZoomController::UpdateState(bool can_show_bubble) {
64 double current_zoom_level = tab_contents_->web_contents()->GetZoomLevel(); 75 bool dummy;
65 double default_zoom_level = default_zoom_level_.GetValue(); 76 zoom_percent_ = tab_contents_->web_contents()->GetZoomPercent(&dummy, &dummy);
66 77
67 ZoomIconState state; 78 if (observer_)
68 if (content::ZoomValuesEqual(current_zoom_level, default_zoom_level)) 79 observer_->OnZoomChanged(tab_contents_, can_show_bubble);
69 state = NONE;
70 else if (current_zoom_level > default_zoom_level)
71 state = ZOOM_PLUS_ICON;
72 else
73 state = ZOOM_MINUS_ICON;
74
75 bool dummy;
76 int zoom_percent = tab_contents_->web_contents()->
77 GetZoomPercent(&dummy, &dummy);
78
79 if (state != zoom_icon_state_) {
80 zoom_icon_state_ = state;
81 if (observer_)
82 observer_->OnZoomIconChanged(tab_contents_, state);
83 }
84
85 if (zoom_percent != zoom_percent_) {
86 zoom_percent_ = zoom_percent;
87 if (observer_)
88 observer_->OnZoomChanged(tab_contents_,
89 zoom_percent,
90 can_show_bubble && state != NONE);
91 }
92 } 80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698