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

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

Issue 1585363002: Fork a subset of ash/shelf for use in mash. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revert ash changes. Created 4 years, 10 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
OLDNEW
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 "mash/shelf/shelf_model.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ash/ash_switches.h" 9 #include "mash/shelf/shelf_model_observer.h"
10 #include "ash/shelf/shelf_model_observer.h" 10 #include "mojo/common/common_type_converters.h"
11 #include "mojo/shell/public/cpp/application_impl.h"
11 12
12 namespace ash { 13 namespace mash {
14 namespace shelf {
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.
21 return 0; 23 return 0;
22 case TYPE_BROWSER_SHORTCUT: 24 case TYPE_BROWSER_SHORTCUT:
23 case TYPE_APP_SHORTCUT: 25 case TYPE_APP_SHORTCUT:
24 return 1; 26 return 1;
25 case TYPE_WINDOWED_APP: 27 case TYPE_WINDOWED_APP:
26 case TYPE_PLATFORM_APP: 28 case TYPE_PLATFORM_APP:
29 case TYPE_MOJO_APP:
27 return 2; 30 return 2;
28 case TYPE_DIALOG: 31 case TYPE_DIALOG:
29 return 3; 32 return 3;
30 case TYPE_APP_PANEL: 33 case TYPE_APP_PANEL:
31 return 4; 34 return 4;
32 case TYPE_UNDEFINED: 35 case TYPE_UNDEFINED:
33 NOTREACHED() << "ShelfItemType must be set"; 36 NOTREACHED() << "ShelfItemType must be set";
34 return -1; 37 return -1;
35 } 38 }
36 39
37 NOTREACHED() << "Invalid type " << type; 40 NOTREACHED() << "Invalid type " << type;
38 return 1; 41 return 1;
39 } 42 }
40 43
41 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { 44 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) {
42 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); 45 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type);
43 } 46 }
44 47
45 } // namespace 48 } // namespace
46 49
47 ShelfModel::ShelfModel() : next_id_(1), status_(STATUS_NORMAL) { 50 ShelfModel::ShelfModel(mojo::ApplicationImpl* app)
51 : next_id_(1), status_(STATUS_NORMAL), binding_(this) {
52 app->ConnectToService("mojo:desktop_wm", &user_window_controller_);
53 user_window_controller_->AddUserWindowObserver(
54 binding_.CreateInterfacePtrAndBind());
48 } 55 }
49 56
50 ShelfModel::~ShelfModel() { 57 ShelfModel::~ShelfModel() {
51 } 58 }
52 59
53 int ShelfModel::Add(const ShelfItem& item) { 60 int ShelfModel::Add(const ShelfItem& item) {
54 return AddAt(items_.size(), item); 61 return AddAt(static_cast<int>(items_.size()), item);
55 } 62 }
56 63
57 int ShelfModel::AddAt(int index, const ShelfItem& item) { 64 int ShelfModel::AddAt(int index, const ShelfItem& item) {
58 index = ValidateInsertionIndex(item.type, index); 65 index = ValidateInsertionIndex(item.type, index);
59 items_.insert(items_.begin() + index, item); 66 items_.insert(items_.begin() + index, item);
60 items_[index].id = next_id_++; 67 items_[index].id = next_id_++;
61 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); 68 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index));
62 return index; 69 return index;
63 } 70 }
64 71
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 } 118 }
112 119
113 int ShelfModel::ItemIndexByID(ShelfID id) const { 120 int ShelfModel::ItemIndexByID(ShelfID id) const {
114 ShelfItems::const_iterator i = ItemByID(id); 121 ShelfItems::const_iterator i = ItemByID(id);
115 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin()); 122 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin());
116 } 123 }
117 124
118 int ShelfModel::GetItemIndexForType(ShelfItemType type) { 125 int ShelfModel::GetItemIndexForType(ShelfItemType type) {
119 for (size_t i = 0; i < items_.size(); ++i) { 126 for (size_t i = 0; i < items_.size(); ++i) {
120 if (items_[i].type == type) 127 if (items_[i].type == type)
121 return i; 128 return static_cast<int>(i);
122 } 129 }
123 return -1; 130 return -1;
124 } 131 }
125 132
126 ShelfItems::const_iterator ShelfModel::ItemByID(int id) const { 133 ShelfItems::const_iterator ShelfModel::ItemByID(int id) const {
127 for (ShelfItems::const_iterator i = items_.begin(); 134 for (ShelfItems::const_iterator i = items_.begin();
128 i != items_.end(); ++i) { 135 i != items_.end(); ++i) {
129 if (i->id == id) 136 if (i->id == id)
130 return i; 137 return i;
131 } 138 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, 182 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy,
176 CompareByWeight) - items_.begin(), 183 CompareByWeight) - items_.begin(),
177 static_cast<ShelfItems::difference_type>(index)); 184 static_cast<ShelfItems::difference_type>(index));
178 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, 185 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy,
179 CompareByWeight) - items_.begin(), 186 CompareByWeight) - items_.begin(),
180 static_cast<ShelfItems::difference_type>(index)); 187 static_cast<ShelfItems::difference_type>(index));
181 188
182 return index; 189 return index;
183 } 190 }
184 191
185 } // namespace ash 192 void ShelfModel::OnUserWindowObserverAdded(
193 mojo::Array<mash::wm::mojom::UserWindowPtr> user_windows) {
194 for (size_t i = 0; i < user_windows.size(); ++i)
195 OnUserWindowAdded(std::move(user_windows[i]));
196 }
197
198 void ShelfModel::OnUserWindowAdded(mash::wm::mojom::UserWindowPtr user_window) {
199 ShelfItem item;
200 item.type = TYPE_MOJO_APP;
201 item.status = STATUS_RUNNING;
202 item.window_id = user_window->window_id;
203 item.title = user_window->window_title.To<base::string16>();
204 Add(item);
205 }
206
207 void ShelfModel::OnUserWindowRemoved(uint32_t window_id) {
208 RemoveItemAt(ItemIndexByWindowID(window_id));
209 }
210
211 void ShelfModel::OnUserWindowTitleChanged(uint32_t window_id,
212 const mojo::String& window_title) {
213 const int index = ItemIndexByWindowID(window_id);
214 ShelfItem old_item(items_[index]);
215 items_[index].title = window_title.To<base::string16>();
216 FOR_EACH_OBSERVER(ShelfModelObserver, observers_,
217 ShelfItemChanged(index, old_item));
218 }
219
220 int ShelfModel::ItemIndexByWindowID(uint32_t window_id) const {
221 for (size_t i = 0; i < items_.size(); ++i) {
222 if (items_[i].window_id == window_id)
223 return static_cast<int>(i);
224 }
225 return -1;
226 }
227
228 } // namespace shelf
229 } // namespace mash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698