| 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/toolbar/toolbar_actions_model.h" | 5 #include "chrome/browser/ui/toolbar/toolbar_actions_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | 226 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
| 227 // hides the browser action and then disables and enables the extension. | 227 // hides the browser action and then disables and enables the extension. |
| 228 if (!HasItem(ToolbarItem(extension->id(), EXTENSION_ACTION))) | 228 if (!HasItem(ToolbarItem(extension->id(), EXTENSION_ACTION))) |
| 229 AddExtension(extension); | 229 AddExtension(extension); |
| 230 } | 230 } |
| 231 | 231 |
| 232 void ToolbarActionsModel::OnExtensionUnloaded( | 232 void ToolbarActionsModel::OnExtensionUnloaded( |
| 233 content::BrowserContext* browser_context, | 233 content::BrowserContext* browser_context, |
| 234 const extensions::Extension* extension, | 234 const extensions::Extension* extension, |
| 235 extensions::UnloadedExtensionInfo::Reason reason) { | 235 extensions::UnloadedExtensionInfo::Reason reason) { |
| 236 size_t index = 0u; | |
| 237 while (toolbar_items().size() > index && | |
| 238 toolbar_items()[index].id != extension->id()) | |
| 239 ++index; | |
| 240 bool was_visible_and_has_overflow = | 236 bool was_visible_and_has_overflow = |
| 241 index < visible_icon_count() && !all_icons_visible(); | 237 IsActionVisible(extension->id()) && !all_icons_visible(); |
| 242 RemoveExtension(extension); | 238 RemoveExtension(extension); |
| 243 // If the extension was previously visible and there are overflowed | 239 // If the extension was previously visible and there are overflowed |
| 244 // extensions, and this extension is being uninstalled, we reduce the visible | 240 // extensions, and this extension is being uninstalled, we reduce the visible |
| 245 // count so that we don't pop out a previously-hidden extension. | 241 // count so that we don't pop out a previously-hidden extension. |
| 246 if (was_visible_and_has_overflow && | 242 if (was_visible_and_has_overflow && |
| 247 reason == extensions::UnloadedExtensionInfo::REASON_UNINSTALL) | 243 reason == extensions::UnloadedExtensionInfo::REASON_UNINSTALL) |
| 248 SetVisibleIconCount(visible_icon_count() - 1); | 244 SetVisibleIconCount(visible_icon_count() - 1); |
| 249 } | 245 } |
| 250 | 246 |
| 251 void ToolbarActionsModel::OnExtensionUninstalled( | 247 void ToolbarActionsModel::OnExtensionUninstalled( |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 } | 637 } |
| 642 | 638 |
| 643 DCHECK(use_redesign_); | 639 DCHECK(use_redesign_); |
| 644 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 640 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 645 DCHECK(!HasItem(component_item)); | 641 DCHECK(!HasItem(component_item)); |
| 646 AddItem(component_item); | 642 AddItem(component_item); |
| 647 } | 643 } |
| 648 | 644 |
| 649 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { | 645 void ToolbarActionsModel::RemoveComponentAction(const std::string& action_id) { |
| 650 DCHECK(use_redesign_); | 646 DCHECK(use_redesign_); |
| 647 // If the action was visible and there are overflowed actions, we reduce the |
| 648 // visible count so that we don't pop out a previously-hidden action. |
| 649 if (IsActionVisible(action_id) && !all_icons_visible()) |
| 650 SetVisibleIconCount(visible_icon_count() - 1); |
| 651 |
| 651 ToolbarItem component_item(action_id, COMPONENT_ACTION); | 652 ToolbarItem component_item(action_id, COMPONENT_ACTION); |
| 652 DCHECK(HasItem(component_item)); | 653 DCHECK(HasItem(component_item)); |
| 653 RemoveItem(component_item); | 654 RemoveItem(component_item); |
| 654 RemovePref(component_item); | 655 RemovePref(component_item); |
| 655 } | 656 } |
| 656 | 657 |
| 657 void ToolbarActionsModel::IncognitoPopulate() { | 658 void ToolbarActionsModel::IncognitoPopulate() { |
| 658 DCHECK(profile_->IsOffTheRecord()); | 659 DCHECK(profile_->IsOffTheRecord()); |
| 659 const ToolbarActionsModel* original_model = | 660 const ToolbarActionsModel* original_model = |
| 660 ToolbarActionsModel::Get(profile_->GetOriginalProfile()); | 661 ToolbarActionsModel::Get(profile_->GetOriginalProfile()); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 802 for (const std::string& action_id : ids) { | 803 for (const std::string& action_id : ids) { |
| 803 for (const ToolbarItem& item : toolbar_items_) { | 804 for (const ToolbarItem& item : toolbar_items_) { |
| 804 if (action_id == item.id) | 805 if (action_id == item.id) |
| 805 highlighted_items_.push_back(item); | 806 highlighted_items_.push_back(item); |
| 806 } | 807 } |
| 807 } | 808 } |
| 808 | 809 |
| 809 // If we have any items in |highlighted_items_|, then we entered highlighting | 810 // If we have any items in |highlighted_items_|, then we entered highlighting |
| 810 // mode. | 811 // mode. |
| 811 if (highlighted_items_.size()) { | 812 if (highlighted_items_.size()) { |
| 812 // It's important that is_highlighting_ is changed immediately before the | 813 // It's important that |highlight_type_| is changed immediately before the |
| 813 // observers are notified since it changes the result of toolbar_items(). | 814 // observers are notified since it changes the result of toolbar_items(). |
| 814 highlight_type_ = highlight_type; | 815 highlight_type_ = highlight_type; |
| 815 for (Observer& observer : observers_) | 816 for (Observer& observer : observers_) |
| 816 observer.OnToolbarHighlightModeChanged(true); | 817 observer.OnToolbarHighlightModeChanged(true); |
| 817 | 818 |
| 818 // We set the visible icon count after the highlight mode change because | 819 // We set the visible icon count after the highlight mode change because |
| 819 // the UI actions are created/destroyed during highlight, and doing that | 820 // the UI actions are created/destroyed during highlight, and doing that |
| 820 // prior to changing the size allows us to still have smooth animations. | 821 // prior to changing the size allows us to still have smooth animations. |
| 821 if (visible_icon_count() < ids.size()) | 822 if (visible_icon_count() < ids.size()) |
| 822 SetVisibleIconCount(ids.size()); | 823 SetVisibleIconCount(ids.size()); |
| 823 | 824 |
| 824 return true; | 825 return true; |
| 825 } | 826 } |
| 826 | 827 |
| 827 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if | 828 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if |
| 828 // we were otherwise in it). | 829 // we were otherwise in it). |
| 829 if (is_highlighting()) | 830 if (is_highlighting()) |
| 830 StopHighlighting(); | 831 StopHighlighting(); |
| 831 return false; | 832 return false; |
| 832 } | 833 } |
| 833 | 834 |
| 834 void ToolbarActionsModel::StopHighlighting() { | 835 void ToolbarActionsModel::StopHighlighting() { |
| 835 if (is_highlighting()) { | 836 if (is_highlighting()) { |
| 836 // It's important that is_highlighting_ is changed immediately before the | 837 // It's important that |highlight_type_| is changed immediately before the |
| 837 // observers are notified since it changes the result of toolbar_items(). | 838 // observers are notified since it changes the result of toolbar_items(). |
| 838 highlight_type_ = HIGHLIGHT_NONE; | 839 highlight_type_ = HIGHLIGHT_NONE; |
| 839 for (Observer& observer : observers_) | 840 for (Observer& observer : observers_) |
| 840 observer.OnToolbarHighlightModeChanged(false); | 841 observer.OnToolbarHighlightModeChanged(false); |
| 841 | 842 |
| 842 // For the same reason, we don't clear highlighted_items_ until after the | 843 // For the same reason, we don't clear highlighted_items_ until after the |
| 843 // mode changed. | 844 // mode changed. |
| 844 highlighted_items_.clear(); | 845 highlighted_items_.clear(); |
| 845 | 846 |
| 846 // We set the visible icon count after the highlight mode change because | 847 // We set the visible icon count after the highlight mode change because |
| 847 // the UI actions are created/destroyed during highlight, and doing that | 848 // the UI actions are created/destroyed during highlight, and doing that |
| 848 // prior to changing the size allows us to still have smooth animations. | 849 // prior to changing the size allows us to still have smooth animations. |
| 849 int saved_icon_count = | 850 int saved_icon_count = |
| 850 prefs_->GetInteger(extensions::pref_names::kToolbarSize); | 851 prefs_->GetInteger(extensions::pref_names::kToolbarSize); |
| 851 if (saved_icon_count != visible_icon_count_) | 852 if (saved_icon_count != visible_icon_count_) |
| 852 SetVisibleIconCount(saved_icon_count); | 853 SetVisibleIconCount(saved_icon_count); |
| 853 } | 854 } |
| 854 } | 855 } |
| 855 | 856 |
| 856 const extensions::Extension* ToolbarActionsModel::GetExtensionById( | 857 const extensions::Extension* ToolbarActionsModel::GetExtensionById( |
| 857 const std::string& id) const { | 858 const std::string& id) const { |
| 858 return extension_registry_->enabled_extensions().GetByID(id); | 859 return extension_registry_->enabled_extensions().GetByID(id); |
| 859 } | 860 } |
| 861 |
| 862 bool ToolbarActionsModel::IsActionVisible(const std::string& action_id) const { |
| 863 size_t index = 0u; |
| 864 while (toolbar_items().size() > index && |
| 865 toolbar_items()[index].id != action_id) |
| 866 ++index; |
| 867 return index < visible_icon_count(); |
| 868 } |
| OLD | NEW |