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 #include "ui/app_list/app_list_item_list.h"
7 #include "ui/app_list/app_list_item_model.h"
11 AppListItemList::AppListItemList() {
14 AppListItemList::~AppListItemList() {
17 void AppListItemList::AddObserver(AppListItemListObserver
* observer
) {
18 observers_
.AddObserver(observer
);
21 void AppListItemList::RemoveObserver(AppListItemListObserver
* observer
) {
22 observers_
.RemoveObserver(observer
);
25 AppListItemModel
* AppListItemList::FindItem(const std::string
& id
) {
26 for (size_t i
= 0; i
< app_list_items_
.size(); ++i
) {
27 AppListItemModel
* item
= app_list_items_
[i
];
34 size_t AppListItemList::AddItem(AppListItemModel
* item
) {
35 size_t index
= GetItemSortOrderIndex(item
);
36 app_list_items_
.insert(app_list_items_
.begin() + index
, item
);
37 FOR_EACH_OBSERVER(AppListItemListObserver
,
39 OnListItemAdded(index
, item
));
43 void AppListItemList::DeleteItem(const std::string
& id
) {
44 for (size_t i
= 0; i
< app_list_items_
.size(); ++i
) {
45 AppListItemModel
* item
= app_list_items_
[i
];
46 if (item
->id() == id
) {
53 void AppListItemList::DeleteItemsByType(const char* type
) {
54 for (int i
= static_cast<int>(app_list_items_
.size()) - 1;
56 AppListItemModel
* item
= app_list_items_
[i
];
57 if (!type
|| item
->GetAppType() == type
)
62 void AppListItemList::MoveItem(size_t from_index
, size_t to_index
) {
63 DCHECK_LT(from_index
, item_count());
64 DCHECK_LT(to_index
, item_count());
65 if (from_index
== to_index
)
68 AppListItemModel
* target_item
= app_list_items_
[from_index
];
69 app_list_items_
.weak_erase(app_list_items_
.begin() + from_index
);
70 app_list_items_
.insert(app_list_items_
.begin() + to_index
, target_item
);
73 AppListItemModel
* prev
= to_index
> 0 ? app_list_items_
[to_index
- 1] : NULL
;
74 AppListItemModel
* next
= to_index
< app_list_items_
.size() - 1 ?
75 app_list_items_
[to_index
+ 1] : NULL
;
78 // It is possible that items were added with the same ordinal. Rather than
79 // resolving a potentially complicated chain of conflicts, just set the
80 // ordinal before |next| (which will place it before both items).
81 if (prev
&& next
&& prev
->position().Equals(next
->position()))
84 VLOG(2) << "Move: " << target_item
->position().ToDebugString()
85 << " Prev: " << (prev
? prev
->position().ToDebugString() : "(none)")
86 << " Next: " << (next
? next
->position().ToDebugString() : "(none)");
88 target_item
->set_position(next
->position().CreateBefore());
90 target_item
->set_position(prev
->position().CreateAfter());
92 target_item
->set_position(prev
->position().CreateBetween(next
->position()));
93 FOR_EACH_OBSERVER(AppListItemListObserver
,
95 OnListItemMoved(from_index
, to_index
, target_item
));
98 // AppListItemList private
100 void AppListItemList::DeleteItemAt(size_t index
) {
101 DCHECK_LT(index
, item_count());
102 AppListItemModel
* item
= app_list_items_
[index
];
103 app_list_items_
.weak_erase(app_list_items_
.begin() + index
);
104 FOR_EACH_OBSERVER(AppListItemListObserver
,
106 OnListItemRemoved(index
, item
));
110 size_t AppListItemList::GetItemSortOrderIndex(AppListItemModel
* item
) {
111 syncer::StringOrdinal position
= item
->position();
112 if (!position
.IsValid()) {
113 size_t nitems
= app_list_items_
.size();
115 position
= syncer::StringOrdinal::CreateInitialOrdinal();
117 position
= app_list_items_
[nitems
- 1]->position().CreateAfter();
119 item
->set_position(position
);
122 // Note: app_list_items_ is sorted by convention, but sorting is not enforced
123 // (items' positions might be changed outside this class).
125 for (; index
< app_list_items_
.size(); ++index
) {
126 if (position
.LessThan(app_list_items_
[index
]->position()))
132 } // namespace app_list