| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #ifndef CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ | 5 #ifndef CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ |
| 6 #define CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ | 6 #define CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/scoped_vector.h" | 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/scoped_observer.h" | 10 #include "base/scoped_observer.h" |
| 11 #include "chrome/browser/extensions/extension_toolbar_model.h" | 11 #include "chrome/browser/extensions/extension_toolbar_model.h" |
| 12 #include "chrome/browser/ui/tabs/tab_strip_model_observer.h" | |
| 13 #include "ui/gfx/animation/tween.h" | 12 #include "ui/gfx/animation/tween.h" |
| 14 #include "ui/gfx/geometry/size.h" | 13 #include "ui/gfx/geometry/size.h" |
| 15 | 14 |
| 16 namespace extensions { | 15 namespace extensions { |
| 17 class Extension; | 16 class Extension; |
| 18 } | 17 } |
| 19 | 18 |
| 20 class ToolbarActionsBarDelegate; | 19 class ToolbarActionsBarDelegate; |
| 21 class ToolbarActionViewController; | 20 class ToolbarActionViewController; |
| 22 | 21 |
| 23 // A platform-independent version of the container for toolbar actions, | 22 // A platform-independent version of the container for toolbar actions, |
| 24 // including extension actions and component actions. | 23 // including extension actions and component actions. |
| 25 // This class manages the order of the actions, the actions' state, and owns the | 24 // This class manages the order of the actions, the actions' state, and owns the |
| 26 // action controllers, in addition to (for extensions) interfacing with the | 25 // action controllers, in addition to (for extensions) interfacing with the |
| 27 // extension toolbar model. Further, it manages dimensions for the bar, | 26 // extension toolbar model. Further, it manages dimensions for the bar, |
| 28 // excluding animations. | 27 // excluding animations. |
| 29 // This can come in two flavors, main and "overflow". The main bar is visible | 28 // This can come in two flavors, main and "overflow". The main bar is visible |
| 30 // next to the omnibox, and the overflow bar is visible inside the chrome | 29 // next to the omnibox, and the overflow bar is visible inside the chrome |
| 31 // (fka wrench) menu. The main bar can have only a single row of icons with | 30 // (fka wrench) menu. The main bar can have only a single row of icons with |
| 32 // flexible width, whereas the overflow bar has multiple rows of icons with a | 31 // flexible width, whereas the overflow bar has multiple rows of icons with a |
| 33 // fixed width (the width of the menu). | 32 // fixed width (the width of the menu). |
| 34 class ToolbarActionsBar : public extensions::ExtensionToolbarModel::Observer, | 33 class ToolbarActionsBar : public extensions::ExtensionToolbarModel::Observer { |
| 35 public TabStripModelObserver { | |
| 36 public: | 34 public: |
| 37 // A struct to contain the platform settings. | 35 // A struct to contain the platform settings. |
| 38 struct PlatformSettings { | 36 struct PlatformSettings { |
| 39 explicit PlatformSettings(bool in_overflow_mode); | 37 explicit PlatformSettings(bool in_overflow_mode); |
| 40 | 38 |
| 41 // The padding that comes before the first icon in the container. | 39 // The padding that comes before the first icon in the container. |
| 42 int left_padding; | 40 int left_padding; |
| 43 // The padding following the final icon in the container. | 41 // The padding following the final icon in the container. |
| 44 int right_padding; | 42 int right_padding; |
| 45 // The spacing between each of the icons. | 43 // The spacing between each of the icons. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 56 // The icon was dragged to the same container it started in. | 54 // The icon was dragged to the same container it started in. |
| 57 DRAG_TO_SAME, | 55 DRAG_TO_SAME, |
| 58 // The icon was dragged from the main container to the overflow. | 56 // The icon was dragged from the main container to the overflow. |
| 59 DRAG_TO_OVERFLOW, | 57 DRAG_TO_OVERFLOW, |
| 60 // The icon was dragged from the overflow container to the main. | 58 // The icon was dragged from the overflow container to the main. |
| 61 DRAG_TO_MAIN, | 59 DRAG_TO_MAIN, |
| 62 }; | 60 }; |
| 63 | 61 |
| 64 ToolbarActionsBar(ToolbarActionsBarDelegate* delegate, | 62 ToolbarActionsBar(ToolbarActionsBarDelegate* delegate, |
| 65 Browser* browser, | 63 Browser* browser, |
| 66 bool in_overflow_mode); | 64 ToolbarActionsBar* main_bar); |
| 67 ~ToolbarActionsBar() override; | 65 ~ToolbarActionsBar() override; |
| 68 | 66 |
| 69 // Returns the width of a browser action icon, optionally including the | 67 // Returns the width of a browser action icon, optionally including the |
| 70 // following padding. | 68 // following padding. |
| 71 static int IconWidth(bool include_padding); | 69 static int IconWidth(bool include_padding); |
| 72 | 70 |
| 73 // Returns the height of a browser action icon. | 71 // Returns the height of a browser action icon. |
| 74 static int IconHeight(); | 72 static int IconHeight(); |
| 75 | 73 |
| 76 // Returns the preferred size for the toolbar; this does *not* reflect any | 74 // Returns the preferred size for the toolbar; this does *not* reflect any |
| (...skipping 26 matching lines...) Expand all Loading... |
| 103 void SetOverflowRowWidth(int width); | 101 void SetOverflowRowWidth(int width); |
| 104 | 102 |
| 105 // Notifies the ToolbarActionsBar that a user completed a resize gesture, and | 103 // Notifies the ToolbarActionsBar that a user completed a resize gesture, and |
| 106 // the new width is |width|. | 104 // the new width is |width|. |
| 107 void OnResizeComplete(int width); | 105 void OnResizeComplete(int width); |
| 108 | 106 |
| 109 // Notifies the ToolbarActionsBar that a user completed a drag and drop event, | 107 // Notifies the ToolbarActionsBar that a user completed a drag and drop event, |
| 110 // and dragged the view from |dragged_index| to |dropped_index|. | 108 // and dragged the view from |dragged_index| to |dropped_index|. |
| 111 // |drag_type| indicates whether or not the icon was dragged between the | 109 // |drag_type| indicates whether or not the icon was dragged between the |
| 112 // overflow and main containers. | 110 // overflow and main containers. |
| 111 // The main container should handle all drag/drop notifications. |
| 113 void OnDragDrop(int dragged_index, | 112 void OnDragDrop(int dragged_index, |
| 114 int dropped_index, | 113 int dropped_index, |
| 115 DragType drag_type); | 114 DragType drag_type); |
| 116 | 115 |
| 117 const std::vector<ToolbarActionViewController*>& toolbar_actions() { | 116 const std::vector<ToolbarActionViewController*>& toolbar_actions() const { |
| 118 return toolbar_actions_.get(); | 117 return toolbar_actions_.get(); |
| 119 } | 118 } |
| 120 bool enabled() const { return model_ != nullptr; } | 119 bool enabled() const { return model_ != nullptr; } |
| 121 bool suppress_layout() const { return suppress_layout_; } | 120 bool suppress_layout() const { return suppress_layout_; } |
| 122 bool suppress_animation() const { | 121 bool suppress_animation() const { |
| 123 return suppress_animation_ || disable_animations_for_testing_; | 122 return suppress_animation_ || disable_animations_for_testing_; |
| 124 } | 123 } |
| 125 bool is_highlighting() const { return model_ && model_->is_highlighting(); } | 124 bool is_highlighting() const { return model_ && model_->is_highlighting(); } |
| 126 const PlatformSettings& platform_settings() const { | 125 const PlatformSettings& platform_settings() const { |
| 127 return platform_settings_; | 126 return platform_settings_; |
| 128 } | 127 } |
| 129 | 128 |
| 130 ToolbarActionsBarDelegate* delegate_for_test() { return delegate_; } | 129 ToolbarActionsBarDelegate* delegate_for_test() { return delegate_; } |
| 131 | 130 |
| 131 static void set_pop_out_actions_to_run_for_testing( |
| 132 bool pop_out_actions_to_run) { |
| 133 pop_out_actions_to_run_ = pop_out_actions_to_run; |
| 134 } |
| 135 |
| 132 // During testing we can disable animations by setting this flag to true, | 136 // During testing we can disable animations by setting this flag to true, |
| 133 // so that the bar resizes instantly, instead of having to poll it while it | 137 // so that the bar resizes instantly, instead of having to poll it while it |
| 134 // animates to open/closed status. | 138 // animates to open/closed status. |
| 135 static bool disable_animations_for_testing_; | 139 static bool disable_animations_for_testing_; |
| 136 | 140 |
| 137 private: | 141 private: |
| 142 class TabOrderHelper; |
| 143 |
| 138 using ToolbarActions = ScopedVector<ToolbarActionViewController>; | 144 using ToolbarActions = ScopedVector<ToolbarActionViewController>; |
| 139 | 145 |
| 140 // ExtensionToolbarModel::Observer: | 146 // ExtensionToolbarModel::Observer: |
| 141 void ToolbarExtensionAdded(const extensions::Extension* extension, | 147 void ToolbarExtensionAdded(const extensions::Extension* extension, |
| 142 int index) override; | 148 int index) override; |
| 143 void ToolbarExtensionRemoved(const extensions::Extension* extension) override; | 149 void ToolbarExtensionRemoved(const extensions::Extension* extension) override; |
| 144 void ToolbarExtensionMoved(const extensions::Extension* extension, | 150 void ToolbarExtensionMoved(const extensions::Extension* extension, |
| 145 int index) override; | 151 int index) override; |
| 146 void ToolbarExtensionUpdated(const extensions::Extension* extension) override; | 152 void ToolbarExtensionUpdated(const extensions::Extension* extension) override; |
| 147 bool ShowExtensionActionPopup(const extensions::Extension* extension, | 153 bool ShowExtensionActionPopup(const extensions::Extension* extension, |
| 148 bool grant_active_tab) override; | 154 bool grant_active_tab) override; |
| 149 void ToolbarVisibleCountChanged() override; | 155 void ToolbarVisibleCountChanged() override; |
| 150 void ToolbarHighlightModeChanged(bool is_highlighting) override; | 156 void ToolbarHighlightModeChanged(bool is_highlighting) override; |
| 151 void OnToolbarModelInitialized() override; | 157 void OnToolbarModelInitialized() override; |
| 152 Browser* GetBrowser() override; | 158 Browser* GetBrowser() override; |
| 153 | 159 |
| 154 // TabStripModelObserver: | |
| 155 void TabInsertedAt(content::WebContents* web_contents, | |
| 156 int index, | |
| 157 bool foreground) override; | |
| 158 void TabDetachedAt(content::WebContents* web_contents, | |
| 159 int index) override; | |
| 160 void TabStripModelDeleted() override; | |
| 161 | |
| 162 // Resizes the delegate (if necessary) to the preferred size using the given | 160 // Resizes the delegate (if necessary) to the preferred size using the given |
| 163 // |tween_type| and optionally suppressing the chevron. | 161 // |tween_type| and optionally suppressing the chevron. |
| 164 void ResizeDelegate(gfx::Tween::Type tween_type, bool suppress_chevron); | 162 void ResizeDelegate(gfx::Tween::Type tween_type, bool suppress_chevron); |
| 165 | 163 |
| 166 // Returns the action for the given |id|, if one exists. | 164 // Returns the action for the given |id|, if one exists. |
| 167 ToolbarActionViewController* GetActionForId(const std::string& id); | 165 ToolbarActionViewController* GetActionForId(const std::string& id); |
| 168 | 166 |
| 169 // Returns the current web contents. | 167 // Returns the current web contents. |
| 170 content::WebContents* GetCurrentWebContents(); | 168 content::WebContents* GetCurrentWebContents(); |
| 171 | 169 |
| 172 // Reorders the toolbar actions to reflect any that need to pop out. | 170 // Reorders the toolbar actions to reflect the model and, optionally, to |
| 171 // "pop out" any overflowed actions that want to run (depending on the |
| 172 // value of |pop_out_actions_to_run|. |
| 173 void ReorderActions(); | 173 void ReorderActions(); |
| 174 | 174 |
| 175 // Copies the action order from the main bar (only called for the overflow). |
| 176 void CopyActionOrder(); |
| 177 |
| 178 bool in_overflow_mode() const { return main_bar_ != nullptr; } |
| 179 |
| 175 // The delegate for this object (in a real build, this is the view). | 180 // The delegate for this object (in a real build, this is the view). |
| 176 ToolbarActionsBarDelegate* delegate_; | 181 ToolbarActionsBarDelegate* delegate_; |
| 177 | 182 |
| 178 // The associated browser. | 183 // The associated browser. |
| 179 Browser* browser_; | 184 Browser* browser_; |
| 180 | 185 |
| 181 // The observed toolbar model. | 186 // The observed toolbar model. |
| 182 extensions::ExtensionToolbarModel* model_; | 187 extensions::ExtensionToolbarModel* model_; |
| 183 | 188 |
| 184 // True if this bar is for the overflow menu. | 189 // The controller for the main toolbar actions bar. This will be null if this |
| 185 bool in_overflow_mode_; | 190 // is the main bar. |
| 191 ToolbarActionsBar* main_bar_; |
| 192 |
| 193 // The controller for the overflow toolbar actions bar; this can be null |
| 194 // even if this is the main bar, since the overflow is not always open. |
| 195 ToolbarActionsBar* overflow_bar_; |
| 186 | 196 |
| 187 // Platform-specific settings for dimensions and the overflow chevron. | 197 // Platform-specific settings for dimensions and the overflow chevron. |
| 188 PlatformSettings platform_settings_; | 198 PlatformSettings platform_settings_; |
| 189 | 199 |
| 190 // The toolbar actions. | 200 // The toolbar actions. |
| 191 ToolbarActions toolbar_actions_; | 201 ToolbarActions toolbar_actions_; |
| 192 | 202 |
| 193 // The set of tabs for the given action (the key) is currently "popped out". | 203 // The TabOrderHelper that manages popping out actions that want to act. |
| 194 // "Popped out" actions are those that were in the overflow menu normally, but | 204 // This is only non-null if |pop_out_actions_to_run| is true. |
| 195 // want to run and are moved to the main bar so the user can see them. | 205 scoped_ptr<TabOrderHelper> tab_order_helper_; |
| 196 std::map<ToolbarActionViewController*, std::set<int>> popped_out_in_tabs_; | |
| 197 | |
| 198 // The set of tab ids that have been checked for whether actions need to be | |
| 199 // popped out or not. | |
| 200 std::set<int> tabs_checked_for_pop_out_; | |
| 201 | 206 |
| 202 ScopedObserver<extensions::ExtensionToolbarModel, | 207 ScopedObserver<extensions::ExtensionToolbarModel, |
| 203 extensions::ExtensionToolbarModel::Observer> model_observer_; | 208 extensions::ExtensionToolbarModel::Observer> model_observer_; |
| 204 | 209 |
| 205 ScopedObserver<TabStripModel, TabStripModelObserver> tab_strip_observer_; | |
| 206 | |
| 207 // True if we should suppress layout, such as when we are creating or | 210 // True if we should suppress layout, such as when we are creating or |
| 208 // adjusting a lot of actions at once. | 211 // adjusting a lot of actions at once. |
| 209 bool suppress_layout_; | 212 bool suppress_layout_; |
| 210 | 213 |
| 211 // True if we should suppress animation; we do this when first creating the | 214 // True if we should suppress animation; we do this when first creating the |
| 212 // toolbar, and also when switching tabs changes the state of the icons. | 215 // toolbar, and also when switching tabs changes the state of the icons. |
| 213 bool suppress_animation_; | 216 bool suppress_animation_; |
| 214 | 217 |
| 218 // If this is true, actions that want to run (e.g., an extension's page |
| 219 // action) will pop out of overflow to draw more attention. |
| 220 // See also TabOrderHelper in the .cc file. |
| 221 static bool pop_out_actions_to_run_; |
| 222 |
| 215 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar); | 223 DISALLOW_COPY_AND_ASSIGN(ToolbarActionsBar); |
| 216 }; | 224 }; |
| 217 | 225 |
| 218 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ | 226 #endif // CHROME_BROWSER_UI_TOOLBAR_TOOLBAR_ACTIONS_BAR_H_ |
| OLD | NEW |