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

Side by Side Diff: ui/app_list/views/apps_grid_view.cc

Issue 27438002: Store AppItems as pages in AppListModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 2 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 | Annotate | Revision Log
OLDNEW
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 "ui/app_list/views/apps_grid_view.h" 5 #include "ui/app_list/views/apps_grid_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "content/public/browser/web_contents.h" 9 #include "content/public/browser/web_contents.h"
10 #include "ui/app_list/app_list_item_model.h" 10 #include "ui/app_list/app_list_item_model.h"
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 } 265 }
266 266
267 AppsGridView::~AppsGridView() { 267 AppsGridView::~AppsGridView() {
268 // Coming here |drag_view_| should already be canceled since otherwise the 268 // Coming here |drag_view_| should already be canceled since otherwise the
269 // drag would disappear after the app list got animated away and closed, 269 // drag would disappear after the app list got animated away and closed,
270 // which would look odd. 270 // which would look odd.
271 DCHECK(!drag_view_); 271 DCHECK(!drag_view_);
272 if (drag_view_) 272 if (drag_view_)
273 EndDrag(true); 273 EndDrag(true);
274 274
275 if (model_) { 275 if (model_)
276 model_->RemoveObserver(this); 276 model_->RemoveObserver(this);
277 model_->apps()->RemoveObserver(this); 277
278 }
279 pagination_model_->RemoveObserver(this); 278 pagination_model_->RemoveObserver(this);
280 } 279 }
281 280
282 void AppsGridView::SetLayout(int icon_size, int cols, int rows_per_page) { 281 void AppsGridView::SetLayout(int icon_size, int cols, int rows_per_page) {
283 icon_size_.SetSize(icon_size, icon_size); 282 icon_size_.SetSize(icon_size, icon_size);
284 cols_ = cols; 283 cols_ = cols;
285 rows_per_page_ = rows_per_page; 284 rows_per_page_ = rows_per_page;
286 285
287 set_border(views::Border::CreateEmptyBorder(kTopPadding, 286 set_border(views::Border::CreateEmptyBorder(kTopPadding,
288 kLeftRightPadding, 287 kLeftRightPadding,
289 0, 288 0,
290 kLeftRightPadding)); 289 kLeftRightPadding));
291 } 290 }
292 291
293 void AppsGridView::SetModel(AppListModel* model) { 292 void AppsGridView::SetModel(AppListModel* model) {
294 if (model_) { 293 if (model_)
295 model_->RemoveObserver(this); 294 model_->RemoveObserver(this);
296 model_->apps()->RemoveObserver(this);
297 }
298 295
299 model_ = model; 296 model_ = model;
300 if (model_) { 297 if (model_)
301 model_->AddObserver(this); 298 model_->AddObserver(this);
302 model_->apps()->AddObserver(this);
303 }
304 Update(); 299 Update();
305 } 300 }
306 301
307 void AppsGridView::SetSelectedView(views::View* view) { 302 void AppsGridView::SetSelectedView(views::View* view) {
308 if (IsSelectedView(view) || IsDraggedView(view)) 303 if (IsSelectedView(view) || IsDraggedView(view))
309 return; 304 return;
310 305
311 Index index = GetIndexOfView(view); 306 Index index = GetIndexOfView(view);
312 if (IsValidIndex(index)) 307 if (IsValidIndex(index))
313 SetSelectedItemByIndex(index); 308 SetSelectedItemByIndex(index);
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 622
628 // static 623 // static
629 AppsGridView* AppsGridView::GetLastGridViewForTest() { 624 AppsGridView* AppsGridView::GetLastGridViewForTest() {
630 return last_created_grid_view_for_test; 625 return last_created_grid_view_for_test;
631 } 626 }
632 627
633 void AppsGridView::Update() { 628 void AppsGridView::Update() {
634 DCHECK(!selected_view_ && !drag_view_); 629 DCHECK(!selected_view_ && !drag_view_);
635 630
636 view_model_.Clear(); 631 view_model_.Clear();
637 if (model_ && model_->apps()->item_count()) 632 if (model_) {
638 ListItemsAdded(0, model_->apps()->item_count()); 633 for (size_t page = 0; page < model_->GetNumAppPages(); ++page) {
634 const AppListModel::AppItems& apps = model_->GetAppItemsForPage(page);
635 if (apps.item_count())
636 OnListItemsAdded(page, 0, apps.item_count());
637 }
638 }
639 } 639 }
640 640
641 void AppsGridView::UpdatePaging() { 641 void AppsGridView::UpdatePaging() {
642 int total_page = start_page_view_ ? 1 : 0; 642 int total_page = start_page_view_ ? 1 : 0;
643 if (view_model_.view_size() && tiles_per_page()) 643 if (view_model_.view_size() && tiles_per_page())
644 total_page += (view_model_.view_size() - 1) / tiles_per_page() + 1; 644 total_page += (view_model_.view_size() - 1) / tiles_per_page() + 1;
645 645
646 pagination_model_->SetTotalPages(total_page); 646 pagination_model_->SetTotalPages(total_page);
647 } 647 }
648 648
649 void AppsGridView::UpdatePulsingBlockViews() { 649 void AppsGridView::UpdatePulsingBlockViews() {
650 if (model_->GetNumAppPages() == 0)
651 return;
652
653 const AppListModel::AppItems& apps = model_->GetAppItemsForPage(0);
650 const int available_slots = 654 const int available_slots =
651 tiles_per_page() - model_->apps()->item_count() % tiles_per_page(); 655 tiles_per_page() - apps.item_count() % tiles_per_page();
652 const int desired = model_->status() == AppListModel::STATUS_SYNCING ? 656 const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
653 available_slots : 0; 657 available_slots : 0;
654 658
655 if (pulsing_blocks_model_.view_size() == desired) 659 if (pulsing_blocks_model_.view_size() == desired)
656 return; 660 return;
657 661
658 while (pulsing_blocks_model_.view_size() > desired) { 662 while (pulsing_blocks_model_.view_size() > desired) {
659 views::View* view = pulsing_blocks_model_.view_at(0); 663 views::View* view = pulsing_blocks_model_.view_at(0);
660 pulsing_blocks_model_.Remove(0); 664 pulsing_blocks_model_.Remove(0);
661 delete view; 665 delete view;
662 } 666 }
663 667
664 while (pulsing_blocks_model_.view_size() < desired) { 668 while (pulsing_blocks_model_.view_size() < desired) {
665 views::View* view = new PulsingBlockView( 669 views::View* view = new PulsingBlockView(
666 gfx::Size(kPreferredTileWidth, kPreferredTileHeight), true); 670 gfx::Size(kPreferredTileWidth, kPreferredTileHeight), true);
667 pulsing_blocks_model_.Add(view, 0); 671 pulsing_blocks_model_.Add(view, 0);
668 AddChildView(view); 672 AddChildView(view);
669 } 673 }
670 } 674 }
671 675
672 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) { 676 views::View* AppsGridView::CreateViewForItemAtIndex(size_t page_idx,
673 DCHECK_LT(index, model_->apps()->item_count()); 677 size_t item_idx) {
674 AppListItemView* view = new AppListItemView(this, 678 AppListItemModel* item = model_->GetItemAt(page_idx, item_idx);
675 model_->apps()->GetItemAt(index)); 679 CHECK(item);
680 AppListItemView* view = new AppListItemView(this, item);
676 view->SetIconSize(icon_size_); 681 view->SetIconSize(icon_size_);
677 #if defined(USE_AURA) 682 #if defined(USE_AURA)
678 view->SetPaintToLayer(true); 683 view->SetPaintToLayer(true);
679 view->SetFillsBoundsOpaquely(false); 684 view->SetFillsBoundsOpaquely(false);
680 #endif 685 #endif
681 return view; 686 return view;
682 } 687 }
683 688
684 AppsGridView::Index AppsGridView::GetIndexFromModelIndex( 689 AppsGridView::Index AppsGridView::GetIndexFromModelIndex(
685 int model_index) const { 690 int model_index) const {
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
1093 } 1098 }
1094 } 1099 }
1095 1100
1096 void AppsGridView::OnPageFlipTimer() { 1101 void AppsGridView::OnPageFlipTimer() {
1097 DCHECK(pagination_model_->is_valid_page(page_flip_target_)); 1102 DCHECK(pagination_model_->is_valid_page(page_flip_target_));
1098 pagination_model_->SelectPage(page_flip_target_, true); 1103 pagination_model_->SelectPage(page_flip_target_, true);
1099 } 1104 }
1100 1105
1101 void AppsGridView::MoveItemInModel(views::View* item_view, 1106 void AppsGridView::MoveItemInModel(views::View* item_view,
1102 const Index& target) { 1107 const Index& target) {
1108 Index current = GetIndexOfView(item_view);
1103 int current_model_index = view_model_.GetIndexOfView(item_view); 1109 int current_model_index = view_model_.GetIndexOfView(item_view);
1104 DCHECK_GE(current_model_index, 0); 1110 DCHECK_GE(current_model_index, 0);
1105 1111
1106 int target_model_index = GetModelIndexFromIndex(target); 1112 int target_model_index = GetModelIndexFromIndex(target);
1107 if (target_model_index == current_model_index) 1113 if (target_model_index == current_model_index)
1108 return; 1114 return;
1109 1115
1110 model_->apps()->RemoveObserver(this); 1116 model_->RemoveObserver(this);
1111 model_->apps()->Move(current_model_index, target_model_index); 1117 model_->MoveItem(current.page, current.slot,
1118 target.page, target.slot);
1112 view_model_.Move(current_model_index, target_model_index); 1119 view_model_.Move(current_model_index, target_model_index);
1113 model_->apps()->AddObserver(this); 1120 model_->AddObserver(this);
1114 1121
1115 if (pagination_model_->selected_page() != target.page) 1122 if (pagination_model_->selected_page() != target.page)
1116 pagination_model_->SelectPage(target.page, false); 1123 pagination_model_->SelectPage(target.page, false);
1117 } 1124 }
1118 1125
1119 void AppsGridView::CancelContextMenusOnCurrentPage() { 1126 void AppsGridView::CancelContextMenusOnCurrentPage() {
1120 int start = pagination_model_->selected_page() * tiles_per_page(); 1127 int start = pagination_model_->selected_page() * tiles_per_page();
1121 int end = std::min(view_model_.view_size(), start + tiles_per_page()); 1128 int end = std::min(view_model_.view_size(), start + tiles_per_page());
1122 for (int i = start; i < end; ++i) { 1129 for (int i = start; i < end; ++i) {
1123 AppListItemView* view = 1130 AppListItemView* view =
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1162 const PaginationModel::Transition& transition = 1169 const PaginationModel::Transition& transition =
1163 pagination_model_->transition(); 1170 pagination_model_->transition();
1164 if (current_page == 0 || transition.target_page == 0) { 1171 if (current_page == 0 || transition.target_page == 0) {
1165 const int dir = transition.target_page > current_page ? -1 : 1; 1172 const int dir = transition.target_page > current_page ? -1 : 1;
1166 start_page_bounds.Offset(transition.progress * page_width * dir, 0); 1173 start_page_bounds.Offset(transition.progress * page_width * dir, 0);
1167 } 1174 }
1168 1175
1169 start_page_view_->SetBoundsRect(start_page_bounds); 1176 start_page_view_->SetBoundsRect(start_page_bounds);
1170 } 1177 }
1171 1178
1172 void AppsGridView::ListItemsAdded(size_t start, size_t count) { 1179 void AppsGridView::OnListItemsAdded(size_t page_index,
1180 size_t start, size_t count) {
1173 EndDrag(true); 1181 EndDrag(true);
1174 1182
1183 size_t offset = page_index * AppListModel::GetNumAppsPerPage();
xiyuan 2013/10/18 23:04:08 int offset = GetModelIndexFromIndex(Index(page_ind
stevenjb 2013/10/19 01:04:43 Ah, thanks. Will do.
1175 for (size_t i = start; i < start + count; ++i) { 1184 for (size_t i = start; i < start + count; ++i) {
1176 views::View* view = CreateViewForItemAtIndex(i); 1185 views::View* view = CreateViewForItemAtIndex(page_index, i);
1177 view_model_.Add(view, i); 1186 view_model_.Add(view, offset + i);
1178 AddChildView(view); 1187 AddChildView(view);
1179 } 1188 }
1180 1189
1181 UpdatePaging(); 1190 UpdatePaging();
1182 UpdatePulsingBlockViews(); 1191 UpdatePulsingBlockViews();
1183 Layout(); 1192 Layout();
1184 SchedulePaint(); 1193 SchedulePaint();
1185 } 1194 }
1186 1195
1187 void AppsGridView::ListItemsRemoved(size_t start, size_t count) { 1196 void AppsGridView::OnListItemsRemoved(size_t page_index,
1197 size_t start, size_t count) {
1188 EndDrag(true); 1198 EndDrag(true);
1189 1199
1200 size_t index = page_index * AppListModel::GetNumAppsPerPage() + start;
xiyuan 2013/10/18 23:04:08 GetModelIndexFromIndex(Index(page_index, start));
stevenjb 2013/10/19 01:04:43 Ditto
1190 for (size_t i = 0; i < count; ++i) { 1201 for (size_t i = 0; i < count; ++i) {
1191 views::View* view = view_model_.view_at(start); 1202 views::View* view = view_model_.view_at(index);
1192 view_model_.Remove(start); 1203 view_model_.Remove(index);
1193 delete view; 1204 delete view;
1194 } 1205 }
1195 1206
1196 UpdatePaging(); 1207 UpdatePaging();
1197 UpdatePulsingBlockViews(); 1208 UpdatePulsingBlockViews();
1198 Layout(); 1209 Layout();
1199 SchedulePaint(); 1210 SchedulePaint();
1200 } 1211 }
1201 1212
1202 void AppsGridView::ListItemMoved(size_t index, size_t target_index) { 1213 void AppsGridView::OnListItemMoved(size_t page_index,
1214 size_t index, size_t target_index) {
1203 EndDrag(true); 1215 EndDrag(true);
1204 view_model_.Move(index, target_index); 1216 size_t offset = page_index * AppListModel::GetNumAppsPerPage();
xiyuan 2013/10/18 23:04:08 use GetModelIndexFromIndex
stevenjb 2013/10/19 01:04:43 Ditto
1217 view_model_.Move(offset + index, offset + target_index);
1205 1218
1206 UpdatePaging(); 1219 UpdatePaging();
1207 AnimateToIdealBounds(); 1220 AnimateToIdealBounds();
1208 } 1221 }
1209 1222
1210 void AppsGridView::ListItemsChanged(size_t start, size_t count) {
1211 NOTREACHED();
1212 }
1213
1214 void AppsGridView::TotalPagesChanged() { 1223 void AppsGridView::TotalPagesChanged() {
1215 } 1224 }
1216 1225
1217 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) { 1226 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
1218 if (dragging()) { 1227 if (dragging()) {
1219 CalculateDropTarget(last_drag_point_, true); 1228 CalculateDropTarget(last_drag_point_, true);
1220 Layout(); 1229 Layout();
1221 MaybeStartPageFlipTimer(last_drag_point_); 1230 MaybeStartPageFlipTimer(last_drag_point_);
1222 } else { 1231 } else {
1223 ClearSelectedView(selected_view_); 1232 ClearSelectedView(selected_view_);
(...skipping 24 matching lines...) Expand all
1248 #if defined(USE_AURA) 1257 #if defined(USE_AURA)
1249 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator()); 1258 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
1250 animator.SetPreemptionStrategy( 1259 animator.SetPreemptionStrategy(
1251 immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET : 1260 immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
1252 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION); 1261 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
1253 view->layer()->SetOpacity(hide ? 0 : 1); 1262 view->layer()->SetOpacity(hide ? 0 : 1);
1254 #endif 1263 #endif
1255 } 1264 }
1256 1265
1257 } // namespace app_list 1266 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698