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_COMMON_SHELF_SHELF_MODEL_H_ | 5 #ifndef ASH_COMMON_SHELF_SHELF_MODEL_H_ |
6 #define ASH_COMMON_SHELF_SHELF_MODEL_H_ | 6 #define ASH_COMMON_SHELF_SHELF_MODEL_H_ |
7 | 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 |
8 #include "ash/ash_export.h" | 11 #include "ash/ash_export.h" |
9 #include "ash/common/shelf/shelf_item_types.h" | 12 #include "ash/common/shelf/shelf_item_types.h" |
10 #include "base/macros.h" | 13 #include "base/macros.h" |
11 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
12 | 15 |
13 namespace ash { | 16 namespace ash { |
14 | 17 |
| 18 class ShelfItemDelegate; |
15 class ShelfModelObserver; | 19 class ShelfModelObserver; |
16 | 20 |
17 // Model used by ShelfView. | 21 // Model used for shelf items. Owns ShelfItemDelegates, but does not create |
| 22 // them. |
18 class ASH_EXPORT ShelfModel { | 23 class ASH_EXPORT ShelfModel { |
19 public: | 24 public: |
20 enum Status { | 25 enum Status { |
21 STATUS_NORMAL, | 26 STATUS_NORMAL, |
22 // A status that indicates apps are syncing/loading. | 27 // A status that indicates apps are syncing/loading. |
23 STATUS_LOADING, | 28 STATUS_LOADING, |
24 }; | 29 }; |
25 | 30 |
26 ShelfModel(); | 31 ShelfModel(); |
27 ~ShelfModel(); | 32 ~ShelfModel(); |
28 | 33 |
| 34 // Cleans up the ShelfItemDelegates. |
| 35 void DestroyItemDelegates(); |
| 36 |
29 // Adds a new item to the model. Returns the resulting index. | 37 // Adds a new item to the model. Returns the resulting index. |
30 int Add(const ShelfItem& item); | 38 int Add(const ShelfItem& item); |
31 | 39 |
32 // Adds the item. |index| is the requested insertion index, which may be | 40 // Adds the item. |index| is the requested insertion index, which may be |
33 // modified to meet type-based ordering. Returns the actual insertion index. | 41 // modified to meet type-based ordering. Returns the actual insertion index. |
34 int AddAt(int index, const ShelfItem& item); | 42 int AddAt(int index, const ShelfItem& item); |
35 | 43 |
36 // Removes the item at |index|. | 44 // Removes the item at |index|. |
37 void RemoveItemAt(int index); | 45 void RemoveItemAt(int index); |
38 | 46 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // Returns an iterator into items() for the item with the specified id, or | 78 // Returns an iterator into items() for the item with the specified id, or |
71 // items().end() if there is no item with the specified id. | 79 // items().end() if there is no item with the specified id. |
72 ShelfItems::const_iterator ItemByID(ShelfID id) const; | 80 ShelfItems::const_iterator ItemByID(ShelfID id) const; |
73 | 81 |
74 const ShelfItems& items() const { return items_; } | 82 const ShelfItems& items() const { return items_; } |
75 int item_count() const { return static_cast<int>(items_.size()); } | 83 int item_count() const { return static_cast<int>(items_.size()); } |
76 | 84 |
77 void set_status(Status status) { status_ = status; } | 85 void set_status(Status status) { status_ = status; } |
78 Status status() const { return status_; } | 86 Status status() const { return status_; } |
79 | 87 |
| 88 // Set |item_delegate| for |id| and takes ownership. |
| 89 void SetShelfItemDelegate(ShelfID id, |
| 90 std::unique_ptr<ShelfItemDelegate> item_delegate); |
| 91 |
| 92 // Returns ShelfItemDelegate for |id|, or null if none exists. |
| 93 ShelfItemDelegate* GetShelfItemDelegate(ShelfID id); |
| 94 |
80 void AddObserver(ShelfModelObserver* observer); | 95 void AddObserver(ShelfModelObserver* observer); |
81 void RemoveObserver(ShelfModelObserver* observer); | 96 void RemoveObserver(ShelfModelObserver* observer); |
82 | 97 |
83 private: | 98 private: |
84 // Makes sure |index| is in line with the type-based order of items. If that | 99 // Makes sure |index| is in line with the type-based order of items. If that |
85 // is not the case, adjusts index by shifting it to the valid range and | 100 // is not the case, adjusts index by shifting it to the valid range and |
86 // returns the new value. | 101 // returns the new value. |
87 int ValidateInsertionIndex(ShelfItemType type, int index) const; | 102 int ValidateInsertionIndex(ShelfItemType type, int index) const; |
88 | 103 |
| 104 // Remove and destroy ShelfItemDelegate for |id|. |
| 105 void RemoveShelfItemDelegate(ShelfID id); |
| 106 |
89 // ID assigned to the next item. | 107 // ID assigned to the next item. |
90 ShelfID next_id_; | 108 ShelfID next_id_; |
91 | 109 |
92 ShelfItems items_; | 110 ShelfItems items_; |
93 Status status_; | 111 Status status_; |
94 base::ObserverList<ShelfModelObserver> observers_; | 112 base::ObserverList<ShelfModelObserver> observers_; |
95 | 113 |
| 114 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> |
| 115 id_to_item_delegate_map_; |
| 116 |
96 DISALLOW_COPY_AND_ASSIGN(ShelfModel); | 117 DISALLOW_COPY_AND_ASSIGN(ShelfModel); |
97 }; | 118 }; |
98 | 119 |
99 } // namespace ash | 120 } // namespace ash |
100 | 121 |
101 #endif // ASH_COMMON_SHELF_SHELF_MODEL_H_ | 122 #endif // ASH_COMMON_SHELF_SHELF_MODEL_H_ |
OLD | NEW |