OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/launcher/launcher_model.h" | 5 #include "ash/launcher/launcher_model.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
10 #include "ash/launcher/launcher_model_observer.h" | 10 #include "ash/shelf/shelf_model_observer.h" |
11 | 11 |
12 namespace ash { | 12 namespace ash { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
16 int LauncherItemTypeToWeight(LauncherItemType type) { | 16 int LauncherItemTypeToWeight(LauncherItemType type) { |
17 if (ash::switches::UseAlternateShelfLayout()) { | 17 if (ash::switches::UseAlternateShelfLayout()) { |
18 switch (type) { | 18 switch (type) { |
19 case TYPE_APP_LIST: | 19 case TYPE_APP_LIST: |
20 // TODO(skuhne): If the app list item becomes movable again, this need | 20 // TODO(skuhne): If the app list item becomes movable again, this need |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 67 } |
68 | 68 |
69 int LauncherModel::Add(const LauncherItem& item) { | 69 int LauncherModel::Add(const LauncherItem& item) { |
70 return AddAt(items_.size(), item); | 70 return AddAt(items_.size(), item); |
71 } | 71 } |
72 | 72 |
73 int LauncherModel::AddAt(int index, const LauncherItem& item) { | 73 int LauncherModel::AddAt(int index, const LauncherItem& item) { |
74 index = ValidateInsertionIndex(item.type, index); | 74 index = ValidateInsertionIndex(item.type, index); |
75 items_.insert(items_.begin() + index, item); | 75 items_.insert(items_.begin() + index, item); |
76 items_[index].id = next_id_++; | 76 items_[index].id = next_id_++; |
77 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 77 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); |
78 LauncherItemAdded(index)); | |
79 return index; | 78 return index; |
80 } | 79 } |
81 | 80 |
82 void LauncherModel::RemoveItemAt(int index) { | 81 void LauncherModel::RemoveItemAt(int index) { |
83 DCHECK(index >= 0 && index < item_count()); | 82 DCHECK(index >= 0 && index < item_count()); |
84 // The app list and browser shortcut can't be removed. | 83 // The app list and browser shortcut can't be removed. |
85 DCHECK(items_[index].type != TYPE_APP_LIST && | 84 DCHECK(items_[index].type != TYPE_APP_LIST && |
86 items_[index].type != TYPE_BROWSER_SHORTCUT); | 85 items_[index].type != TYPE_BROWSER_SHORTCUT); |
87 LauncherID id = items_[index].id; | 86 LauncherID id = items_[index].id; |
88 items_.erase(items_.begin() + index); | 87 items_.erase(items_.begin() + index); |
89 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 88 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
90 LauncherItemRemoved(index, id)); | 89 ShelfItemRemoved(index, id)); |
91 } | 90 } |
92 | 91 |
93 void LauncherModel::Move(int index, int target_index) { | 92 void LauncherModel::Move(int index, int target_index) { |
94 if (index == target_index) | 93 if (index == target_index) |
95 return; | 94 return; |
96 // TODO: this needs to enforce valid ranges. | 95 // TODO: this needs to enforce valid ranges. |
97 LauncherItem item(items_[index]); | 96 LauncherItem item(items_[index]); |
98 items_.erase(items_.begin() + index); | 97 items_.erase(items_.begin() + index); |
99 items_.insert(items_.begin() + target_index, item); | 98 items_.insert(items_.begin() + target_index, item); |
100 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 99 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
101 LauncherItemMoved(index, target_index)); | 100 ShelfItemMoved(index, target_index)); |
102 } | 101 } |
103 | 102 |
104 void LauncherModel::Set(int index, const LauncherItem& item) { | 103 void LauncherModel::Set(int index, const LauncherItem& item) { |
105 DCHECK(index >= 0 && index < item_count()); | 104 DCHECK(index >= 0 && index < item_count()); |
106 int new_index = item.type == items_[index].type ? | 105 int new_index = item.type == items_[index].type ? |
107 index : ValidateInsertionIndex(item.type, index); | 106 index : ValidateInsertionIndex(item.type, index); |
108 | 107 |
109 LauncherItem old_item(items_[index]); | 108 LauncherItem old_item(items_[index]); |
110 items_[index] = item; | 109 items_[index] = item; |
111 items_[index].id = old_item.id; | 110 items_[index].id = old_item.id; |
112 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 111 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
113 LauncherItemChanged(index, old_item)); | 112 ShelfItemChanged(index, old_item)); |
114 | 113 |
115 // If the type changes confirm that the item is still in the right order. | 114 // If the type changes confirm that the item is still in the right order. |
116 if (new_index != index) { | 115 if (new_index != index) { |
117 // The move function works by removing one item and then inserting it at the | 116 // The move function works by removing one item and then inserting it at the |
118 // new location. However - by removing the item first the order will change | 117 // new location. However - by removing the item first the order will change |
119 // so that our target index needs to be corrected. | 118 // so that our target index needs to be corrected. |
120 // TODO(skuhne): Moving this into the Move function breaks lots of unit | 119 // TODO(skuhne): Moving this into the Move function breaks lots of unit |
121 // tests. So several functions were already using this incorrectly. | 120 // tests. So several functions were already using this incorrectly. |
122 // That needs to be cleaned up. | 121 // That needs to be cleaned up. |
123 if (index < new_index) | 122 if (index < new_index) |
(...skipping 22 matching lines...) Expand all Loading... |
146 weight_dummy.type = TYPE_APP_PANEL; | 145 weight_dummy.type = TYPE_APP_PANEL; |
147 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 146 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
148 CompareByWeight) - items_.begin(); | 147 CompareByWeight) - items_.begin(); |
149 } | 148 } |
150 | 149 |
151 void LauncherModel::SetStatus(Status status) { | 150 void LauncherModel::SetStatus(Status status) { |
152 if (status_ == status) | 151 if (status_ == status) |
153 return; | 152 return; |
154 | 153 |
155 status_ = status; | 154 status_ = status; |
156 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 155 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfStatusChanged()); |
157 LauncherStatusChanged()); | |
158 } | 156 } |
159 | 157 |
160 void LauncherModel::AddObserver(LauncherModelObserver* observer) { | 158 void LauncherModel::AddObserver(ShelfModelObserver* observer) { |
161 observers_.AddObserver(observer); | 159 observers_.AddObserver(observer); |
162 } | 160 } |
163 | 161 |
164 void LauncherModel::RemoveObserver(LauncherModelObserver* observer) { | 162 void LauncherModel::RemoveObserver(ShelfModelObserver* observer) { |
165 observers_.RemoveObserver(observer); | 163 observers_.RemoveObserver(observer); |
166 } | 164 } |
167 | 165 |
168 int LauncherModel::ValidateInsertionIndex(LauncherItemType type, | 166 int LauncherModel::ValidateInsertionIndex(LauncherItemType type, |
169 int index) const { | 167 int index) const { |
170 DCHECK(index >= 0 && index <= item_count() + | 168 DCHECK(index >= 0 && index <= item_count() + |
171 (ash::switches::UseAlternateShelfLayout() ? 1 : 0)); | 169 (ash::switches::UseAlternateShelfLayout() ? 1 : 0)); |
172 | 170 |
173 // Clamp |index| to the allowed range for the type as determined by |weight|. | 171 // Clamp |index| to the allowed range for the type as determined by |weight|. |
174 LauncherItem weight_dummy; | 172 LauncherItem weight_dummy; |
175 weight_dummy.type = type; | 173 weight_dummy.type = type; |
176 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 174 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
177 CompareByWeight) - items_.begin(), | 175 CompareByWeight) - items_.begin(), |
178 static_cast<LauncherItems::difference_type>(index)); | 176 static_cast<LauncherItems::difference_type>(index)); |
179 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 177 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
180 CompareByWeight) - items_.begin(), | 178 CompareByWeight) - items_.begin(), |
181 static_cast<LauncherItems::difference_type>(index)); | 179 static_cast<LauncherItems::difference_type>(index)); |
182 | 180 |
183 return index; | 181 return index; |
184 } | 182 } |
185 | 183 |
186 } // namespace ash | 184 } // namespace ash |
OLD | NEW |