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

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

Powered by Google App Engine
This is Rietveld 408576698