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_model_observer.h" | 9 #include "ash/common/shelf/shelf_model_observer.h" |
10 #include "ash/public/cpp/shelf_item_delegate.h" | 10 #include "ash/public/cpp/shelf_item_delegate.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 items_[index].id = next_id_++; | 63 items_[index].id = next_id_++; |
64 for (auto& observer : observers_) | 64 for (auto& observer : observers_) |
65 observer.ShelfItemAdded(index); | 65 observer.ShelfItemAdded(index); |
66 return index; | 66 return index; |
67 } | 67 } |
68 | 68 |
69 void ShelfModel::RemoveItemAt(int index) { | 69 void ShelfModel::RemoveItemAt(int index) { |
70 DCHECK(index >= 0 && index < item_count()); | 70 DCHECK(index >= 0 && index < item_count()); |
71 ShelfItem old_item(items_[index]); | 71 ShelfItem old_item(items_[index]); |
72 items_.erase(items_.begin() + index); | 72 items_.erase(items_.begin() + index); |
73 id_to_item_delegate_map_.erase(old_item.id); | 73 RemoveShelfItemDelegate(old_item.id); |
| 74 // TODO(jamescook): Fold this into ShelfItemRemoved in existing observers. |
| 75 for (auto& observer : observers_) |
| 76 observer.OnSetShelfItemDelegate(old_item.id, nullptr); |
74 for (auto& observer : observers_) | 77 for (auto& observer : observers_) |
75 observer.ShelfItemRemoved(index, old_item); | 78 observer.ShelfItemRemoved(index, old_item); |
76 } | 79 } |
77 | 80 |
78 void ShelfModel::Move(int index, int target_index) { | 81 void ShelfModel::Move(int index, int target_index) { |
79 if (index == target_index) | 82 if (index == target_index) |
80 return; | 83 return; |
81 // TODO: this needs to enforce valid ranges. | 84 // TODO: this needs to enforce valid ranges. |
82 ShelfItem item(items_[index]); | 85 ShelfItem item(items_[index]); |
83 items_.erase(items_.begin() + index); | 86 items_.erase(items_.begin() + index); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 ShelfItem weight_dummy; | 153 ShelfItem weight_dummy; |
151 weight_dummy.type = TYPE_APP_PANEL; | 154 weight_dummy.type = TYPE_APP_PANEL; |
152 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 155 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
153 CompareByWeight) - | 156 CompareByWeight) - |
154 items_.begin(); | 157 items_.begin(); |
155 } | 158 } |
156 | 159 |
157 void ShelfModel::SetShelfItemDelegate( | 160 void ShelfModel::SetShelfItemDelegate( |
158 ShelfID id, | 161 ShelfID id, |
159 std::unique_ptr<ShelfItemDelegate> item_delegate) { | 162 std::unique_ptr<ShelfItemDelegate> item_delegate) { |
160 if (item_delegate) | 163 // If another ShelfItemDelegate is already registered for |id|, we assume |
161 item_delegate->set_shelf_id(id); | 164 // that this request is replacing ShelfItemDelegate for |id| with |
162 // This assignment replaces any ShelfItemDelegate already registered for |id|. | 165 // |item_delegate|. |
| 166 RemoveShelfItemDelegate(id); |
| 167 |
| 168 for (auto& observer : observers_) |
| 169 observer.OnSetShelfItemDelegate(id, item_delegate.get()); |
| 170 |
163 id_to_item_delegate_map_[id] = std::move(item_delegate); | 171 id_to_item_delegate_map_[id] = std::move(item_delegate); |
164 } | 172 } |
165 | 173 |
166 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(ShelfID id) { | 174 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(ShelfID id) { |
167 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) | 175 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) |
168 return id_to_item_delegate_map_[id].get(); | 176 return id_to_item_delegate_map_[id].get(); |
169 return nullptr; | 177 return nullptr; |
170 } | 178 } |
171 | 179 |
172 void ShelfModel::AddObserver(ShelfModelObserver* observer) { | 180 void ShelfModel::AddObserver(ShelfModelObserver* observer) { |
(...skipping 15 matching lines...) Expand all Loading... |
188 items_.begin(), | 196 items_.begin(), |
189 static_cast<ShelfItems::difference_type>(index)); | 197 static_cast<ShelfItems::difference_type>(index)); |
190 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 198 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
191 CompareByWeight) - | 199 CompareByWeight) - |
192 items_.begin(), | 200 items_.begin(), |
193 static_cast<ShelfItems::difference_type>(index)); | 201 static_cast<ShelfItems::difference_type>(index)); |
194 | 202 |
195 return index; | 203 return index; |
196 } | 204 } |
197 | 205 |
| 206 void ShelfModel::RemoveShelfItemDelegate(ShelfID id) { |
| 207 if (id_to_item_delegate_map_.find(id) != id_to_item_delegate_map_.end()) |
| 208 id_to_item_delegate_map_.erase(id); |
| 209 } |
| 210 |
198 } // namespace ash | 211 } // namespace ash |
OLD | NEW |