Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / app_list_item_list.h
blob32b434ef2397fbaab3d25558842f918ab7649202
1 // Copyright (c) 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.
5 #ifndef UI_APP_LIST_APP_LIST_ITEM_LIST_H_
6 #define UI_APP_LIST_APP_LIST_ITEM_LIST_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/observer_list.h"
13 #include "sync/api/string_ordinal.h"
14 #include "ui/app_list/app_list_export.h"
15 #include "ui/app_list/app_list_item_list_observer.h"
17 namespace app_list {
19 class AppListItem;
21 // Class to manage items in the app list. Used both by AppListModel and
22 // AppListFolderItem. Manages the position ordinal of items in the list, and
23 // notifies observers when items in the list are added / deleted / moved.
24 class APP_LIST_EXPORT AppListItemList {
25 public:
26 AppListItemList();
27 virtual ~AppListItemList();
29 void AddObserver(AppListItemListObserver* observer);
30 void RemoveObserver(AppListItemListObserver* observer);
32 // Finds item matching |id|. NOTE: Requires a linear search.
33 // Generally this should not be used directly, AppListModel::FindItem
34 // should be used instead.
35 AppListItem* FindItem(const std::string& id);
37 // Finds the |index| of the the item matching |id| in |app_list_items_|.
38 // Returns true if the matching item is found.
39 // Note: Requires a linear search.
40 bool FindItemIndex(const std::string& id, size_t* index);
42 // Moves item at |from_index| to |to_index|.
43 // Triggers observers_.OnListItemMoved().
44 void MoveItem(size_t from_index, size_t to_index);
46 // Sets the position of |item| which is expected to be a member of
47 // |app_list_items_| and sorts the list accordingly. If |new_position| is
48 // invalid, move the item to the end of the list.
49 void SetItemPosition(AppListItem* item, syncer::StringOrdinal new_position);
51 // Highlights the given item in the app list. If not present and it is later
52 // added, the item will be highlighted after being added.
53 void HighlightItemInstalledFromUI(const std::string& id);
55 AppListItem* item_at(size_t index) {
56 DCHECK_LT(index, app_list_items_.size());
57 return app_list_items_[index];
59 const AppListItem* item_at(size_t index) const {
60 DCHECK_LT(index, app_list_items_.size());
61 return app_list_items_[index];
63 size_t item_count() const { return app_list_items_.size(); }
65 private:
66 friend class AppListItemListTest;
67 friend class AppListModel;
69 // Returns a unique, valid StringOrdinal immediately before |position| or at
70 // the end of the list if |position| is invalid.
71 syncer::StringOrdinal CreatePositionBefore(
72 const syncer::StringOrdinal& position);
74 // Adds |item| to the end of |app_list_items_|. Takes ownership of |item|.
75 // Triggers observers_.OnListItemAdded(). Returns a pointer to the added item
76 // that is safe to use (e.g. after releasing a scoped ptr).
77 AppListItem* AddItem(scoped_ptr<AppListItem> item_ptr);
79 // Finds item matching |id| in |app_list_items_| (linear search) and deletes
80 // it. Triggers observers_.OnListItemRemoved() after removing the item from
81 // the list and before deleting it.
82 void DeleteItem(const std::string& id);
84 // Removes the item with matching |id| in |app_list_items_| without deleting
85 // it. Returns a scoped pointer containing the removed item.
86 scoped_ptr<AppListItem> RemoveItem(const std::string& id);
88 // Removes the item at |index| from |app_list_items_| without deleting it.
89 // Returns a scoped pointer containing the removed item.
90 scoped_ptr<AppListItem> RemoveItemAt(size_t index);
92 // Deletes item at |index| and signals observers.
93 void DeleteItemAt(size_t index);
95 // If |item|->position() is not a valid ordinal, sets |item|->position()
96 // to a valid ordinal after the last item in the list.
97 void EnsureValidItemPosition(AppListItem* item);
99 // Returns the index at which to insert an item in |app_list_items_| based on
100 // |position| (which must be valid) and |id| (if the positions are equal).
101 size_t GetItemSortOrderIndex(const syncer::StringOrdinal& position,
102 const std::string& id);
104 // Fixes the position of the item at |index| when the position matches the
105 // previous item's position. |index| must be > 0.
106 void FixItemPosition(size_t index);
108 ScopedVector<AppListItem> app_list_items_;
109 base::ObserverList<AppListItemListObserver, true> observers_;
110 std::string highlighted_id_;
112 DISALLOW_COPY_AND_ASSIGN(AppListItemList);
115 } // namespace app_list
117 #endif // UI_APP_LIST_APP_LIST_ITEM_LIST_H_