| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/common/shelf/shelf_model.h" | 5 #include "ash/common/shelf/shelf_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/shelf/shelf_item_delegate.h" | 9 #include "ash/common/shelf/shelf_item_delegate.h" |
| 10 #include "ash/common/shelf/shelf_model_observer.h" | 10 #include "ash/common/shelf/shelf_model_observer.h" |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 57 } | 57 } |
| 58 | 58 |
| 59 int ShelfModel::Add(const ShelfItem& item) { | 59 int ShelfModel::Add(const ShelfItem& item) { |
| 60 return AddAt(items_.size(), item); | 60 return AddAt(items_.size(), item); |
| 61 } | 61 } |
| 62 | 62 |
| 63 int ShelfModel::AddAt(int index, const ShelfItem& item) { | 63 int ShelfModel::AddAt(int index, const ShelfItem& item) { |
| 64 index = ValidateInsertionIndex(item.type, index); | 64 index = ValidateInsertionIndex(item.type, index); |
| 65 items_.insert(items_.begin() + index, item); | 65 items_.insert(items_.begin() + index, item); |
| 66 items_[index].id = next_id_++; | 66 items_[index].id = next_id_++; |
| 67 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); | 67 for (auto& observer : observers_) |
| 68 observer.ShelfItemAdded(index); |
| 68 return index; | 69 return index; |
| 69 } | 70 } |
| 70 | 71 |
| 71 void ShelfModel::RemoveItemAt(int index) { | 72 void ShelfModel::RemoveItemAt(int index) { |
| 72 DCHECK(index >= 0 && index < item_count()); | 73 DCHECK(index >= 0 && index < item_count()); |
| 73 // The app list and browser shortcut can't be removed. | 74 // The app list and browser shortcut can't be removed. |
| 74 DCHECK(items_[index].type != TYPE_APP_LIST && | 75 DCHECK(items_[index].type != TYPE_APP_LIST && |
| 75 items_[index].type != TYPE_BROWSER_SHORTCUT); | 76 items_[index].type != TYPE_BROWSER_SHORTCUT); |
| 76 ShelfID id = items_[index].id; | 77 ShelfID id = items_[index].id; |
| 77 items_.erase(items_.begin() + index); | 78 items_.erase(items_.begin() + index); |
| 78 RemoveShelfItemDelegate(id); | 79 RemoveShelfItemDelegate(id); |
| 79 // TODO(jamescook): Fold this into ShelfItemRemoved in existing observers. | 80 // TODO(jamescook): Fold this into ShelfItemRemoved in existing observers. |
| 80 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 81 for (auto& observer : observers_) |
| 81 OnSetShelfItemDelegate(id, nullptr)); | 82 observer.OnSetShelfItemDelegate(id, nullptr); |
| 82 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 83 for (auto& observer : observers_) |
| 83 ShelfItemRemoved(index, id)); | 84 observer.ShelfItemRemoved(index, id); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void ShelfModel::Move(int index, int target_index) { | 87 void ShelfModel::Move(int index, int target_index) { |
| 87 if (index == target_index) | 88 if (index == target_index) |
| 88 return; | 89 return; |
| 89 // TODO: this needs to enforce valid ranges. | 90 // TODO: this needs to enforce valid ranges. |
| 90 ShelfItem item(items_[index]); | 91 ShelfItem item(items_[index]); |
| 91 items_.erase(items_.begin() + index); | 92 items_.erase(items_.begin() + index); |
| 92 items_.insert(items_.begin() + target_index, item); | 93 items_.insert(items_.begin() + target_index, item); |
| 93 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 94 for (auto& observer : observers_) |
| 94 ShelfItemMoved(index, target_index)); | 95 observer.ShelfItemMoved(index, target_index); |
| 95 } | 96 } |
| 96 | 97 |
| 97 void ShelfModel::Set(int index, const ShelfItem& item) { | 98 void ShelfModel::Set(int index, const ShelfItem& item) { |
| 98 DCHECK(index >= 0 && index < item_count()); | 99 DCHECK(index >= 0 && index < item_count()); |
| 99 int new_index = item.type == items_[index].type | 100 int new_index = item.type == items_[index].type |
| 100 ? index | 101 ? index |
| 101 : ValidateInsertionIndex(item.type, index); | 102 : ValidateInsertionIndex(item.type, index); |
| 102 | 103 |
| 103 ShelfItem old_item(items_[index]); | 104 ShelfItem old_item(items_[index]); |
| 104 items_[index] = item; | 105 items_[index] = item; |
| 105 items_[index].id = old_item.id; | 106 items_[index].id = old_item.id; |
| 106 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 107 for (auto& observer : observers_) |
| 107 ShelfItemChanged(index, old_item)); | 108 observer.ShelfItemChanged(index, old_item); |
| 108 | 109 |
| 109 // If the type changes confirm that the item is still in the right order. | 110 // If the type changes confirm that the item is still in the right order. |
| 110 if (new_index != index) { | 111 if (new_index != index) { |
| 111 // The move function works by removing one item and then inserting it at the | 112 // The move function works by removing one item and then inserting it at the |
| 112 // new location. However - by removing the item first the order will change | 113 // new location. However - by removing the item first the order will change |
| 113 // so that our target index needs to be corrected. | 114 // so that our target index needs to be corrected. |
| 114 // TODO(skuhne): Moving this into the Move function breaks lots of unit | 115 // TODO(skuhne): Moving this into the Move function breaks lots of unit |
| 115 // tests. So several functions were already using this incorrectly. | 116 // tests. So several functions were already using this incorrectly. |
| 116 // That needs to be cleaned up. | 117 // That needs to be cleaned up. |
| 117 if (index < new_index) | 118 if (index < new_index) |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 } | 164 } |
| 164 | 165 |
| 165 void ShelfModel::SetShelfItemDelegate( | 166 void ShelfModel::SetShelfItemDelegate( |
| 166 ShelfID id, | 167 ShelfID id, |
| 167 std::unique_ptr<ShelfItemDelegate> item_delegate) { | 168 std::unique_ptr<ShelfItemDelegate> item_delegate) { |
| 168 // If another ShelfItemDelegate is already registered for |id|, we assume | 169 // If another ShelfItemDelegate is already registered for |id|, we assume |
| 169 // that this request is replacing ShelfItemDelegate for |id| with | 170 // that this request is replacing ShelfItemDelegate for |id| with |
| 170 // |item_delegate|. | 171 // |item_delegate|. |
| 171 RemoveShelfItemDelegate(id); | 172 RemoveShelfItemDelegate(id); |
| 172 | 173 |
| 173 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 174 for (auto& observer : observers_) |
| 174 OnSetShelfItemDelegate(id, item_delegate.get())); | 175 observer.OnSetShelfItemDelegate(id, item_delegate.get()); |
| 175 | 176 |
| 176 id_to_item_delegate_map_[id] = std::move(item_delegate); | 177 id_to_item_delegate_map_[id] = std::move(item_delegate); |
| 177 } | 178 } |
| 178 | 179 |
| 179 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(ShelfID id) { | 180 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(ShelfID id) { |
| 180 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) | 181 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) |
| 181 return id_to_item_delegate_map_[id].get(); | 182 return id_to_item_delegate_map_[id].get(); |
| 182 return nullptr; | 183 return nullptr; |
| 183 } | 184 } |
| 184 | 185 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 207 | 208 |
| 208 return index; | 209 return index; |
| 209 } | 210 } |
| 210 | 211 |
| 211 void ShelfModel::RemoveShelfItemDelegate(ShelfID id) { | 212 void ShelfModel::RemoveShelfItemDelegate(ShelfID id) { |
| 212 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) | 213 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) |
| 213 id_to_item_delegate_map_.erase(id); | 214 id_to_item_delegate_map_.erase(id); |
| 214 } | 215 } |
| 215 | 216 |
| 216 } // namespace ash | 217 } // namespace ash |
| OLD | NEW |