| OLD | NEW |
| 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 #ifndef ASH_SHELF_SHELF_MODEL_H_ | 5 #ifndef ASH_SHELF_SHELF_MODEL_H_ |
| 6 #define ASH_SHELF_SHELF_MODEL_H_ | 6 #define ASH_SHELF_SHELF_MODEL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "ash/ash_export.h" | 11 #include "ash/ash_export.h" |
| 12 #include "ash/public/cpp/shelf_item.h" | 12 #include "ash/public/cpp/shelf_item.h" |
| 13 #include "ash/public/interfaces/shelf.mojom.h" | 13 #include "ash/public/interfaces/shelf.mojom.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 | 16 |
| 17 namespace ash { | 17 namespace ash { |
| 18 | 18 |
| 19 class ShelfItemDelegate; | 19 class ShelfItemDelegate; |
| 20 class ShelfModelObserver; | 20 class ShelfModelObserver; |
| 21 | 21 |
| 22 // Model used for shelf items. Owns ShelfItemDelegates but does not create them. | 22 // Model used for shelf items. Owns ShelfItemDelegates but does not create them. |
| 23 // TODO(msw): Remove id conversion functions; check for item presence as needed. | 23 // TODO(msw): Remove id conversion functions; check for item presence as needed. |
| 24 class ASH_EXPORT ShelfModel { | 24 class ASH_EXPORT ShelfModel { |
| 25 public: | 25 public: |
| 26 ShelfModel(); | 26 ShelfModel(); |
| 27 ~ShelfModel(); | 27 ~ShelfModel(); |
| 28 | 28 |
| 29 // Get the shelf ID from an application ID. Returns an empty ShelfID if the | |
| 30 // app id is unknown to the model, or has no associated ShelfID. | |
| 31 ShelfID GetShelfIDForAppID(const std::string& app_id); | |
| 32 | |
| 33 // Get the shelf ID from an application ID and a launch ID. | |
| 34 // The launch ID can be passed to an app when launched in order to support | |
| 35 // multiple shelf items per app. This id is used together with the app_id to | |
| 36 // uniquely identify each shelf item that has the same app_id. | |
| 37 // For example, a single virtualization app might want to show different | |
| 38 // shelf icons for different remote apps. Returns an empty ShelfID() if the | |
| 39 // app id is unknown to the model, or has no associated ShelfID. | |
| 40 ShelfID GetShelfIDForAppIDAndLaunchID(const std::string& app_id, | |
| 41 const std::string& launch_id); | |
| 42 | |
| 43 // Get the application ID for a given shelf ID. Returns an empty string for | |
| 44 // an unknown or invalid ShelfID. | |
| 45 const std::string& GetAppIDForShelfID(const ShelfID& id); | |
| 46 | |
| 47 // Pins an app with |app_id| to shelf. A running instance will get pinned. | 29 // Pins an app with |app_id| to shelf. A running instance will get pinned. |
| 48 // In case there is no running instance a new shelf item is created and | 30 // In case there is no running instance a new shelf item is created and |
| 49 // pinned. | 31 // pinned. |
| 50 void PinAppWithID(const std::string& app_id); | 32 void PinAppWithID(const std::string& app_id); |
| 51 | 33 |
| 52 // Check if the app with |app_id_| is pinned to the shelf. | 34 // Check if the app with |app_id_| is pinned to the shelf. |
| 53 bool IsAppPinned(const std::string& app_id); | 35 bool IsAppPinned(const std::string& app_id); |
| 54 | 36 |
| 55 // Unpins app item with |app_id|. | 37 // Unpins app item with |app_id|. |
| 56 void UnpinAppWithID(const std::string& app_id); | 38 void UnpinAppWithID(const std::string& app_id); |
| 57 | 39 |
| 58 // Cleans up the ShelfItemDelegates. | 40 // Cleans up the ShelfItemDelegates. |
| 59 void DestroyItemDelegates(); | 41 void DestroyItemDelegates(); |
| 60 | 42 |
| 61 // Adds a new item to the model. Returns the resulting index. | 43 // Adds a new item to the model. Returns the resulting index. |
| 62 int Add(const ShelfItem& item); | 44 int Add(const ShelfItem& item); |
| 63 | 45 |
| 64 // Adds the item. |index| is the requested insertion index, which may be | 46 // Adds the item. |index| is the requested insertion index, which may be |
| 65 // modified to meet type-based ordering. Returns the actual insertion index. | 47 // modified to meet type-based ordering. Returns the actual insertion index. |
| 66 int AddAt(int index, const ShelfItem& item); | 48 int AddAt(int index, const ShelfItem& item); |
| 67 | 49 |
| 68 // Removes the item at |index|. | 50 // Removes the item at |index|. |
| 69 void RemoveItemAt(int index); | 51 void RemoveItemAt(int index); |
| 70 | 52 |
| 71 // Moves the item at |index| to |target_index|. |target_index| is in terms | 53 // Moves the item at |index| to |target_index|. |target_index| is in terms |
| 72 // of the model *after* the item at |index| is removed. | 54 // of the model *after* the item at |index| is removed. |
| 73 void Move(int index, int target_index); | 55 void Move(int index, int target_index); |
| 74 | 56 |
| 75 // Resets the item at the specified index. The item maintains its existing | 57 // Resets the item at the specified index. The item's id should not change. |
| 76 // id and type. | |
| 77 void Set(int index, const ShelfItem& item); | 58 void Set(int index, const ShelfItem& item); |
| 78 | 59 |
| 79 // Returns the index of the item by id. | 60 // Returns the index of the item with id |shelf_id|, or -1 if none exists. |
| 80 int ItemIndexByID(const ShelfID& id) const; | 61 int ItemIndexByID(const ShelfID& shelf_id) const; |
| 81 | 62 |
| 82 // Returns the |index| of the item matching |type| in |items_|. | 63 // Returns the |index| of the item matching |type| in |items_|. |
| 83 // Returns -1 if the matching item is not found. | 64 // Returns -1 if the matching item is not found. |
| 84 // Note: Requires a linear search. | |
| 85 int GetItemIndexForType(ShelfItemType type); | 65 int GetItemIndexForType(ShelfItemType type); |
| 86 | 66 |
| 87 // Returns the index of the first running application or the index where the | 67 // Returns the index of the first running application or the index where the |
| 88 // first running application would go if there are no running (non pinned) | 68 // first running application would go if there are no running (non pinned) |
| 89 // applications yet. | 69 // applications yet. |
| 90 int FirstRunningAppIndex() const; | 70 int FirstRunningAppIndex() const; |
| 91 | 71 |
| 92 // Returns the index of the first panel or the index where the first panel | 72 // Returns the index of the first panel or the index where the first panel |
| 93 // would go if there are no panels. | 73 // would go if there are no panels. |
| 94 int FirstPanelIndex() const; | 74 int FirstPanelIndex() const; |
| 95 | 75 |
| 96 // Returns an iterator into items() for the item with the specified id, or | 76 // Returns an iterator into items() for the item with the specified id, or |
| 97 // items().end() if there is no item with the specified id. | 77 // items().end() if there is no item with the specified id. |
| 98 ShelfItems::const_iterator ItemByID(const ShelfID& id) const; | 78 ShelfItems::const_iterator ItemByID(const ShelfID& shelf_id) const; |
| 99 | 79 |
| 100 const ShelfItems& items() const { return items_; } | 80 const ShelfItems& items() const { return items_; } |
| 101 int item_count() const { return static_cast<int>(items_.size()); } | 81 int item_count() const { return static_cast<int>(items_.size()); } |
| 102 | 82 |
| 103 // Set |item_delegate| for |id| and takes ownership. | 83 // Sets |item_delegate| for the given |shelf_id| and takes ownership. |
| 104 void SetShelfItemDelegate(const ShelfID& id, | 84 void SetShelfItemDelegate(const ShelfID& shelf_id, |
| 105 std::unique_ptr<ShelfItemDelegate> item_delegate); | 85 std::unique_ptr<ShelfItemDelegate> item_delegate); |
| 106 | 86 |
| 107 // Returns ShelfItemDelegate for |id|, or null if none exists. | 87 // Returns ShelfItemDelegate for |shelf_id|, or nullptr if none exists. |
| 108 ShelfItemDelegate* GetShelfItemDelegate(const ShelfID& id); | 88 ShelfItemDelegate* GetShelfItemDelegate(const ShelfID& shelf_id); |
| 109 | 89 |
| 110 void AddObserver(ShelfModelObserver* observer); | 90 void AddObserver(ShelfModelObserver* observer); |
| 111 void RemoveObserver(ShelfModelObserver* observer); | 91 void RemoveObserver(ShelfModelObserver* observer); |
| 112 | 92 |
| 113 private: | 93 private: |
| 114 // Makes sure |index| is in line with the type-based order of items. If that | 94 // Makes sure |index| is in line with the type-based order of items. If that |
| 115 // is not the case, adjusts index by shifting it to the valid range and | 95 // is not the case, adjusts index by shifting it to the valid range and |
| 116 // returns the new value. | 96 // returns the new value. |
| 117 int ValidateInsertionIndex(ShelfItemType type, int index) const; | 97 int ValidateInsertionIndex(ShelfItemType type, int index) const; |
| 118 | 98 |
| 119 ShelfItems items_; | 99 ShelfItems items_; |
| 120 base::ObserverList<ShelfModelObserver> observers_; | 100 base::ObserverList<ShelfModelObserver> observers_; |
| 121 | 101 |
| 122 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> | 102 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> |
| 123 id_to_item_delegate_map_; | 103 id_to_item_delegate_map_; |
| 124 | 104 |
| 125 DISALLOW_COPY_AND_ASSIGN(ShelfModel); | 105 DISALLOW_COPY_AND_ASSIGN(ShelfModel); |
| 126 }; | 106 }; |
| 127 | 107 |
| 128 } // namespace ash | 108 } // namespace ash |
| 129 | 109 |
| 130 #endif // ASH_SHELF_SHELF_MODEL_H_ | 110 #endif // ASH_SHELF_SHELF_MODEL_H_ |
| OLD | NEW |