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.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 AppListItem
* AppListItemList::FindItem(const std::string
& id
) {
26 for (size_t i
= 0; i
< app_list_items_
.size(); ++i
) {
27 AppListItem
* item
= app_list_items_
[i
];
34 bool AppListItemList::FindItemIndex(const std::string
& id
, size_t* index
) {
35 for (size_t i
= 0; i
< app_list_items_
.size(); ++i
) {
36 AppListItem
* item
= app_list_items_
[i
];
37 if (item
->id() == id
) {
45 void AppListItemList::MoveItem(size_t from_index
, size_t to_index
) {
46 DCHECK_LT(from_index
, item_count());
47 DCHECK_LT(to_index
, item_count());
48 if (from_index
== to_index
)
51 AppListItem
* target_item
= app_list_items_
[from_index
];
52 DVLOG(2) << "MoveItem: " << from_index
<< " -> " << to_index
<< " ["
53 << target_item
->position().ToDebugString() << "]";
55 // Remove the target item
56 app_list_items_
.weak_erase(app_list_items_
.begin() + from_index
);
58 // Update the position
59 AppListItem
* prev
= to_index
> 0 ? app_list_items_
[to_index
- 1] : NULL
;
61 to_index
< item_count() ? app_list_items_
[to_index
] : NULL
;
63 syncer::StringOrdinal new_position
;
65 new_position
= next
->position().CreateBefore();
67 new_position
= prev
->position().CreateAfter();
69 // It is possible that items were added with the same ordinal. To
70 // successfully move the item we need to fix this. We do not try to fix this
71 // when an item is added in order to avoid possible edge cases with sync.
72 if (prev
->position().Equals(next
->position()))
73 FixItemPosition(to_index
);
74 new_position
= prev
->position().CreateBetween(next
->position());
76 target_item
->set_position(new_position
);
79 << " Prev: " << (prev
? prev
->position().ToDebugString() : "(none)")
80 << " Next: " << (next
? next
->position().ToDebugString() : "(none)")
81 << " -> " << new_position
.ToDebugString();
83 // Insert the item and notify observers.
84 app_list_items_
.insert(app_list_items_
.begin() + to_index
, target_item
);
85 FOR_EACH_OBSERVER(AppListItemListObserver
,
87 OnListItemMoved(from_index
, to_index
, target_item
));
90 void AppListItemList::SetItemPosition(AppListItem
* item
,
91 syncer::StringOrdinal new_position
) {
94 if (!FindItemIndex(item
->id(), &from_index
)) {
95 LOG(ERROR
) << "SetItemPosition: Not in list: " << item
->id().substr(0, 8);
98 DCHECK(app_list_items_
[from_index
] == item
);
99 if (!new_position
.IsValid()) {
100 size_t last_index
= app_list_items_
.size() - 1;
101 if (from_index
== last_index
)
102 return; // Already last item, do nothing.
103 new_position
= app_list_items_
[last_index
]->position().CreateAfter();
105 // First check if the order would remain the same, in which case just update
107 size_t to_index
= GetItemSortOrderIndex(new_position
, item
->id());
108 if (to_index
== from_index
) {
109 DVLOG(2) << "SetItemPosition: No change: " << item
->id().substr(0, 8);
110 item
->set_position(new_position
);
113 // Remove the item and get the updated to index.
114 app_list_items_
.weak_erase(app_list_items_
.begin() + from_index
);
115 to_index
= GetItemSortOrderIndex(new_position
, item
->id());
116 DVLOG(2) << "SetItemPosition: " << item
->id().substr(0, 8) << " -> "
117 << new_position
.ToDebugString() << " From: " << from_index
118 << " To: " << to_index
;
119 item
->set_position(new_position
);
120 app_list_items_
.insert(app_list_items_
.begin() + to_index
, item
);
121 FOR_EACH_OBSERVER(AppListItemListObserver
,
123 OnListItemMoved(from_index
, to_index
, item
));
126 // AppListItemList private
128 syncer::StringOrdinal
AppListItemList::CreatePositionBefore(
129 const syncer::StringOrdinal
& position
) {
130 if (app_list_items_
.empty())
131 return syncer::StringOrdinal::CreateInitialOrdinal();
133 size_t nitems
= app_list_items_
.size();
135 if (!position
.IsValid()) {
138 for (index
= 0; index
< nitems
; ++index
) {
139 if (!app_list_items_
[index
]->position().LessThan(position
))
144 return app_list_items_
[0]->position().CreateBefore();
146 return app_list_items_
[nitems
- 1]->position().CreateAfter();
147 return app_list_items_
[index
- 1]->position().CreateBetween(
148 app_list_items_
[index
]->position());
151 AppListItem
* AppListItemList::AddItem(scoped_ptr
<AppListItem
> item_ptr
) {
152 AppListItem
* item
= item_ptr
.get();
153 CHECK(std::find(app_list_items_
.begin(), app_list_items_
.end(), item
)
154 == app_list_items_
.end());
155 EnsureValidItemPosition(item
);
156 size_t index
= GetItemSortOrderIndex(item
->position(), item
->id());
157 app_list_items_
.insert(app_list_items_
.begin() + index
, item_ptr
.release());
158 FOR_EACH_OBSERVER(AppListItemListObserver
,
160 OnListItemAdded(index
, item
));
164 void AppListItemList::DeleteItem(const std::string
& id
) {
165 scoped_ptr
<AppListItem
> item
= RemoveItem(id
);
166 // |item| will be deleted on destruction.
169 scoped_ptr
<AppListItem
> AppListItemList::RemoveItem(
170 const std::string
& id
) {
172 if (FindItemIndex(id
, &index
))
173 return RemoveItemAt(index
);
175 return scoped_ptr
<AppListItem
>();
178 scoped_ptr
<AppListItem
> AppListItemList::RemoveItemAt(size_t index
) {
179 DCHECK_LT(index
, item_count());
180 AppListItem
* item
= app_list_items_
[index
];
181 app_list_items_
.weak_erase(app_list_items_
.begin() + index
);
182 FOR_EACH_OBSERVER(AppListItemListObserver
,
184 OnListItemRemoved(index
, item
));
185 return make_scoped_ptr
<AppListItem
>(item
);
188 void AppListItemList::DeleteItemAt(size_t index
) {
189 scoped_ptr
<AppListItem
> item
= RemoveItemAt(index
);
190 // |item| will be deleted on destruction.
193 void AppListItemList::EnsureValidItemPosition(AppListItem
* item
) {
194 syncer::StringOrdinal position
= item
->position();
195 if (position
.IsValid())
197 size_t nitems
= app_list_items_
.size();
199 position
= syncer::StringOrdinal::CreateInitialOrdinal();
201 position
= app_list_items_
[nitems
- 1]->position().CreateAfter();
203 item
->set_position(position
);
206 size_t AppListItemList::GetItemSortOrderIndex(
207 const syncer::StringOrdinal
& position
,
208 const std::string
& id
) {
209 DCHECK(position
.IsValid());
210 for (size_t index
= 0; index
< app_list_items_
.size(); ++index
) {
211 if (position
.LessThan(app_list_items_
[index
]->position()) ||
212 (position
.Equals(app_list_items_
[index
]->position()) &&
213 (id
< app_list_items_
[index
]->id()))) {
217 return app_list_items_
.size();
220 void AppListItemList::FixItemPosition(size_t index
) {
221 DVLOG(1) << "FixItemPosition: " << index
;
222 size_t nitems
= item_count();
223 DCHECK_LT(index
, nitems
);
224 DCHECK_GT(index
, 0u);
225 // Update the position of |index| and any necessary subsequent items.
226 // First, find the next item that has a different position.
227 AppListItem
* prev
= app_list_items_
[index
- 1];
228 size_t last_index
= index
+ 1;
229 for (; last_index
< nitems
; ++last_index
) {
230 if (!app_list_items_
[last_index
]->position().Equals(prev
->position()))
233 AppListItem
* last
= last_index
< nitems
? app_list_items_
[last_index
] : NULL
;
234 for (size_t i
= index
; i
< last_index
; ++i
) {
235 AppListItem
* cur
= app_list_items_
[i
];
237 cur
->set_position(prev
->position().CreateBetween(last
->position()));
239 cur
->set_position(prev
->position().CreateAfter());
242 FOR_EACH_OBSERVER(AppListItemListObserver
,
244 OnListItemMoved(index
, index
, app_list_items_
[index
]));
247 } // namespace app_list