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 3 // found in the LICENSE file. 4 5 #include "ui/app_list/views/apps_grid_view.h" 6 7 #include <algorithm> 8 9 #include "ui/app_list/app_list_item_model.h" 10 #include "ui/app_list/apps_grid_view_delegate.h" 11 #include "ui/app_list/pagination_model.h" 12 #include "ui/app_list/views/app_list_drag_and_drop_host.h" 13 #include "ui/app_list/views/app_list_item_view.h" 14 #include "ui/app_list/views/page_switcher.h" 15 #include "ui/app_list/views/pulsing_block_view.h" 16 #include "ui/base/animation/animation.h" 17 #include "ui/base/events/event.h" 18 #include "ui/compositor/scoped_layer_animation_settings.h" 19 #include "ui/views/border.h" 20 #include "ui/views/view_model_utils.h" 21 #include "ui/views/widget/widget.h" 22 23 #if defined(USE_AURA) 24 #include "ui/aura/root_window.h" 25 #endif 26 27 #if defined(OS_WIN) && !defined(USE_AURA) 28 #include "base/command_line.h" 29 #include "base/files/file_path.h" 30 #include "base/win/shortcut.h" 31 #include "ui/base/dragdrop/drag_utils.h" 32 #include "ui/base/dragdrop/drop_target_win.h" 33 #include "ui/base/dragdrop/os_exchange_data.h" 34 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" 35 #endif 36 37 namespace { 38 39 // Distance a drag needs to be from the app grid to be considered 'outside', at 40 // which point we rearrange the apps to their pre-drag configuration, as a drop 41 // then would be canceled. We have a buffer to make it easier to drag apps to 42 // other pages. 43 const int kDragBufferPx = 20; 44 45 // Padding space in pixels for fixed layout. 46 const int kLeftRightPadding = 20; 47 const int kTopPadding = 1; 48 49 // Padding space in pixels between pages. 50 const int kPagePadding = 40; 51 52 // Preferred tile size when showing in fixed layout. 53 const int kPreferredTileWidth = 88; 54 const int kPreferredTileHeight = 98; 55 56 // Width in pixels of the area on the sides that triggers a page flip. 57 const int kPageFlipZoneSize = 40; 58 59 // Delay in milliseconds to do the page flip. 60 const int kPageFlipDelayInMs = 1000; 61 62 // How many pages on either side of the selected one we prerender. 63 const int kPrerenderPages = 1; 64 65 // The drag and drop proxy should get scaled by this factor. 66 const float kDragAndDropProxyScale = 1.5f; 67 68 // For testing we remember the last created grid view. 69 app_list::AppsGridView* last_created_grid_view_for_test = NULL; 70 71 // RowMoveAnimationDelegate is used when moving an item into a different row. 72 // Before running the animation, the item's layer is re-created and kept in 73 // the original position, then the item is moved to just before its target 74 // position and opacity set to 0. When the animation runs, this delegate moves 75 // the layer and fades it out while fading in the item at the same time. 76 class RowMoveAnimationDelegate 77 : public views::BoundsAnimator::OwnedAnimationDelegate { 78 public: 79 RowMoveAnimationDelegate(views::View* view, 80 ui::Layer* layer, 81 const gfx::Rect& layer_target) 82 : view_(view), 83 layer_(layer), 84 layer_start_(layer ? layer->bounds() : gfx::Rect()), 85 layer_target_(layer_target) { 86 } 87 virtual ~RowMoveAnimationDelegate() {} 88 89 // ui::AnimationDelegate overrides: 90 virtual void AnimationProgressed(const ui::Animation* animation) OVERRIDE { 91 view_->layer()->SetOpacity(animation->GetCurrentValue()); 92 view_->layer()->ScheduleDraw(); 93 94 if (layer_) { 95 layer_->SetOpacity(1 - animation->GetCurrentValue()); 96 layer_->SetBounds(animation->CurrentValueBetween(layer_start_, 97 layer_target_)); 98 layer_->ScheduleDraw(); 99 } 100 } 101 virtual void AnimationEnded(const ui::Animation* animation) OVERRIDE { 102 view_->layer()->SetOpacity(1.0f); 103 view_->layer()->ScheduleDraw(); 104 } 105 virtual void AnimationCanceled(const ui::Animation* animation) OVERRIDE { 106 view_->layer()->SetOpacity(1.0f); 107 view_->layer()->ScheduleDraw(); 108 } 109 110 private: 111 // The view that needs to be wrapped. Owned by views hierarchy. 112 views::View* view_; 113 114 scoped_ptr<ui::Layer> layer_; 115 const gfx::Rect layer_start_; 116 const gfx::Rect layer_target_; 117 118 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate); 119 }; 120 121 } // namespace 122 123 namespace app_list { 124 125 #if defined(OS_WIN) && !defined(USE_AURA) 126 // Interprets drag events sent from Windows via the drag/drop API and forwards 127 // them to AppsGridView. 128 // On Windows, in order to have the OS perform the drag properly we need to 129 // provide it with a shortcut file which may or may not exist at the time the 130 // drag is started. Therefore while waiting for that shortcut to be located we 131 // just do a regular "internal" drag and transition into the synchronous drag 132 // when the shortcut is found/created. Hence a synchronous drag is an optional 133 // phase of a regular drag and non-Windows platforms drags are equivalent to a 134 // Windows drag that never enters the synchronous drag phase. 135 class SynchronousDrag : public ui::DragSourceWin { 136 public: 137 SynchronousDrag(app_list::AppsGridView* grid_view, 138 app_list::AppListItemView* drag_view, 139 const gfx::Point& drag_view_offset) 140 : grid_view_(grid_view), 141 drag_view_(drag_view), 142 drag_view_offset_(drag_view_offset), 143 has_shortcut_path_(false), 144 running_(false), 145 canceled_(false) { 146 } 147 148 void set_shortcut_path(const base::FilePath& shortcut_path) { 149 has_shortcut_path_ = true; 150 shortcut_path_ = shortcut_path; 151 } 152 153 bool CanRun() { 154 return has_shortcut_path_ && !running_; 155 } 156 157 void Run() { 158 DCHECK(CanRun()); 159 running_ = true; 160 161 ui::OSExchangeData data; 162 SetupExchangeData(&data); 163 164 // Hide the dragged view because the OS is going to create its own. 165 const gfx::Size drag_view_size = drag_view_->size(); 166 drag_view_->SetSize(gfx::Size(0, 0)); 167 168 // Blocks until the drag is finished. Calls into the ui::DragSourceWin 169 // methods. 170 DWORD effects; 171 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data), 172 this, DROPEFFECT_MOVE | DROPEFFECT_LINK, &effects); 173 174 // Restore the dragged view to its original size. 175 drag_view_->SetSize(drag_view_size); 176 177 grid_view_->EndDrag(canceled_ || !IsCursorWithinGridView()); 178 } 179 180 private: 181 // Overridden from ui::DragSourceWin. 182 virtual void OnDragSourceCancel() OVERRIDE { 183 canceled_ = true; 184 } 185 186 virtual void OnDragSourceDrop() OVERRIDE { 187 } 188 189 virtual void OnDragSourceMove() OVERRIDE { 190 grid_view_->UpdateDrag(app_list::AppsGridView::MOUSE, 191 GetCursorInGridViewCoords()); 192 } 193 194 void SetupExchangeData(ui::OSExchangeData* data) { 195 data->SetFilename(shortcut_path_); 196 gfx::ImageSkia image(drag_view_->GetDragImage()); 197 gfx::Size image_size(image.size()); 198 drag_utils::SetDragImageOnDataObject( 199 image, 200 image.size(), 201 gfx::Vector2d(drag_view_offset_.x(), drag_view_offset_.y()), 202 data); 203 } 204 205 HWND GetGridViewHWND() { 206 return grid_view_->GetWidget()->GetTopLevelWidget()->GetNativeView(); 207 } 208 209 bool IsCursorWithinGridView() { 210 POINT p; 211 GetCursorPos(&p); 212 return GetGridViewHWND() == WindowFromPoint(p); 213 } 214 215 gfx::Point GetCursorInGridViewCoords() { 216 POINT p; 217 GetCursorPos(&p); 218 ScreenToClient(GetGridViewHWND(), &p); 219 gfx::Point grid_view_pt(p.x, p.y); 220 views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt); 221 return grid_view_pt; 222 } 223 224 app_list::AppsGridView* grid_view_; 225 app_list::AppListItemView* drag_view_; 226 gfx::Point drag_view_offset_; 227 bool has_shortcut_path_; 228 base::FilePath shortcut_path_; 229 bool running_; 230 bool canceled_; 231 232 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag); 233 }; 234 #endif // defined(OS_WIN) && !defined(USE_AURA) 235 236 AppsGridView::AppsGridView(AppsGridViewDelegate* delegate, 237 PaginationModel* pagination_model) 238 : model_(NULL), 239 delegate_(delegate), 240 pagination_model_(pagination_model), 241 page_switcher_view_(new PageSwitcher(pagination_model)), 242 cols_(0), 243 rows_per_page_(0), 244 selected_view_(NULL), 245 drag_view_(NULL), 246 drag_start_page_(-1), 247 drag_pointer_(NONE), 248 drag_and_drop_host_(NULL), 249 forward_events_to_drag_and_drop_host_(false), 250 page_flip_target_(-1), 251 page_flip_delay_in_ms_(kPageFlipDelayInMs), 252 bounds_animator_(this) { 253 last_created_grid_view_for_test = this; 254 pagination_model_->AddObserver(this); 255 AddChildView(page_switcher_view_); 256 } 257 258 AppsGridView::~AppsGridView() { 259 if (model_) { 260 model_->RemoveObserver(this); 261 model_->apps()->RemoveObserver(this); 262 } 263 pagination_model_->RemoveObserver(this); 264 } 265 266 void AppsGridView::SetLayout(int icon_size, int cols, int rows_per_page) { 267 icon_size_.SetSize(icon_size, icon_size); 268 cols_ = cols; 269 rows_per_page_ = rows_per_page; 270 271 set_border(views::Border::CreateEmptyBorder(kTopPadding, 272 kLeftRightPadding, 273 0, 274 kLeftRightPadding)); 275 } 276 277 void AppsGridView::SetModel(AppListModel* model) { 278 if (model_) { 279 model_->RemoveObserver(this); 280 model_->apps()->RemoveObserver(this); 281 } 282 283 model_ = model; 284 if (model_) { 285 model_->AddObserver(this); 286 model_->apps()->AddObserver(this); 287 } 288 Update(); 289 } 290 291 void AppsGridView::SetSelectedView(views::View* view) { 292 if (IsSelectedView(view) || IsDraggedView(view)) 293 return; 294 295 Index index = GetIndexOfView(view); 296 if (IsValidIndex(index)) 297 SetSelectedItemByIndex(index); 298 } 299 300 void AppsGridView::ClearSelectedView(views::View* view) { 301 if (view && IsSelectedView(view)) { 302 selected_view_->SchedulePaint(); 303 selected_view_ = NULL; 304 } 305 } 306 307 bool AppsGridView::IsSelectedView(const views::View* view) const { 308 return selected_view_ == view; 309 } 310 311 void AppsGridView::EnsureViewVisible(const views::View* view) { 312 if (pagination_model_->has_transition()) 313 return; 314 315 Index index = GetIndexOfView(view); 316 if (IsValidIndex(index)) 317 pagination_model_->SelectPage(index.page, false); 318 } 319 320 void AppsGridView::InitiateDrag(AppListItemView* view, 321 Pointer pointer, 322 const ui::LocatedEvent& event) { 323 DCHECK(view); 324 if (drag_view_ || pulsing_blocks_model_.view_size()) 325 return; 326 327 drag_view_ = view; 328 drag_view_offset_ = event.location(); 329 drag_start_page_ = pagination_model_->selected_page(); 330 ExtractDragLocation(event, &drag_start_grid_view_); 331 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y()); 332 } 333 334 void AppsGridView::OnGotShortcutPath(const base::FilePath& path) { 335 #if defined(OS_WIN) && !defined(USE_AURA) 336 // Drag may have ended before we get the shortcut path. 337 if (!synchronous_drag_) 338 return; 339 // Setting the shortcut path here means the next time we hit UpdateDrag() 340 // we'll enter the synchronous drag. 341 // NOTE we don't Run() the drag here because that causes animations not to 342 // update for some reason. 343 synchronous_drag_->set_shortcut_path(path); 344 DCHECK(synchronous_drag_->CanRun()); 345 #endif 346 } 347 348 void AppsGridView::StartSettingUpSynchronousDrag() { 349 #if defined(OS_WIN) && !defined(USE_AURA) 350 delegate_->GetShortcutPathForApp( 351 drag_view_->model()->app_id(), 352 base::Bind(&AppsGridView::OnGotShortcutPath, base::Unretained(this))); 353 synchronous_drag_ = new SynchronousDrag(this, drag_view_, drag_view_offset_); 354 #endif 355 } 356 357 bool AppsGridView::RunSynchronousDrag() { 358 #if defined(OS_WIN) && !defined(USE_AURA) 359 if (synchronous_drag_ && synchronous_drag_->CanRun()) { 360 synchronous_drag_->Run(); 361 synchronous_drag_ = NULL; 362 return true; 363 } 364 #endif 365 return false; 366 } 367 368 void AppsGridView::CleanUpSynchronousDrag() { 369 #if defined(OS_WIN) && !defined(USE_AURA) 370 synchronous_drag_ = NULL; 371 #endif 372 } 373 374 void AppsGridView::UpdateDragFromItem(Pointer pointer, 375 const ui::LocatedEvent& event) { 376 gfx::Point drag_point_in_grid_view; 377 ExtractDragLocation(event, &drag_point_in_grid_view); 378 UpdateDrag(pointer, drag_point_in_grid_view); 379 if (!dragging()) 380 return; 381 382 // If a drag and drop host is provided, see if the drag operation needs to be 383 // forwarded. 384 DispatchDragEventToDragAndDropHost(event.root_location()); 385 if (drag_and_drop_host_) 386 drag_and_drop_host_->UpdateDragIconProxy(event.root_location()); 387 } 388 389 void AppsGridView::UpdateDrag(Pointer pointer, const gfx::Point& point) { 390 // EndDrag was called before if |drag_view_| is NULL. 391 if (!drag_view_) 392 return; 393 394 if (RunSynchronousDrag()) 395 return; 396 397 gfx::Vector2d drag_vector(point - drag_start_grid_view_); 398 if (!dragging() && ExceededDragThreshold(drag_vector)) { 399 drag_pointer_ = pointer; 400 // Move the view to the front so that it appears on top of other views. 401 ReorderChildView(drag_view_, -1); 402 bounds_animator_.StopAnimatingView(drag_view_); 403 StartSettingUpSynchronousDrag(); 404 StartDragAndDropHostDrag(point); 405 } 406 407 if (drag_pointer_ != pointer) 408 return; 409 410 last_drag_point_ = point; 411 const Index last_drop_target = drop_target_; 412 CalculateDropTarget(last_drag_point_, false); 413 414 if (IsPointWithinDragBuffer(last_drag_point_)) 415 MaybeStartPageFlipTimer(last_drag_point_); 416 else 417 StopPageFlipTimer(); 418 419 gfx::Point page_switcher_point(last_drag_point_); 420 views::View::ConvertPointToTarget(this, page_switcher_view_, 421 &page_switcher_point); 422 page_switcher_view_->UpdateUIForDragPoint(page_switcher_point); 423 424 if (last_drop_target != drop_target_) 425 AnimateToIdealBounds(); 426 427 drag_view_->SetPosition(drag_view_start_ + drag_vector); 428 } 429 430 void AppsGridView::EndDrag(bool cancel) { 431 // EndDrag was called before if |drag_view_| is NULL. 432 if (!drag_view_) 433 return; 434 435 if (forward_events_to_drag_and_drop_host_) { 436 forward_events_to_drag_and_drop_host_ = false; 437 drag_and_drop_host_->EndDrag(cancel); 438 } else if (!cancel && dragging()) { 439 CalculateDropTarget(last_drag_point_, true); 440 if (IsValidIndex(drop_target_)) 441 MoveItemInModel(drag_view_, drop_target_); 442 } 443 444 if (drag_and_drop_host_) { 445 // If we had a drag and drop proxy icon, we delete it and make the real 446 // item visible again. 447 drag_and_drop_host_->DestroyDragIconProxy(); 448 HideView(drag_view_, false); 449 } 450 451 // The drag can be ended after the synchronous drag is created but before it 452 // is Run(). 453 CleanUpSynchronousDrag(); 454 455 drag_pointer_ = NONE; 456 drop_target_ = Index(); 457 drag_view_ = NULL; 458 drag_start_grid_view_ = gfx::Point(); 459 drag_start_page_ = -1; 460 drag_view_offset_ = gfx::Point(); 461 AnimateToIdealBounds(); 462 463 StopPageFlipTimer(); 464 } 465 466 void AppsGridView::StopPageFlipTimer() { 467 page_flip_timer_.Stop(); 468 page_flip_target_ = -1; 469 } 470 471 bool AppsGridView::IsDraggedView(const views::View* view) const { 472 return drag_view_ == view; 473 } 474 475 void AppsGridView::SetDragAndDropHostOfCurrentAppList( 476 ApplicationDragAndDropHost* drag_and_drop_host) { 477 drag_and_drop_host_ = drag_and_drop_host; 478 } 479 480 void AppsGridView::Prerender(int page_index) { 481 Layout(); 482 int start = std::max(0, (page_index - kPrerenderPages) * tiles_per_page()); 483 int end = std::min(view_model_.view_size(), 484 (page_index + 1 + kPrerenderPages) * tiles_per_page()); 485 for (int i = start; i < end; i++) { 486 AppListItemView* v = static_cast<AppListItemView*>(view_model_.view_at(i)); 487 v->Prerender(); 488 } 489 } 490 491 gfx::Size AppsGridView::GetPreferredSize() { 492 const gfx::Insets insets(GetInsets()); 493 const gfx::Size tile_size = gfx::Size(kPreferredTileWidth, 494 kPreferredTileHeight); 495 const int page_switcher_height = 496 page_switcher_view_->GetPreferredSize().height(); 497 return gfx::Size( 498 tile_size.width() * cols_ + insets.width(), 499 tile_size.height() * rows_per_page_ + 500 page_switcher_height + insets.height()); 501 } 502 503 bool AppsGridView::GetDropFormats( 504 int* formats, 505 std::set<OSExchangeData::CustomFormat>* custom_formats) { 506 // TODO(koz): Only accept a specific drag type for app shortcuts. 507 *formats = OSExchangeData::FILE_NAME; 508 return true; 509 } 510 511 bool AppsGridView::CanDrop(const OSExchangeData& data) { 512 return true; 513 } 514 515 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent& event) { 516 return ui::DragDropTypes::DRAG_MOVE; 517 } 518 519 void AppsGridView::Layout() { 520 if (bounds_animator_.IsAnimating()) 521 bounds_animator_.Cancel(); 522 523 CalculateIdealBounds(); 524 for (int i = 0; i < view_model_.view_size(); ++i) { 525 views::View* view = view_model_.view_at(i); 526 if (view != drag_view_) 527 view->SetBoundsRect(view_model_.ideal_bounds(i)); 528 } 529 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_); 530 531 const int page_switcher_height = 532 page_switcher_view_->GetPreferredSize().height(); 533 gfx::Rect rect(GetContentsBounds()); 534 rect.set_y(rect.bottom() - page_switcher_height); 535 rect.set_height(page_switcher_height); 536 page_switcher_view_->SetBoundsRect(rect); 537 } 538 539 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) { 540 bool handled = false; 541 if (selected_view_) 542 handled = selected_view_->OnKeyPressed(event); 543 544 if (!handled) { 545 const int forward_dir = base::i18n::IsRTL() ? -1 : 1; 546 switch (event.key_code()) { 547 case ui::VKEY_LEFT: 548 MoveSelected(0, -forward_dir, 0); 549 return true; 550 case ui::VKEY_RIGHT: 551 MoveSelected(0, forward_dir, 0); 552 return true; 553 case ui::VKEY_UP: 554 MoveSelected(0, 0, -1); 555 return true; 556 case ui::VKEY_DOWN: 557 MoveSelected(0, 0, 1); 558 return true; 559 case ui::VKEY_PRIOR: { 560 MoveSelected(-1, 0, 0); 561 return true; 562 } 563 case ui::VKEY_NEXT: { 564 MoveSelected(1, 0, 0); 565 return true; 566 } 567 default: 568 break; 569 } 570 } 571 572 return handled; 573 } 574 575 bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) { 576 bool handled = false; 577 if (selected_view_) 578 handled = selected_view_->OnKeyReleased(event); 579 580 return handled; 581 } 582 583 void AppsGridView::ViewHierarchyChanged( 584 const ViewHierarchyChangedDetails& details) { 585 if (!details.is_add && details.parent == this) { 586 if (selected_view_ == details.child) 587 selected_view_ = NULL; 588 589 if (drag_view_ == details.child) 590 EndDrag(true); 591 592 bounds_animator_.StopAnimatingView(details.child); 593 } 594 } 595 596 // static 597 AppsGridView* AppsGridView::GetLastGridViewForTest() { 598 return last_created_grid_view_for_test; 599 } 600 601 void AppsGridView::Update() { 602 DCHECK(!selected_view_ && !drag_view_); 603 604 view_model_.Clear(); 605 if (model_ && model_->apps()->item_count()) 606 ListItemsAdded(0, model_->apps()->item_count()); 607 } 608 609 void AppsGridView::UpdatePaging() { 610 if (!view_model_.view_size() || !tiles_per_page()) { 611 pagination_model_->SetTotalPages(0); 612 return; 613 } 614 615 pagination_model_->SetTotalPages( 616 (view_model_.view_size() - 1) / tiles_per_page() + 1); 617 } 618 619 void AppsGridView::UpdatePulsingBlockViews() { 620 const int available_slots = 621 tiles_per_page() - model_->apps()->item_count() % tiles_per_page(); 622 const int desired = model_->status() == AppListModel::STATUS_SYNCING ? 623 available_slots : 0; 624 625 if (pulsing_blocks_model_.view_size() == desired) 626 return; 627 628 while (pulsing_blocks_model_.view_size() > desired) { 629 views::View* view = pulsing_blocks_model_.view_at(0); 630 pulsing_blocks_model_.Remove(0); 631 delete view; 632 } 633 634 while (pulsing_blocks_model_.view_size() < desired) { 635 views::View* view = new PulsingBlockView( 636 gfx::Size(kPreferredTileWidth, kPreferredTileHeight), true); 637 pulsing_blocks_model_.Add(view, 0); 638 AddChildView(view); 639 } 640 } 641 642 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) { 643 DCHECK_LT(index, model_->apps()->item_count()); 644 AppListItemView* view = new AppListItemView(this, 645 model_->apps()->GetItemAt(index)); 646 view->SetIconSize(icon_size_); 647 #if defined(USE_AURA) 648 view->SetPaintToLayer(true); 649 view->SetFillsBoundsOpaquely(false); 650 #endif 651 return view; 652 } 653 654 void AppsGridView::SetSelectedItemByIndex(const Index& index) { 655 if (GetIndexOfView(selected_view_) == index) 656 return; 657 658 views::View* new_selection = GetViewAtIndex(index); 659 if (!new_selection) 660 return; // Keep current selection. 661 662 if (selected_view_) 663 selected_view_->SchedulePaint(); 664 665 EnsureViewVisible(new_selection); 666 selected_view_ = new_selection; 667 selected_view_->SchedulePaint(); 668 selected_view_->NotifyAccessibilityEvent( 669 ui::AccessibilityTypes::EVENT_FOCUS, true); 670 } 671 672 bool AppsGridView::IsValidIndex(const Index& index) const { 673 return index.page >= 0 && index.page < pagination_model_->total_pages() && 674 index.slot >= 0 && index.slot < tiles_per_page() && 675 index.page * tiles_per_page() + index.slot < view_model_.view_size(); 676 } 677 678 AppsGridView::Index AppsGridView::GetIndexOfView( 679 const views::View* view) const { 680 const int model_index = view_model_.GetIndexOfView(view); 681 if (model_index == -1) 682 return Index(); 683 684 return Index(model_index / tiles_per_page(), model_index % tiles_per_page()); 685 } 686 687 views::View* AppsGridView::GetViewAtIndex(const Index& index) const { 688 if (!IsValidIndex(index)) 689 return NULL; 690 691 const int model_index = index.page * tiles_per_page() + index.slot; 692 return view_model_.view_at(model_index); 693 } 694 695 void AppsGridView::MoveSelected(int page_delta, 696 int slot_x_delta, 697 int slot_y_delta) { 698 if (!selected_view_) 699 return SetSelectedItemByIndex(Index(pagination_model_->selected_page(), 0)); 700 701 const Index& selected = GetIndexOfView(selected_view_); 702 int target_slot = selected.slot + slot_x_delta + slot_y_delta * cols_; 703 704 if (selected.slot % cols_ == 0 && slot_x_delta == -1) { 705 if (selected.page > 0) { 706 page_delta = -1; 707 target_slot = selected.slot + cols_ - 1; 708 } else { 709 target_slot = selected.slot; 710 } 711 } 712 713 if (selected.slot % cols_ == cols_ - 1 && slot_x_delta == 1) { 714 if (selected.page < pagination_model_->total_pages() - 1) { 715 page_delta = 1; 716 target_slot = selected.slot - cols_ + 1; 717 } else { 718 target_slot = selected.slot; 719 } 720 } 721 722 // Clamp the target slot to the last item if we are moving to the last page 723 // but our target slot is past the end of the item list. 724 if (page_delta && 725 selected.page + page_delta == pagination_model_->total_pages() - 1) { 726 int last_item_slot = (view_model_.view_size() - 1) % tiles_per_page(); 727 if (last_item_slot < target_slot) { 728 target_slot = last_item_slot; 729 } 730 } 731 732 int target_page = std::min(pagination_model_->total_pages() - 1, 733 std::max(selected.page + page_delta, 0)); 734 SetSelectedItemByIndex(Index(target_page, target_slot)); 735 } 736 737 void AppsGridView::CalculateIdealBounds() { 738 gfx::Rect rect(GetContentsBounds()); 739 if (rect.IsEmpty()) 740 return; 741 742 gfx::Size tile_size(kPreferredTileWidth, kPreferredTileHeight); 743 744 gfx::Rect grid_rect(gfx::Size(tile_size.width() * cols_, 745 tile_size.height() * rows_per_page_)); 746 grid_rect.Intersect(rect); 747 748 // Page width including padding pixels. A tile.x + page_width means the same 749 // tile slot in the next page. 750 const int page_width = grid_rect.width() + kPagePadding; 751 752 // If there is a transition, calculates offset for current and target page. 753 const int current_page = pagination_model_->selected_page(); 754 const PaginationModel::Transition& transition = 755 pagination_model_->transition(); 756 const bool is_valid = 757 pagination_model_->is_valid_page(transition.target_page); 758 759 // Transition to right means negative offset. 760 const int dir = transition.target_page > current_page ? -1 : 1; 761 const int transition_offset = is_valid ? 762 transition.progress * page_width * dir : 0; 763 764 const int total_views = 765 view_model_.view_size() + pulsing_blocks_model_.view_size(); 766 int slot_index = 0; 767 for (int i = 0; i < total_views; ++i) { 768 if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_) 769 continue; 770 771 int page = slot_index / tiles_per_page(); 772 int slot = slot_index % tiles_per_page(); 773 774 if (drop_target_.page == page && drop_target_.slot == slot) { 775 ++slot_index; 776 page = slot_index / tiles_per_page(); 777 slot = slot_index % tiles_per_page(); 778 } 779 780 // Decides an x_offset for current item. 781 int x_offset = 0; 782 if (page < current_page) 783 x_offset = -page_width; 784 else if (page > current_page) 785 x_offset = page_width; 786 787 if (is_valid) { 788 if (page == current_page || page == transition.target_page) 789 x_offset += transition_offset; 790 } 791 792 const int row = slot / cols_; 793 const int col = slot % cols_; 794 gfx::Rect tile_slot( 795 gfx::Point(grid_rect.x() + col * tile_size.width() + x_offset, 796 grid_rect.y() + row * tile_size.height()), 797 tile_size); 798 if (i < view_model_.view_size()) { 799 view_model_.set_ideal_bounds(i, tile_slot); 800 } else { 801 pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(), 802 tile_slot); 803 } 804 805 ++slot_index; 806 } 807 } 808 809 void AppsGridView::AnimateToIdealBounds() { 810 const gfx::Rect visible_bounds(GetVisibleBounds()); 811 812 CalculateIdealBounds(); 813 for (int i = 0; i < view_model_.view_size(); ++i) { 814 views::View* view = view_model_.view_at(i); 815 if (view == drag_view_) 816 continue; 817 818 const gfx::Rect& target = view_model_.ideal_bounds(i); 819 if (bounds_animator_.GetTargetBounds(view) == target) 820 continue; 821 822 const gfx::Rect& current = view->bounds(); 823 const bool current_visible = visible_bounds.Intersects(current); 824 const bool target_visible = visible_bounds.Intersects(target); 825 const bool visible = current_visible || target_visible; 826 827 const int y_diff = target.y() - current.y(); 828 if (visible && y_diff && y_diff % kPreferredTileHeight == 0) { 829 AnimationBetweenRows(view, 830 current_visible, 831 current, 832 target_visible, 833 target); 834 } else { 835 bounds_animator_.AnimateViewTo(view, target); 836 } 837 } 838 } 839 840 void AppsGridView::AnimationBetweenRows(views::View* view, 841 bool animate_current, 842 const gfx::Rect& current, 843 bool animate_target, 844 const gfx::Rect& target) { 845 // Determine page of |current| and |target|. -1 means in the left invisible 846 // page, 0 is the center visible page and 1 means in the right invisible page. 847 const int current_page = current.x() < 0 ? -1 : 848 current.x() >= width() ? 1 : 0; 849 const int target_page = target.x() < 0 ? -1 : 850 target.x() >= width() ? 1 : 0; 851 852 const int dir = current_page < target_page || 853 (current_page == target_page && current.y() < target.y()) ? 1 : -1; 854 855 #if defined(USE_AURA) 856 scoped_ptr<ui::Layer> layer; 857 if (animate_current) { 858 layer.reset(view->RecreateLayer()); 859 layer->SuppressPaint(); 860 861 view->SetFillsBoundsOpaquely(false); 862 view->layer()->SetOpacity(0.f); 863 } 864 865 gfx::Rect current_out(current); 866 current_out.Offset(dir * kPreferredTileWidth, 0); 867 #endif 868 869 gfx::Rect target_in(target); 870 if (animate_target) 871 target_in.Offset(-dir * kPreferredTileWidth, 0); 872 view->SetBoundsRect(target_in); 873 bounds_animator_.AnimateViewTo(view, target); 874 875 #if defined(USE_AURA) 876 bounds_animator_.SetAnimationDelegate( 877 view, 878 new RowMoveAnimationDelegate(view, layer.release(), current_out), 879 true); 880 #endif 881 } 882 883 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event, 884 gfx::Point* drag_point) { 885 #if defined(USE_AURA) 886 // Use root location of |event| instead of location in |drag_view_|'s 887 // coordinates because |drag_view_| has a scale transform and location 888 // could have integer round error and causes jitter. 889 *drag_point = event.root_location(); 890 891 // GetWidget() could be NULL for tests. 892 if (GetWidget()) { 893 aura::Window::ConvertPointToTarget( 894 GetWidget()->GetNativeWindow()->GetRootWindow(), 895 GetWidget()->GetNativeWindow(), 896 drag_point); 897 } 898 899 views::View::ConvertPointFromWidget(this, drag_point); 900 #else 901 // For non-aura, root location is not clearly defined but |drag_view_| does 902 // not have the scale transform. So no round error would be introduced and 903 // it's okay to use View::ConvertPointToTarget. 904 *drag_point = event.location(); 905 views::View::ConvertPointToTarget(drag_view_, this, drag_point); 906 #endif 907 } 908 909 void AppsGridView::CalculateDropTarget(const gfx::Point& drag_point, 910 bool use_page_button_hovering) { 911 int current_page = pagination_model_->selected_page(); 912 gfx::Point point(drag_point); 913 if (!IsPointWithinDragBuffer(drag_point)) { 914 point = drag_start_grid_view_; 915 current_page = drag_start_page_; 916 } 917 918 if (use_page_button_hovering && 919 page_switcher_view_->bounds().Contains(point)) { 920 gfx::Point page_switcher_point(point); 921 views::View::ConvertPointToTarget(this, page_switcher_view_, 922 &page_switcher_point); 923 int page = page_switcher_view_->GetPageForPoint(page_switcher_point); 924 if (pagination_model_->is_valid_page(page)) { 925 drop_target_.page = page; 926 drop_target_.slot = tiles_per_page() - 1; 927 } 928 } else { 929 gfx::Rect bounds(GetContentsBounds()); 930 const int drop_row = (point.y() - bounds.y()) / kPreferredTileHeight; 931 const int drop_col = std::min(cols_ - 1, 932 (point.x() - bounds.x()) / kPreferredTileWidth); 933 934 drop_target_.page = current_page; 935 drop_target_.slot = std::max(0, std::min( 936 tiles_per_page() - 1, 937 drop_row * cols_ + drop_col)); 938 } 939 940 // Limits to the last possible slot on last page. 941 if (drop_target_.page == pagination_model_->total_pages() - 1) { 942 drop_target_.slot = std::min( 943 (view_model_.view_size() - 1) % tiles_per_page(), 944 drop_target_.slot); 945 } 946 } 947 948 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) { 949 // When a drag and drop host is given, the item can be dragged out of the app 950 // list window. In that case a proxy widget needs to be used. 951 // Note: This code has very likely to be changed for Windows (non metro mode) 952 // when a |drag_and_drop_host_| gets implemented. 953 if (!drag_view_ || !drag_and_drop_host_) 954 return; 955 956 gfx::Point screen_location = grid_location; 957 views::View::ConvertPointToScreen(this, &screen_location); 958 959 // Determine the mouse offset to the center of the icon so that the drag and 960 // drop host follows this layer. 961 gfx::Vector2d delta = drag_view_offset_ - 962 drag_view_->GetLocalBounds().CenterPoint(); 963 delta.set_y(delta.y() + drag_view_->title()->size().height() / 2); 964 965 // We have to hide the original item since the drag and drop host will do 966 // the OS dependent code to "lift off the dragged item". 967 drag_and_drop_host_->CreateDragIconProxy(screen_location, 968 drag_view_->model()->icon(), 969 drag_view_, 970 delta, 971 kDragAndDropProxyScale); 972 HideView(drag_view_, true); 973 } 974 975 void AppsGridView::DispatchDragEventToDragAndDropHost( 976 const gfx::Point& point) { 977 if (!drag_view_ || !drag_and_drop_host_) 978 return; 979 if (bounds().Contains(last_drag_point_)) { 980 // The event was issued inside the app menu and we should get all events. 981 if (forward_events_to_drag_and_drop_host_) { 982 // The DnD host was previously called and needs to be informed that the 983 // session returns to the owner. 984 forward_events_to_drag_and_drop_host_ = false; 985 drag_and_drop_host_->EndDrag(true); 986 } 987 } else { 988 // The event happened outside our app menu and we might need to dispatch. 989 if (forward_events_to_drag_and_drop_host_) { 990 // Dispatch since we have already started. 991 if (!drag_and_drop_host_->Drag(point)) { 992 // The host is not active any longer and we cancel the operation. 993 forward_events_to_drag_and_drop_host_ = false; 994 drag_and_drop_host_->EndDrag(true); 995 } 996 } else { 997 if (drag_and_drop_host_->StartDrag(drag_view_->model()->app_id(), 998 point)) { 999 // From now on we forward the drag events. 1000 forward_events_to_drag_and_drop_host_ = true; 1001 // Any flip operations are stopped. 1002 StopPageFlipTimer(); 1003 } 1004 } 1005 } 1006 } 1007 1008 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) { 1009 if (!IsPointWithinDragBuffer(drag_point)) 1010 StopPageFlipTimer(); 1011 int new_page_flip_target = -1; 1012 1013 if (page_switcher_view_->bounds().Contains(drag_point)) { 1014 gfx::Point page_switcher_point(drag_point); 1015 views::View::ConvertPointToTarget(this, page_switcher_view_, 1016 &page_switcher_point); 1017 new_page_flip_target = 1018 page_switcher_view_->GetPageForPoint(page_switcher_point); 1019 } 1020 1021 // TODO(xiyuan): Fix this for RTL. 1022 if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize) 1023 new_page_flip_target = pagination_model_->selected_page() - 1; 1024 1025 if (new_page_flip_target == -1 && 1026 drag_point.x() > width() - kPageFlipZoneSize) { 1027 new_page_flip_target = pagination_model_->selected_page() + 1; 1028 } 1029 1030 if (new_page_flip_target == page_flip_target_) 1031 return; 1032 1033 StopPageFlipTimer(); 1034 if (pagination_model_->is_valid_page(new_page_flip_target)) { 1035 page_flip_target_ = new_page_flip_target; 1036 1037 if (page_flip_target_ != pagination_model_->selected_page()) { 1038 page_flip_timer_.Start(FROM_HERE, 1039 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_), 1040 this, &AppsGridView::OnPageFlipTimer); 1041 } 1042 } 1043 } 1044 1045 void AppsGridView::OnPageFlipTimer() { 1046 DCHECK(pagination_model_->is_valid_page(page_flip_target_)); 1047 pagination_model_->SelectPage(page_flip_target_, true); 1048 } 1049 1050 void AppsGridView::MoveItemInModel(views::View* item_view, 1051 const Index& target) { 1052 int current_model_index = view_model_.GetIndexOfView(item_view); 1053 DCHECK_GE(current_model_index, 0); 1054 1055 int target_model_index = target.page * tiles_per_page() + target.slot; 1056 if (target_model_index == current_model_index) 1057 return; 1058 1059 model_->apps()->RemoveObserver(this); 1060 model_->apps()->Move(current_model_index, target_model_index); 1061 view_model_.Move(current_model_index, target_model_index); 1062 model_->apps()->AddObserver(this); 1063 1064 if (pagination_model_->selected_page() != target.page) 1065 pagination_model_->SelectPage(target.page, false); 1066 } 1067 1068 void AppsGridView::CancelContextMenusOnCurrentPage() { 1069 int start = pagination_model_->selected_page() * tiles_per_page(); 1070 int end = std::min(view_model_.view_size(), start + tiles_per_page()); 1071 for (int i = start; i < end; ++i) { 1072 AppListItemView* view = 1073 static_cast<AppListItemView*>(view_model_.view_at(i)); 1074 view->CancelContextMenu(); 1075 } 1076 } 1077 1078 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const { 1079 gfx::Rect rect(GetLocalBounds()); 1080 rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx); 1081 return rect.Contains(point); 1082 } 1083 1084 void AppsGridView::ButtonPressed(views::Button* sender, 1085 const ui::Event& event) { 1086 if (dragging()) 1087 return; 1088 1089 if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName)) 1090 return; 1091 1092 if (delegate_) { 1093 delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->model(), 1094 event.flags()); 1095 } 1096 } 1097 1098 void AppsGridView::ListItemsAdded(size_t start, size_t count) { 1099 EndDrag(true); 1100 1101 for (size_t i = start; i < start + count; ++i) { 1102 views::View* view = CreateViewForItemAtIndex(i); 1103 view_model_.Add(view, i); 1104 AddChildView(view); 1105 } 1106 1107 UpdatePaging(); 1108 UpdatePulsingBlockViews(); 1109 Layout(); 1110 SchedulePaint(); 1111 } 1112 1113 void AppsGridView::ListItemsRemoved(size_t start, size_t count) { 1114 EndDrag(true); 1115 1116 for (size_t i = 0; i < count; ++i) { 1117 views::View* view = view_model_.view_at(start); 1118 view_model_.Remove(start); 1119 delete view; 1120 } 1121 1122 UpdatePaging(); 1123 UpdatePulsingBlockViews(); 1124 Layout(); 1125 SchedulePaint(); 1126 } 1127 1128 void AppsGridView::ListItemMoved(size_t index, size_t target_index) { 1129 EndDrag(true); 1130 view_model_.Move(index, target_index); 1131 1132 UpdatePaging(); 1133 AnimateToIdealBounds(); 1134 } 1135 1136 void AppsGridView::ListItemsChanged(size_t start, size_t count) { 1137 NOTREACHED(); 1138 } 1139 1140 void AppsGridView::TotalPagesChanged() { 1141 } 1142 1143 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) { 1144 if (dragging()) { 1145 CalculateDropTarget(last_drag_point_, true); 1146 Layout(); 1147 MaybeStartPageFlipTimer(last_drag_point_); 1148 } else { 1149 ClearSelectedView(selected_view_); 1150 Layout(); 1151 } 1152 } 1153 1154 void AppsGridView::TransitionStarted() { 1155 CancelContextMenusOnCurrentPage(); 1156 } 1157 1158 void AppsGridView::TransitionChanged() { 1159 // Update layout for valid page transition only since over-scroll no longer 1160 // animates app icons. 1161 const PaginationModel::Transition& transition = 1162 pagination_model_->transition(); 1163 if (pagination_model_->is_valid_page(transition.target_page)) 1164 Layout(); 1165 } 1166 1167 void AppsGridView::OnAppListModelStatusChanged() { 1168 UpdatePulsingBlockViews(); 1169 Layout(); 1170 SchedulePaint(); 1171 } 1172 1173 void AppsGridView::HideView(views::View* view, bool hide) { 1174 #if defined(USE_AURA) 1175 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator()); 1176 animator.SetPreemptionStrategy(ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET); 1177 view->layer()->SetOpacity(hide ? 0 : 1); 1178 #endif 1179 } 1180 1181 } // namespace app_list 1182