OLD | NEW |
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" |
17 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
18 #include "content/public/browser/web_contents_view.h" | 19 #include "content/public/browser/web_contents_view.h" |
| 20 #include "extensions/common/manifest_handlers/icons_handler.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 Loading... |
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 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. |
58 if (zoom_bubble_ && | 67 if (zoom_bubble_ && |
59 zoom_bubble_->GetAnchorView() == anchor_view && | 68 zoom_bubble_->GetAnchorView() == anchor_view && |
60 zoom_bubble_->auto_close_ == auto_close) { | 69 zoom_bubble_->auto_close_ == auto_close && |
61 zoom_bubble_->Refresh(); | 70 !extension) { |
| 71 zoom_bubble_->Refresh(); |
62 } else { | 72 } else { |
63 // 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 |
64 // 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 |
65 // current bubble must be closed and a new one created. | 75 // current bubble must be closed and a new one created. |
66 CloseBubble(); | 76 CloseBubble(); |
67 | 77 |
68 zoom_bubble_ = new ZoomBubbleView(anchor_view, | 78 zoom_bubble_ = new ZoomBubbleView(anchor_view, |
69 web_contents, | 79 web_contents, |
70 auto_close, | 80 auto_close, |
71 browser_view->immersive_mode_controller(), | 81 browser_view->immersive_mode_controller(), |
72 browser->fullscreen_controller()); | 82 browser->fullscreen_controller()); |
73 | 83 |
| 84 // If the zoom change was initiated by an extension, capture the relevent |
| 85 // information from it. |
| 86 if (extension) { |
| 87 zoom_bubble_->SetExtensionInfo(extension); |
| 88 } |
| 89 |
74 // 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. |
75 if (!anchor_to_view) { | 91 if (!anchor_to_view) { |
76 zoom_bubble_->set_parent_window( | 92 zoom_bubble_->set_parent_window( |
77 web_contents->GetView()->GetTopLevelNativeWindow()); | 93 web_contents->GetView()->GetTopLevelNativeWindow()); |
78 } | 94 } |
79 | 95 |
80 views::BubbleDelegateView::CreateBubble(zoom_bubble_); | 96 views::BubbleDelegateView::CreateBubble(zoom_bubble_); |
81 | 97 |
82 // 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. |
83 if (is_fullscreen) | 99 if (is_fullscreen) |
(...skipping 24 matching lines...) Expand all Loading... |
108 } | 124 } |
109 | 125 |
110 ZoomBubbleView::ZoomBubbleView( | 126 ZoomBubbleView::ZoomBubbleView( |
111 views::View* anchor_view, | 127 views::View* anchor_view, |
112 content::WebContents* web_contents, | 128 content::WebContents* web_contents, |
113 bool auto_close, | 129 bool auto_close, |
114 ImmersiveModeController* immersive_mode_controller, | 130 ImmersiveModeController* immersive_mode_controller, |
115 FullscreenController* fullscreen_controller) | 131 FullscreenController* fullscreen_controller) |
116 : BubbleDelegateView(anchor_view, anchor_view ? | 132 : BubbleDelegateView(anchor_view, anchor_view ? |
117 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE), | 133 views::BubbleBorder::TOP_RIGHT : views::BubbleBorder::NONE), |
| 134 image_button_(NULL), |
118 label_(NULL), | 135 label_(NULL), |
119 web_contents_(web_contents), | 136 web_contents_(web_contents), |
120 auto_close_(auto_close), | 137 auto_close_(auto_close), |
121 immersive_mode_controller_(immersive_mode_controller) { | 138 immersive_mode_controller_(immersive_mode_controller) { |
122 // 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. |
123 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); | 140 set_anchor_view_insets(gfx::Insets(5, 0, 5, 0)); |
124 set_use_focusless(auto_close); | 141 set_use_focusless(auto_close); |
125 set_notify_enter_exit_on_child(true); | 142 set_notify_enter_exit_on_child(true); |
126 | 143 |
127 // 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 27 matching lines...) Expand all Loading... |
155 int zoom_percent = zoom_controller->zoom_percent(); | 172 int zoom_percent = zoom_controller->zoom_percent(); |
156 label_->SetText( | 173 label_->SetText( |
157 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); | 174 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); |
158 StartTimerIfNecessary(); | 175 StartTimerIfNecessary(); |
159 } | 176 } |
160 | 177 |
161 void ZoomBubbleView::Close() { | 178 void ZoomBubbleView::Close() { |
162 GetWidget()->Close(); | 179 GetWidget()->Close(); |
163 } | 180 } |
164 | 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 extension_info_.icon_image.reset( |
| 191 new extensions::IconImage(web_contents_->GetBrowserContext(), |
| 192 extension, |
| 193 extensions::IconsInfo::GetIcons(extension), |
| 194 16, |
| 195 default_extension_icon_image, |
| 196 this)); |
| 197 } |
| 198 |
165 void ZoomBubbleView::StartTimerIfNecessary() { | 199 void ZoomBubbleView::StartTimerIfNecessary() { |
166 if (auto_close_) { | 200 if (auto_close_) { |
167 if (timer_.IsRunning()) { | 201 if (timer_.IsRunning()) { |
168 timer_.Reset(); | 202 timer_.Reset(); |
169 } else { | 203 } else { |
170 timer_.Start( | 204 timer_.Start( |
171 FROM_HERE, | 205 FROM_HERE, |
172 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), | 206 base::TimeDelta::FromMilliseconds(kBubbleCloseDelay), |
173 this, | 207 this, |
174 &ZoomBubbleView::Close); | 208 &ZoomBubbleView::Close); |
175 } | 209 } |
176 } | 210 } |
177 } | 211 } |
178 | 212 |
179 void ZoomBubbleView::StopTimer() { | 213 void ZoomBubbleView::StopTimer() { |
180 timer_.Stop(); | 214 timer_.Stop(); |
181 } | 215 } |
182 | 216 |
| 217 void ZoomBubbleView::OnExtensionIconImageChanged(extensions::IconImage* image) { |
| 218 DCHECK_EQ(image, extension_info_.icon_image.get()) << |
| 219 "Unknown ImageIcon update."; |
| 220 image_button_->SetImage(views::Button::STATE_NORMAL, |
| 221 &extension_info_.icon_image->image_skia()); |
| 222 image_button_->SchedulePaint(); |
| 223 } |
| 224 |
183 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { | 225 void ZoomBubbleView::OnMouseEntered(const ui::MouseEvent& event) { |
184 set_use_focusless(false); | 226 set_use_focusless(false); |
185 StopTimer(); | 227 StopTimer(); |
186 } | 228 } |
187 | 229 |
188 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { | 230 void ZoomBubbleView::OnMouseExited(const ui::MouseEvent& event) { |
189 set_use_focusless(auto_close_); | 231 set_use_focusless(auto_close_); |
190 StartTimerIfNecessary(); | 232 StartTimerIfNecessary(); |
191 } | 233 } |
192 | 234 |
193 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { | 235 void ZoomBubbleView::OnGestureEvent(ui::GestureEvent* event) { |
194 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || | 236 if (!zoom_bubble_ || !zoom_bubble_->auto_close_ || |
195 event->type() != ui::ET_GESTURE_TAP) { | 237 event->type() != ui::ET_GESTURE_TAP) { |
196 return; | 238 return; |
197 } | 239 } |
198 | 240 |
199 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in | 241 // If an auto-closing bubble was tapped, show a non-auto-closing bubble in |
200 // its place. | 242 // its place. |
201 ShowBubble(zoom_bubble_->web_contents_, false); | 243 ShowBubble(zoom_bubble_->web_contents_, false); |
202 event->SetHandled(); | 244 event->SetHandled(); |
203 } | 245 } |
204 | 246 |
205 void ZoomBubbleView::ButtonPressed(views::Button* sender, | 247 void ZoomBubbleView::ButtonPressed(views::Button* sender, |
206 const ui::Event& event) { | 248 const ui::Event& event) { |
207 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); | 249 if (sender == image_button_) { |
| 250 DCHECK(extension_info_.icon_image.get()) << "Invalid button press."; |
| 251 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); |
| 252 std::string url("chrome://extensions?id="); |
| 253 url += extension_info_.id; |
| 254 chrome::AddSelectedTabWithURL(browser, |
| 255 GURL(url), |
| 256 content::PAGE_TRANSITION_FROM_API); |
| 257 } else { |
| 258 chrome_page_zoom::Zoom(web_contents_, content::PAGE_ZOOM_RESET); |
| 259 } |
208 } | 260 } |
209 | 261 |
210 void ZoomBubbleView::Init() { | 262 void ZoomBubbleView::Init() { |
211 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, | 263 // Set up the layout of the zoom bubble. A grid layout is used because |
212 0, 0, views::kRelatedControlVerticalSpacing)); | 264 // sometimes an extension icon is shown next to the zoom label. |
| 265 views::GridLayout* grid_layout = new views::GridLayout(this); |
| 266 SetLayoutManager(grid_layout); |
| 267 views::ColumnSet* columns = grid_layout->AddColumnSet(0); |
| 268 // First row. |
| 269 if (extension_info_.icon_image.get()) { |
| 270 columns->AddColumn(views::GridLayout::CENTER,views::GridLayout::CENTER, 2, |
| 271 views::GridLayout::USE_PREF, 0, 0); |
| 272 } |
| 273 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 274 views::GridLayout::USE_PREF, 0, 0); |
| 275 grid_layout->StartRow(0, 0); |
213 | 276 |
| 277 // If this zoom change was initiated by an extension, that extension will be |
| 278 // attributed by showing its icon in the zoom bubble. |
| 279 if (extension_info_.icon_image.get()) { |
| 280 image_button_ = new views::ImageButton(this); |
| 281 image_button_->SetTooltipText(l10n_util::GetStringFUTF16( |
| 282 IDS_TOOLTIP_ZOOM_EXTENSION_ICON, |
| 283 base::UTF8ToUTF16(extension_info_.name))); |
| 284 image_button_->SetImage(views::Button::STATE_NORMAL, |
| 285 &extension_info_.icon_image->image_skia()); |
| 286 grid_layout->AddView(image_button_); |
| 287 } |
| 288 |
| 289 // Add zoom label with the new zoom percent. |
214 ZoomController* zoom_controller = | 290 ZoomController* zoom_controller = |
215 ZoomController::FromWebContents(web_contents_); | 291 ZoomController::FromWebContents(web_contents_); |
216 int zoom_percent = zoom_controller->zoom_percent(); | 292 int zoom_percent = zoom_controller->zoom_percent(); |
217 label_ = new views::Label( | 293 label_ = new views::Label( |
218 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); | 294 l10n_util::GetStringFUTF16Int(IDS_TOOLTIP_ZOOM, zoom_percent)); |
219 label_->SetFontList( | 295 label_->SetFontList( |
220 ui::ResourceBundle::GetSharedInstance().GetFontList( | 296 ui::ResourceBundle::GetSharedInstance().GetFontList( |
221 ui::ResourceBundle::MediumFont)); | 297 ui::ResourceBundle::MediumFont)); |
222 AddChildView(label_); | 298 grid_layout->AddView(label_); |
223 | 299 |
| 300 // Second row. |
| 301 grid_layout->AddPaddingRow(0, 8); |
| 302 columns = grid_layout->AddColumnSet(1); |
| 303 columns->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, |
| 304 views::GridLayout::USE_PREF, 0, 0); |
| 305 grid_layout->StartRow(0, 1); |
| 306 |
| 307 // Add "Reset to Default" button. |
224 views::LabelButton* set_default_button = new views::LabelButton( | 308 views::LabelButton* set_default_button = new views::LabelButton( |
225 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); | 309 this, l10n_util::GetStringUTF16(IDS_ZOOM_SET_DEFAULT)); |
226 set_default_button->SetStyle(views::Button::STYLE_BUTTON); | 310 set_default_button->SetStyle(views::Button::STYLE_BUTTON); |
227 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER); | 311 set_default_button->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
228 AddChildView(set_default_button); | 312 grid_layout->AddView(set_default_button); |
229 | 313 |
230 StartTimerIfNecessary(); | 314 StartTimerIfNecessary(); |
231 } | 315 } |
232 | 316 |
233 void ZoomBubbleView::Observe(int type, | 317 void ZoomBubbleView::Observe(int type, |
234 const content::NotificationSource& source, | 318 const content::NotificationSource& source, |
235 const content::NotificationDetails& details) { | 319 const content::NotificationDetails& details) { |
236 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED); | 320 DCHECK_EQ(type, chrome::NOTIFICATION_FULLSCREEN_CHANGED); |
237 CloseBubble(); | 321 CloseBubble(); |
238 } | 322 } |
239 | 323 |
240 void ZoomBubbleView::OnImmersiveRevealStarted() { | 324 void ZoomBubbleView::OnImmersiveRevealStarted() { |
241 CloseBubble(); | 325 CloseBubble(); |
242 } | 326 } |
243 | 327 |
244 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { | 328 void ZoomBubbleView::OnImmersiveModeControllerDestroyed() { |
245 immersive_mode_controller_ = NULL; | 329 immersive_mode_controller_ = NULL; |
246 } | 330 } |
247 | 331 |
248 void ZoomBubbleView::WindowClosing() { | 332 void ZoomBubbleView::WindowClosing() { |
249 // |zoom_bubble_| can be a new bubble by this point (as Close(); doesn't | 333 // |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. | 334 // call this right away). Only set to NULL when it's this bubble. |
251 if (zoom_bubble_ == this) | 335 if (zoom_bubble_ == this) |
252 zoom_bubble_ = NULL; | 336 zoom_bubble_ = NULL; |
253 } | 337 } |
| 338 |
| 339 ZoomBubbleView::ExtensionInfo::ExtensionInfo() {} |
| 340 |
| 341 ZoomBubbleView::ExtensionInfo::~ExtensionInfo() {} |
OLD | NEW |