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/shelf/shelf_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/shelf/shelf_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 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 NOTREACHED() << "Invalid type " << type; | 53 NOTREACHED() << "Invalid type " << type; |
54 return 1; | 54 return 1; |
55 } | 55 } |
56 | 56 |
57 bool CompareByWeight(const LauncherItem& a, const LauncherItem& b) { | 57 bool CompareByWeight(const LauncherItem& a, const LauncherItem& b) { |
58 return LauncherItemTypeToWeight(a.type) < LauncherItemTypeToWeight(b.type); | 58 return LauncherItemTypeToWeight(a.type) < LauncherItemTypeToWeight(b.type); |
59 } | 59 } |
60 | 60 |
61 } // namespace | 61 } // namespace |
62 | 62 |
63 LauncherModel::LauncherModel() : next_id_(1), status_(STATUS_NORMAL) { | 63 ShelfModel::ShelfModel() : next_id_(1), status_(STATUS_NORMAL) { |
64 } | 64 } |
65 | 65 |
66 LauncherModel::~LauncherModel() { | 66 ShelfModel::~ShelfModel() { |
67 } | 67 } |
68 | 68 |
69 int LauncherModel::Add(const LauncherItem& item) { | 69 int ShelfModel::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 ShelfModel::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(ShelfModelObserver, observers_, ShelfItemAdded(index)); | 77 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfItemAdded(index)); |
78 return index; | 78 return index; |
79 } | 79 } |
80 | 80 |
81 void LauncherModel::RemoveItemAt(int index) { | 81 void ShelfModel::RemoveItemAt(int index) { |
82 DCHECK(index >= 0 && index < item_count()); | 82 DCHECK(index >= 0 && index < item_count()); |
83 // The app list and browser shortcut can't be removed. | 83 // The app list and browser shortcut can't be removed. |
84 DCHECK(items_[index].type != TYPE_APP_LIST && | 84 DCHECK(items_[index].type != TYPE_APP_LIST && |
85 items_[index].type != TYPE_BROWSER_SHORTCUT); | 85 items_[index].type != TYPE_BROWSER_SHORTCUT); |
86 LauncherID id = items_[index].id; | 86 LauncherID id = items_[index].id; |
87 items_.erase(items_.begin() + index); | 87 items_.erase(items_.begin() + index); |
88 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 88 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
89 ShelfItemRemoved(index, id)); | 89 ShelfItemRemoved(index, id)); |
90 } | 90 } |
91 | 91 |
92 void LauncherModel::Move(int index, int target_index) { | 92 void ShelfModel::Move(int index, int target_index) { |
93 if (index == target_index) | 93 if (index == target_index) |
94 return; | 94 return; |
95 // TODO: this needs to enforce valid ranges. | 95 // TODO: this needs to enforce valid ranges. |
96 LauncherItem item(items_[index]); | 96 LauncherItem item(items_[index]); |
97 items_.erase(items_.begin() + index); | 97 items_.erase(items_.begin() + index); |
98 items_.insert(items_.begin() + target_index, item); | 98 items_.insert(items_.begin() + target_index, item); |
99 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 99 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
100 ShelfItemMoved(index, target_index)); | 100 ShelfItemMoved(index, target_index)); |
101 } | 101 } |
102 | 102 |
103 void LauncherModel::Set(int index, const LauncherItem& item) { | 103 void ShelfModel::Set(int index, const LauncherItem& item) { |
104 DCHECK(index >= 0 && index < item_count()); | 104 DCHECK(index >= 0 && index < item_count()); |
105 int new_index = item.type == items_[index].type ? | 105 int new_index = item.type == items_[index].type ? |
106 index : ValidateInsertionIndex(item.type, index); | 106 index : ValidateInsertionIndex(item.type, index); |
107 | 107 |
108 LauncherItem old_item(items_[index]); | 108 LauncherItem old_item(items_[index]); |
109 items_[index] = item; | 109 items_[index] = item; |
110 items_[index].id = old_item.id; | 110 items_[index].id = old_item.id; |
111 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, | 111 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, |
112 ShelfItemChanged(index, old_item)); | 112 ShelfItemChanged(index, old_item)); |
113 | 113 |
114 // 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. |
115 if (new_index != index) { | 115 if (new_index != index) { |
116 // 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 |
117 // 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 |
118 // so that our target index needs to be corrected. | 118 // so that our target index needs to be corrected. |
119 // 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 |
120 // tests. So several functions were already using this incorrectly. | 120 // tests. So several functions were already using this incorrectly. |
121 // That needs to be cleaned up. | 121 // That needs to be cleaned up. |
122 if (index < new_index) | 122 if (index < new_index) |
123 new_index--; | 123 new_index--; |
124 | 124 |
125 Move(index, new_index); | 125 Move(index, new_index); |
126 } | 126 } |
127 } | 127 } |
128 | 128 |
129 int LauncherModel::ItemIndexByID(LauncherID id) const { | 129 int ShelfModel::ItemIndexByID(LauncherID id) const { |
130 LauncherItems::const_iterator i = ItemByID(id); | 130 LauncherItems::const_iterator i = ItemByID(id); |
131 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin()); | 131 return i == items_.end() ? -1 : static_cast<int>(i - items_.begin()); |
132 } | 132 } |
133 | 133 |
134 LauncherItems::const_iterator LauncherModel::ItemByID(int id) const { | 134 LauncherItems::const_iterator ShelfModel::ItemByID(int id) const { |
135 for (LauncherItems::const_iterator i = items_.begin(); | 135 for (LauncherItems::const_iterator i = items_.begin(); |
136 i != items_.end(); ++i) { | 136 i != items_.end(); ++i) { |
137 if (i->id == id) | 137 if (i->id == id) |
138 return i; | 138 return i; |
139 } | 139 } |
140 return items_.end(); | 140 return items_.end(); |
141 } | 141 } |
142 | 142 |
143 int LauncherModel::FirstPanelIndex() const { | 143 int ShelfModel::FirstPanelIndex() const { |
144 LauncherItem weight_dummy; | 144 LauncherItem weight_dummy; |
145 weight_dummy.type = TYPE_APP_PANEL; | 145 weight_dummy.type = TYPE_APP_PANEL; |
146 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 146 return std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
147 CompareByWeight) - items_.begin(); | 147 CompareByWeight) - items_.begin(); |
148 } | 148 } |
149 | 149 |
150 void LauncherModel::SetStatus(Status status) { | 150 void ShelfModel::SetStatus(Status status) { |
151 if (status_ == status) | 151 if (status_ == status) |
152 return; | 152 return; |
153 | 153 |
154 status_ = status; | 154 status_ = status; |
155 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfStatusChanged()); | 155 FOR_EACH_OBSERVER(ShelfModelObserver, observers_, ShelfStatusChanged()); |
156 } | 156 } |
157 | 157 |
158 void LauncherModel::AddObserver(ShelfModelObserver* observer) { | 158 void ShelfModel::AddObserver(ShelfModelObserver* observer) { |
159 observers_.AddObserver(observer); | 159 observers_.AddObserver(observer); |
160 } | 160 } |
161 | 161 |
162 void LauncherModel::RemoveObserver(ShelfModelObserver* observer) { | 162 void ShelfModel::RemoveObserver(ShelfModelObserver* observer) { |
163 observers_.RemoveObserver(observer); | 163 observers_.RemoveObserver(observer); |
164 } | 164 } |
165 | 165 |
166 int LauncherModel::ValidateInsertionIndex(LauncherItemType type, | 166 int ShelfModel::ValidateInsertionIndex(LauncherItemType type, int index) const { |
167 int index) const { | |
168 DCHECK(index >= 0 && index <= item_count() + | 167 DCHECK(index >= 0 && index <= item_count() + |
169 (ash::switches::UseAlternateShelfLayout() ? 1 : 0)); | 168 (ash::switches::UseAlternateShelfLayout() ? 1 : 0)); |
170 | 169 |
171 // Clamp |index| to the allowed range for the type as determined by |weight|. | 170 // Clamp |index| to the allowed range for the type as determined by |weight|. |
172 LauncherItem weight_dummy; | 171 LauncherItem weight_dummy; |
173 weight_dummy.type = type; | 172 weight_dummy.type = type; |
174 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 173 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
175 CompareByWeight) - items_.begin(), | 174 CompareByWeight) - items_.begin(), |
176 static_cast<LauncherItems::difference_type>(index)); | 175 static_cast<LauncherItems::difference_type>(index)); |
177 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 176 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
178 CompareByWeight) - items_.begin(), | 177 CompareByWeight) - items_.begin(), |
179 static_cast<LauncherItems::difference_type>(index)); | 178 static_cast<LauncherItems::difference_type>(index)); |
180 | 179 |
181 return index; | 180 return index; |
182 } | 181 } |
183 | 182 |
184 } // namespace ash | 183 } // namespace ash |
OLD | NEW |