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