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

Side by Side Diff: ui/app_list/cocoa/apps_grid_controller.mm

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 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 #import "ui/app_list/cocoa/apps_grid_controller.h" 5 #import "ui/app_list/cocoa/apps_grid_controller.h"
6 6
7 #include "base/mac/foundation_util.h" 7 #include "base/mac/foundation_util.h"
8 #include "ui/app_list/app_list_item_model.h" 8 #include "ui/app_list/app_list_item_model.h"
9 #include "ui/app_list/app_list_model.h" 9 #include "ui/app_list/app_list_model.h"
10 #include "ui/app_list/app_list_model_observer.h" 10 #include "ui/app_list/app_list_model_observer.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 - (void)listItemMovedFromIndex:(size_t)fromIndex 86 - (void)listItemMovedFromIndex:(size_t)fromIndex
87 toModelIndex:(size_t)toIndex; 87 toModelIndex:(size_t)toIndex;
88 88
89 // Moves the selection by |indexDelta| items. 89 // Moves the selection by |indexDelta| items.
90 - (BOOL)moveSelectionByDelta:(int)indexDelta; 90 - (BOOL)moveSelectionByDelta:(int)indexDelta;
91 91
92 @end 92 @end
93 93
94 namespace app_list { 94 namespace app_list {
95 95
96 class AppsGridDelegateBridge : public ui::ListModelObserver { 96 class AppsGridDelegateBridge : public AppListModelObserver {
97 public: 97 public:
98 AppsGridDelegateBridge(AppsGridController* parent) : parent_(parent) {} 98 AppsGridDelegateBridge(AppsGridController* parent) : parent_(parent) {}
99 99
100 private: 100 private:
101 // Overridden from ui::ListModelObserver: 101 // Overridden from ui::ListModelObserver:
102 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE { 102 virtual void OnListItemsAdded(size_t page_index,
103 [parent_ listItemsAdded:start 103 size_t start, size_t count) OVERRIDE {
104 size_t start_index = page_index * kItemsPerPage + start;
105 [parent_ listItemsAdded:start_index
104 count:count]; 106 count:count];
105 } 107 }
106 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE { 108 virtual void OnListItemsRemoved(size_t page_index,
107 [parent_ listItemsRemoved:start 109 size_t start, size_t count) OVERRIDE {
110 size_t start_index = page_index * kItemsPerPage + start;
111 [parent_ listItemsRemoved:start_index
108 count:count]; 112 count:count];
109 } 113 }
110 virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE { 114 virtual void OnListItemMoved(size_t page_index,
111 [parent_ listItemMovedFromIndex:index 115 size_t index, size_t target_index) OVERRIDE {
112 toModelIndex:target_index]; 116 size_t from_index = page_index * kItemsPerPage + index;
113 } 117 size_t to_index = page_index * kItemsPerPage + target_index;
114 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE { 118 [parent_ listItemMovedFromIndex:from_index
115 NOTREACHED(); 119 toModelIndex:to_index];
116 } 120 }
117 121
118 AppsGridController* parent_; // Weak, owns us. 122 AppsGridController* parent_; // Weak, owns us.
119 123
120 DISALLOW_COPY_AND_ASSIGN(AppsGridDelegateBridge); 124 DISALLOW_COPY_AND_ASSIGN(AppsGridDelegateBridge);
121 }; 125 };
122 126
123 } // namespace app_list 127 } // namespace app_list
124 128
125 @interface PageContainerView : NSView; 129 @interface PageContainerView : NSView;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 } 185 }
182 return NSNotFound; 186 return NSNotFound;
183 } 187 }
184 188
185 - (app_list::AppListModel*)model { 189 - (app_list::AppListModel*)model {
186 return model_.get(); 190 return model_.get();
187 } 191 }
188 192
189 - (void)setModel:(scoped_ptr<app_list::AppListModel>)newModel { 193 - (void)setModel:(scoped_ptr<app_list::AppListModel>)newModel {
190 if (model_) { 194 if (model_) {
191 model_->apps()->RemoveObserver(bridge_.get()); 195 model_->RemoveObserver(bridge_.get());
192 196
193 // Since the model is about to be deleted, and the AppKit objects might be 197 // Since the model is about to be deleted, and the AppKit objects might be
194 // sitting in an NSAutoreleasePool, ensure there are no references to the 198 // sitting in an NSAutoreleasePool, ensure there are no references to the
195 // model. 199 // model.
196 for (size_t i = 0; i < [items_ count]; ++i) 200 for (size_t i = 0; i < [items_ count]; ++i)
197 [[self itemAtIndex:i] setModel:NULL]; 201 [[self itemAtIndex:i] setModel:NULL];
198 202
199 [items_ removeAllObjects]; 203 [items_ removeAllObjects];
200 [self updatePages:0]; 204 [self updatePages:0];
201 [self scrollToPage:0]; 205 [self scrollToPage:0];
202 } 206 }
203 207
204 model_.reset(newModel.release()); 208 model_.reset(newModel.release());
205 if (!model_) 209 if (!model_)
206 return; 210 return;
207 211
208 model_->apps()->AddObserver(bridge_.get()); 212 model_->AddObserver(bridge_.get());
209 [self listItemsAdded:0 213 for (size_t p = 0; p < model_->GetNumAppPages(); ++p) {
210 count:model_->apps()->item_count()]; 214 size_t start_index = p * kItemsPerPage;
215 [self listItemsAdded:start_index
216 count:model_->GetAppItemsForPage(p).item_count()];
217 }
211 } 218 }
212 219
213 - (void)setDelegate:(app_list::AppListViewDelegate*)newDelegate { 220 - (void)setDelegate:(app_list::AppListViewDelegate*)newDelegate {
214 scoped_ptr<app_list::AppListModel> newModel(new app_list::AppListModel); 221 scoped_ptr<app_list::AppListModel> newModel(new app_list::AppListModel);
215 delegate_ = newDelegate; 222 delegate_ = newDelegate;
216 if (delegate_) 223 if (delegate_)
217 delegate_->InitModel(newModel.get()); // Populates items. 224 delegate_->InitModel(newModel.get()); // Populates items.
218 [self setModel:newModel.Pass()]; 225 [self setModel:newModel.Pass()];
219 } 226 }
220 227
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
515 } 522 }
516 } 523 }
517 524
518 // Compare with views implementation in AppsGridView::MoveItemInModel(). 525 // Compare with views implementation in AppsGridView::MoveItemInModel().
519 - (void)moveItemWithIndex:(size_t)itemIndex 526 - (void)moveItemWithIndex:(size_t)itemIndex
520 toModelIndex:(size_t)modelIndex { 527 toModelIndex:(size_t)modelIndex {
521 // Ingore no-op moves. Note that this is always the case when canceled. 528 // Ingore no-op moves. Note that this is always the case when canceled.
522 if (itemIndex == modelIndex) 529 if (itemIndex == modelIndex)
523 return; 530 return;
524 531
525 model_->apps()->RemoveObserver(bridge_.get()); 532 size_t itemPageIndex = itemIndex / kItemsPerPage;
526 model_->apps()->Move(itemIndex, modelIndex); 533 itemIndex -= itemPageIndex * kItemsPerPage;
527 model_->apps()->AddObserver(bridge_.get()); 534 size_t modelPageIndex = modelIndex / kItemsPerPage;
535 modelIndex -= modelPageIndex * kItemsPerPage;
536 model_->RemoveObserver(bridge_.get());
537 model_->MoveItem(itemPageIndex, itemIndex, modelPageIndex, modelIndex);
538 model_->AddObserver(bridge_.get());
528 } 539 }
529 540
530 - (AppsCollectionViewDragManager*)dragManager { 541 - (AppsCollectionViewDragManager*)dragManager {
531 return dragManager_; 542 return dragManager_;
532 } 543 }
533 544
534 - (size_t)scheduledScrollPage { 545 - (size_t)scheduledScrollPage {
535 return scheduledScrollPage_; 546 return scheduledScrollPage_;
536 } 547 }
537 548
538 - (void)listItemsAdded:(size_t)start 549 - (void)listItemsAdded:(size_t)start
539 count:(size_t)count { 550 count:(size_t)count {
540 // Cancel any drag, to ensure the model stays consistent. 551 // Cancel any drag, to ensure the model stays consistent.
541 [dragManager_ cancelDrag]; 552 [dragManager_ cancelDrag];
542 553
543 for (size_t i = start; i < start + count; ++i) { 554 for (size_t i = start; i < start + count; ++i) {
544 app_list::AppListItemModel* itemModel = model_->apps()->GetItemAt(i); 555 size_t page = i / kItemsPerPage;
556 size_t index = i - page * kItemsPerPage;
557 app_list::AppListItemModel* itemModel = model_->GetItemAt(page, index);
545 [items_ insertObject:[NSValue valueWithPointer:itemModel] 558 [items_ insertObject:[NSValue valueWithPointer:itemModel]
546 atIndex:i]; 559 atIndex:i];
547 } 560 }
548 561
549 [self updatePages:start]; 562 [self updatePages:start];
550 } 563 }
551 564
552 - (void)listItemsRemoved:(size_t)start 565 - (void)listItemsRemoved:(size_t)start
553 count:(size_t)count { 566 count:(size_t)count {
554 [dragManager_ cancelDrag]; 567 [dragManager_ cancelDrag];
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 return [self moveSelectionByDelta:-kItemsPerPage]; 684 return [self moveSelectionByDelta:-kItemsPerPage];
672 685
673 if (command == @selector(pageDown:) || 686 if (command == @selector(pageDown:) ||
674 command == @selector(scrollPageDown:)) 687 command == @selector(scrollPageDown:))
675 return [self moveSelectionByDelta:kItemsPerPage]; 688 return [self moveSelectionByDelta:kItemsPerPage];
676 689
677 return NO; 690 return NO;
678 } 691 }
679 692
680 @end 693 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698