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(
92 const syncer::StringOrdinal
& new_position
) {
95 if (!FindItemIndex(item
->id(), &from_index
)) {
96 LOG(ERROR
) << "SetItemPosition: Not in list: " << item
->id().substr(0, 8);
99 DCHECK(app_list_items_
[from_index
] == item
);
100 // First check if the order would remain the same, in which case just update
102 size_t to_index
= GetItemSortOrderIndex(new_position
, item
->id());
103 if (to_index
== from_index
) {
104 DVLOG(2) << "SetItemPosition: No change: " << item
->id().substr(0, 8);
105 item
->set_position(new_position
);
108 // Remove the item and get the updated to index.
109 app_list_items_
.weak_erase(app_list_items_
.begin() + from_index
);
110 to_index
= GetItemSortOrderIndex(new_position
, item
->id());
111 DVLOG(2) << "SetItemPosition: " << item
->id().substr(0, 8) << " -> "
112 << new_position
.ToDebugString() << " From: " << from_index
113 << " To: " << to_index
;
114 item
->set_position(new_position
);
115 app_list_items_
.insert(app_list_items_
.begin() + to_index
, item
);
116 FOR_EACH_OBSERVER(AppListItemListObserver
,
118 OnListItemMoved(from_index
, to_index
, item
));
121 // AppListItemList private
123 syncer::StringOrdinal
AppListItemList::CreatePositionBefore(
124 const syncer::StringOrdinal
& position
) {
125 if (app_list_items_
.empty())
126 return syncer::StringOrdinal::CreateInitialOrdinal();
128 size_t nitems
= app_list_items_
.size();
130 if (!position
.IsValid()) {
133 for (index
= 0; index
< nitems
; ++index
) {
134 if (!app_list_items_
[index
]->position().LessThan(position
))
139 return app_list_items_
[0]->position().CreateBefore();
141 return app_list_items_
[nitems
- 1]->position().CreateAfter();
142 return app_list_items_
[index
- 1]->position().CreateBetween(
143 app_list_items_
[index
]->position());
146 AppListItem
* AppListItemList::AddItem(scoped_ptr
<AppListItem
> item_ptr
) {
147 AppListItem
* item
= item_ptr
.get();
148 CHECK(std::find(app_list_items_
.begin(), app_list_items_
.end(), item
)
149 == app_list_items_
.end());
150 EnsureValidItemPosition(item
);
151 size_t index
= GetItemSortOrderIndex(item
->position(), item
->id());
152 app_list_items_
.insert(app_list_items_
.begin() + index
, item_ptr
.release());
153 FOR_EACH_OBSERVER(AppListItemListObserver
,
155 OnListItemAdded(index
, item
));
159 void AppListItemList::DeleteItem(const std::string
& id
) {
160 scoped_ptr
<AppListItem
> item
= RemoveItem(id
);
161 // |item| will be deleted on destruction.
164 scoped_ptr
<AppListItem
> AppListItemList::RemoveItem(
165 const std::string
& id
) {
167 if (FindItemIndex(id
, &index
))
168 return RemoveItemAt(index
);
170 return scoped_ptr
<AppListItem
>();
173 scoped_ptr
<AppListItem
> AppListItemList::RemoveItemAt(size_t index
) {
174 DCHECK_LT(index
, item_count());
175 AppListItem
* item
= app_list_items_
[index
];
176 app_list_items_
.weak_erase(app_list_items_
.begin() + index
);
177 FOR_EACH_OBSERVER(AppListItemListObserver
,
179 OnListItemRemoved(index
, item
));
180 return make_scoped_ptr
<AppListItem
>(item
);
183 void AppListItemList::DeleteItemAt(size_t index
) {
184 scoped_ptr
<AppListItem
> item
= RemoveItemAt(index
);
185 // |item| will be deleted on destruction.
188 void AppListItemList::EnsureValidItemPosition(AppListItem
* item
) {
189 syncer::StringOrdinal position
= item
->position();
190 if (position
.IsValid())
192 size_t nitems
= app_list_items_
.size();
194 position
= syncer::StringOrdinal::CreateInitialOrdinal();
196 position
= app_list_items_
[nitems
- 1]->position().CreateAfter();
198 item
->set_position(position
);
201 size_t AppListItemList::GetItemSortOrderIndex(
202 const syncer::StringOrdinal
& position
,
203 const std::string
& id
) {
204 DCHECK(position
.IsValid());
205 for (size_t index
= 0; index
< app_list_items_
.size(); ++index
) {
206 if (position
.LessThan(app_list_items_
[index
]->position()) ||
207 (position
.Equals(app_list_items_
[index
]->position()) &&
208 (id
< app_list_items_
[index
]->id()))) {
212 return app_list_items_
.size();
215 void AppListItemList::FixItemPosition(size_t index
) {
216 DVLOG(1) << "FixItemPosition: " << index
;
217 size_t nitems
= item_count();
218 DCHECK_LT(index
, nitems
);
219 DCHECK_GT(index
, 0u);
220 // Update the position of |index| and any necessary subsequent items.
221 // First, find the next item that has a different position.
222 AppListItem
* prev
= app_list_items_
[index
- 1];
223 size_t last_index
= index
+ 1;
224 for (; last_index
< nitems
; ++last_index
) {
225 if (!app_list_items_
[last_index
]->position().Equals(prev
->position()))
228 AppListItem
* last
= last_index
< nitems
? app_list_items_
[last_index
] : NULL
;
229 for (size_t i
= index
; i
< last_index
; ++i
) {
230 AppListItem
* cur
= app_list_items_
[i
];
232 cur
->set_position(prev
->position().CreateBetween(last
->position()));
234 cur
->set_position(prev
->position().CreateAfter());
237 FOR_EACH_OBSERVER(AppListItemListObserver
,
239 OnListItemMoved(index
, index
, app_list_items_
[index
]));
242 } // namespace app_list