OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/views/browser_actions_container.h" | 5 #include "chrome/browser/views/browser_actions_container.h" |
6 | 6 |
7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "chrome/browser/extensions/extension_browser_event_router.h" | 11 #include "chrome/browser/extensions/extension_browser_event_router.h" |
12 #include "chrome/browser/extensions/extensions_service.h" | 12 #include "chrome/browser/extensions/extensions_service.h" |
13 #include "chrome/browser/extensions/extension_tabs_module.h" | 13 #include "chrome/browser/extensions/extension_tabs_module.h" |
14 #include "chrome/browser/extensions/image_loading_tracker.h" | 14 #include "chrome/browser/extensions/image_loading_tracker.h" |
15 #include "chrome/browser/profile.h" | 15 #include "chrome/browser/profile.h" |
16 #include "chrome/browser/view_ids.h" | 16 #include "chrome/browser/view_ids.h" |
17 #include "chrome/browser/views/extensions/extension_popup.h" | 17 #include "chrome/browser/views/extensions/extension_popup.h" |
18 #include "chrome/browser/views/toolbar_view.h" | 18 #include "chrome/browser/views/toolbar_view.h" |
19 #include "chrome/common/extensions/extension_action.h" | 19 #include "chrome/common/extensions/extension_action.h" |
20 #include "chrome/common/notification_source.h" | 20 #include "chrome/common/notification_source.h" |
21 #include "chrome/common/notification_type.h" | 21 #include "chrome/common/notification_type.h" |
22 #include "grit/app_resources.h" | 22 #include "grit/app_resources.h" |
23 #include "third_party/skia/include/core/SkBitmap.h" | 23 #include "third_party/skia/include/core/SkBitmap.h" |
24 #include "third_party/skia/include/core/SkTypeface.h" | 24 #include "third_party/skia/include/core/SkTypeface.h" |
25 #include "third_party/skia/include/effects/SkGradientShader.h" | 25 #include "third_party/skia/include/effects/SkGradientShader.h" |
26 #include "views/controls/button/menu_button.h" | 26 #include "views/controls/button/menu_button.h" |
27 #include "views/controls/button/text_button.h" | 27 #include "views/controls/button/text_button.h" |
28 | 28 |
29 // The size of the icon for page actions. | 29 // The size (both dimensions) of the buttons for page actions. |
30 static const int kIconSize = 29; | 30 static const int kButtonSize = 29; |
31 | 31 |
32 // The padding between the browser actions and the omnibox/page menu. | 32 // The padding between the browser actions and the omnibox/page menu. |
33 static const int kHorizontalPadding = 4; | 33 static const int kHorizontalPadding = 4; |
34 | 34 |
35 // This is the same value from toolbar.cc. We position the browser actions | 35 // This is the same value from toolbar.cc. We position the browser actions |
36 // container flush with the edges of the toolbar as a special case so that we | 36 // container flush with the edges of the toolbar as a special case so that we |
37 // can draw the badge outside the visual bounds of the container. | 37 // can draw the badge outside the visual bounds of the container. |
38 static const int kControlVertOffset = 6; | 38 static const int kControlVertOffset = 6; |
39 | 39 |
40 // The maximum of the minimum number of browser actions present when there is | 40 // The maximum of the minimum number of browser actions present when there is |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 | 132 |
133 // Load the images this view needs asynchronously on the file thread. We'll | 133 // Load the images this view needs asynchronously on the file thread. We'll |
134 // get a call back into OnImageLoaded if the image loads successfully. If not, | 134 // get a call back into OnImageLoaded if the image loads successfully. If not, |
135 // the ImageView will have no image and will not appear in the browser chrome. | 135 // the ImageView will have no image and will not appear in the browser chrome. |
136 if (!browser_action->icon_paths().empty()) { | 136 if (!browser_action->icon_paths().empty()) { |
137 const std::vector<std::string>& icon_paths = browser_action->icon_paths(); | 137 const std::vector<std::string>& icon_paths = browser_action->icon_paths(); |
138 browser_action_icons_.resize(icon_paths.size()); | 138 browser_action_icons_.resize(icon_paths.size()); |
139 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); | 139 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); |
140 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); | 140 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); |
141 iter != icon_paths.end(); ++iter) { | 141 iter != icon_paths.end(); ++iter) { |
142 tracker_->PostLoadImageTask(extension->GetResource(*iter)); | 142 tracker_->PostLoadImageTask( |
| 143 extension->GetResource(*iter), |
| 144 gfx::Size(Extension::kBrowserActionIconMaxSize, |
| 145 Extension::kBrowserActionIconMaxSize)); |
143 } | 146 } |
144 } | 147 } |
145 | 148 |
146 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 149 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
147 Source<ExtensionAction>(browser_action_)); | 150 Source<ExtensionAction>(browser_action_)); |
148 } | 151 } |
149 | 152 |
150 BrowserActionButton::~BrowserActionButton() { | 153 BrowserActionButton::~BrowserActionButton() { |
151 if (tracker_) { | 154 if (tracker_) { |
152 tracker_->StopTrackingImageLoad(); | 155 tracker_->StopTrackingImageLoad(); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 }; | 285 }; |
283 | 286 |
284 BrowserActionView::BrowserActionView(ExtensionAction* browser_action, | 287 BrowserActionView::BrowserActionView(ExtensionAction* browser_action, |
285 Extension* extension, | 288 Extension* extension, |
286 BrowserActionsContainer* panel) { | 289 BrowserActionsContainer* panel) { |
287 button_ = new BrowserActionButton(browser_action, extension, panel); | 290 button_ = new BrowserActionButton(browser_action, extension, panel); |
288 AddChildView(button_); | 291 AddChildView(button_); |
289 } | 292 } |
290 | 293 |
291 void BrowserActionView::Layout() { | 294 void BrowserActionView::Layout() { |
292 button_->SetBounds(0, kControlVertOffset, width(), | 295 button_->SetBounds(0, kControlVertOffset, width(), kButtonSize); |
293 height() - 2 * kControlVertOffset); | |
294 } | 296 } |
295 | 297 |
296 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 298 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
297 View::PaintChildren(canvas); | 299 View::PaintChildren(canvas); |
298 | 300 |
299 const std::string& text = button_->browser_action_state()->badge_text(); | 301 const std::string& text = button_->browser_action_state()->badge_text(); |
300 if (text.empty()) | 302 if (text.empty()) |
301 return; | 303 return; |
302 | 304 |
303 const int kTextSize = 8; | 305 const int kTextSize = 8; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 | 490 |
489 // Otherwise, we send the action to the extension. | 491 // Otherwise, we send the action to the extension. |
490 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( | 492 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( |
491 profile_, browser_action.extension_id(), toolbar_->browser()); | 493 profile_, browser_action.extension_id(), toolbar_->browser()); |
492 } | 494 } |
493 | 495 |
494 gfx::Size BrowserActionsContainer::GetPreferredSize() { | 496 gfx::Size BrowserActionsContainer::GetPreferredSize() { |
495 if (browser_action_views_.empty()) | 497 if (browser_action_views_.empty()) |
496 return gfx::Size(0, 0); | 498 return gfx::Size(0, 0); |
497 int width = kHorizontalPadding * 2 + | 499 int width = kHorizontalPadding * 2 + |
498 browser_action_views_.size() * kIconSize; | 500 browser_action_views_.size() * kButtonSize; |
499 return gfx::Size(width, kIconSize); | 501 return gfx::Size(width, kButtonSize); |
500 } | 502 } |
501 | 503 |
502 void BrowserActionsContainer::Layout() { | 504 void BrowserActionsContainer::Layout() { |
503 for (size_t i = 0; i < browser_action_views_.size(); ++i) { | 505 for (size_t i = 0; i < browser_action_views_.size(); ++i) { |
504 BrowserActionView* view = browser_action_views_[i]; | 506 BrowserActionView* view = browser_action_views_[i]; |
505 int x = kHorizontalPadding + i * kIconSize; | 507 int x = kHorizontalPadding + i * kButtonSize; |
506 if (x + kIconSize <= width()) { | 508 if (x + kButtonSize <= width()) { |
507 view->SetBounds(x, 0, kIconSize, height()); | 509 view->SetBounds(x, 0, kButtonSize, height()); |
508 view->SetVisible(true); | 510 view->SetVisible(true); |
509 } else { | 511 } else { |
510 view->SetVisible(false); | 512 view->SetVisible(false); |
511 } | 513 } |
512 } | 514 } |
513 } | 515 } |
514 | 516 |
515 void BrowserActionsContainer::Observe(NotificationType type, | 517 void BrowserActionsContainer::Observe(NotificationType type, |
516 const NotificationSource& source, | 518 const NotificationSource& source, |
517 const NotificationDetails& details) { | 519 const NotificationDetails& details) { |
(...skipping 29 matching lines...) Expand all Loading... |
547 task_factory_.NewRunnableMethod(&BrowserActionsContainer::HidePopup)); | 549 task_factory_.NewRunnableMethod(&BrowserActionsContainer::HidePopup)); |
548 } | 550 } |
549 | 551 |
550 int BrowserActionsContainer::GetClippedPreferredWidth(int available_width) { | 552 int BrowserActionsContainer::GetClippedPreferredWidth(int available_width) { |
551 if (browser_action_views_.size() == 0) | 553 if (browser_action_views_.size() == 0) |
552 return 0; | 554 return 0; |
553 | 555 |
554 // We have at least one browser action. Make some of them sticky. | 556 // We have at least one browser action. Make some of them sticky. |
555 int min_width = kHorizontalPadding * 2 + | 557 int min_width = kHorizontalPadding * 2 + |
556 std::min(static_cast<int>(browser_action_views_.size()), | 558 std::min(static_cast<int>(browser_action_views_.size()), |
557 kMinimumNumberOfVisibleBrowserActions) * kIconSize; | 559 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; |
558 | 560 |
559 // Even if available_width is <= 0, we still return at least the |min_width|. | 561 // Even if available_width is <= 0, we still return at least the |min_width|. |
560 if (available_width <= 0) | 562 if (available_width <= 0) |
561 return min_width; | 563 return min_width; |
562 | 564 |
563 return std::max(min_width, available_width - available_width % kIconSize + | 565 return std::max(min_width, available_width - available_width % kButtonSize + |
564 kHorizontalPadding * 2); | 566 kHorizontalPadding * 2); |
565 } | 567 } |
OLD | NEW |