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

Side by Side Diff: chrome/browser/ui/views/toolbar/browser_actions_container.cc

Issue 476873002: Make hiding an extension action cause it to go to the overflow menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/toolbar/browser_actions_container.h" 5 #include "chrome/browser/ui/views/toolbar/browser_actions_container.h"
6 6
7 #include "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "chrome/browser/extensions/extension_action_manager.h" 9 #include "chrome/browser/extensions/extension_action_manager.h"
10 #include "chrome/browser/extensions/extension_util.h" 10 #include "chrome/browser/extensions/extension_util.h"
(...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 BrowserActionsContainerObserver* observer) { 311 BrowserActionsContainerObserver* observer) {
312 observers_.AddObserver(observer); 312 observers_.AddObserver(observer);
313 } 313 }
314 314
315 void BrowserActionsContainer::RemoveObserver( 315 void BrowserActionsContainer::RemoveObserver(
316 BrowserActionsContainerObserver* observer) { 316 BrowserActionsContainerObserver* observer) {
317 observers_.RemoveObserver(observer); 317 observers_.RemoveObserver(observer);
318 } 318 }
319 319
320 gfx::Size BrowserActionsContainer::GetPreferredSize() const { 320 gfx::Size BrowserActionsContainer::GetPreferredSize() const {
321 size_t icon_count = browser_action_views_.size() - 321 size_t icon_count = GetIconCount();
322 (in_overflow_mode() ? main_container_->VisibleBrowserActions() : 0);
323 322
324 // If there are no actions to show, or we are in overflow mode and the main 323 // If there are no actions to show, or we are in overflow mode and the main
325 // container is already showing them all, then no further work is required. 324 // container is already showing them all, then no further work is required.
326 if (icon_count == 0) 325 if (icon_count == 0)
327 return gfx::Size(); 326 return gfx::Size();
328 327
329 if (in_overflow_mode()) { 328 if (in_overflow_mode()) {
330 // When in overflow, y is multiline, so the pixel count is IconHeight() 329 // When in overflow, y is multiline, so the pixel count is IconHeight()
331 // times the number of rows needed. 330 // times the number of rows needed.
332 return gfx::Size( 331 return gfx::Size(
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
617 OnBrowserActionVisibilityChanged(); 616 OnBrowserActionVisibilityChanged();
618 return; 617 return;
619 } 618 }
620 619
621 // Up until now we've only been modifying the resize_amount, but now it is 620 // Up until now we've only been modifying the resize_amount, but now it is
622 // time to set the container size to the size we have resized to, and then 621 // time to set the container size to the size we have resized to, and then
623 // animate to the nearest icon count size if necessary (which may be 0). 622 // animate to the nearest icon count size if necessary (which may be 0).
624 int max_width = IconCountToWidth(-1, false); 623 int max_width = IconCountToWidth(-1, false);
625 container_width_ = 624 container_width_ =
626 std::min(std::max(0, container_width_ - resize_amount), max_width); 625 std::min(std::max(0, container_width_ - resize_amount), max_width);
627 SaveDesiredSizeAndAnimate(gfx::Tween::EASE_OUT, 626
628 WidthToIconCount(container_width_)); 627 // Save off the desired number of visible icons. We do this now instead of at
628 // the end of the animation so that even if the browser is shut down while
629 // animating, the right value will be restored on next run.
630 // NOTE: Don't save the icon count in incognito because there may be fewer
631 // icons in that mode. The result is that the container in a normal window is
632 // always at least as wide as in an incognito window.
633 int visible_icons = WidthToIconCount(container_width_);
634 if (!profile_->IsOffTheRecord())
635 model_->SetVisibleIconCount(visible_icons);
636
637 Animate(gfx::Tween::EASE_OUT, visible_icons);
629 } 638 }
630 639
631 void BrowserActionsContainer::AnimationProgressed( 640 void BrowserActionsContainer::AnimationProgressed(
632 const gfx::Animation* animation) { 641 const gfx::Animation* animation) {
633 DCHECK_EQ(resize_animation_.get(), animation); 642 DCHECK_EQ(resize_animation_.get(), animation);
634 resize_amount_ = static_cast<int>(resize_animation_->GetCurrentValue() * 643 resize_amount_ = static_cast<int>(resize_animation_->GetCurrentValue() *
635 (container_width_ - animation_target_size_)); 644 (container_width_ - animation_target_size_));
636 OnBrowserActionVisibilityChanged(); 645 OnBrowserActionVisibilityChanged();
637 } 646 }
638 647
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
789 DCHECK(browser_action_views_[i]->extension() != extension) << 798 DCHECK(browser_action_views_[i]->extension() != extension) <<
790 "Asked to add a browser action view for an extension that already " 799 "Asked to add a browser action view for an extension that already "
791 "exists."; 800 "exists.";
792 } 801 }
793 #endif 802 #endif
794 CloseOverflowMenu(); 803 CloseOverflowMenu();
795 804
796 if (!ShouldDisplayBrowserAction(extension)) 805 if (!ShouldDisplayBrowserAction(extension))
797 return; 806 return;
798 807
799 size_t visible_actions = VisibleBrowserActionsAfterAnimation();
800
801 // Add the new browser action to the vector and the view hierarchy. 808 // Add the new browser action to the vector and the view hierarchy.
802 if (profile_->IsOffTheRecord()) 809 if (profile_->IsOffTheRecord())
803 index = model_->OriginalIndexToIncognito(index); 810 index = model_->OriginalIndexToIncognito(index);
804 BrowserActionView* view = 811 BrowserActionView* view =
805 new BrowserActionView(extension, 812 new BrowserActionView(extension,
806 GetExtensionAction(*extension, profile_), 813 GetExtensionAction(*extension, profile_),
807 browser_, 814 browser_,
808 this); 815 this);
809 browser_action_views_.insert(browser_action_views_.begin() + index, view); 816 browser_action_views_.insert(browser_action_views_.begin() + index, view);
810 AddChildViewAt(view, index); 817 AddChildViewAt(view, index);
811 818
812 // If we are still initializing the container, don't bother animating. 819 // If we are still initializing the container, don't bother animating.
813 if (!model_->extensions_initialized()) 820 if (!model_->extensions_initialized())
814 return; 821 return;
815 822
816 // Enlarge the container if it was already at maximum size and we're not in 823 // Enlarge the container if it was already at maximum size and we're not in
817 // the middle of upgrading. 824 // the middle of upgrading.
818 if ((model_->GetVisibleIconCount() < 0) && 825 if ((model_->GetVisibleIconCount() < 0) &&
819 !extensions::ExtensionSystem::Get(profile_)->runtime_data()-> 826 !extensions::ExtensionSystem::Get(profile_)->runtime_data()->
820 IsBeingUpgraded(extension)) { 827 IsBeingUpgraded(extension)) {
821 suppress_chevron_ = true; 828 suppress_chevron_ = true;
822 SaveDesiredSizeAndAnimate(gfx::Tween::LINEAR, visible_actions + 1); 829 Animate(gfx::Tween::LINEAR, browser_action_views_.size());
823 } else { 830 } else {
824 // Just redraw the (possibly modified) visible icon set. 831 // Just redraw the (possibly modified) visible icon set.
825 OnBrowserActionVisibilityChanged(); 832 OnBrowserActionVisibilityChanged();
826 } 833 }
827 } 834 }
828 835
829 void BrowserActionsContainer::ToolbarExtensionRemoved( 836 void BrowserActionsContainer::ToolbarExtensionRemoved(
830 const Extension* extension) { 837 const Extension* extension) {
831 CloseOverflowMenu(); 838 CloseOverflowMenu();
832 839
(...skipping 14 matching lines...) Expand all
847 // If we have more icons than we can show, then we must not be changing 854 // If we have more icons than we can show, then we must not be changing
848 // the container size (since we either removed an icon from the main 855 // the container size (since we either removed an icon from the main
849 // area and one from the overflow list will have shifted in, or we 856 // area and one from the overflow list will have shifted in, or we
850 // removed an entry directly from the overflow list). 857 // removed an entry directly from the overflow list).
851 OnBrowserActionVisibilityChanged(); 858 OnBrowserActionVisibilityChanged();
852 } else { 859 } else {
853 // Either we went from overflow to no-overflow, or we shrunk the no- 860 // Either we went from overflow to no-overflow, or we shrunk the no-
854 // overflow container by 1. Either way the size changed, so animate. 861 // overflow container by 1. Either way the size changed, so animate.
855 if (chevron_) 862 if (chevron_)
856 chevron_->SetVisible(false); 863 chevron_->SetVisible(false);
857 SaveDesiredSizeAndAnimate(gfx::Tween::EASE_OUT, 864 Animate(gfx::Tween::EASE_OUT, browser_action_views_.size());
Finnur 2014/08/21 10:47:51 I can't say for sure if this is correct. I suspect
Devlin 2014/08/21 13:56:30 Sorry; I didn't explain very well. I'm fairly cer
Finnur 2014/08/21 14:08:30 No, I would agree that there's no need to save the
Devlin 2014/08/21 15:24:59 Ah yes, sorry, width. :) (that should teach me to
858 browser_action_views_.size());
859 } 865 }
860 return; // We have found the action to remove, bail out. 866 return; // We have found the action to remove, bail out.
861 } 867 }
862 } 868 }
863 } 869 }
864 870
865 void BrowserActionsContainer::ToolbarExtensionMoved(const Extension* extension, 871 void BrowserActionsContainer::ToolbarExtensionMoved(const Extension* extension,
866 int index) { 872 int index) {
867 if (!ShouldDisplayBrowserAction(extension)) 873 if (!ShouldDisplayBrowserAction(extension))
868 return; 874 return;
(...skipping 30 matching lines...) Expand all
899 if (view) 905 if (view)
900 view->UpdateState(); 906 view->UpdateState();
901 } 907 }
902 908
903 bool BrowserActionsContainer::ShowExtensionActionPopup( 909 bool BrowserActionsContainer::ShowExtensionActionPopup(
904 const Extension* extension) { 910 const Extension* extension) {
905 return ShowPopupForExtension(extension, false, false); 911 return ShowPopupForExtension(extension, false, false);
906 } 912 }
907 913
908 void BrowserActionsContainer::ToolbarVisibleCountChanged() { 914 void BrowserActionsContainer::ToolbarVisibleCountChanged() {
915 int old_container_width = container_width_;
909 SetContainerWidth(); 916 SetContainerWidth();
917 if (old_container_width != container_width_)
918 Animate(gfx::Tween::EASE_OUT, GetIconCount());
Finnur 2014/08/21 10:47:51 I am unclear on this one too...
Devlin 2014/08/21 13:56:30 Here, we just say "Animate to the number of icons
910 } 919 }
911 920
912 void BrowserActionsContainer::ToolbarHighlightModeChanged( 921 void BrowserActionsContainer::ToolbarHighlightModeChanged(
913 bool is_highlighting) { 922 bool is_highlighting) {
914 // The visual highlighting is done in OnPaint(). It's a bit of a pain that 923 // The visual highlighting is done in OnPaint(). It's a bit of a pain that
915 // we delete and recreate everything here, but given everything else going on 924 // we delete and recreate everything here, but given everything else going on
916 // (the lack of highlight, n more extensions appearing, etc), it's not worth 925 // (the lack of highlight, n more extensions appearing, etc), it's not worth
917 // the extra complexity to create and insert only the new extensions. 926 // the extra complexity to create and insert only the new extensions.
918 DeleteBrowserActionViews(); 927 DeleteBrowserActionViews();
919 CreateBrowserActionViews(); 928 CreateBrowserActionViews();
920 SaveDesiredSizeAndAnimate(gfx::Tween::LINEAR, browser_action_views_.size()); 929 Animate(gfx::Tween::LINEAR, browser_action_views_.size());
Finnur 2014/08/21 10:47:50 This is probably a good change, since if the brows
921 } 930 }
922 931
923 void BrowserActionsContainer::LoadImages() { 932 void BrowserActionsContainer::LoadImages() {
924 ui::ThemeProvider* tp = GetThemeProvider(); 933 ui::ThemeProvider* tp = GetThemeProvider();
925 if (!tp || !chevron_) 934 if (!tp || !chevron_)
926 return; 935 return;
927 936
928 chevron_->SetImage(views::Button::STATE_NORMAL, 937 chevron_->SetImage(views::Button::STATE_NORMAL,
929 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW)); 938 *tp->GetImageSkiaNamed(IDR_BROWSER_ACTIONS_OVERFLOW));
930 939
931 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT); 940 const int kImages[] = IMAGE_GRID(IDR_DEVELOPER_MODE_HIGHLIGHT);
932 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages)); 941 highlight_painter_.reset(views::Painter::CreateImageGridPainter(kImages));
933 } 942 }
934 943
935 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() { 944 void BrowserActionsContainer::OnBrowserActionVisibilityChanged() {
936 SetVisible(!browser_action_views_.empty()); 945 SetVisible(!browser_action_views_.empty());
937 if (owner_view_) { 946 if (owner_view_) {
938 owner_view_->Layout(); 947 owner_view_->Layout();
939 owner_view_->SchedulePaint(); 948 owner_view_->SchedulePaint();
940 } 949 }
941 } 950 }
942 951
943 void BrowserActionsContainer::SetContainerWidth() { 952 void BrowserActionsContainer::SetContainerWidth() {
944 // The slave only draws the overflow (what isn't visible in the other 953 int visible_actions = GetIconCount();
945 // container).
946 int visible_actions = in_overflow_mode() ?
947 model_->toolbar_items().size() - model_->GetVisibleIconCount() :
948 model_->GetVisibleIconCount();
949 if (visible_actions < 0) // All icons should be visible.
950 visible_actions = model_->toolbar_items().size();
951 if (chevron_) { 954 if (chevron_) {
952 chevron_->SetVisible( 955 chevron_->SetVisible(
953 static_cast<size_t>(visible_actions) < model_->toolbar_items().size()); 956 static_cast<size_t>(visible_actions) < model_->toolbar_items().size());
954 } 957 }
955 container_width_ = 958 container_width_ =
956 IconCountToWidth(visible_actions, chevron_ && chevron_->visible()); 959 IconCountToWidth(visible_actions, chevron_ && chevron_->visible());
957 } 960 }
958 961
959 void BrowserActionsContainer::CloseOverflowMenu() { 962 void BrowserActionsContainer::CloseOverflowMenu() {
960 if (overflow_menu_) 963 if (overflow_menu_)
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
1017 std::max(0, available_space + kItemSpacing) / IconWidth(true)); 1020 std::max(0, available_space + kItemSpacing) / IconWidth(true));
1018 } 1021 }
1019 1022
1020 int BrowserActionsContainer::MinimumNonemptyWidth() const { 1023 int BrowserActionsContainer::MinimumNonemptyWidth() const {
1021 if (!chevron_) 1024 if (!chevron_)
1022 return ToolbarView::kStandardSpacing; 1025 return ToolbarView::kStandardSpacing;
1023 return (ToolbarView::kStandardSpacing * 2) + kChevronSpacing + 1026 return (ToolbarView::kStandardSpacing * 2) + kChevronSpacing +
1024 chevron_->GetPreferredSize().width(); 1027 chevron_->GetPreferredSize().width();
1025 } 1028 }
1026 1029
1027 void BrowserActionsContainer::SaveDesiredSizeAndAnimate( 1030 void BrowserActionsContainer::Animate(gfx::Tween::Type tween_type,
1028 gfx::Tween::Type tween_type, 1031 size_t num_visible_icons) {
1029 size_t num_visible_icons) {
1030 // Save off the desired number of visible icons. We do this now instead of at
1031 // the end of the animation so that even if the browser is shut down while
1032 // animating, the right value will be restored on next run.
1033 // NOTE: Don't save the icon count in incognito because there may be fewer
1034 // icons in that mode. The result is that the container in a normal window is
1035 // always at least as wide as in an incognito window.
1036 if (!profile_->IsOffTheRecord())
1037 model_->SetVisibleIconCount(num_visible_icons);
Devlin 2014/08/21 00:06:29 Had to move this to OnResize because otherwise the
1038 int target_size = IconCountToWidth(num_visible_icons, 1032 int target_size = IconCountToWidth(num_visible_icons,
1039 num_visible_icons < browser_action_views_.size()); 1033 num_visible_icons < browser_action_views_.size());
1040 if (resize_animation_ && !disable_animations_during_testing_) { 1034 if (resize_animation_ && !disable_animations_during_testing_) {
1041 // Animate! We have to set the animation_target_size_ after calling Reset(), 1035 // Animate! We have to set the animation_target_size_ after calling Reset(),
1042 // because that could end up calling AnimationEnded which clears the value. 1036 // because that could end up calling AnimationEnded which clears the value.
1043 resize_animation_->Reset(); 1037 resize_animation_->Reset();
1044 resize_animation_->SetTweenType(tween_type); 1038 resize_animation_->SetTweenType(tween_type);
1045 animation_target_size_ = target_size; 1039 animation_target_size_ = target_size;
1046 resize_animation_->Show(); 1040 resize_animation_->Show();
1047 } else { 1041 } else {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 BrowserActionView* BrowserActionsContainer::GetViewForExtension( 1074 BrowserActionView* BrowserActionsContainer::GetViewForExtension(
1081 const Extension* extension) { 1075 const Extension* extension) {
1082 for (BrowserActionViews::iterator view = browser_action_views_.begin(); 1076 for (BrowserActionViews::iterator view = browser_action_views_.begin();
1083 view != browser_action_views_.end(); ++view) { 1077 view != browser_action_views_.end(); ++view) {
1084 if ((*view)->extension() == extension) 1078 if ((*view)->extension() == extension)
1085 return *view; 1079 return *view;
1086 } 1080 }
1087 1081
1088 return NULL; 1082 return NULL;
1089 } 1083 }
1084
1085 size_t BrowserActionsContainer::GetIconCount() const {
1086 int visible_icons = model_->GetVisibleIconCount();
1087 if (visible_icons == -1)
1088 visible_icons = model_->toolbar_items().size();
1089 return in_overflow_mode() ?
1090 model_->toolbar_items().size() - visible_icons : visible_icons;
1091 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698