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/shelf/shelf_model.h" | 5 #include "ash/shelf/shelf_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ash/public/cpp/shelf_item_delegate.h" | 9 #include "ash/public/cpp/shelf_item_delegate.h" |
10 #include "ash/shelf/shelf_model_observer.h" | 10 #include "ash/shelf/shelf_model_observer.h" |
| 11 #include "ash/strings/grit/ash_strings.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
11 | 13 |
12 namespace ash { | 14 namespace ash { |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 int ShelfItemTypeToWeight(ShelfItemType type) { | 18 int ShelfItemTypeToWeight(ShelfItemType type) { |
17 switch (type) { | 19 switch (type) { |
18 case TYPE_APP_LIST: | 20 case TYPE_APP_LIST: |
19 // TODO(skuhne): If the app list item becomes movable again, this need | 21 // TODO(skuhne): If the app list item becomes movable again, this need |
20 // to be a fallthrough. | 22 // to be a fallthrough. |
(...skipping 15 matching lines...) Expand all Loading... |
36 NOTREACHED() << "Invalid type " << type; | 38 NOTREACHED() << "Invalid type " << type; |
37 return 1; | 39 return 1; |
38 } | 40 } |
39 | 41 |
40 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { | 42 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { |
41 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); | 43 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); |
42 } | 44 } |
43 | 45 |
44 } // namespace | 46 } // namespace |
45 | 47 |
46 ShelfModel::ShelfModel() = default; | 48 const char kAppListId[] = "jlfapfmkapbjlfbpjedlinehodkccjee"; |
| 49 |
| 50 ShelfModel::ShelfModel() { |
| 51 // Add the app list item. |
| 52 ShelfItem item; |
| 53 item.type = TYPE_APP_LIST; |
| 54 item.id = ShelfID(kAppListId); |
| 55 item.title = l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE); |
| 56 const int index = Add(item); |
| 57 DCHECK_EQ(0, index); |
| 58 } |
47 | 59 |
48 ShelfModel::~ShelfModel() = default; | 60 ShelfModel::~ShelfModel() = default; |
49 | 61 |
50 void ShelfModel::PinAppWithID(const std::string& app_id) { | 62 void ShelfModel::PinAppWithID(const std::string& app_id) { |
51 const ShelfID shelf_id(app_id); | 63 const ShelfID shelf_id(app_id); |
52 | 64 |
53 // If the app is already pinned, do nothing and return. | 65 // If the app is already pinned, do nothing and return. |
54 if (IsAppPinned(shelf_id.app_id)) | 66 if (IsAppPinned(shelf_id.app_id)) |
55 return; | 67 return; |
56 | 68 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // need early cleanup. | 111 // need early cleanup. |
100 id_to_item_delegate_map_.clear(); | 112 id_to_item_delegate_map_.clear(); |
101 } | 113 } |
102 | 114 |
103 int ShelfModel::Add(const ShelfItem& item) { | 115 int ShelfModel::Add(const ShelfItem& item) { |
104 return AddAt(items_.size(), item); | 116 return AddAt(items_.size(), item); |
105 } | 117 } |
106 | 118 |
107 int ShelfModel::AddAt(int index, const ShelfItem& item) { | 119 int ShelfModel::AddAt(int index, const ShelfItem& item) { |
108 // Items should have unique non-empty ids to avoid undefined model behavior. | 120 // Items should have unique non-empty ids to avoid undefined model behavior. |
109 DCHECK(!item.id.IsNull()); | 121 DCHECK(!item.id.IsNull()) << " The id is null."; |
110 DCHECK_EQ(ItemIndexByID(item.id), -1); | 122 DCHECK_EQ(ItemIndexByID(item.id), -1) << " The id is not unique: " << item.id; |
111 index = ValidateInsertionIndex(item.type, index); | 123 index = ValidateInsertionIndex(item.type, index); |
112 items_.insert(items_.begin() + index, item); | 124 items_.insert(items_.begin() + index, item); |
113 for (auto& observer : observers_) | 125 for (auto& observer : observers_) |
114 observer.ShelfItemAdded(index); | 126 observer.ShelfItemAdded(index); |
115 return index; | 127 return index; |
116 } | 128 } |
117 | 129 |
118 void ShelfModel::RemoveItemAt(int index) { | 130 void ShelfModel::RemoveItemAt(int index) { |
119 DCHECK(index >= 0 && index < item_count()); | 131 DCHECK(index >= 0 && index < item_count()); |
120 ShelfItem old_item(items_[index]); | 132 ShelfItem old_item(items_[index]); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 items_.begin(); | 215 items_.begin(); |
204 } | 216 } |
205 | 217 |
206 void ShelfModel::SetShelfItemDelegate( | 218 void ShelfModel::SetShelfItemDelegate( |
207 const ShelfID& shelf_id, | 219 const ShelfID& shelf_id, |
208 std::unique_ptr<ShelfItemDelegate> item_delegate) { | 220 std::unique_ptr<ShelfItemDelegate> item_delegate) { |
209 if (item_delegate) | 221 if (item_delegate) |
210 item_delegate->set_shelf_id(shelf_id); | 222 item_delegate->set_shelf_id(shelf_id); |
211 // This assignment replaces any ShelfItemDelegate already registered for |id|. | 223 // This assignment replaces any ShelfItemDelegate already registered for |id|. |
212 id_to_item_delegate_map_[shelf_id] = std::move(item_delegate); | 224 id_to_item_delegate_map_[shelf_id] = std::move(item_delegate); |
| 225 for (auto& observer : observers_) { |
| 226 observer.ShelfItemDelegateChanged(shelf_id, |
| 227 id_to_item_delegate_map_[shelf_id].get()); |
| 228 } |
213 } | 229 } |
214 | 230 |
215 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(const ShelfID& shelf_id) { | 231 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(const ShelfID& shelf_id) { |
216 auto it = id_to_item_delegate_map_.find(shelf_id); | 232 auto it = id_to_item_delegate_map_.find(shelf_id); |
217 if (it != id_to_item_delegate_map_.end()) | 233 if (it != id_to_item_delegate_map_.end()) |
218 return it->second.get(); | 234 return it->second.get(); |
219 return nullptr; | 235 return nullptr; |
220 } | 236 } |
221 | 237 |
222 void ShelfModel::AddObserver(ShelfModelObserver* observer) { | 238 void ShelfModel::AddObserver(ShelfModelObserver* observer) { |
(...skipping 16 matching lines...) Expand all Loading... |
239 static_cast<ShelfItems::difference_type>(index)); | 255 static_cast<ShelfItems::difference_type>(index)); |
240 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 256 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
241 CompareByWeight) - | 257 CompareByWeight) - |
242 items_.begin(), | 258 items_.begin(), |
243 static_cast<ShelfItems::difference_type>(index)); | 259 static_cast<ShelfItems::difference_type>(index)); |
244 | 260 |
245 return index; | 261 return index; |
246 } | 262 } |
247 | 263 |
248 } // namespace ash | 264 } // namespace ash |
OLD | NEW |