Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: ash/shelf/shelf_model.cc

Issue 2917013002: mash: Move ShelfModel* to ash/public/cpp. (Closed)
Patch Set: Comment on AppList item init. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/shelf/shelf_model.h ('k') | ash/shelf/shelf_model_observer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/shelf/shelf_model.h"
6
7 #include <algorithm>
8
9 #include "ash/public/cpp/shelf_item_delegate.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"
13
14 namespace ash {
15
16 namespace {
17
18 int ShelfItemTypeToWeight(ShelfItemType type) {
19 switch (type) {
20 case TYPE_APP_LIST:
21 // TODO(skuhne): If the app list item becomes movable again, this need
22 // to be a fallthrough.
23 return 0;
24 case TYPE_BROWSER_SHORTCUT:
25 case TYPE_PINNED_APP:
26 return 1;
27 case TYPE_APP:
28 return 2;
29 case TYPE_DIALOG:
30 return 3;
31 case TYPE_APP_PANEL:
32 return 4;
33 case TYPE_UNDEFINED:
34 NOTREACHED() << "ShelfItemType must be set";
35 return -1;
36 }
37
38 NOTREACHED() << "Invalid type " << type;
39 return 1;
40 }
41
42 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) {
43 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type);
44 }
45
46 } // namespace
47
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 }
59
60 ShelfModel::~ShelfModel() = default;
61
62 void ShelfModel::PinAppWithID(const std::string& app_id) {
63 const ShelfID shelf_id(app_id);
64
65 // If the app is already pinned, do nothing and return.
66 if (IsAppPinned(shelf_id.app_id))
67 return;
68
69 // Convert an existing item to be pinned, or create a new pinned item.
70 const int index = ItemIndexByID(shelf_id);
71 if (index >= 0) {
72 ShelfItem item = items_[index];
73 DCHECK_EQ(item.type, TYPE_APP);
74 DCHECK(!item.pinned_by_policy);
75 item.type = TYPE_PINNED_APP;
76 Set(index, item);
77 } else if (!shelf_id.IsNull()) {
78 ShelfItem item;
79 item.type = TYPE_PINNED_APP;
80 item.id = shelf_id;
81 Add(item);
82 }
83 }
84
85 bool ShelfModel::IsAppPinned(const std::string& app_id) {
86 const int index = ItemIndexByID(ShelfID(app_id));
87 return index >= 0 && (items_[index].type == TYPE_PINNED_APP ||
88 items_[index].type == TYPE_BROWSER_SHORTCUT);
89 }
90
91 void ShelfModel::UnpinAppWithID(const std::string& app_id) {
92 // If the app is already not pinned, do nothing and return.
93 if (!IsAppPinned(app_id))
94 return;
95
96 // Remove the item if it is closed, or mark it as unpinned.
97 const int index = ItemIndexByID(ShelfID(app_id));
98 ShelfItem item = items_[index];
99 DCHECK_EQ(item.type, TYPE_PINNED_APP);
100 DCHECK(!item.pinned_by_policy);
101 if (item.status == STATUS_CLOSED) {
102 RemoveItemAt(index);
103 } else {
104 item.type = TYPE_APP;
105 Set(index, item);
106 }
107 }
108
109 void ShelfModel::DestroyItemDelegates() {
110 // Some ShelfItemDelegates access this model in their destructors and hence
111 // need early cleanup.
112 id_to_item_delegate_map_.clear();
113 }
114
115 int ShelfModel::Add(const ShelfItem& item) {
116 return AddAt(items_.size(), item);
117 }
118
119 int ShelfModel::AddAt(int index, const ShelfItem& item) {
120 // Items should have unique non-empty ids to avoid undefined model behavior.
121 DCHECK(!item.id.IsNull()) << " The id is null.";
122 DCHECK_EQ(ItemIndexByID(item.id), -1) << " The id is not unique: " << item.id;
123 index = ValidateInsertionIndex(item.type, index);
124 items_.insert(items_.begin() + index, item);
125 for (auto& observer : observers_)
126 observer.ShelfItemAdded(index);
127 return index;
128 }
129
130 void ShelfModel::RemoveItemAt(int index) {
131 DCHECK(index >= 0 && index < item_count());
132 ShelfItem old_item(items_[index]);
133 items_.erase(items_.begin() + index);
134 id_to_item_delegate_map_.erase(old_item.id);
135 for (auto& observer : observers_)
136 observer.ShelfItemRemoved(index, old_item);
137 }
138
139 void ShelfModel::Move(int index, int target_index) {
140 if (index == target_index)
141 return;
142 // TODO: this needs to enforce valid ranges.
143 ShelfItem item(items_[index]);
144 items_.erase(items_.begin() + index);
145 items_.insert(items_.begin() + target_index, item);
146 for (auto& observer : observers_)
147 observer.ShelfItemMoved(index, target_index);
148 }
149
150 void ShelfModel::Set(int index, const ShelfItem& item) {
151 if (index < 0 || index >= item_count()) {
152 NOTREACHED();
153 return;
154 }
155
156 int new_index = item.type == items_[index].type
157 ? index
158 : ValidateInsertionIndex(item.type, index);
159
160 ShelfItem old_item(items_[index]);
161 items_[index] = item;
162 DCHECK(old_item.id == item.id);
163 for (auto& observer : observers_)
164 observer.ShelfItemChanged(index, old_item);
165
166 // If the type changes confirm that the item is still in the right order.
167 if (new_index != index) {
168 // The move function works by removing one item and then inserting it at the
169 // new location. However - by removing the item first the order will change
170 // so that our target index needs to be corrected.
171 // TODO(skuhne): Moving this into the Move function breaks lots of unit
172 // tests. So several functions were already using this incorrectly.
173 // That needs to be cleaned up.
174 if (index < new_index)
175 new_index--;
176
177 Move(index, new_index);
178 }
179 }
180
181 int ShelfModel::ItemIndexByID(const ShelfID& shelf_id) const {
182 ShelfItems::const_iterator i = ItemByID(shelf_id);
183 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin());
184 }
185
186 int ShelfModel::GetItemIndexForType(ShelfItemType type) {
187 for (size_t i = 0; i < items_.size(); ++i) {
188 if (items_[i].type == type)
189 return i;
190 }
191 return -1;
192 }
193
194 ShelfItems::const_iterator ShelfModel::ItemByID(const ShelfID& shelf_id) const {
195 for (ShelfItems::const_iterator i = items_.begin(); i != items_.end(); ++i) {
196 if (i->id == shelf_id)
197 return i;
198 }
199 return items_.end();
200 }
201
202 int ShelfModel::FirstRunningAppIndex() const {
203 ShelfItem weight_dummy;
204 weight_dummy.type = TYPE_APP;
205 return std::lower_bound(items_.begin(), items_.end(), weight_dummy,
206 CompareByWeight) -
207 items_.begin();
208 }
209
210 int ShelfModel::FirstPanelIndex() const {
211 ShelfItem weight_dummy;
212 weight_dummy.type = TYPE_APP_PANEL;
213 return std::lower_bound(items_.begin(), items_.end(), weight_dummy,
214 CompareByWeight) -
215 items_.begin();
216 }
217
218 void ShelfModel::SetShelfItemDelegate(
219 const ShelfID& shelf_id,
220 std::unique_ptr<ShelfItemDelegate> item_delegate) {
221 if (item_delegate)
222 item_delegate->set_shelf_id(shelf_id);
223 // This assignment replaces any ShelfItemDelegate already registered for |id|.
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 }
229 }
230
231 ShelfItemDelegate* ShelfModel::GetShelfItemDelegate(const ShelfID& shelf_id) {
232 auto it = id_to_item_delegate_map_.find(shelf_id);
233 if (it != id_to_item_delegate_map_.end())
234 return it->second.get();
235 return nullptr;
236 }
237
238 void ShelfModel::AddObserver(ShelfModelObserver* observer) {
239 observers_.AddObserver(observer);
240 }
241
242 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) {
243 observers_.RemoveObserver(observer);
244 }
245
246 int ShelfModel::ValidateInsertionIndex(ShelfItemType type, int index) const {
247 DCHECK(index >= 0 && index <= item_count() + 1);
248
249 // Clamp |index| to the allowed range for the type as determined by |weight|.
250 ShelfItem weight_dummy;
251 weight_dummy.type = type;
252 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy,
253 CompareByWeight) -
254 items_.begin(),
255 static_cast<ShelfItems::difference_type>(index));
256 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy,
257 CompareByWeight) -
258 items_.begin(),
259 static_cast<ShelfItems::difference_type>(index));
260
261 return index;
262 }
263
264 } // namespace ash
OLDNEW
« no previous file with comments | « ash/shelf/shelf_model.h ('k') | ash/shelf/shelf_model_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698