| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef ASH_SHELF_SHELF_MODEL_H_ | |
| 6 #define ASH_SHELF_SHELF_MODEL_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 | |
| 11 #include "ash/ash_export.h" | |
| 12 #include "ash/public/cpp/shelf_item.h" | |
| 13 #include "ash/public/interfaces/shelf.mojom.h" | |
| 14 #include "base/macros.h" | |
| 15 #include "base/observer_list.h" | |
| 16 | |
| 17 namespace ash { | |
| 18 | |
| 19 class ShelfItemDelegate; | |
| 20 class ShelfModelObserver; | |
| 21 | |
| 22 // An id for the AppList item, which is added in the ShelfModel constructor. | |
| 23 // Generated as crx_file::id_util::GenerateId("org.chromium.applist") | |
| 24 ASH_EXPORT extern const char kAppListId[]; | |
| 25 | |
| 26 // Model used for shelf items. Owns ShelfItemDelegates but does not create them. | |
| 27 // TODO(msw): Move this to ash/public/cpp and use ASH_PUBLIC_EXPORT. | |
| 28 class ASH_EXPORT ShelfModel { | |
| 29 public: | |
| 30 ShelfModel(); | |
| 31 ~ShelfModel(); | |
| 32 | |
| 33 // Pins an app with |app_id| to shelf. A running instance will get pinned. | |
| 34 // If there is no running instance, a new shelf item is created and pinned. | |
| 35 void PinAppWithID(const std::string& app_id); | |
| 36 | |
| 37 // Check if the app with |app_id_| is pinned to the shelf. | |
| 38 bool IsAppPinned(const std::string& app_id); | |
| 39 | |
| 40 // Unpins app item with |app_id|. | |
| 41 void UnpinAppWithID(const std::string& app_id); | |
| 42 | |
| 43 // Cleans up the ShelfItemDelegates. | |
| 44 void DestroyItemDelegates(); | |
| 45 | |
| 46 // Adds a new item to the model. Returns the resulting index. | |
| 47 int Add(const ShelfItem& item); | |
| 48 | |
| 49 // Adds the item. |index| is the requested insertion index, which may be | |
| 50 // modified to meet type-based ordering. Returns the actual insertion index. | |
| 51 int AddAt(int index, const ShelfItem& item); | |
| 52 | |
| 53 // Removes the item at |index|. | |
| 54 void RemoveItemAt(int index); | |
| 55 | |
| 56 // Moves the item at |index| to |target_index|. |target_index| is in terms | |
| 57 // of the model *after* the item at |index| is removed. | |
| 58 void Move(int index, int target_index); | |
| 59 | |
| 60 // Resets the item at the specified index. The item's id should not change. | |
| 61 void Set(int index, const ShelfItem& item); | |
| 62 | |
| 63 // Returns the index of the item with id |shelf_id|, or -1 if none exists. | |
| 64 int ItemIndexByID(const ShelfID& shelf_id) const; | |
| 65 | |
| 66 // Returns the |index| of the item matching |type| in |items_|. | |
| 67 // Returns -1 if the matching item is not found. | |
| 68 int GetItemIndexForType(ShelfItemType type); | |
| 69 | |
| 70 // Returns the index of the first running application or the index where the | |
| 71 // first running application would go if there are no running (non pinned) | |
| 72 // applications yet. | |
| 73 int FirstRunningAppIndex() const; | |
| 74 | |
| 75 // Returns the index of the first panel or the index where the first panel | |
| 76 // would go if there are no panels. | |
| 77 int FirstPanelIndex() const; | |
| 78 | |
| 79 // Returns an iterator into items() for the item with the specified id, or | |
| 80 // items().end() if there is no item with the specified id. | |
| 81 ShelfItems::const_iterator ItemByID(const ShelfID& shelf_id) const; | |
| 82 | |
| 83 const ShelfItems& items() const { return items_; } | |
| 84 int item_count() const { return static_cast<int>(items_.size()); } | |
| 85 | |
| 86 // Sets |item_delegate| for the given |shelf_id| and takes ownership. | |
| 87 void SetShelfItemDelegate(const ShelfID& shelf_id, | |
| 88 std::unique_ptr<ShelfItemDelegate> item_delegate); | |
| 89 | |
| 90 // Returns ShelfItemDelegate for |shelf_id|, or nullptr if none exists. | |
| 91 ShelfItemDelegate* GetShelfItemDelegate(const ShelfID& shelf_id); | |
| 92 | |
| 93 void AddObserver(ShelfModelObserver* observer); | |
| 94 void RemoveObserver(ShelfModelObserver* observer); | |
| 95 | |
| 96 private: | |
| 97 // Makes sure |index| is in line with the type-based order of items. If that | |
| 98 // is not the case, adjusts index by shifting it to the valid range and | |
| 99 // returns the new value. | |
| 100 int ValidateInsertionIndex(ShelfItemType type, int index) const; | |
| 101 | |
| 102 ShelfItems items_; | |
| 103 base::ObserverList<ShelfModelObserver> observers_; | |
| 104 | |
| 105 std::map<ShelfID, std::unique_ptr<ShelfItemDelegate>> | |
| 106 id_to_item_delegate_map_; | |
| 107 | |
| 108 DISALLOW_COPY_AND_ASSIGN(ShelfModel); | |
| 109 }; | |
| 110 | |
| 111 } // namespace ash | |
| 112 | |
| 113 #endif // ASH_SHELF_SHELF_MODEL_H_ | |
| OLD | NEW |