Chromium Code Reviews| 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/launcher/launcher_model_observer.h" | 9 #include "ash/launcher/launcher_model_observer.h" |
| 10 #include "ui/aura/window.h" | |
| 11 | 10 |
| 12 namespace ash { | 11 namespace ash { |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 15 const int kMaxLoadingPlaceholder = 4; | |
| 16 | |
| 16 int LauncherItemTypeToWeight(LauncherItemType type) { | 17 int LauncherItemTypeToWeight(LauncherItemType type) { |
| 17 switch (type) { | 18 switch (type) { |
| 18 case TYPE_BROWSER_SHORTCUT: | 19 case TYPE_BROWSER_SHORTCUT: |
| 19 return 0; | 20 return 0; |
| 20 case TYPE_APP_SHORTCUT: | 21 case TYPE_APP_SHORTCUT: |
| 21 return 1; | 22 return 1; |
| 23 case TYPE_APP_PLACEHOLDER: | |
| 24 return 2; | |
| 22 case TYPE_TABBED: | 25 case TYPE_TABBED: |
| 23 case TYPE_APP_PANEL: | 26 case TYPE_APP_PANEL: |
| 24 case TYPE_PLATFORM_APP: | 27 case TYPE_PLATFORM_APP: |
| 25 return 2; | 28 return 3; |
| 26 case TYPE_APP_LIST: | 29 case TYPE_APP_LIST: |
| 27 return 3; | 30 return 4; |
| 28 } | 31 } |
| 29 | 32 |
| 30 NOTREACHED() << "Invalid type " << type; | 33 NOTREACHED() << "Invalid type " << type; |
| 31 return 2; | 34 return 3; |
| 32 } | 35 } |
| 33 | 36 |
| 34 bool CompareByWeight(const LauncherItem& a, const LauncherItem& b) { | 37 bool CompareByWeight(const LauncherItem& a, const LauncherItem& b) { |
| 35 return LauncherItemTypeToWeight(a.type) < LauncherItemTypeToWeight(b.type); | 38 return LauncherItemTypeToWeight(a.type) < LauncherItemTypeToWeight(b.type); |
| 36 } | 39 } |
| 37 | 40 |
| 38 } // namespace | 41 } // namespace |
| 39 | 42 |
| 40 LauncherModel::LauncherModel() : next_id_(1) { | 43 LauncherModel::LauncherModel() : next_id_(1), ui_state_(NORMAL) { |
| 41 LauncherItem app_list; | 44 LauncherItem app_list; |
| 42 app_list.type = TYPE_APP_LIST; | 45 app_list.type = TYPE_APP_LIST; |
| 43 app_list.is_incognito = false; | 46 app_list.is_incognito = false; |
| 44 | 47 |
| 45 LauncherItem browser_shortcut; | 48 LauncherItem browser_shortcut; |
| 46 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | 49 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; |
| 47 browser_shortcut.is_incognito = false; | 50 browser_shortcut.is_incognito = false; |
| 48 | 51 |
| 49 AddAt(0, browser_shortcut); | 52 AddAt(0, browser_shortcut); |
| 50 AddAt(1, app_list); | 53 AddAt(1, app_list); |
| 51 } | 54 } |
| 52 | 55 |
| 53 LauncherModel::~LauncherModel() { | 56 LauncherModel::~LauncherModel() { |
| 54 } | 57 } |
| 55 | 58 |
| 56 int LauncherModel::Add(const LauncherItem& item) { | 59 int LauncherModel::Add(const LauncherItem& item) { |
| 57 return AddAt(items_.size(), item); | 60 return AddAt(items_.size(), item); |
| 58 } | 61 } |
| 59 | 62 |
| 60 int LauncherModel::AddAt(int index, const LauncherItem& item) { | 63 int LauncherModel::AddAt(int index, const LauncherItem& item) { |
| 64 // Remove loading placeholders when adding app shortcuts in loading state. | |
| 65 if (ui_state_ == LOADING && item.type == TYPE_APP_SHORTCUT) { | |
| 66 int placeholders = GetDesiredNumOfPlaceholders(); | |
| 67 if (placeholders > 0) | |
| 68 UpdatePlaceholders(placeholders - 1); | |
| 69 } | |
| 70 | |
| 61 index = ValidateInsertionIndex(item.type, index); | 71 index = ValidateInsertionIndex(item.type, index); |
| 62 items_.insert(items_.begin() + index, item); | 72 items_.insert(items_.begin() + index, item); |
| 63 items_[index].id = next_id_++; | 73 items_[index].id = next_id_++; |
| 64 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | 74 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, |
| 65 LauncherItemAdded(index)); | 75 LauncherItemAdded(index)); |
| 66 return index; | 76 return index; |
| 67 } | 77 } |
| 68 | 78 |
| 69 void LauncherModel::RemoveItemAt(int index) { | 79 void LauncherModel::RemoveItemAt(int index) { |
| 70 DCHECK(index >= 0 && index < item_count()); | 80 DCHECK(index >= 0 && index < item_count()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 } | 129 } |
| 120 | 130 |
| 121 void LauncherModel::AddObserver(LauncherModelObserver* observer) { | 131 void LauncherModel::AddObserver(LauncherModelObserver* observer) { |
| 122 observers_.AddObserver(observer); | 132 observers_.AddObserver(observer); |
| 123 } | 133 } |
| 124 | 134 |
| 125 void LauncherModel::RemoveObserver(LauncherModelObserver* observer) { | 135 void LauncherModel::RemoveObserver(LauncherModelObserver* observer) { |
| 126 observers_.RemoveObserver(observer); | 136 observers_.RemoveObserver(observer); |
| 127 } | 137 } |
| 128 | 138 |
| 139 void LauncherModel::SetUIState(UIState ui_state) { | |
| 140 if (ui_state_ == ui_state) | |
| 141 return; | |
| 142 | |
| 143 ui_state_ = ui_state; | |
| 144 UpdatePlaceholders(ui_state_ == LOADING ? GetDesiredNumOfPlaceholders() : 0); | |
| 145 FOR_EACH_OBSERVER(LauncherModelObserver, observers_, | |
| 146 LauncherUIStateChanged()); | |
| 147 } | |
| 148 | |
| 129 int LauncherModel::ValidateInsertionIndex(LauncherItemType type, | 149 int LauncherModel::ValidateInsertionIndex(LauncherItemType type, |
| 130 int index) const { | 150 int index) const { |
| 131 DCHECK(index >= 0 && index <= item_count()); | 151 DCHECK(index >= 0 && index <= item_count()); |
| 132 | 152 |
| 133 // Clamp |index| to the allowed range for the type as determined by |weight|. | 153 // Clamp |index| to the allowed range for the type as determined by |weight|. |
| 134 LauncherItem weight_dummy; | 154 LauncherItem weight_dummy; |
| 135 weight_dummy.type = type; | 155 weight_dummy.type = type; |
| 136 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, | 156 index = std::max(std::lower_bound(items_.begin(), items_.end(), weight_dummy, |
| 137 CompareByWeight) - items_.begin(), | 157 CompareByWeight) - items_.begin(), |
| 138 static_cast<LauncherItems::difference_type>(index)); | 158 static_cast<LauncherItems::difference_type>(index)); |
| 139 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, | 159 index = std::min(std::upper_bound(items_.begin(), items_.end(), weight_dummy, |
| 140 CompareByWeight) - items_.begin(), | 160 CompareByWeight) - items_.begin(), |
| 141 static_cast<LauncherItems::difference_type>(index)); | 161 static_cast<LauncherItems::difference_type>(index)); |
| 142 | 162 |
| 143 return index; | 163 return index; |
| 144 } | 164 } |
| 145 | 165 |
| 166 int LauncherModel::GetDesiredNumOfPlaceholders() { | |
| 167 int desired_placholders = kMaxLoadingPlaceholder; | |
| 168 | |
| 169 for (int i = 0; i < item_count() && desired_placholders; ++i) { | |
| 170 if (items_[i].type == TYPE_APP_SHORTCUT) | |
| 171 --desired_placholders; | |
| 172 } | |
| 173 | |
| 174 return desired_placholders; | |
| 175 } | |
| 176 | |
| 177 void LauncherModel::UpdatePlaceholders(int n) { | |
| 178 int count = 0; | |
| 179 | |
| 180 for (int index = item_count() - 1; index >= 0; --index) { | |
| 181 if (items_[index].type == TYPE_APP_PLACEHOLDER) { | |
|
sky
2012/08/14 20:02:34
Having to toggle the type like this feels hacky. W
xiyuan
2012/08/14 23:11:32
The problem with STATUS_IS_PENDING approach is tha
| |
| 182 if (count == n) | |
| 183 RemoveItemAt(index); | |
| 184 else | |
| 185 ++count; | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 while (count < n) { | |
| 190 LauncherItem placeholder; | |
| 191 placeholder.type = TYPE_APP_PLACEHOLDER; | |
| 192 placeholder.is_incognito = false; | |
| 193 | |
| 194 Add(placeholder); | |
| 195 ++count; | |
| 196 } | |
| 197 } | |
| 198 | |
| 146 } // namespace ash | 199 } // namespace ash |
| OLD | NEW |