| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "app/slide_animation.h" | 9 #include "app/slide_animation.h" |
| 10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 69 |
| 70 // The padding above and below the divider. | 70 // The padding above and below the divider. |
| 71 static const int kDividerVerticalPadding = 9; | 71 static const int kDividerVerticalPadding = 9; |
| 72 | 72 |
| 73 // The margin above the chevron. | 73 // The margin above the chevron. |
| 74 static const int kChevronTopMargin = 9; | 74 static const int kChevronTopMargin = 9; |
| 75 | 75 |
| 76 // The margin to the right of the chevron. | 76 // The margin to the right of the chevron. |
| 77 static const int kChevronRightMargin = 4; | 77 static const int kChevronRightMargin = 4; |
| 78 | 78 |
| 79 // Extra hit-area for the resize gripper. | 79 // Width for the resize area. |
| 80 static const int kExtraResizeArea = 4; | 80 static const int kResizeAreaWidth = 4; |
| 81 | 81 |
| 82 // Width of the drop indicator. | 82 // Width of the drop indicator. |
| 83 static const int kDropIndicatorWidth = 2; | 83 static const int kDropIndicatorWidth = 2; |
| 84 | 84 |
| 85 // Color of the drop indicator. | 85 // Color of the drop indicator. |
| 86 static const SkColor kDropIndicatorColor = SK_ColorBLACK; | 86 static const SkColor kDropIndicatorColor = SK_ColorBLACK; |
| 87 | 87 |
| 88 // The x offset for the drop indicator (how much we shift it by). | 88 // The x offset for the drop indicator (how much we shift it by). |
| 89 static const int kDropIndicatorOffsetLtr = 3; | 89 static const int kDropIndicatorOffsetLtr = 3; |
| 90 static const int kDropIndicatorOffsetRtl = 9; | 90 static const int kDropIndicatorOffsetRtl = 9; |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 // BrowserActionsContainer | 361 // BrowserActionsContainer |
| 362 | 362 |
| 363 BrowserActionsContainer::BrowserActionsContainer( | 363 BrowserActionsContainer::BrowserActionsContainer( |
| 364 Browser* browser, View* owner_view) | 364 Browser* browser, View* owner_view) |
| 365 : profile_(browser->profile()), | 365 : profile_(browser->profile()), |
| 366 browser_(browser), | 366 browser_(browser), |
| 367 owner_view_(owner_view), | 367 owner_view_(owner_view), |
| 368 popup_(NULL), | 368 popup_(NULL), |
| 369 popup_button_(NULL), | 369 popup_button_(NULL), |
| 370 model_(NULL), | 370 model_(NULL), |
| 371 resize_gripper_(NULL), | |
| 372 chevron_(NULL), | 371 chevron_(NULL), |
| 373 overflow_menu_(NULL), | 372 overflow_menu_(NULL), |
| 374 suppress_chevron_(false), | 373 suppress_chevron_(false), |
| 375 resize_amount_(0), | 374 resize_amount_(0), |
| 376 animation_target_size_(0), | 375 animation_target_size_(0), |
| 377 drop_indicator_position_(-1), | 376 drop_indicator_position_(-1), |
| 378 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), | 377 ALLOW_THIS_IN_INITIALIZER_LIST(task_factory_(this)), |
| 379 ALLOW_THIS_IN_INITIALIZER_LIST(show_menu_task_factory_(this)) { | 378 ALLOW_THIS_IN_INITIALIZER_LIST(show_menu_task_factory_(this)) { |
| 380 SetID(VIEW_ID_BROWSER_ACTION_TOOLBAR); | 379 SetID(VIEW_ID_BROWSER_ACTION_TOOLBAR); |
| 381 | 380 |
| 382 if (profile_->GetExtensionsService()) { | 381 if (profile_->GetExtensionsService()) { |
| 383 model_ = profile_->GetExtensionsService()->toolbar_model(); | 382 model_ = profile_->GetExtensionsService()->toolbar_model(); |
| 384 model_->AddObserver(this); | 383 model_->AddObserver(this); |
| 385 } | 384 } |
| 386 resize_animation_.reset(new SlideAnimation(this)); | 385 resize_animation_.reset(new SlideAnimation(this)); |
| 387 resize_gripper_ = new views::ResizeGripper(this); | 386 resize_area_ = new views::ResizeArea(this); |
| 388 resize_gripper_->SetAccessibleName( | 387 resize_area_->SetAccessibleName(l10n_util::GetString(IDS_ACCNAME_SEPARATOR)); |
| 389 l10n_util::GetString(IDS_ACCNAME_SEPARATOR)); | 388 AddChildView(resize_area_); |
| 390 resize_gripper_->SetVisible(false); | |
| 391 AddChildView(resize_gripper_); | |
| 392 | 389 |
| 393 // TODO(glen): Come up with a new bitmap for the chevron. | 390 // TODO(glen): Come up with a new bitmap for the chevron. |
| 394 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 391 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 395 SkBitmap* chevron_image = rb.GetBitmapNamed(IDR_BOOKMARK_BAR_CHEVRONS); | 392 SkBitmap* chevron_image = rb.GetBitmapNamed(IDR_BOOKMARK_BAR_CHEVRONS); |
| 396 chevron_ = new views::MenuButton(NULL, std::wstring(), this, false); | 393 chevron_ = new views::MenuButton(NULL, std::wstring(), this, false); |
| 397 chevron_->SetVisible(false); | 394 chevron_->SetVisible(false); |
| 398 chevron_->SetIcon(*chevron_image); | 395 chevron_->SetIcon(*chevron_image); |
| 399 chevron_->SetAccessibleName( | 396 chevron_->SetAccessibleName( |
| 400 l10n_util::GetString(IDS_ACCNAME_EXTENSIONS_CHEVRON)); | 397 l10n_util::GetString(IDS_ACCNAME_EXTENSIONS_CHEVRON)); |
| 401 // Chevron contains >> that should point left in LTR locales. | 398 // Chevron contains >> that should point left in LTR locales. |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 for (size_t i = 0; i < browser_action_views_.size(); ++i) | 511 for (size_t i = 0; i < browser_action_views_.size(); ++i) |
| 515 RemoveChildView(browser_action_views_[i]); | 512 RemoveChildView(browser_action_views_[i]); |
| 516 STLDeleteContainerPointers(browser_action_views_.begin(), | 513 STLDeleteContainerPointers(browser_action_views_.begin(), |
| 517 browser_action_views_.end()); | 514 browser_action_views_.end()); |
| 518 browser_action_views_.clear(); | 515 browser_action_views_.clear(); |
| 519 } | 516 } |
| 520 } | 517 } |
| 521 | 518 |
| 522 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { | 519 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { |
| 523 SetVisible(browser_action_views_.size() > 0); | 520 SetVisible(browser_action_views_.size() > 0); |
| 524 resize_gripper_->SetVisible(browser_action_views_.size() > 0); | |
| 525 | |
| 526 owner_view_->Layout(); | 521 owner_view_->Layout(); |
| 527 owner_view_->SchedulePaint(); | 522 owner_view_->SchedulePaint(); |
| 528 } | 523 } |
| 529 | 524 |
| 530 void BrowserActionsContainer::HidePopup() { | 525 void BrowserActionsContainer::HidePopup() { |
| 531 if (popup_) | 526 if (popup_) |
| 532 popup_->Close(); | 527 popup_->Close(); |
| 533 } | 528 } |
| 534 | 529 |
| 535 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { | 530 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 611 return gfx::Size(width, kButtonSize); | 606 return gfx::Size(width, kButtonSize); |
| 612 } | 607 } |
| 613 | 608 |
| 614 void BrowserActionsContainer::Layout() { | 609 void BrowserActionsContainer::Layout() { |
| 615 // The parent can be visible, but collapsed. In this case we don't | 610 // The parent can be visible, but collapsed. In this case we don't |
| 616 // want the browser action container to be visible. | 611 // want the browser action container to be visible. |
| 617 ToolbarView* parent = reinterpret_cast<ToolbarView*>(GetParent()); | 612 ToolbarView* parent = reinterpret_cast<ToolbarView*>(GetParent()); |
| 618 | 613 |
| 619 if (browser_action_views_.size() == 0 || parent->collapsed()) { | 614 if (browser_action_views_.size() == 0 || parent->collapsed()) { |
| 620 SetVisible(false); | 615 SetVisible(false); |
| 621 resize_gripper_->SetVisible(false); | |
| 622 chevron_->SetVisible(false); | 616 chevron_->SetVisible(false); |
| 623 return; | 617 return; |
| 624 } else { | 618 } else { |
| 625 SetVisible(true); | 619 SetVisible(true); |
| 626 resize_gripper_->SetVisible(true); | |
| 627 } | 620 } |
| 628 | 621 |
| 629 int x = 0; | 622 resize_area_->SetBounds(0, 0, kResizeAreaWidth, height()); |
| 630 if (resize_gripper_->IsVisible()) { | 623 int x = kResizeAreaWidth; |
| 631 // We'll draw the resize gripper a little wider, to add some invisible hit | |
| 632 // target area - but we don't account for it anywhere. | |
| 633 gfx::Size sz = resize_gripper_->GetPreferredSize(); | |
| 634 resize_gripper_->SetBounds(x, (height() - sz.height()) / 2 + 1, | |
| 635 sz.width() + kExtraResizeArea, sz.height()); | |
| 636 x += sz.width(); | |
| 637 } | |
| 638 | 624 |
| 639 x += base::i18n::IsRTL() ? kHorizontalPaddingRtl : kHorizontalPadding; | 625 x += base::i18n::IsRTL() ? kHorizontalPaddingRtl : kHorizontalPadding; |
| 640 | 626 |
| 641 // Calculate if all icons fit without showing the chevron. We need to know | 627 // Calculate if all icons fit without showing the chevron. We need to know |
| 642 // this beforehand, because showing the chevron will decrease the space that | 628 // this beforehand, because showing the chevron will decrease the space that |
| 643 // we have to draw the visible ones (ie. if one icon is visible and another | 629 // we have to draw the visible ones (ie. if one icon is visible and another |
| 644 // doesn't have enough room). | 630 // doesn't have enough room). |
| 645 int last_x_of_icons = x + | 631 int last_x_of_icons = x + |
| 646 (browser_action_views_.size() * kButtonSize) + | 632 (browser_action_views_.size() * kButtonSize) + |
| 647 ((browser_action_views_.size() - 1) * | 633 ((browser_action_views_.size() - 1) * |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 int pixelWidth, bool allow_shrink_to_minimum) const { | 882 int pixelWidth, bool allow_shrink_to_minimum) const { |
| 897 // Calculate the width of one icon. | 883 // Calculate the width of one icon. |
| 898 int icon_width = (kButtonSize + kBrowserActionButtonPadding); | 884 int icon_width = (kButtonSize + kBrowserActionButtonPadding); |
| 899 | 885 |
| 900 // Calculate pixel count for the area not used by the icons. | 886 // Calculate pixel count for the area not used by the icons. |
| 901 int extras = WidthOfNonIconArea(); | 887 int extras = WidthOfNonIconArea(); |
| 902 | 888 |
| 903 size_t icon_count = 0u; | 889 size_t icon_count = 0u; |
| 904 if (pixelWidth >= 0) { | 890 if (pixelWidth >= 0) { |
| 905 // Caller wants to know how many icons fit within a given space so we start | 891 // Caller wants to know how many icons fit within a given space so we start |
| 906 // by subtracting the padding, gripper and dividers. | 892 // by subtracting the padding, resize area and dividers. |
| 907 int icon_area = pixelWidth - extras; | 893 int icon_area = pixelWidth - extras; |
| 908 icon_area = std::max(0, icon_area); | 894 icon_area = std::max(0, icon_area); |
| 909 | 895 |
| 910 // Make sure we never throw an icon into the chevron menu just because | 896 // Make sure we never throw an icon into the chevron menu just because |
| 911 // there isn't enough enough space for the invisible padding around buttons. | 897 // there isn't enough enough space for the invisible padding around buttons. |
| 912 icon_area += kBrowserActionButtonPadding - 1; | 898 icon_area += kBrowserActionButtonPadding - 1; |
| 913 | 899 |
| 914 // Count the number of icons that fit within that area. | 900 // Count the number of icons that fit within that area. |
| 915 icon_count = icon_area / icon_width; | 901 icon_count = icon_area / icon_width; |
| 916 | 902 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1045 else | 1031 else |
| 1046 chevron_->SetVisible(true); | 1032 chevron_->SetVisible(true); |
| 1047 container_size_ = gfx::Size(IconCountToWidth(visible_actions), kButtonSize); | 1033 container_size_ = gfx::Size(IconCountToWidth(visible_actions), kButtonSize); |
| 1048 } | 1034 } |
| 1049 | 1035 |
| 1050 int BrowserActionsContainer::WidthOfNonIconArea() const { | 1036 int BrowserActionsContainer::WidthOfNonIconArea() const { |
| 1051 int chevron_size = (chevron_->IsVisible()) ? | 1037 int chevron_size = (chevron_->IsVisible()) ? |
| 1052 chevron_->GetPreferredSize().width() : 0; | 1038 chevron_->GetPreferredSize().width() : 0; |
| 1053 int padding = base::i18n::IsRTL() ? | 1039 int padding = base::i18n::IsRTL() ? |
| 1054 kHorizontalPaddingRtl : kHorizontalPadding; | 1040 kHorizontalPaddingRtl : kHorizontalPadding; |
| 1055 return resize_gripper_->GetPreferredSize().width() + padding + | 1041 return kResizeAreaWidth + padding + chevron_size + kChevronRightMargin + |
| 1056 chevron_size + kChevronRightMargin + kDividerHorizontalMargin; | 1042 kDividerHorizontalMargin; |
| 1057 } | 1043 } |
| 1058 | 1044 |
| 1059 int BrowserActionsContainer::IconCountToWidth(int icons) const { | 1045 int BrowserActionsContainer::IconCountToWidth(int icons) const { |
| 1060 DCHECK_GE(icons, 0); | 1046 DCHECK_GE(icons, 0); |
| 1061 if (icons == 0) | 1047 if (icons == 0) |
| 1062 return ContainerMinSize(); | 1048 return ContainerMinSize(); |
| 1063 | 1049 |
| 1064 int icon_width = kButtonSize + kBrowserActionButtonPadding; | 1050 int icon_width = kButtonSize + kBrowserActionButtonPadding; |
| 1065 | 1051 |
| 1066 return WidthOfNonIconArea() + (icons * icon_width); | 1052 return WidthOfNonIconArea() + (icons * icon_width); |
| 1067 } | 1053 } |
| 1068 | 1054 |
| 1069 int BrowserActionsContainer::ContainerMinSize() const { | 1055 int BrowserActionsContainer::ContainerMinSize() const { |
| 1070 return resize_gripper_->width() + chevron_->width() + kChevronRightMargin; | 1056 return kResizeAreaWidth + chevron_->width() + kChevronRightMargin; |
| 1071 } | 1057 } |
| 1072 | 1058 |
| 1073 void BrowserActionsContainer::Animate(Tween::Type tween_type, int target_size) { | 1059 void BrowserActionsContainer::Animate(Tween::Type tween_type, int target_size) { |
| 1074 if (!disable_animations_during_testing_) { | 1060 if (!disable_animations_during_testing_) { |
| 1075 // Animate! We have to set the animation_target_size_ after calling Reset(), | 1061 // Animate! We have to set the animation_target_size_ after calling Reset(), |
| 1076 // because that could end up calling AnimationEnded which clears the value. | 1062 // because that could end up calling AnimationEnded which clears the value. |
| 1077 resize_animation_->Reset(); | 1063 resize_animation_->Reset(); |
| 1078 resize_animation_->SetTweenType(tween_type); | 1064 resize_animation_->SetTweenType(tween_type); |
| 1079 animation_target_size_ = target_size; | 1065 animation_target_size_ = target_size; |
| 1080 resize_animation_->Show(); | 1066 resize_animation_->Show(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1162 popup_ = NULL; | 1148 popup_ = NULL; |
| 1163 popup_button_->SetButtonNotPushed(); | 1149 popup_button_->SetButtonNotPushed(); |
| 1164 popup_button_ = NULL; | 1150 popup_button_ = NULL; |
| 1165 } | 1151 } |
| 1166 | 1152 |
| 1167 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { | 1153 bool BrowserActionsContainer::ShouldDisplayBrowserAction(Extension* extension) { |
| 1168 // Only display incognito-enabled extensions while in incognito mode. | 1154 // Only display incognito-enabled extensions while in incognito mode. |
| 1169 return (!profile_->IsOffTheRecord() || | 1155 return (!profile_->IsOffTheRecord() || |
| 1170 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); | 1156 profile_->GetExtensionsService()->IsIncognitoEnabled(extension)); |
| 1171 } | 1157 } |
| OLD | NEW |