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

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

Issue 11371003: app_list: Add sync animation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix missed init Created 8 years, 1 month 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
« no previous file with comments | « ui/app_list/apps_grid_view.h ('k') | ui/app_list/apps_grid_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/apps_grid_view.h" 5 #include "ui/app_list/apps_grid_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ui/app_list/app_list_item_model.h" 9 #include "ui/app_list/app_list_item_model.h"
10 #include "ui/app_list/app_list_item_view.h" 10 #include "ui/app_list/app_list_item_view.h"
11 #include "ui/app_list/apps_grid_view_delegate.h" 11 #include "ui/app_list/apps_grid_view_delegate.h"
12 #include "ui/app_list/page_switcher.h" 12 #include "ui/app_list/page_switcher.h"
13 #include "ui/app_list/pagination_model.h" 13 #include "ui/app_list/pagination_model.h"
14 #include "ui/app_list/pulsing_block_view.h"
14 #include "ui/base/animation/animation.h" 15 #include "ui/base/animation/animation.h"
15 #include "ui/base/events/event.h" 16 #include "ui/base/events/event.h"
16 #include "ui/views/border.h" 17 #include "ui/views/border.h"
17 #include "ui/views/view_model_utils.h" 18 #include "ui/views/view_model_utils.h"
18 #include "ui/views/widget/widget.h" 19 #include "ui/views/widget/widget.h"
19 20
20 namespace { 21 namespace {
21 22
22 // Padding space in pixels for fixed layout. 23 // Padding space in pixels for fixed layout.
23 const int kLeftRightPadding = 20; 24 const int kLeftRightPadding = 20;
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 drag_view_(NULL), 103 drag_view_(NULL),
103 drag_pointer_(NONE), 104 drag_pointer_(NONE),
104 page_flip_target_(-1), 105 page_flip_target_(-1),
105 page_flip_delay_in_ms_(kPageFlipDelayInMs), 106 page_flip_delay_in_ms_(kPageFlipDelayInMs),
106 ALLOW_THIS_IN_INITIALIZER_LIST(bounds_animator_(this)) { 107 ALLOW_THIS_IN_INITIALIZER_LIST(bounds_animator_(this)) {
107 pagination_model_->AddObserver(this); 108 pagination_model_->AddObserver(this);
108 AddChildView(page_switcher_view_); 109 AddChildView(page_switcher_view_);
109 } 110 }
110 111
111 AppsGridView::~AppsGridView() { 112 AppsGridView::~AppsGridView() {
112 if (model_) 113 if (model_) {
113 model_->RemoveObserver(this); 114 model_->RemoveObserver(this);
115 model_->apps()->RemoveObserver(this);
116 }
114 pagination_model_->RemoveObserver(this); 117 pagination_model_->RemoveObserver(this);
115 } 118 }
116 119
117 void AppsGridView::SetLayout(int icon_size, int cols, int rows_per_page) { 120 void AppsGridView::SetLayout(int icon_size, int cols, int rows_per_page) {
118 icon_size_.SetSize(icon_size, icon_size); 121 icon_size_.SetSize(icon_size, icon_size);
119 cols_ = cols; 122 cols_ = cols;
120 rows_per_page_ = rows_per_page; 123 rows_per_page_ = rows_per_page;
121 124
122 set_border(views::Border::CreateEmptyBorder(kTopPadding, 125 set_border(views::Border::CreateEmptyBorder(kTopPadding,
123 kLeftRightPadding, 126 kLeftRightPadding,
124 0, 127 0,
125 kLeftRightPadding)); 128 kLeftRightPadding));
126 } 129 }
127 130
128 void AppsGridView::SetModel(AppListModel::Apps* model) { 131 void AppsGridView::SetModel(AppListModel* model) {
129 if (model_) 132 if (model_) {
130 model_->RemoveObserver(this); 133 model_->RemoveObserver(this);
134 model_->apps()->RemoveObserver(this);
135 }
131 136
132 model_ = model; 137 model_ = model;
133 if (model_) 138 if (model_) {
134 model_->AddObserver(this); 139 model_->AddObserver(this);
140 model_->apps()->AddObserver(this);
141 }
135 Update(); 142 Update();
136 } 143 }
137 144
138 void AppsGridView::SetSelectedView(views::View* view) { 145 void AppsGridView::SetSelectedView(views::View* view) {
139 if (IsSelectedView(view) || IsDraggedView(view)) 146 if (IsSelectedView(view) || IsDraggedView(view))
140 return; 147 return;
141 148
142 Index index = GetIndexOfView(view); 149 Index index = GetIndexOfView(view);
143 if (IsValidIndex(index)) 150 if (IsValidIndex(index))
144 SetSelectedItemByIndex(index); 151 SetSelectedItemByIndex(index);
(...skipping 13 matching lines...) Expand all
158 return; 165 return;
159 166
160 Index index = GetIndexOfView(view); 167 Index index = GetIndexOfView(view);
161 if (IsValidIndex(index)) 168 if (IsValidIndex(index))
162 pagination_model_->SelectPage(index.page, false); 169 pagination_model_->SelectPage(index.page, false);
163 } 170 }
164 171
165 void AppsGridView::InitiateDrag(views::View* view, 172 void AppsGridView::InitiateDrag(views::View* view,
166 Pointer pointer, 173 Pointer pointer,
167 const ui::LocatedEvent& event) { 174 const ui::LocatedEvent& event) {
168 if (drag_view_) 175 if (drag_view_ || pulsing_blocks_model_.view_size())
169 return; 176 return;
170 177
171 drag_view_ = view; 178 drag_view_ = view;
172 drag_start_ = event.location(); 179 drag_start_ = event.location();
173 } 180 }
174 181
175 void AppsGridView::UpdateDrag(views::View* view, 182 void AppsGridView::UpdateDrag(views::View* view,
176 Pointer pointer, 183 Pointer pointer,
177 const ui::LocatedEvent& event) { 184 const ui::LocatedEvent& event) {
178 if (!dragging() && drag_view_ && 185 if (!dragging() && drag_view_ &&
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 void AppsGridView::Layout() { 243 void AppsGridView::Layout() {
237 if (bounds_animator_.IsAnimating()) 244 if (bounds_animator_.IsAnimating())
238 bounds_animator_.Cancel(); 245 bounds_animator_.Cancel();
239 246
240 CalculateIdealBounds(); 247 CalculateIdealBounds();
241 for (int i = 0; i < view_model_.view_size(); ++i) { 248 for (int i = 0; i < view_model_.view_size(); ++i) {
242 views::View* view = view_model_.view_at(i); 249 views::View* view = view_model_.view_at(i);
243 if (view != drag_view_) 250 if (view != drag_view_)
244 view->SetBoundsRect(view_model_.ideal_bounds(i)); 251 view->SetBoundsRect(view_model_.ideal_bounds(i));
245 } 252 }
253 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_);
246 254
247 const int page_switcher_height = 255 const int page_switcher_height =
248 page_switcher_view_->GetPreferredSize().height(); 256 page_switcher_view_->GetPreferredSize().height();
249 gfx::Rect rect(GetContentsBounds()); 257 gfx::Rect rect(GetContentsBounds());
250 rect.set_y(rect.bottom() - page_switcher_height); 258 rect.set_y(rect.bottom() - page_switcher_height);
251 rect.set_height(page_switcher_height); 259 rect.set_height(page_switcher_height);
252 page_switcher_view_->SetBoundsRect(rect); 260 page_switcher_view_->SetBoundsRect(rect);
253 } 261 }
254 262
255 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) { 263 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 if (drag_view_ == child) 313 if (drag_view_ == child)
306 EndDrag(true); 314 EndDrag(true);
307 315
308 bounds_animator_.StopAnimatingView(child); 316 bounds_animator_.StopAnimatingView(child);
309 } 317 }
310 } 318 }
311 319
312 void AppsGridView::Update() { 320 void AppsGridView::Update() {
313 selected_view_ = NULL; 321 selected_view_ = NULL;
314 view_model_.Clear(); 322 view_model_.Clear();
315 if (model_ && model_->item_count()) 323 if (model_ && model_->apps()->item_count())
316 ListItemsAdded(0, model_->item_count()); 324 ListItemsAdded(0, model_->apps()->item_count());
317 } 325 }
318 326
319 void AppsGridView::UpdatePaging() { 327 void AppsGridView::UpdatePaging() {
320 if (!view_model_.view_size() || !tiles_per_page()) { 328 if (!view_model_.view_size() || !tiles_per_page()) {
321 pagination_model_->SetTotalPages(0); 329 pagination_model_->SetTotalPages(0);
322 return; 330 return;
323 } 331 }
324 332
325 pagination_model_->SetTotalPages( 333 pagination_model_->SetTotalPages(
326 (view_model_.view_size() - 1) / tiles_per_page() + 1); 334 (view_model_.view_size() - 1) / tiles_per_page() + 1);
327 } 335 }
328 336
337 void AppsGridView::UpdatePulsingBlockViews() {
338 const int available_slots =
339 tiles_per_page() - model_->apps()->item_count() % tiles_per_page();
340 const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
341 available_slots : 0;
342
343 if (pulsing_blocks_model_.view_size() == desired)
344 return;
345
346 while (pulsing_blocks_model_.view_size() > desired) {
347 views::View* view = pulsing_blocks_model_.view_at(0);
348 pulsing_blocks_model_.Remove(0);
349 delete view;
350 }
351
352 while (pulsing_blocks_model_.view_size() < desired) {
353 views::View* view = new PulsingBlockView(
354 gfx::Size(kPreferredTileWidth, kPreferredTileHeight), true);
355 pulsing_blocks_model_.Add(view, 0);
356 AddChildView(view);
357 }
358 }
359
329 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) { 360 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) {
330 DCHECK_LT(index, model_->item_count()); 361 DCHECK_LT(index, model_->apps()->item_count());
331 AppListItemView* view = new AppListItemView(this, 362 AppListItemView* view = new AppListItemView(this,
332 model_->GetItemAt(index)); 363 model_->apps()->GetItemAt(index));
333 view->SetIconSize(icon_size_); 364 view->SetIconSize(icon_size_);
334 #if !defined(OS_WIN) 365 #if !defined(OS_WIN)
335 view->SetPaintToLayer(true); 366 view->SetPaintToLayer(true);
336 view->SetFillsBoundsOpaquely(false); 367 view->SetFillsBoundsOpaquely(false);
337 #endif 368 #endif
338 return view; 369 return view;
339 } 370 }
340 371
341 void AppsGridView::SetSelectedItemByIndex(const Index& index) { 372 void AppsGridView::SetSelectedItemByIndex(const Index& index) {
342 if (GetIndexOfView(selected_view_) == index) 373 if (GetIndexOfView(selected_view_) == index)
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 const PaginationModel::Transition& transition = 448 const PaginationModel::Transition& transition =
418 pagination_model_->transition(); 449 pagination_model_->transition();
419 const bool is_valid = 450 const bool is_valid =
420 pagination_model_->is_valid_page(transition.target_page); 451 pagination_model_->is_valid_page(transition.target_page);
421 452
422 // Transition to right means negative offset. 453 // Transition to right means negative offset.
423 const int dir = transition.target_page > current_page ? -1 : 1; 454 const int dir = transition.target_page > current_page ? -1 : 1;
424 const int transition_offset = is_valid ? 455 const int transition_offset = is_valid ?
425 transition.progress * page_width * dir : 0; 456 transition.progress * page_width * dir : 0;
426 457
458 const int total_views =
459 view_model_.view_size() + pulsing_blocks_model_.view_size();
427 int slot_index = 0; 460 int slot_index = 0;
428 for (int i = 0; i < view_model_.view_size(); ++i) { 461 for (int i = 0; i < total_views; ++i) {
429 if (view_model_.view_at(i) == drag_view_) 462 if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_)
430 continue; 463 continue;
431 464
432 int page = slot_index / tiles_per_page(); 465 int page = slot_index / tiles_per_page();
433 int slot = slot_index % tiles_per_page(); 466 int slot = slot_index % tiles_per_page();
434 467
435 if (drop_target_.page == page && drop_target_.slot == slot) { 468 if (drop_target_.page == page && drop_target_.slot == slot) {
436 ++slot_index; 469 ++slot_index;
437 page = slot_index / tiles_per_page(); 470 page = slot_index / tiles_per_page();
438 slot = slot_index % tiles_per_page(); 471 slot = slot_index % tiles_per_page();
439 } 472 }
440 473
441 // Decides an x_offset for current item. 474 // Decides an x_offset for current item.
442 int x_offset = 0; 475 int x_offset = 0;
443 if (page < current_page) 476 if (page < current_page)
444 x_offset = -page_width; 477 x_offset = -page_width;
445 else if (page > current_page) 478 else if (page > current_page)
446 x_offset = page_width; 479 x_offset = page_width;
447 480
448 if (is_valid) { 481 if (is_valid) {
449 if (page == current_page || page == transition.target_page) 482 if (page == current_page || page == transition.target_page)
450 x_offset += transition_offset; 483 x_offset += transition_offset;
451 } 484 }
452 485
453 const int row = slot / cols_; 486 const int row = slot / cols_;
454 const int col = slot % cols_; 487 const int col = slot % cols_;
455 gfx::Rect tile_slot( 488 gfx::Rect tile_slot(
456 gfx::Point(grid_rect.x() + col * tile_size.width() + x_offset, 489 gfx::Point(grid_rect.x() + col * tile_size.width() + x_offset,
457 grid_rect.y() + row * tile_size.height()), 490 grid_rect.y() + row * tile_size.height()),
458 tile_size); 491 tile_size);
459 view_model_.set_ideal_bounds(i, tile_slot); 492 if (i < view_model_.view_size()) {
493 view_model_.set_ideal_bounds(i, tile_slot);
494 } else {
495 pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
496 tile_slot);
497 }
460 498
461 ++slot_index; 499 ++slot_index;
462 } 500 }
463 } 501 }
464 502
465 void AppsGridView::AnimateToIdealBounds() { 503 void AppsGridView::AnimateToIdealBounds() {
466 const gfx::Rect visible_bounds(GetVisibleBounds()); 504 const gfx::Rect visible_bounds(GetVisibleBounds());
467 505
468 CalculateIdealBounds(); 506 CalculateIdealBounds();
469 for (int i = 0; i < view_model_.view_size(); ++i) { 507 for (int i = 0; i < view_model_.view_size(); ++i) {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
612 650
613 void AppsGridView::MoveItemInModel(views::View* item_view, 651 void AppsGridView::MoveItemInModel(views::View* item_view,
614 const Index& target) { 652 const Index& target) {
615 int current_model_index = view_model_.GetIndexOfView(item_view); 653 int current_model_index = view_model_.GetIndexOfView(item_view);
616 DCHECK_GE(current_model_index, 0); 654 DCHECK_GE(current_model_index, 0);
617 655
618 int target_model_index = target.page * tiles_per_page() + target.slot; 656 int target_model_index = target.page * tiles_per_page() + target.slot;
619 if (target_model_index == current_model_index) 657 if (target_model_index == current_model_index)
620 return; 658 return;
621 659
622 model_->RemoveObserver(this); 660 model_->apps()->RemoveObserver(this);
623 model_->Move(current_model_index, target_model_index); 661 model_->apps()->Move(current_model_index, target_model_index);
624 view_model_.Move(current_model_index, target_model_index); 662 view_model_.Move(current_model_index, target_model_index);
625 model_->AddObserver(this); 663 model_->apps()->AddObserver(this);
626 664
627 if (pagination_model_->selected_page() != target.page) 665 if (pagination_model_->selected_page() != target.page)
628 pagination_model_->SelectPage(target.page, false); 666 pagination_model_->SelectPage(target.page, false);
629 } 667 }
630 668
631 void AppsGridView::ButtonPressed(views::Button* sender, 669 void AppsGridView::ButtonPressed(views::Button* sender,
632 const ui::Event& event) { 670 const ui::Event& event) {
633 if (dragging()) 671 if (dragging())
634 return; 672 return;
635 673
636 if (sender->GetClassName() != AppListItemView::kViewClassName) 674 if (sender->GetClassName() != AppListItemView::kViewClassName)
637 return; 675 return;
638 676
639 if (delegate_) { 677 if (delegate_) {
640 delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->model(), 678 delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->model(),
641 event.flags()); 679 event.flags());
642 } 680 }
643 } 681 }
644 682
645 void AppsGridView::ListItemsAdded(size_t start, size_t count) { 683 void AppsGridView::ListItemsAdded(size_t start, size_t count) {
646 EndDrag(true); 684 EndDrag(true);
647 685
648 for (size_t i = start; i < start + count; ++i) { 686 for (size_t i = start; i < start + count; ++i) {
649 views::View* view = CreateViewForItemAtIndex(i); 687 views::View* view = CreateViewForItemAtIndex(i);
650 view_model_.Add(view, i); 688 view_model_.Add(view, i);
651 AddChildView(view); 689 AddChildView(view);
652 } 690 }
653 691
654 UpdatePaging(); 692 UpdatePaging();
693 UpdatePulsingBlockViews();
655 Layout(); 694 Layout();
656 SchedulePaint(); 695 SchedulePaint();
657 } 696 }
658 697
659 void AppsGridView::ListItemsRemoved(size_t start, size_t count) { 698 void AppsGridView::ListItemsRemoved(size_t start, size_t count) {
660 EndDrag(true); 699 EndDrag(true);
661 700
662 for (size_t i = 0; i < count; ++i) { 701 for (size_t i = 0; i < count; ++i) {
663 views::View* view = view_model_.view_at(start); 702 views::View* view = view_model_.view_at(start);
664 view_model_.Remove(start); 703 view_model_.Remove(start);
665 delete view; 704 delete view;
666 } 705 }
667 706
668 UpdatePaging(); 707 UpdatePaging();
708 UpdatePulsingBlockViews();
669 Layout(); 709 Layout();
670 SchedulePaint(); 710 SchedulePaint();
671 } 711 }
672 712
673 void AppsGridView::ListItemMoved(size_t index, size_t target_index) { 713 void AppsGridView::ListItemMoved(size_t index, size_t target_index) {
674 EndDrag(true); 714 EndDrag(true);
675 view_model_.Move(index, target_index); 715 view_model_.Move(index, target_index);
676 716
677 UpdatePaging(); 717 UpdatePaging();
678 AnimateToIdealBounds(); 718 AnimateToIdealBounds();
(...skipping 18 matching lines...) Expand all
697 737
698 void AppsGridView::TransitionChanged() { 738 void AppsGridView::TransitionChanged() {
699 // Update layout for valid page transition only since over-scroll no longer 739 // Update layout for valid page transition only since over-scroll no longer
700 // animates app icons. 740 // animates app icons.
701 const PaginationModel::Transition& transition = 741 const PaginationModel::Transition& transition =
702 pagination_model_->transition(); 742 pagination_model_->transition();
703 if (pagination_model_->is_valid_page(transition.target_page)) 743 if (pagination_model_->is_valid_page(transition.target_page))
704 Layout(); 744 Layout();
705 } 745 }
706 746
747 void AppsGridView::OnAppListModelStatusChanged() {
748 UpdatePulsingBlockViews();
749 Layout();
750 SchedulePaint();
751 }
752
707 } // namespace app_list 753 } // namespace app_list
OLDNEW
« no previous file with comments | « ui/app_list/apps_grid_view.h ('k') | ui/app_list/apps_grid_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698