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

Side by Side Diff: chrome/browser/ui/views/location_bar/zoom_bubble_view.cc

Issue 225093019: Zoom Extension API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 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/views/location_bar/zoom_bubble_view.h" 5 #include "chrome/browser/ui/views/location_bar/zoom_bubble_view.h"
6 6
7 #include "base/i18n/rtl.h" 7 #include "base/i18n/rtl.h"
8 #include "chrome/browser/chrome_notification_types.h" 8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/chrome_page_zoom.h" 9 #include "chrome/browser/chrome_page_zoom.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_finder.h" 11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/browser_tabstrip.h"
12 #include "chrome/browser/ui/browser_window.h" 13 #include "chrome/browser/ui/browser_window.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h" 14 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
15 #include "chrome/browser/ui/views/location_bar/zoom_view.h" 16 #include "chrome/browser/ui/views/location_bar/zoom_view.h"
16 #include "chrome/browser/ui/zoom/zoom_controller.h" 17 #include "chrome/browser/ui/zoom/zoom_controller.h"
18 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
17 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
18 #include "content/public/browser/web_contents_view.h" 20 #include "content/public/browser/web_contents_view.h"
19 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
22 #include "grit/theme_resources.h"
20 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/views/controls/button/label_button.h" 25 #include "ui/views/controls/button/label_button.h"
23 #include "ui/views/controls/separator.h" 26 #include "ui/views/controls/separator.h"
24 #include "ui/views/layout/box_layout.h" 27 #include "ui/views/layout/grid_layout.h"
25 #include "ui/views/layout/layout_constants.h" 28 #include "ui/views/layout/layout_constants.h"
26 #include "ui/views/widget/widget.h" 29 #include "ui/views/widget/widget.h"
27 30
28 namespace { 31 namespace {
29 32
30 // The number of milliseconds the bubble should stay on the screen if it will 33 // The number of milliseconds the bubble should stay on the screen if it will
31 // close automatically. 34 // close automatically.
32 const int kBubbleCloseDelay = 1500; 35 const int kBubbleCloseDelay = 1500;
33 36
34 // The bubble's padding from the screen edge, used in fullscreen. 37 // The bubble's padding from the screen edge, used in fullscreen.
(...skipping 10 matching lines...) Expand all
45 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 48 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
46 DCHECK(browser && browser->window() && browser->fullscreen_controller()); 49 DCHECK(browser && browser->window() && browser->fullscreen_controller());
47 50
48 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); 51 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
49 bool is_fullscreen = browser_view->IsFullscreen(); 52 bool is_fullscreen = browser_view->IsFullscreen();
50 bool anchor_to_view = !is_fullscreen || 53 bool anchor_to_view = !is_fullscreen ||
51 browser_view->immersive_mode_controller()->IsRevealed(); 54 browser_view->immersive_mode_controller()->IsRevealed();
52 views::View* anchor_view = anchor_to_view ? 55 views::View* anchor_view = anchor_to_view ?
53 browser_view->GetLocationBarView()->zoom_view() : NULL; 56 browser_view->GetLocationBarView()->zoom_view() : NULL;
54 57
55 // If the bubble is already showing in this window and its |auto_close_| value 58 // Find the extension that initiated the zoom change, if any.
56 // is equal to |auto_close|, the bubble can be reused and only the label text 59 ZoomController* zoom_controller =
57 // needs to be updated. 60 ZoomController::FromWebContents(web_contents);
61 int zoom_id = zoom_controller->last_zoom_id();
62 const extensions::Extension* extension = zoom_id ?
63 zoom_controller->GetExtensionById(zoom_id) : NULL;
64
65 // If the bubble is already showing in this window, its |auto_close_| value
66 // is equal to |auto_close|, and the zoom change was not initiated by an
67 // extension, then the bubble can be reused and only the label text needs to
68 // be updated.
58 if (zoom_bubble_ && 69 if (zoom_bubble_ &&
59 zoom_bubble_->GetAnchorView() == anchor_view && 70 zoom_bubble_->GetAnchorView() == anchor_view &&
60 zoom_bubble_->auto_close_ == auto_close) { 71 zoom_bubble_->auto_close_ == auto_close &&
61 zoom_bubble_->Refresh(); 72 !extension) {
73 zoom_bubble_->Refresh();
62 } else { 74 } else {
63 // If the bubble is already showing but its |auto_close_| value is not equal 75 // If the bubble is already showing but its |auto_close_| value is not equal
64 // to |auto_close|, the bubble's focus properties must change, so the 76 // to |auto_close|, the bubble's focus properties must change, so the
65 // current bubble must be closed and a new one created. 77 // current bubble must be closed and a new one created.
66 CloseBubble(); 78 CloseBubble();
67 79
68 zoom_bubble_ = new ZoomBubbleView(anchor_view, 80 zoom_bubble_ = new ZoomBubbleView(anchor_view,
69 web_contents, 81 web_contents,
70 auto_close, 82 auto_close,
71 browser_view->immersive_mode_controller(), 83 browser_view->immersive_mode_controller(),
72 browser->fullscreen_controller()); 84 browser->fullscreen_controller());
73 85
86 // If the zoom change was initiated by an extension, capture the relevent
87 // information from it.
88 if (extension) {
89 zoom_bubble_->SetExtensionInfo(extension);
90 }
91
74 // If we do not have an anchor view, parent the bubble to the content area. 92 // If we do not have an anchor view, parent the bubble to the content area.
75 if (!anchor_to_view) { 93 if (!anchor_to_view) {
76 zoom_bubble_->set_parent_window( 94 zoom_bubble_->set_parent_window(
77 web_contents->GetView()->GetTopLevelNativeWindow()); 95 web_contents->GetView()->GetTopLevelNativeWindow());
78 } 96 }
79 97
80 views::BubbleDelegateView::CreateBubble(zoom_bubble_); 98 views::BubbleDelegateView::CreateBubble(zoom_bubble_);
81 99
82 // Adjust for fullscreen after creation as it relies on the content size. 100 // Adjust for fullscreen after creation as it relies on the content size.
83 if (is_fullscreen) 101 if (is_fullscreen)
(...skipping 24 matching lines...) Expand all
108 } 126 }
109 127
110 ZoomBubbleView::ZoomBubbleView( 128 ZoomBubbleView::ZoomBubbleView(
111 views::View* anchor_view, 129 views::View* anchor_view,
112 content::WebContents* web_contents, 130 content::WebContents* web_contents,
113 bool auto_close, 131 bool auto_close,
114 ImmersiveModeController* immersive_mode_controller, 132 ImmersiveModeController* immersive_mode_controller,
115 FullscreenController* fullscreen_controller) 133 FullscreenController* fullscreen_controller)
116 : BubbleDelegateView(anchor_view, anchor_view ? 134 : BubbleDelegateView(anchor_view, anchor_view ?
117 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE), 135 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE),
136 image_button_(NULL),
118 label_(NULL), 137 label_(NULL),
119 web_contents_(web_contents), 138 web_contents_(web_contents),
120 auto_close_(auto_close), 139 auto_close_(auto_close),
121 immersive_mode_controller_(immersive_mode_controller) { 140 immersive_mode_controller_(immersive_mode_controller) {
122 // Compensate for built-in vertical padding in the anchor view's image. 141 // Compensate for built-in vertical padding in the anchor view's image.
123 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); 142 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
124 set_use_focusless(auto_close); 143 set_use_focusless(auto_close);
125 set_notify_enter_exit_on_child(true); 144 set_notify_enter_exit_on_child(true);
126 145
127 // Add observers to close the bubble if the fullscreen state or immersive 146 // Add observers to close the bubble if the fullscreen state or immersive
(...skipping 27 matching lines...) Expand all
155 int zoom_percent = zoom_controller->zoom_percent(); 174 int zoom_percent = zoom_controller->zoom_percent();
156 label_->SetText( 175 label_->SetText(
157 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 176 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
158 StartTimerIfNecessary(); 177 StartTimerIfNecessary();
159 } 178 }
160 179
161 void ZoomBubbleView::Close() { 180 void ZoomBubbleView::Close() {
162 GetWidget()->Close(); 181 GetWidget()->Close();
163 } 182 }
164 183
184 void ZoomBubbleView::SetExtensionInfo(const extensions::Extension* extension) {
185 DCHECK(extension);
186 extension_info_.id = extension->id();
187 extension_info_.name = extension->name();
188
189 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
190 const gfx::ImageSkia& default_extension_icon_image =
191 *rb.GetImageSkiaNamed(IDR_EXTENSIONS_FAVICON);
192 extension_info_.icon_image.reset(
193 new extensions::IconImage(web_contents_->GetBrowserContext(),
194 extension,
195 extensions::IconsInfo::GetIcons(extension),
196 16,
197 default_extension_icon_image,
198 this));
199 }
200
165 void ZoomBubbleView::StartTimerIfNecessary() { 201 void ZoomBubbleView::StartTimerIfNecessary() {
166 if (auto_close_) { 202 if (auto_close_) {
167 if (timer_.IsRunning()) { 203 if (timer_.IsRunning()) {
168 timer_.Reset(); 204 timer_.Reset();
169 } else { 205 } else {
170 timer_.Start( 206 timer_.Start(
171 FROM_HERE, 207 FROM_HERE,
172 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), 208 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
173 this, 209 this,
174 &ZoomBubbleView::Close); 210 &ZoomBubbleView::Close);
175 } 211 }
176 } 212 }
177 } 213 }
178 214
179 void ZoomBubbleView::StopTimer() { 215 void ZoomBubbleView::StopTimer() {
180 timer_.Stop(); 216 timer_.Stop();
181 } 217 }
182 218
219 void ZoomBubbleView::OnExtensionIconImageChanged(extensions::IconImage* image) {
220 DCHECK_EQ(image, extension_info_.icon_image.get()) <<
221 "Unknown ImageIcon update.";
222 image_button_->SetImage(views::Button::STATE_NORMAL,
223 &extension_info_.icon_image->image_skia());
224 image_button_->SchedulePaint();
225 }
226
183 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 227 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
184 set_use_focusless(false); 228 set_use_focusless(false);
185 StopTimer(); 229 StopTimer();
186 } 230 }
187 231
188 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 232 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
189 set_use_focusless(auto_close_); 233 set_use_focusless(auto_close_);
190 StartTimerIfNecessary(); 234 StartTimerIfNecessary();
191 } 235 }
192 236
193 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { 237 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
194 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || 238 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
195 event->type() != ui::ET_GESTURE_TAP) { 239 event->type() != ui::ET_GESTURE_TAP) {
196 return; 240 return;
197 } 241 }
198 242
199 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in 243 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in
200 // its place. 244 // its place.
201 ShowBubble(zoom_bubble_->web_contents_, false); 245 ShowBubble(zoom_bubble_->web_contents_, false);
202 event->SetHandled(); 246 event->SetHandled();
203 } 247 }
204 248
205 void ZoomBubbleView::ButtonPressed(views::Button* sender, 249 void ZoomBubbleView::ButtonPressed(views::Button* sender,
206 const ui::Event& event) { 250 const ui::Event& event) {
207 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 251 if (sender == image_button_) {
252 DCHECK(extension_info_.icon_image.get()) << "Invalid button press.";
253 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
254 std::string url("chrome://extensions?id=");
255 url += extension_info_.id;
256 chrome::AddSelectedTabWithURL(browser,
257 GURL(url),
258 content::PAGE_TRANSITION_FROM_API);
259 } else {
260 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
261 }
208 } 262 }
209 263
210 void ZoomBubbleView::Init() { 264 void ZoomBubbleView::Init() {
211 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 265 // Set up the layout of the zoom bubble.
212 0, 0, views::kRelatedControlVerticalSpacing)); 266 views::GridLayout* grid_layout = new views::GridLayout(this);
267 SetLayoutManager(grid_layout);
Fady Samuel 2014/04/07 20:01:14 Please comment on what this code is doing. Presuma
paulmeyer 2014/04/07 20:56:10 Done.
268 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
269 // First row.
270 if (extension_info_.icon_image.get()) {
271 columns->AddColumn(views::GridLayout::CENTER,views::GridLayout::CENTER, 2,
272 views::GridLayout::USE_PREF, 0, 0);
273 }
274 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
275 views::GridLayout::USE_PREF, 0, 0);
276 grid_layout->StartRow(0, 0);
213 277
278 // If this zoom change was initiated by an extension, that extension will be
279 // attributed by showing its icon in the zoom bubble.
280 if (extension_info_.icon_image.get()) {
281 image_button_ = new views::ImageButton(this);
282 image_button_->SetTooltipText(l10n_util::GetStringFUTF16(
283 IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
284 base::UTF8ToUTF16(extension_info_.name)));
285 image_button_->SetImage(views::Button::STATE_NORMAL,
286 &extension_info_.icon_image->image_skia());
287 grid_layout->AddView(image_button_);
288 }
289
290 // Add zoom label with the new zoom percent.
214 ZoomController* zoom_controller = 291 ZoomController* zoom_controller =
215 ZoomController::FromWebContents(web_contents_); 292 ZoomController::FromWebContents(web_contents_);
216 int zoom_percent = zoom_controller->zoom_percent(); 293 int zoom_percent = zoom_controller->zoom_percent();
217 label_ = new views::Label( 294 label_ = new views::Label(
218 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 295 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
219 label_->SetFontList( 296 label_->SetFontList(
220 ui::ResourceBundle::GetSharedInstance().GetFontList( 297 ui::ResourceBundle::GetSharedInstance().GetFontList(
221 ui::ResourceBundle::MediumFont)); 298 ui::ResourceBundle::MediumFont));
222 AddChildView(label_); 299 grid_layout->AddView(label_);
223 300
301 // Second row.
302 grid_layout->AddPaddingRow(0, 8);
303 columns = grid_layout->AddColumnSet(1);
304 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
305 views::GridLayout::USE_PREF, 0, 0);
306 grid_layout->StartRow(0, 1);
307
308 // Add "Reset to Default" button.
224 views::LabelButton* set_default_button = new views::LabelButton( 309 views::LabelButton* set_default_button = new views::LabelButton(
225 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); 310 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
226 set_default_button->SetStyle(views::Button::STYLE_BUTTON); 311 set_default_button->SetStyle(views::Button::STYLE_BUTTON);
227 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER); 312 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
228 AddChildView(set_default_button); 313 grid_layout->AddView(set_default_button);
229 314
230 StartTimerIfNecessary(); 315 StartTimerIfNecessary();
231 } 316 }
232 317
233 void ZoomBubbleView::Observe(int type, 318 void ZoomBubbleView::Observe(int type,
234 const content::NotificationSource& source, 319 const content::NotificationSource& source,
235 const content::NotificationDetails& details) { 320 const content::NotificationDetails& details) {
236 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED); 321 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED);
237 CloseBubble(); 322 CloseBubble();
238 } 323 }
239 324
240 void ZoomBubbleView::OnImmersiveRevealStarted() { 325 void ZoomBubbleView::OnImmersiveRevealStarted() {
241 CloseBubble(); 326 CloseBubble();
242 } 327 }
243 328
244 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 329 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
245 immersive_mode_controller_ = NULL; 330 immersive_mode_controller_ = NULL;
246 } 331 }
247 332
248 void ZoomBubbleView::WindowClosing() { 333 void ZoomBubbleView::WindowClosing() {
249 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 334 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
250 // call this right away). Only set to NULL when it's this bubble. 335 // call this right away). Only set to NULL when it's this bubble.
251 if (zoom_bubble_ == this) 336 if (zoom_bubble_ == this)
252 zoom_bubble_ = NULL; 337 zoom_bubble_ = NULL;
253 } 338 }
339
340 ZoomBubbleView::ExtensionInfo::ExtensionInfo() {}
341
342 ZoomBubbleView::ExtensionInfo::~ExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698