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

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

Issue 301733006: Zoom Extension API (chrome) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments. Created 6 years, 6 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/api/extension_action/action_info.h"
17 #include "content/public/browser/notification_source.h" 19 #include "content/public/browser/notification_source.h"
20 #include "extensions/common/manifest_handlers/icons_handler.h"
18 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
22 #include "grit/theme_resources.h"
19 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h" 24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/favicon_size.h"
26 #include "ui/views/controls/button/image_button.h"
21 #include "ui/views/controls/button/label_button.h" 27 #include "ui/views/controls/button/label_button.h"
22 #include "ui/views/controls/separator.h" 28 #include "ui/views/controls/separator.h"
23 #include "ui/views/layout/box_layout.h" 29 #include "ui/views/layout/grid_layout.h"
24 #include "ui/views/layout/layout_constants.h" 30 #include "ui/views/layout/layout_constants.h"
25 #include "ui/views/widget/widget.h" 31 #include "ui/views/widget/widget.h"
26 32
27 namespace { 33 namespace {
28 34
29 // The number of milliseconds the bubble should stay on the screen if it will 35 // The number of milliseconds the bubble should stay on the screen if it will
30 // close automatically. 36 // close automatically.
31 const int kBubbleCloseDelay = 1500; 37 const int kBubbleCloseDelay = 1500;
32 38
33 // The bubble's padding from the screen edge, used in fullscreen. 39 // The bubble's padding from the screen edge, used in fullscreen.
(...skipping 10 matching lines...) Expand all
44 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); 50 Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
45 DCHECK(browser && browser->window() && browser->fullscreen_controller()); 51 DCHECK(browser && browser->window() && browser->fullscreen_controller());
46 52
47 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); 53 BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser);
48 bool is_fullscreen = browser_view->IsFullscreen(); 54 bool is_fullscreen = browser_view->IsFullscreen();
49 bool anchor_to_view = !is_fullscreen || 55 bool anchor_to_view = !is_fullscreen ||
50 browser_view->immersive_mode_controller()->IsRevealed(); 56 browser_view->immersive_mode_controller()->IsRevealed();
51 views::View* anchor_view = anchor_to_view ? 57 views::View* anchor_view = anchor_to_view ?
52 browser_view->GetLocationBarView()->zoom_view() : NULL; 58 browser_view->GetLocationBarView()->zoom_view() : NULL;
53 59
54 // If the bubble is already showing in this window and its |auto_close_| value 60 // Find the extension that initiated the zoom change, if any.
55 // is equal to |auto_close|, the bubble can be reused and only the label text 61 ZoomController* zoom_controller =
56 // needs to be updated. 62 ZoomController::FromWebContents(web_contents);
63 const extensions::Extension* extension = zoom_controller->last_extension();
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.
57 if (zoom_bubble_ && 69 if (zoom_bubble_ &&
58 zoom_bubble_->GetAnchorView() == anchor_view && 70 zoom_bubble_->GetAnchorView() == anchor_view &&
59 zoom_bubble_->auto_close_ == auto_close) { 71 zoom_bubble_->auto_close_ == auto_close &&
60 zoom_bubble_->Refresh(); 72 !extension) {
73 zoom_bubble_->Refresh();
Devlin 2014/06/19 21:15:29 Can we make this more readable and just put an ear
wjmaclean 2014/06/20 22:01:33 Done.
61 } else { 74 } else {
62 // 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
63 // 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
64 // current bubble must be closed and a new one created. 77 // current bubble must be closed and a new one created.
65 CloseBubble(); 78 CloseBubble();
66 79
67 zoom_bubble_ = new ZoomBubbleView(anchor_view, 80 zoom_bubble_ = new ZoomBubbleView(anchor_view,
68 web_contents, 81 web_contents,
69 auto_close, 82 auto_close,
70 browser_view->immersive_mode_controller(), 83 browser_view->immersive_mode_controller(),
71 browser->fullscreen_controller()); 84 browser->fullscreen_controller());
72 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
73 // If we do not have an anchor view, parent the bubble to the content area. 91 // If we do not have an anchor view, parent the bubble to the content area.
74 if (!anchor_to_view) { 92 if (!anchor_to_view) {
75 zoom_bubble_->set_parent_window(web_contents->GetTopLevelNativeWindow()); 93 zoom_bubble_->set_parent_window(web_contents->GetTopLevelNativeWindow());
76 } 94 }
77 95
78 views::BubbleDelegateView::CreateBubble(zoom_bubble_); 96 views::BubbleDelegateView::CreateBubble(zoom_bubble_);
79 97
80 // Adjust for fullscreen after creation as it relies on the content size. 98 // Adjust for fullscreen after creation as it relies on the content size.
81 if (is_fullscreen) 99 if (is_fullscreen)
82 zoom_bubble_->AdjustForFullscreen(browser_view->GetBoundsInScreen()); 100 zoom_bubble_->AdjustForFullscreen(browser_view->GetBoundsInScreen());
(...skipping 23 matching lines...) Expand all
106 } 124 }
107 125
108 ZoomBubbleView::ZoomBubbleView( 126 ZoomBubbleView::ZoomBubbleView(
109 views::View* anchor_view, 127 views::View* anchor_view,
110 content::WebContents* web_contents, 128 content::WebContents* web_contents,
111 bool auto_close, 129 bool auto_close,
112 ImmersiveModeController* immersive_mode_controller, 130 ImmersiveModeController* immersive_mode_controller,
113 FullscreenController* fullscreen_controller) 131 FullscreenController* fullscreen_controller)
114 : BubbleDelegateView(anchor_view, anchor_view ? 132 : BubbleDelegateView(anchor_view, anchor_view ?
115 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE), 133 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE),
134 image_button_(NULL),
116 label_(NULL), 135 label_(NULL),
117 web_contents_(web_contents), 136 web_contents_(web_contents),
118 auto_close_(auto_close), 137 auto_close_(auto_close),
119 immersive_mode_controller_(immersive_mode_controller) { 138 immersive_mode_controller_(immersive_mode_controller) {
120 // Compensate for built-in vertical padding in the anchor view's image. 139 // Compensate for built-in vertical padding in the anchor view's image.
121 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); 140 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0));
122 set_use_focusless(auto_close); 141 set_use_focusless(auto_close);
123 set_notify_enter_exit_on_child(true); 142 set_notify_enter_exit_on_child(true);
124 143
125 // Add observers to close the bubble if the fullscreen state or immersive 144 // Add observers to close the bubble if the fullscreen state or immersive
(...skipping 17 matching lines...) Expand all
143 const size_t bubble_half_width = width() / 2; 162 const size_t bubble_half_width = width() / 2;
144 const int x_pos = base::i18n::IsRTL() ? 163 const int x_pos = base::i18n::IsRTL() ?
145 screen_bounds.x() + bubble_half_width + kFullscreenPaddingEnd : 164 screen_bounds.x() + bubble_half_width + kFullscreenPaddingEnd :
146 screen_bounds.right() - bubble_half_width - kFullscreenPaddingEnd; 165 screen_bounds.right() - bubble_half_width - kFullscreenPaddingEnd;
147 SetAnchorRect(gfx::Rect(x_pos, screen_bounds.y(), 0, 0)); 166 SetAnchorRect(gfx::Rect(x_pos, screen_bounds.y(), 0, 0));
148 } 167 }
149 168
150 void ZoomBubbleView::Refresh() { 169 void ZoomBubbleView::Refresh() {
151 ZoomController* zoom_controller = 170 ZoomController* zoom_controller =
152 ZoomController::FromWebContents(web_contents_); 171 ZoomController::FromWebContents(web_contents_);
153 int zoom_percent = zoom_controller->zoom_percent(); 172 int zoom_percent = zoom_controller->GetZoomPercent();
154 label_->SetText( 173 label_->SetText(
155 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 174 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
156 StartTimerIfNecessary(); 175 StartTimerIfNecessary();
157 } 176 }
158 177
159 void ZoomBubbleView::Close() { 178 void ZoomBubbleView::Close() {
160 GetWidget()->Close(); 179 GetWidget()->Close();
161 } 180 }
162 181
182 void ZoomBubbleView::SetExtensionInfo(const extensions::Extension* extension) {
183 DCHECK(extension);
184 extension_info_.id = extension->id();
185 extension_info_.name = extension->name();
186
187 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
188 const gfx::ImageSkia& default_extension_icon_image =
189 *rb.GetImageSkiaNamed(IDR_EXTENSIONS_FAVICON);
190 int icon_size = gfx::kFaviconSize;
191
192 // We give first preference to an icon from the extension's icon set that
193 // matches the size of the default. But not all extensions will declare an
194 // icon set, or may not have an icon of the default size (we don't want the
195 // bubble to display, for example, a very large icon). In that case, if there
196 // is a browser-action icon (size-19) this is an acceptable alternative.
197 const ExtensionIconSet& icons = extensions::IconsInfo::GetIcons(extension);
198 const ExtensionIconSet& action_icon =
199 extensions::ActionInfo::GetBrowserActionInfo(extension)
200 ->default_icon;
201 bool has_default_sized_icon =
202 !icons.Get(gfx::kFaviconSize, ExtensionIconSet::MATCH_EXACTLY).empty();
203 if (!has_default_sized_icon && !action_icon.empty())
204 icon_size = action_icon.map().begin()->first;
205 const ExtensionIconSet& icon_set_to_use =
206 has_default_sized_icon ? icons : action_icon;
207 extension_info_.icon_image.reset(
208 new extensions::IconImage(web_contents_->GetBrowserContext(),
209 extension,
210 icon_set_to_use,
211 icon_size,
212 default_extension_icon_image,
213 this));
214 }
215
163 void ZoomBubbleView::StartTimerIfNecessary() { 216 void ZoomBubbleView::StartTimerIfNecessary() {
164 if (auto_close_) { 217 if (auto_close_) {
165 if (timer_.IsRunning()) { 218 if (timer_.IsRunning()) {
166 timer_.Reset(); 219 timer_.Reset();
167 } else { 220 } else {
168 timer_.Start( 221 timer_.Start(
169 FROM_HERE, 222 FROM_HERE,
170 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), 223 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay),
171 this, 224 this,
172 &ZoomBubbleView::Close); 225 &ZoomBubbleView::Close);
173 } 226 }
174 } 227 }
175 } 228 }
176 229
177 void ZoomBubbleView::StopTimer() { 230 void ZoomBubbleView::StopTimer() {
178 timer_.Stop(); 231 timer_.Stop();
179 } 232 }
180 233
234 void ZoomBubbleView::OnExtensionIconImageChanged(extensions::IconImage* image) {
235 DCHECK_EQ(image, extension_info_.icon_image.get()) <<
236 "Unknown ImageIcon update.";
237 image_button_->SetImage(views::Button::STATE_NORMAL,
238 &extension_info_.icon_image->image_skia());
239 image_button_->SchedulePaint();
240 }
241
181 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { 242 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) {
182 set_use_focusless(false); 243 set_use_focusless(false);
183 StopTimer(); 244 StopTimer();
184 } 245 }
185 246
186 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { 247 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) {
187 set_use_focusless(auto_close_); 248 set_use_focusless(auto_close_);
188 StartTimerIfNecessary(); 249 StartTimerIfNecessary();
189 } 250 }
190 251
191 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { 252 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) {
192 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || 253 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ ||
193 event->type() != ui::ET_GESTURE_TAP) { 254 event->type() != ui::ET_GESTURE_TAP) {
194 return; 255 return;
195 } 256 }
196 257
197 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in 258 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in
198 // its place. 259 // its place.
199 ShowBubble(zoom_bubble_->web_contents_, false); 260 ShowBubble(zoom_bubble_->web_contents_, false);
200 event->SetHandled(); 261 event->SetHandled();
201 } 262 }
202 263
203 void ZoomBubbleView::ButtonPressed(views::Button* sender, 264 void ZoomBubbleView::ButtonPressed(views::Button* sender,
204 const ui::Event& event) { 265 const ui::Event& event) {
205 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); 266 if (sender == image_button_) {
267 DCHECK(extension_info_.icon_image.get()) << "Invalid button press.";
268 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
269 std::string url("chrome://extensions?id=");
Devlin 2014/06/19 21:15:29 nit: stringprintf will be slightly faster than thi
wjmaclean 2014/06/20 22:01:33 Done.
270 url += extension_info_.id;
271 chrome::AddSelectedTabWithURL(browser,
272 GURL(url),
273 content::PAGE_TRANSITION_FROM_API);
Devlin 2014/06/19 21:15:29 I don't know if "FROM_API" is strictly correct her
wjmaclean 2014/06/20 22:01:33 OK, can you suggest someone, and/or add them to th
274 } else {
275 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET);
276 }
206 } 277 }
207 278
208 void ZoomBubbleView::Init() { 279 void ZoomBubbleView::Init() {
209 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 280 // Set up the layout of the zoom bubble. A grid layout is used because
210 0, 0, views::kRelatedControlVerticalSpacing)); 281 // sometimes an extension icon is shown next to the zoom label.
282 views::GridLayout* grid_layout = new views::GridLayout(this);
283 SetLayoutManager(grid_layout);
284 views::ColumnSet* columns = grid_layout->AddColumnSet(0);
285 // First row.
286 if (extension_info_.icon_image.get()) {
287 columns->AddColumn(views::GridLayout::CENTER,views::GridLayout::CENTER, 2,
288 views::GridLayout::USE_PREF, 0, 0);
289 }
290 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
291 views::GridLayout::USE_PREF, 0, 0);
292 grid_layout->StartRow(0, 0);
211 293
294 // If this zoom change was initiated by an extension, that extension will be
295 // attributed by showing its icon in the zoom bubble.
296 if (extension_info_.icon_image.get()) {
297 image_button_ = new views::ImageButton(this);
298 image_button_->SetTooltipText(l10n_util::GetStringFUTF16(
299 IDS_TOOLTIP_ZOOM_EXTENSION_ICON,
300 base::UTF8ToUTF16(extension_info_.name)));
301 image_button_->SetImage(views::Button::STATE_NORMAL,
302 &extension_info_.icon_image->image_skia());
303 grid_layout->AddView(image_button_);
304 }
305
306 // Add zoom label with the new zoom percent.
212 ZoomController* zoom_controller = 307 ZoomController* zoom_controller =
213 ZoomController::FromWebContents(web_contents_); 308 ZoomController::FromWebContents(web_contents_);
214 int zoom_percent = zoom_controller->zoom_percent(); 309 int zoom_percent = zoom_controller->GetZoomPercent();
215 label_ = new views::Label( 310 label_ = new views::Label(
216 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); 311 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent));
217 label_->SetFontList( 312 label_->SetFontList(
218 ui::ResourceBundle::GetSharedInstance().GetFontList( 313 ui::ResourceBundle::GetSharedInstance().GetFontList(
219 ui::ResourceBundle::MediumFont)); 314 ui::ResourceBundle::MediumFont));
220 AddChildView(label_); 315 grid_layout->AddView(label_);
221 316
317 // Second row.
318 grid_layout->AddPaddingRow(0, 8);
319 columns = grid_layout->AddColumnSet(1);
320 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
321 views::GridLayout::USE_PREF, 0, 0);
322 grid_layout->StartRow(0, 1);
323
324 // Add "Reset to Default" button.
222 views::LabelButton* set_default_button = new views::LabelButton( 325 views::LabelButton* set_default_button = new views::LabelButton(
223 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); 326 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT));
224 set_default_button->SetStyle(views::Button::STYLE_BUTTON); 327 set_default_button->SetStyle(views::Button::STYLE_BUTTON);
225 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER); 328 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
226 AddChildView(set_default_button); 329 grid_layout->AddView(set_default_button);
227 330
228 StartTimerIfNecessary(); 331 StartTimerIfNecessary();
229 } 332 }
230 333
231 void ZoomBubbleView::Observe(int type, 334 void ZoomBubbleView::Observe(int type,
232 const content::NotificationSource& source, 335 const content::NotificationSource& source,
233 const content::NotificationDetails& details) { 336 const content::NotificationDetails& details) {
234 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED); 337 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED);
235 CloseBubble(); 338 CloseBubble();
236 } 339 }
237 340
238 void ZoomBubbleView::OnImmersiveRevealStarted() { 341 void ZoomBubbleView::OnImmersiveRevealStarted() {
239 CloseBubble(); 342 CloseBubble();
240 } 343 }
241 344
242 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { 345 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() {
243 immersive_mode_controller_ = NULL; 346 immersive_mode_controller_ = NULL;
244 } 347 }
245 348
246 void ZoomBubbleView::WindowClosing() { 349 void ZoomBubbleView::WindowClosing() {
247 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't 350 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't
248 // call this right away). Only set to NULL when it's this bubble. 351 // call this right away). Only set to NULL when it's this bubble.
249 if (zoom_bubble_ == this) 352 if (zoom_bubble_ == this)
250 zoom_bubble_ = NULL; 353 zoom_bubble_ = NULL;
251 } 354 }
355
356 ZoomBubbleView::ZoomBubbleExtensionInfo::ZoomBubbleExtensionInfo() {}
357
358 ZoomBubbleView::ZoomBubbleExtensionInfo::~ZoomBubbleExtensionInfo() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698