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/app_launch_id.h" | |
13 #include "ash/public/cpp/shelf_item.h" | 12 #include "ash/public/cpp/shelf_item.h" |
14 #include "ash/public/interfaces/shelf.mojom.h" | 13 #include "ash/public/interfaces/shelf.mojom.h" |
15 #include "base/macros.h" | 14 #include "base/macros.h" |
16 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
17 | 16 |
18 namespace ash { | 17 namespace ash { |
19 | 18 |
20 class ShelfItemDelegate; | 19 class ShelfItemDelegate; |
21 class ShelfModelObserver; | 20 class ShelfModelObserver; |
22 | 21 |
23 // 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. |
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 kInvalidShelfID if the | 29 // Get the shelf ID from an application ID. Returns an empty ShelfID if the |
30 // app id is unknown, or has no associated ShelfID. | 30 // app id is unknown to the model, or has no associated ShelfID. |
31 ShelfID GetShelfIDForAppID(const std::string& app_id); | 31 ShelfID GetShelfIDForAppID(const std::string& app_id); |
32 | 32 |
33 // Get the shelf ID from an application ID and a launch ID. | 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 | 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 | 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. | 36 // uniquely identify each shelf item that has the same app_id. |
37 // For example, a single virtualization app might want to show different | 37 // For example, a single virtualization app might want to show different |
38 // shelf icons for different remote apps. Returns kInvalidShelfID if the app | 38 // shelf icons for different remote apps. Returns an empty ShelfID() if the |
39 // id is unknown or has no associated ShelfID. | 39 // app id is unknown to the model, or has no associated ShelfID. |
40 ShelfID GetShelfIDForAppIDAndLaunchID(const std::string& app_id, | 40 ShelfID GetShelfIDForAppIDAndLaunchID(const std::string& app_id, |
41 const std::string& launch_id); | 41 const std::string& launch_id); |
42 | 42 |
43 // Get the application ID for a given shelf ID. Returns an empty string for | 43 // Get the application ID for a given shelf ID. Returns an empty string for |
44 // an unknown or invalid ShelfID. | 44 // an unknown or invalid ShelfID. |
45 const std::string& GetAppIDForShelfID(ShelfID id); | 45 const std::string& GetAppIDForShelfID(const ShelfID& id); |
46 | 46 |
47 // Pins an app with |app_id| to shelf. A running instance will get pinned. | 47 // 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 | 48 // In case there is no running instance a new shelf item is created and |
49 // pinned. | 49 // pinned. |
50 void PinAppWithID(const std::string& app_id); | 50 void PinAppWithID(const std::string& app_id); |
51 | 51 |
52 // Check if the app with |app_id_| is pinned to the shelf. | 52 // Check if the app with |app_id_| is pinned to the shelf. |
53 bool IsAppPinned(const std::string& app_id); | 53 bool IsAppPinned(const std::string& app_id); |
54 | 54 |
55 // Unpins app item with |app_id|. | 55 // Unpins app item with |app_id|. |
(...skipping 14 matching lines...) Expand all Loading... |
70 | 70 |
71 // Moves the item at |index| to |target_index|. |target_index| is in terms | 71 // Moves the item at |index| to |target_index|. |target_index| is in terms |
72 // of the model *after* the item at |index| is removed. | 72 // of the model *after* the item at |index| is removed. |
73 void Move(int index, int target_index); | 73 void Move(int index, int target_index); |
74 | 74 |
75 // Resets the item at the specified index. The item maintains its existing | 75 // Resets the item at the specified index. The item maintains its existing |
76 // id and type. | 76 // id and type. |
77 void Set(int index, const ShelfItem& item); | 77 void Set(int index, const ShelfItem& item); |
78 | 78 |
79 // Returns the index of the item by id. | 79 // Returns the index of the item by id. |
80 int ItemIndexByID(ShelfID id) const; | 80 int ItemIndexByID(const ShelfID& id) const; |
81 | 81 |
82 // Returns the |index| of the item matching |type| in |items_|. | 82 // Returns the |index| of the item matching |type| in |items_|. |
83 // Returns -1 if the matching item is not found. | 83 // Returns -1 if the matching item is not found. |
84 // Note: Requires a linear search. | 84 // Note: Requires a linear search. |
85 int GetItemIndexForType(ShelfItemType type); | 85 int GetItemIndexForType(ShelfItemType type); |
86 | 86 |
87 // Returns the index of the first running application or the index where the | 87 // 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) | 88 // first running application would go if there are no running (non pinned) |
89 // applications yet. | 89 // applications yet. |
90 int FirstRunningAppIndex() const; | 90 int FirstRunningAppIndex() const; |
91 | 91 |
92 // Returns the index of the first panel or the index where the first panel | 92 // Returns the index of the first panel or the index where the first panel |
93 // would go if there are no panels. | 93 // would go if there are no panels. |
94 int FirstPanelIndex() const; | 94 int FirstPanelIndex() const; |
95 | 95 |
96 // Returns the id assigned to the next item added. | |
97 ShelfID next_id() const { return next_id_; } | |
98 | |
99 // Returns an iterator into items() for the item with the specified id, or | 96 // Returns an iterator into items() for the item with the specified id, or |
100 // items().end() if there is no item with the specified id. | 97 // items().end() if there is no item with the specified id. |
101 ShelfItems::const_iterator ItemByID(ShelfID id) const; | 98 ShelfItems::const_iterator ItemByID(const ShelfID& id) const; |
102 | 99 |
103 const ShelfItems& items() const { return items_; } | 100 const ShelfItems& items() const { return items_; } |
104 int item_count() const { return static_cast<int>(items_.size()); } | 101 int item_count() const { return static_cast<int>(items_.size()); } |
105 | 102 |
106 // Set |item_delegate| for |id| and takes ownership. | 103 // Set |item_delegate| for |id| and takes ownership. |
107 void SetShelfItemDelegate(ShelfID id, | 104 void SetShelfItemDelegate(const ShelfID& id, |
108 std::unique_ptr<ShelfItemDelegate> item_delegate); | 105 std::unique_ptr<ShelfItemDelegate> item_delegate); |
109 | 106 |
110 // Returns ShelfItemDelegate for |id|, or null if none exists. | 107 // Returns ShelfItemDelegate for |id|, or null if none exists. |
111 ShelfItemDelegate* GetShelfItemDelegate(ShelfID id); | 108 ShelfItemDelegate* GetShelfItemDelegate(const ShelfID& id); |
112 | 109 |
113 void AddObserver(ShelfModelObserver* observer); | 110 void AddObserver(ShelfModelObserver* observer); |
114 void RemoveObserver(ShelfModelObserver* observer); | 111 void RemoveObserver(ShelfModelObserver* observer); |
115 | 112 |
116 private: | 113 private: |
117 // Makes sure |index| is in line with the type-based order of items. If that | 114 // Makes sure |index| is in line with the type-based order of items. If that |
118 // is not the case, adjusts index by shifting it to the valid range and | 115 // is not the case, adjusts index by shifting it to the valid range and |
119 // returns the new value. | 116 // returns the new value. |
120 int ValidateInsertionIndex(ShelfItemType type, int index) const; | 117 int ValidateInsertionIndex(ShelfItemType type, int index) const; |
121 | 118 |
122 // ID assigned to the next item. | |
123 ShelfID next_id_; | |
124 | |
125 ShelfItems items_; | 119 ShelfItems items_; |
126 base::ObserverList<ShelfModelObserver> observers_; | 120 base::ObserverList<ShelfModelObserver> observers_; |
127 | 121 |
128 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> | 122 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> |
129 id_to_item_delegate_map_; | 123 id_to_item_delegate_map_; |
130 | 124 |
131 DISALLOW_COPY_AND_ASSIGN(ShelfModel); | 125 DISALLOW_COPY_AND_ASSIGN(ShelfModel); |
132 }; | 126 }; |
133 | 127 |
134 } // namespace ash | 128 } // namespace ash |
135 | 129 |
136 #endif // ASH_SHELF_SHELF_MODEL_H_ | 130 #endif // ASH_SHELF_SHELF_MODEL_H_ |
OLD | NEW |