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 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 NOTREACHED() << "Invalid type " << type; | 38 NOTREACHED() << "Invalid type " << type; |
39 return 1; | 39 return 1; |
40 } | 40 } |
41 | 41 |
42 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { | 42 bool CompareByWeight(const ShelfItem& a, const ShelfItem& b) { |
43 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); | 43 return ShelfItemTypeToWeight(a.type) < ShelfItemTypeToWeight(b.type); |
44 } | 44 } |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 ShelfModel::ShelfModel() : next_id_(1), status_(STATUS_NORMAL) { | 48 ShelfModel::ShelfModel() : next_id_(1), status_(STATUS_NORMAL) {} |
49 } | |
50 | 49 |
51 ShelfModel::~ShelfModel() { | 50 ShelfModel::~ShelfModel() {} |
52 } | |
53 | 51 |
54 int ShelfModel::Add(const ShelfItem& item) { | 52 int ShelfModel::Add(const ShelfItem& item) { |
55 return AddAt(items_.size(), item); | 53 return AddAt(items_.size(), item); |
56 } | 54 } |
57 | 55 |
58 int ShelfModel::AddAt(int index, const ShelfItem& item) { | 56 int ShelfModel::AddAt(int index, const ShelfItem& item) { |
59 index = ValidateInsertionIndex(item.type, index); | 57 index = ValidateInsertionIndex(item.type, index); |
60 items_.insert(items_.begin() + index, item); | 58 items_.insert(items_.begin() + index, item); |
61 items_[index].id = next_id_++; | 59 items_[index].id = next_id_++; |
62 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); | 60 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); |
(...skipping 17 matching lines...) Expand all Loading... |
80 // TODO: this needs to enforce valid ranges. | 78 // TODO: this needs to enforce valid ranges. |
81 ShelfItem item(items_[index]); | 79 ShelfItem item(items_[index]); |
82 items_.erase(items_.begin() + index); | 80 items_.erase(items_.begin() + index); |
83 items_.insert(items_.begin() + target_index, item); | 81 items_.insert(items_.begin() + target_index, item); |
84 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 82 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
85 ShelfItemMoved(index, target_index)); | 83 ShelfItemMoved(index, target_index)); |
86 } | 84 } |
87 | 85 |
88 void ShelfModel::Set(int index, const ShelfItem& item) { | 86 void ShelfModel::Set(int index, const ShelfItem& item) { |
89 DCHECK(index >= 0 && index < item_count()); | 87 DCHECK(index >= 0 && index < item_count()); |
90 int new_index = item.type == items_[index].type ? | 88 int new_index = item.type == items_[index].type |
91 index : ValidateInsertionIndex(item.type, index); | 89 ? index |
| 90 : ValidateInsertionIndex(item.type, index); |
92 | 91 |
93 ShelfItem old_item(items_[index]); | 92 ShelfItem old_item(items_[index]); |
94 items_[index] = item; | 93 items_[index] = item; |
95 items_[index].id = old_item.id; | 94 items_[index].id = old_item.id; |
96 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 95 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
97 ShelfItemChanged(index, old_item)); | 96 ShelfItemChanged(index, old_item)); |
98 | 97 |
99 // If the type changes confirm that the item is still in the right order. | 98 // If the type changes confirm that the item is still in the right order. |
100 if (new_index != index) { | 99 if (new_index != index) { |
101 // The move function works by removing one item and then inserting it at the | 100 // The move function works by removing one item and then inserting it at the |
(...skipping 16 matching lines...) Expand all Loading... |
118 | 117 |
119 int ShelfModel::GetItemIndexForType(ShelfItemType type) { | 118 int ShelfModel::GetItemIndexForType(ShelfItemType type) { |
120 for (size_t i = 0; i < items_.size(); ++i) { | 119 for (size_t i = 0; i < items_.size(); ++i) { |
121 if (items_[i].type == type) | 120 if (items_[i].type == type) |
122 return i; | 121 return i; |
123 } | 122 } |
124 return -1; | 123 return -1; |
125 } | 124 } |
126 | 125 |
127 ShelfItems::const_iterator ShelfModel::ItemByID(int id) const { | 126 ShelfItems::const_iterator ShelfModel::ItemByID(int id) const { |
128 for (ShelfItems::const_iterator i = items_.begin(); | 127 for (ShelfItems::const_iterator i = items_.begin(); i != items_.end(); ++i) { |
129 i != items_.end(); ++i) { | |
130 if (i->id == id) | 128 if (i->id == id) |
131 return i; | 129 return i; |
132 } | 130 } |
133 return items_.end(); | 131 return items_.end(); |
134 } | 132 } |
135 | 133 |
136 int ShelfModel::FirstRunningAppIndex() const { | 134 int ShelfModel::FirstRunningAppIndex() const { |
137 // Since lower_bound only checks weights against each other, we do not need | 135 // Since lower_bound only checks weights against each other, we do not need |
138 // to explicitly change different running application types. | 136 // to explicitly change different running application types. |
139 DCHECK_EQ(ShelfItemTypeToWeight(TYPE_WINDOWED_APP), | 137 DCHECK_EQ(ShelfItemTypeToWeight(TYPE_WINDOWED_APP), |
140 ShelfItemTypeToWeight(TYPE_PLATFORM_APP)); | 138 ShelfItemTypeToWeight(TYPE_PLATFORM_APP)); |
141 ShelfItem weight_dummy; | 139 ShelfItem weight_dummy; |
142 weight_dummy.type = TYPE_WINDOWED_APP; | 140 weight_dummy.type = TYPE_WINDOWED_APP; |
143 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 141 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
144 CompareByWeight) - items_.begin(); | 142 CompareByWeight) - |
| 143 items_.begin(); |
145 } | 144 } |
146 | 145 |
147 int ShelfModel::FirstPanelIndex() const { | 146 int ShelfModel::FirstPanelIndex() const { |
148 ShelfItem weight_dummy; | 147 ShelfItem weight_dummy; |
149 weight_dummy.type = TYPE_APP_PANEL; | 148 weight_dummy.type = TYPE_APP_PANEL; |
150 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 149 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
151 CompareByWeight) - items_.begin(); | 150 CompareByWeight) - |
| 151 items_.begin(); |
152 } | 152 } |
153 | 153 |
154 void ShelfModel::AddObserver(ShelfModelObserver* observer) { | 154 void ShelfModel::AddObserver(ShelfModelObserver* observer) { |
155 observers_.AddObserver(observer); | 155 observers_.AddObserver(observer); |
156 } | 156 } |
157 | 157 |
158 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) { | 158 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) { |
159 observers_.RemoveObserver(observer); | 159 observers_.RemoveObserver(observer); |
160 } | 160 } |
161 | 161 |
162 int ShelfModel::ValidateInsertionIndex(ShelfItemType type, int index) const { | 162 int ShelfModel::ValidateInsertionIndex(ShelfItemType type, int index) const { |
163 DCHECK(index >= 0 && index <= item_count() + 1); | 163 DCHECK(index >= 0 && index <= item_count() + 1); |
164 | 164 |
165 // Clamp |index| to the allowed range for the type as determined by |weight|. | 165 // Clamp |index| to the allowed range for the type as determined by |weight|. |
166 ShelfItem weight_dummy; | 166 ShelfItem weight_dummy; |
167 weight_dummy.type = type; | 167 weight_dummy.type = type; |
168 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 168 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
169 CompareByWeight) - items_.begin(), | 169 CompareByWeight) - |
| 170 items_.begin(), |
170 static_cast<ShelfItems::difference_type>(index)); | 171 static_cast<ShelfItems::difference_type>(index)); |
171 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 172 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
172 CompareByWeight) - items_.begin(), | 173 CompareByWeight) - |
| 174 items_.begin(), |
173 static_cast<ShelfItems::difference_type>(index)); | 175 static_cast<ShelfItems::difference_type>(index)); |
174 | 176 |
175 return index; | 177 return index; |
176 } | 178 } |
177 | 179 |
178 } // namespace ash | 180 } // namespace ash |
OLD | NEW |