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.
5 #include "ui/app_list/views/apps_grid_view.h"
11 #include "base/guid.h"
12 #include "ui/app_list/app_list_constants.h"
13 #include "ui/app_list/app_list_folder_item.h"
14 #include "ui/app_list/app_list_item.h"
15 #include "ui/app_list/app_list_switches.h"
16 #include "ui/app_list/pagination_controller.h"
17 #include "ui/app_list/views/app_list_drag_and_drop_host.h"
18 #include "ui/app_list/views/app_list_folder_view.h"
19 #include "ui/app_list/views/app_list_item_view.h"
20 #include "ui/app_list/views/apps_grid_view_delegate.h"
21 #include "ui/app_list/views/page_switcher.h"
22 #include "ui/app_list/views/pulsing_block_view.h"
23 #include "ui/app_list/views/top_icon_animation_view.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event.h"
26 #include "ui/gfx/animation/animation.h"
27 #include "ui/gfx/geometry/vector2d.h"
28 #include "ui/gfx/geometry/vector2d_conversions.h"
29 #include "ui/views/border.h"
30 #include "ui/views/view_model_utils.h"
31 #include "ui/views/widget/widget.h"
34 #include "ui/aura/window.h"
35 #include "ui/aura/window_event_dispatcher.h"
37 #include "ui/views/win/hwnd_util.h"
38 #endif // defined(OS_WIN)
39 #endif // defined(USE_AURA)
42 #include "base/command_line.h"
43 #include "base/files/file_path.h"
44 #include "base/win/shortcut.h"
45 #include "ui/base/dragdrop/drag_utils.h"
46 #include "ui/base/dragdrop/drop_target_win.h"
47 #include "ui/base/dragdrop/os_exchange_data.h"
48 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
49 #include "ui/gfx/win/dpi.h"
56 // Distance a drag needs to be from the app grid to be considered 'outside', at
57 // which point we rearrange the apps to their pre-drag configuration, as a drop
58 // then would be canceled. We have a buffer to make it easier to drag apps to
60 const int kDragBufferPx = 20;
62 // Padding space in pixels for fixed layout.
63 const int kBottomPadding = 3;
64 const int kLeftRightPadding = 24;
66 // Padding space in pixels between pages.
67 const int kPagePadding = 40;
69 // Preferred tile size when showing in fixed layout.
70 const int kPreferredTileWidth = 88;
71 const int kPreferredTileHeight = 98;
73 const int kExperimentalPreferredTileWidth = 90;
74 const int kExperimentalPreferredTileHeight = 90;
76 // Padding on each side of a tile.
77 const int kExperimentalTileLeftRightPadding = 15;
78 const int kExperimentalTileTopBottomPadding = 11;
80 // Width in pixels of the area on the sides that triggers a page flip.
81 const int kPageFlipZoneSize = 40;
83 // Delay in milliseconds to do the page flip.
84 const int kPageFlipDelayInMs = 1000;
86 // How many pages on either side of the selected one we prerender.
87 const int kPrerenderPages = 1;
89 // The drag and drop proxy should get scaled by this factor.
90 const float kDragAndDropProxyScale = 1.5f;
92 // Delays in milliseconds to show folder dropping preview circle.
93 const int kFolderDroppingDelay = 150;
95 // Delays in milliseconds to show re-order preview.
96 const int kReorderDelay = 120;
98 // Delays in milliseconds to show folder item reparent UI.
99 const int kFolderItemReparentDelay = 50;
101 // Radius of the circle, in which if entered, show folder dropping preview
103 const int kFolderDroppingCircleRadius = 39;
105 // Returns the size of a tile view excluding its padding.
106 gfx::Size GetTileViewSize() {
107 return switches::IsExperimentalAppListEnabled()
108 ? gfx::Size(kExperimentalPreferredTileWidth,
109 kExperimentalPreferredTileHeight)
110 : gfx::Size(kPreferredTileWidth, kPreferredTileHeight);
113 // Returns the size of a tile view inccluding its padding.
114 gfx::Size GetTotalTileSize() {
115 gfx::Size size = GetTileViewSize();
116 if (switches::IsExperimentalAppListEnabled())
117 size.Enlarge(2 * kExperimentalTileLeftRightPadding,
118 2 * kExperimentalTileTopBottomPadding);
122 // RowMoveAnimationDelegate is used when moving an item into a different row.
123 // Before running the animation, the item's layer is re-created and kept in
124 // the original position, then the item is moved to just before its target
125 // position and opacity set to 0. When the animation runs, this delegate moves
126 // the layer and fades it out while fading in the item at the same time.
127 class RowMoveAnimationDelegate : public gfx::AnimationDelegate {
129 RowMoveAnimationDelegate(views::View* view,
131 const gfx::Rect& layer_target)
134 layer_start_(layer ? layer->bounds() : gfx::Rect()),
135 layer_target_(layer_target) {
137 ~RowMoveAnimationDelegate() override {}
139 // gfx::AnimationDelegate overrides:
140 void AnimationProgressed(const gfx::Animation* animation) override {
141 view_->layer()->SetOpacity(animation->GetCurrentValue());
142 view_->layer()->ScheduleDraw();
145 layer_->SetOpacity(1 - animation->GetCurrentValue());
146 layer_->SetBounds(animation->CurrentValueBetween(layer_start_,
148 layer_->ScheduleDraw();
151 void AnimationEnded(const gfx::Animation* animation) override {
152 view_->layer()->SetOpacity(1.0f);
153 view_->SchedulePaint();
155 void AnimationCanceled(const gfx::Animation* animation) override {
156 view_->layer()->SetOpacity(1.0f);
157 view_->SchedulePaint();
161 // The view that needs to be wrapped. Owned by views hierarchy.
164 scoped_ptr<ui::Layer> layer_;
165 const gfx::Rect layer_start_;
166 const gfx::Rect layer_target_;
168 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate);
171 // ItemRemoveAnimationDelegate is used to show animation for removing an item.
172 // This happens when user drags an item into a folder. The dragged item will
173 // be removed from the original list after it is dropped into the folder.
174 class ItemRemoveAnimationDelegate : public gfx::AnimationDelegate {
176 explicit ItemRemoveAnimationDelegate(views::View* view)
180 ~ItemRemoveAnimationDelegate() override {}
182 // gfx::AnimationDelegate overrides:
183 void AnimationProgressed(const gfx::Animation* animation) override {
184 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
185 view_->layer()->ScheduleDraw();
189 scoped_ptr<views::View> view_;
191 DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate);
194 // ItemMoveAnimationDelegate observes when an item finishes animating when it is
195 // not moving between rows. This is to ensure an item is repainted for the
196 // "zoom out" case when releasing an item being dragged.
197 class ItemMoveAnimationDelegate : public gfx::AnimationDelegate {
199 ItemMoveAnimationDelegate(views::View* view) : view_(view) {}
201 void AnimationEnded(const gfx::Animation* animation) override {
202 view_->SchedulePaint();
204 void AnimationCanceled(const gfx::Animation* animation) override {
205 view_->SchedulePaint();
211 DISALLOW_COPY_AND_ASSIGN(ItemMoveAnimationDelegate);
214 // Returns true if the |item| is a folder item.
215 bool IsFolderItem(AppListItem* item) {
216 return (item->GetItemType() == AppListFolderItem::kItemType);
219 bool IsOEMFolderItem(AppListItem* item) {
220 return IsFolderItem(item) &&
221 (static_cast<AppListFolderItem*>(item))->folder_type() ==
222 AppListFolderItem::FOLDER_TYPE_OEM;
225 int ClampToRange(int value, int min, int max) {
226 return std::min(std::max(value, min), max);
232 // Interprets drag events sent from Windows via the drag/drop API and forwards
233 // them to AppsGridView.
234 // On Windows, in order to have the OS perform the drag properly we need to
235 // provide it with a shortcut file which may or may not exist at the time the
236 // drag is started. Therefore while waiting for that shortcut to be located we
237 // just do a regular "internal" drag and transition into the synchronous drag
238 // when the shortcut is found/created. Hence a synchronous drag is an optional
239 // phase of a regular drag and non-Windows platforms drags are equivalent to a
240 // Windows drag that never enters the synchronous drag phase.
241 class SynchronousDrag : public ui::DragSourceWin {
243 SynchronousDrag(AppsGridView* grid_view,
244 AppListItemView* drag_view,
245 const gfx::Point& drag_view_offset)
246 : grid_view_(grid_view),
247 drag_view_(drag_view),
248 drag_view_offset_(drag_view_offset),
249 has_shortcut_path_(false),
253 void set_shortcut_path(const base::FilePath& shortcut_path) {
254 has_shortcut_path_ = true;
255 shortcut_path_ = shortcut_path;
258 bool running() { return running_; }
261 return has_shortcut_path_ && !running_;
267 // Prevent the synchronous dragger being destroyed while the drag is
269 scoped_refptr<SynchronousDrag> this_ref = this;
272 ui::OSExchangeData data;
273 SetupExchangeData(&data);
275 // Hide the dragged view because the OS is going to create its own.
276 drag_view_->SetVisible(false);
278 // Blocks until the drag is finished. Calls into the ui::DragSourceWin
281 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
282 this, DROPEFFECT_MOVE | DROPEFFECT_LINK, &effects);
284 // If |drag_view_| is NULL the drag was ended by some reentrant code.
286 // Make the drag view visible again.
287 drag_view_->SetVisible(true);
288 drag_view_->OnSyncDragEnd();
290 grid_view_->EndDrag(canceled_ || !IsCursorWithinGridView());
294 void EndDragExternally() {
297 drag_view_->SetVisible(true);
302 // Overridden from ui::DragSourceWin.
303 virtual void OnDragSourceCancel() override {
307 virtual void OnDragSourceDrop() override {
310 virtual void OnDragSourceMove() override {
311 grid_view_->UpdateDrag(AppsGridView::MOUSE, GetCursorInGridViewCoords());
314 void SetupExchangeData(ui::OSExchangeData* data) {
315 data->SetFilename(shortcut_path_);
316 gfx::ImageSkia image(drag_view_->GetDragImage());
317 gfx::Size image_size(image.size());
318 drag_utils::SetDragImageOnDataObject(
320 drag_view_offset_ - drag_view_->GetDragImageOffset(),
324 HWND GetGridViewHWND() {
325 return views::HWNDForView(grid_view_);
328 bool IsCursorWithinGridView() {
331 return GetGridViewHWND() == WindowFromPoint(p);
334 gfx::Point GetCursorInGridViewCoords() {
337 ScreenToClient(GetGridViewHWND(), &p);
338 gfx::Point grid_view_pt(p.x, p.y);
339 grid_view_pt = gfx::win::ScreenToDIPPoint(grid_view_pt);
340 views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt);
344 AppsGridView* grid_view_;
345 AppListItemView* drag_view_;
346 gfx::Point drag_view_offset_;
347 bool has_shortcut_path_;
348 base::FilePath shortcut_path_;
352 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag);
354 #endif // defined(OS_WIN)
356 AppsGridView::AppsGridView(AppsGridViewDelegate* delegate)
360 folder_delegate_(NULL),
361 page_switcher_view_(NULL),
364 selected_view_(NULL),
366 drag_start_page_(-1),
368 use_synchronous_drag_(true),
371 drop_attempt_(DROP_FOR_NONE),
372 drag_and_drop_host_(NULL),
373 forward_events_to_drag_and_drop_host_(false),
374 page_flip_target_(-1),
375 page_flip_delay_in_ms_(kPageFlipDelayInMs),
376 bounds_animator_(this),
377 activated_folder_item_view_(NULL),
378 dragging_for_reparent_item_(false) {
379 SetPaintToLayer(true);
380 // Clip any icons that are outside the grid view's bounds. These icons would
381 // otherwise be visible to the user when the grid view is off screen.
382 layer()->SetMasksToBounds(true);
383 SetFillsBoundsOpaquely(false);
385 pagination_model_.SetTransitionDurations(kPageTransitionDurationInMs,
386 kOverscrollPageTransitionDurationMs);
388 pagination_model_.AddObserver(this);
389 // The experimental app list transitions vertically.
390 PaginationController::ScrollAxis scroll_axis =
391 app_list::switches::IsExperimentalAppListEnabled()
392 ? PaginationController::SCROLL_AXIS_VERTICAL
393 : PaginationController::SCROLL_AXIS_HORIZONTAL;
394 pagination_controller_.reset(
395 new PaginationController(&pagination_model_, scroll_axis));
396 if (!switches::IsExperimentalAppListEnabled()) {
397 page_switcher_view_ = new PageSwitcher(&pagination_model_);
398 AddChildView(page_switcher_view_);
402 AppsGridView::~AppsGridView() {
403 // Coming here |drag_view_| should already be canceled since otherwise the
404 // drag would disappear after the app list got animated away and closed,
405 // which would look odd.
411 model_->RemoveObserver(this);
412 pagination_model_.RemoveObserver(this);
415 item_list_->RemoveObserver(this);
417 // Make sure |page_switcher_view_| is deleted before |pagination_model_|.
419 RemoveAllChildViews(true);
422 void AppsGridView::SetLayout(int cols, int rows_per_page) {
424 rows_per_page_ = rows_per_page;
426 if (switches::IsExperimentalAppListEnabled()) {
427 SetBorder(views::Border::CreateEmptyBorder(
428 0, kExperimentalWindowPadding, 0, kExperimentalWindowPadding));
430 SetBorder(views::Border::CreateEmptyBorder(
431 0, kLeftRightPadding, kBottomPadding, kLeftRightPadding));
435 void AppsGridView::ResetForShowApps() {
436 activated_folder_item_view_ = NULL;
438 layer()->SetOpacity(1.0f);
440 // Set all views to visible in case they weren't made visible again by an
441 // incomplete animation.
442 for (int i = 0; i < view_model_.view_size(); ++i) {
443 view_model_.view_at(i)->SetVisible(true);
445 CHECK_EQ(item_list_->item_count(),
446 static_cast<size_t>(view_model_.view_size()));
449 void AppsGridView::SetModel(AppListModel* model) {
451 model_->RemoveObserver(this);
455 model_->AddObserver(this);
460 void AppsGridView::SetItemList(AppListItemList* item_list) {
462 item_list_->RemoveObserver(this);
463 item_list_ = item_list;
465 item_list_->AddObserver(this);
469 void AppsGridView::SetSelectedView(AppListItemView* view) {
470 if (IsSelectedView(view) || IsDraggedView(view))
473 Index index = GetIndexOfView(view);
474 if (IsValidIndex(index))
475 SetSelectedItemByIndex(index);
478 void AppsGridView::ClearSelectedView(AppListItemView* view) {
479 if (view && IsSelectedView(view)) {
480 selected_view_->SchedulePaint();
481 selected_view_ = NULL;
485 void AppsGridView::ClearAnySelectedView() {
486 if (selected_view_) {
487 selected_view_->SchedulePaint();
488 selected_view_ = NULL;
492 bool AppsGridView::IsSelectedView(const AppListItemView* view) const {
493 return selected_view_ == view;
496 void AppsGridView::InitiateDrag(AppListItemView* view,
498 const ui::LocatedEvent& event) {
500 if (drag_view_ || pulsing_blocks_model_.view_size())
504 drag_view_init_index_ = GetIndexOfView(drag_view_);
505 drag_view_offset_ = event.location();
506 drag_start_page_ = pagination_model_.selected_page();
507 reorder_placeholder_ = drag_view_init_index_;
508 ExtractDragLocation(event, &drag_start_grid_view_);
509 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
512 void AppsGridView::StartSettingUpSynchronousDrag() {
514 if (!delegate_ || !use_synchronous_drag_)
517 // Folders and downloading items can't be integrated with the OS.
518 if (IsFolderItem(drag_view_->item()) || drag_view_->item()->is_installing())
521 // Favor the drag and drop host over native win32 drag. For the Win8/ash
522 // launcher we want to have ashes drag and drop over win32's.
523 if (drag_and_drop_host_)
526 // Never create a second synchronous drag if the drag started in a folder.
527 if (IsDraggingForReparentInRootLevelGridView())
530 synchronous_drag_ = new SynchronousDrag(this, drag_view_, drag_view_offset_);
531 delegate_->GetShortcutPathForApp(drag_view_->item()->id(),
532 base::Bind(&AppsGridView::OnGotShortcutPath,
533 base::Unretained(this),
538 bool AppsGridView::RunSynchronousDrag() {
540 if (!synchronous_drag_)
543 if (synchronous_drag_->CanRun()) {
544 if (IsDraggingForReparentInHiddenGridView())
545 folder_delegate_->SetRootLevelDragViewVisible(false);
546 synchronous_drag_->Run();
547 synchronous_drag_ = NULL;
549 } else if (!synchronous_drag_->running()) {
550 // The OS drag is not ready yet. If the root grid has a drag view because
551 // a reparent has started, ensure it is visible.
552 if (IsDraggingForReparentInHiddenGridView())
553 folder_delegate_->SetRootLevelDragViewVisible(true);
559 void AppsGridView::CleanUpSynchronousDrag() {
561 if (synchronous_drag_)
562 synchronous_drag_->EndDragExternally();
564 synchronous_drag_ = NULL;
569 void AppsGridView::OnGotShortcutPath(
570 scoped_refptr<SynchronousDrag> synchronous_drag,
571 const base::FilePath& path) {
572 // Drag may have ended before we get the shortcut path or a new drag may have
574 if (synchronous_drag_ != synchronous_drag)
576 // Setting the shortcut path here means the next time we hit UpdateDrag()
577 // we'll enter the synchronous drag.
578 // NOTE we don't Run() the drag here because that causes animations not to
579 // update for some reason.
580 synchronous_drag_->set_shortcut_path(path);
581 DCHECK(synchronous_drag_->CanRun());
585 bool AppsGridView::UpdateDragFromItem(Pointer pointer,
586 const ui::LocatedEvent& event) {
588 return false; // Drag canceled.
590 gfx::Point drag_point_in_grid_view;
591 ExtractDragLocation(event, &drag_point_in_grid_view);
592 UpdateDrag(pointer, drag_point_in_grid_view);
596 // If a drag and drop host is provided, see if the drag operation needs to be
598 gfx::Point location_in_screen = drag_point_in_grid_view;
599 views::View::ConvertPointToScreen(this, &location_in_screen);
600 DispatchDragEventToDragAndDropHost(location_in_screen);
601 if (drag_and_drop_host_)
602 drag_and_drop_host_->UpdateDragIconProxy(location_in_screen);
606 void AppsGridView::UpdateDrag(Pointer pointer, const gfx::Point& point) {
607 if (folder_delegate_)
608 UpdateDragStateInsideFolder(pointer, point);
611 return; // Drag canceled.
613 if (RunSynchronousDrag())
616 gfx::Vector2d drag_vector(point - drag_start_grid_view_);
617 if (!dragging() && ExceededDragThreshold(drag_vector)) {
618 drag_pointer_ = pointer;
619 // Move the view to the front so that it appears on top of other views.
620 ReorderChildView(drag_view_, -1);
621 bounds_animator_.StopAnimatingView(drag_view_);
622 // Stopping the animation may have invalidated our drag view due to the
623 // view hierarchy changing.
627 StartSettingUpSynchronousDrag();
628 if (!dragging_for_reparent_item_)
629 StartDragAndDropHostDrag(point);
632 if (drag_pointer_ != pointer)
635 drag_view_->SetPosition(drag_view_start_ + drag_vector);
637 last_drag_point_ = point;
638 const Index last_reorder_drop_target = reorder_drop_target_;
639 const Index last_folder_drop_target = folder_drop_target_;
640 DropAttempt last_drop_attempt = drop_attempt_;
641 CalculateDropTarget();
643 MaybeStartPageFlipTimer(last_drag_point_);
645 if (page_switcher_view_) {
646 gfx::Point page_switcher_point(last_drag_point_);
647 views::View::ConvertPointToTarget(
648 this, page_switcher_view_, &page_switcher_point);
649 page_switcher_view_->UpdateUIForDragPoint(page_switcher_point);
652 if (last_folder_drop_target != folder_drop_target_ ||
653 last_reorder_drop_target != reorder_drop_target_ ||
654 last_drop_attempt != drop_attempt_) {
655 if (drop_attempt_ == DROP_FOR_REORDER) {
656 folder_dropping_timer_.Stop();
657 reorder_timer_.Start(FROM_HERE,
658 base::TimeDelta::FromMilliseconds(kReorderDelay),
659 this, &AppsGridView::OnReorderTimer);
660 } else if (drop_attempt_ == DROP_FOR_FOLDER) {
661 reorder_timer_.Stop();
662 folder_dropping_timer_.Start(FROM_HERE,
663 base::TimeDelta::FromMilliseconds(kFolderDroppingDelay),
664 this, &AppsGridView::OnFolderDroppingTimer);
667 // Reset the previous drop target.
668 SetAsFolderDroppingTarget(last_folder_drop_target, false);
672 void AppsGridView::EndDrag(bool cancel) {
673 // EndDrag was called before if |drag_view_| is NULL.
677 // Coming here a drag and drop was in progress.
678 bool landed_in_drag_and_drop_host = forward_events_to_drag_and_drop_host_;
679 if (forward_events_to_drag_and_drop_host_) {
680 DCHECK(!IsDraggingForReparentInRootLevelGridView());
681 forward_events_to_drag_and_drop_host_ = false;
682 drag_and_drop_host_->EndDrag(cancel);
683 if (IsDraggingForReparentInHiddenGridView()) {
684 folder_delegate_->DispatchEndDragEventForReparent(
685 true /* events_forwarded_to_drag_drop_host */,
686 cancel /* cancel_drag */);
689 if (IsDraggingForReparentInHiddenGridView()) {
690 // Forward the EndDrag event to the root level grid view.
691 folder_delegate_->DispatchEndDragEventForReparent(
692 false /* events_forwarded_to_drag_drop_host */,
693 cancel /* cancel_drag */);
694 EndDragForReparentInHiddenFolderGridView();
698 if (IsDraggingForReparentInRootLevelGridView()) {
699 // An EndDrag can be received during a reparent via a model change. This
700 // is always a cancel and needs to be forwarded to the folder.
702 delegate_->CancelDragInActiveFolder();
706 if (!cancel && dragging()) {
707 // Regular drag ending path, ie, not for reparenting.
708 CalculateDropTarget();
709 if (EnableFolderDragDropUI() && drop_attempt_ == DROP_FOR_FOLDER &&
710 IsValidIndex(folder_drop_target_)) {
711 MoveItemToFolder(drag_view_, folder_drop_target_);
712 } else if (IsValidIndex(reorder_drop_target_)) {
713 MoveItemInModel(drag_view_, reorder_drop_target_);
718 if (drag_and_drop_host_) {
719 // If we had a drag and drop proxy icon, we delete it and make the real
720 // item visible again.
721 drag_and_drop_host_->DestroyDragIconProxy();
722 if (landed_in_drag_and_drop_host) {
723 // Move the item directly to the target location, avoiding the "zip back"
724 // animation if the user was pinning it to the shelf.
725 int i = reorder_drop_target_.slot;
726 gfx::Rect bounds = view_model_.ideal_bounds(i);
727 drag_view_->SetBoundsRect(bounds);
729 // Fade in slowly if it landed in the shelf.
730 SetViewHidden(drag_view_,
732 !landed_in_drag_and_drop_host /* animate */);
735 // The drag can be ended after the synchronous drag is created but before it
737 CleanUpSynchronousDrag();
739 SetAsFolderDroppingTarget(folder_drop_target_, false);
741 AnimateToIdealBounds();
745 // If user releases mouse inside a folder's grid view, burst the folder
746 // container ink bubble.
747 if (folder_delegate_ && !IsDraggingForReparentInHiddenGridView())
748 folder_delegate_->UpdateFolderViewBackground(false);
751 void AppsGridView::StopPageFlipTimer() {
752 page_flip_timer_.Stop();
753 page_flip_target_ = -1;
756 AppListItemView* AppsGridView::GetItemViewAt(int index) const {
757 return view_model_.view_at(index);
760 void AppsGridView::SetTopItemViewsVisible(bool visible) {
761 int top_item_count = std::min(static_cast<int>(kNumFolderTopItems),
762 view_model_.view_size());
763 for (int i = 0; i < top_item_count; ++i)
764 GetItemViewAt(i)->icon()->SetVisible(visible);
767 void AppsGridView::ScheduleShowHideAnimation(bool show) {
768 // Stop any previous animation.
769 layer()->GetAnimator()->StopAnimating();
771 // Set initial state.
773 layer()->SetOpacity(show ? 0.0f : 1.0f);
775 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
776 animation.AddObserver(this);
777 animation.SetTweenType(
778 show ? kFolderFadeInTweenType : kFolderFadeOutTweenType);
779 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
780 show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
782 layer()->SetOpacity(show ? 1.0f : 0.0f);
785 void AppsGridView::InitiateDragFromReparentItemInRootLevelGridView(
786 AppListItemView* original_drag_view,
787 const gfx::Rect& drag_view_rect,
788 const gfx::Point& drag_point,
789 bool has_native_drag) {
790 DCHECK(original_drag_view && !drag_view_);
791 DCHECK(!dragging_for_reparent_item_);
793 // Since the item is new, its placeholder is conceptually at the back of the
795 reorder_placeholder_ = GetLastViewIndex();
797 // Create a new AppListItemView to duplicate the original_drag_view in the
798 // folder's grid view.
799 AppListItemView* view = new AppListItemView(this, original_drag_view->item());
802 drag_view_->SetPaintToLayer(true);
803 // Note: For testing purpose, SetFillsBoundsOpaquely can be set to true to
804 // show the gray background.
805 drag_view_->SetFillsBoundsOpaquely(false);
806 drag_view_->SetBoundsRect(drag_view_rect);
807 drag_view_->SetDragUIState(); // Hide the title of the drag_view_.
809 // Hide the drag_view_ for drag icon proxy when a native drag is responsible
810 // for showing the icon.
812 SetViewHidden(drag_view_, true /* hide */, true /* no animate */);
814 // Add drag_view_ to the end of the view_model_.
815 view_model_.Add(drag_view_, view_model_.view_size());
817 drag_start_page_ = pagination_model_.selected_page();
818 drag_start_grid_view_ = drag_point;
820 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
822 // Set the flag in root level grid view.
823 dragging_for_reparent_item_ = true;
826 void AppsGridView::UpdateDragFromReparentItem(Pointer pointer,
827 const gfx::Point& drag_point) {
828 // Note that if a cancel ocurrs while reparenting, the |drag_view_| in both
829 // root and folder grid views is cleared, so the check in UpdateDragFromItem()
830 // for |drag_view_| being NULL (in the folder grid) is sufficient.
832 DCHECK(IsDraggingForReparentInRootLevelGridView());
834 UpdateDrag(pointer, drag_point);
837 bool AppsGridView::IsDraggedView(const AppListItemView* view) const {
838 return drag_view_ == view;
841 void AppsGridView::ClearDragState() {
842 drop_attempt_ = DROP_FOR_NONE;
843 drag_pointer_ = NONE;
844 reorder_drop_target_ = Index();
845 folder_drop_target_ = Index();
846 reorder_placeholder_ = Index();
847 drag_start_grid_view_ = gfx::Point();
848 drag_start_page_ = -1;
849 drag_view_offset_ = gfx::Point();
852 drag_view_->OnDragEnded();
853 if (IsDraggingForReparentInRootLevelGridView()) {
854 const int drag_view_index = view_model_.GetIndexOfView(drag_view_);
855 CHECK_EQ(view_model_.view_size() - 1, drag_view_index);
856 DeleteItemViewAtIndex(drag_view_index);
860 dragging_for_reparent_item_ = false;
863 void AppsGridView::SetDragViewVisible(bool visible) {
865 SetViewHidden(drag_view_, !visible, true);
868 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
869 ApplicationDragAndDropHost* drag_and_drop_host) {
870 drag_and_drop_host_ = drag_and_drop_host;
873 void AppsGridView::Prerender() {
875 int selected_page = std::max(0, pagination_model_.selected_page());
876 int start = std::max(0, (selected_page - kPrerenderPages) * tiles_per_page());
877 int end = std::min(view_model_.view_size(),
878 (selected_page + 1 + kPrerenderPages) * tiles_per_page());
879 for (int i = start; i < end; i++)
880 GetItemViewAt(i)->Prerender();
883 bool AppsGridView::IsAnimatingView(AppListItemView* view) {
884 return bounds_animator_.IsAnimating(view);
887 gfx::Size AppsGridView::GetPreferredSize() const {
888 const gfx::Insets insets(GetInsets());
889 int page_switcher_height = 0;
890 if (page_switcher_view_)
891 page_switcher_height = page_switcher_view_->GetPreferredSize().height();
892 gfx::Size size = GetTileGridSize();
893 size.Enlarge(insets.width(), insets.height() + page_switcher_height);
897 bool AppsGridView::GetDropFormats(
899 std::set<OSExchangeData::CustomFormat>* custom_formats) {
900 // TODO(koz): Only accept a specific drag type for app shortcuts.
901 *formats = OSExchangeData::FILE_NAME;
905 bool AppsGridView::CanDrop(const OSExchangeData& data) {
909 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent& event) {
910 return ui::DragDropTypes::DRAG_MOVE;
913 void AppsGridView::Layout() {
914 if (bounds_animator_.IsAnimating())
915 bounds_animator_.Cancel();
917 CalculateIdealBounds();
918 for (int i = 0; i < view_model_.view_size(); ++i) {
919 AppListItemView* view = GetItemViewAt(i);
920 if (view != drag_view_)
921 view->SetBoundsRect(view_model_.ideal_bounds(i));
923 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_);
925 if (page_switcher_view_) {
926 const int page_switcher_height =
927 page_switcher_view_->GetPreferredSize().height();
928 gfx::Rect rect(GetContentsBounds());
929 rect.set_y(rect.bottom() - page_switcher_height);
930 rect.set_height(page_switcher_height);
931 page_switcher_view_->SetBoundsRect(rect);
935 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) {
936 bool handled = false;
938 handled = static_cast<views::View*>(selected_view_)->OnKeyPressed(event);
941 const int forward_dir = base::i18n::IsRTL() ? -1 : 1;
942 switch (event.key_code()) {
944 MoveSelected(0, -forward_dir, 0);
947 MoveSelected(0, forward_dir, 0);
950 MoveSelected(0, 0, -1);
953 MoveSelected(0, 0, 1);
955 case ui::VKEY_PRIOR: {
956 MoveSelected(-1, 0, 0);
959 case ui::VKEY_NEXT: {
960 MoveSelected(1, 0, 0);
971 bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) {
972 bool handled = false;
974 handled = selected_view_->OnKeyReleased(event);
979 bool AppsGridView::OnMouseWheel(const ui::MouseWheelEvent& event) {
980 return pagination_controller_->OnScroll(
981 gfx::Vector2d(event.x_offset(), event.y_offset()));
984 void AppsGridView::ViewHierarchyChanged(
985 const ViewHierarchyChangedDetails& details) {
986 if (!details.is_add && details.parent == this) {
987 // The view being delete should not have reference in |view_model_|.
988 CHECK_EQ(-1, view_model_.GetIndexOfView(details.child));
990 if (selected_view_ == details.child)
991 selected_view_ = NULL;
992 if (activated_folder_item_view_ == details.child)
993 activated_folder_item_view_ = NULL;
995 if (drag_view_ == details.child)
998 bounds_animator_.StopAnimatingView(details.child);
1002 void AppsGridView::OnGestureEvent(ui::GestureEvent* event) {
1003 if (pagination_controller_->OnGestureEvent(*event, GetContentsBounds()))
1004 event->SetHandled();
1007 void AppsGridView::OnScrollEvent(ui::ScrollEvent* event) {
1008 if (event->type() == ui::ET_SCROLL_FLING_CANCEL)
1011 gfx::Vector2dF offset(event->x_offset(), event->y_offset());
1012 if (pagination_controller_->OnScroll(gfx::ToFlooredVector2d(offset))) {
1013 event->SetHandled();
1014 event->StopPropagation();
1018 void AppsGridView::Update() {
1019 DCHECK(!selected_view_ && !drag_view_);
1020 view_model_.Clear();
1021 if (!item_list_ || !item_list_->item_count())
1023 for (size_t i = 0; i < item_list_->item_count(); ++i) {
1024 AppListItemView* view = CreateViewForItemAtIndex(i);
1025 view_model_.Add(view, i);
1029 UpdatePulsingBlockViews();
1034 void AppsGridView::UpdatePaging() {
1035 int total_page = view_model_.view_size() && tiles_per_page()
1036 ? (view_model_.view_size() - 1) / tiles_per_page() + 1
1039 pagination_model_.SetTotalPages(total_page);
1042 void AppsGridView::UpdatePulsingBlockViews() {
1043 const int existing_items = item_list_ ? item_list_->item_count() : 0;
1044 const int available_slots =
1045 tiles_per_page() - existing_items % tiles_per_page();
1046 const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
1047 available_slots : 0;
1049 if (pulsing_blocks_model_.view_size() == desired)
1052 while (pulsing_blocks_model_.view_size() > desired) {
1053 PulsingBlockView* view = pulsing_blocks_model_.view_at(0);
1054 pulsing_blocks_model_.Remove(0);
1058 while (pulsing_blocks_model_.view_size() < desired) {
1059 PulsingBlockView* view = new PulsingBlockView(GetTotalTileSize(), true);
1060 pulsing_blocks_model_.Add(view, 0);
1065 AppListItemView* AppsGridView::CreateViewForItemAtIndex(size_t index) {
1066 // The drag_view_ might be pending for deletion, therefore view_model_
1067 // may have one more item than item_list_.
1068 DCHECK_LE(index, item_list_->item_count());
1069 AppListItemView* view = new AppListItemView(this,
1070 item_list_->item_at(index));
1071 view->SetPaintToLayer(true);
1072 view->SetFillsBoundsOpaquely(false);
1076 AppsGridView::Index AppsGridView::GetIndexFromModelIndex(
1077 int model_index) const {
1078 return Index(model_index / tiles_per_page(), model_index % tiles_per_page());
1081 int AppsGridView::GetModelIndexFromIndex(const Index& index) const {
1082 return index.page * tiles_per_page() + index.slot;
1085 void AppsGridView::EnsureViewVisible(const Index& index) {
1086 if (pagination_model_.has_transition())
1089 if (IsValidIndex(index))
1090 pagination_model_.SelectPage(index.page, false);
1093 void AppsGridView::SetSelectedItemByIndex(const Index& index) {
1094 if (GetIndexOfView(selected_view_) == index)
1097 AppListItemView* new_selection = GetViewAtIndex(index);
1099 return; // Keep current selection.
1102 selected_view_->SchedulePaint();
1104 EnsureViewVisible(index);
1105 selected_view_ = new_selection;
1106 selected_view_->SchedulePaint();
1107 selected_view_->NotifyAccessibilityEvent(
1108 ui::AX_EVENT_FOCUS, true);
1111 bool AppsGridView::IsValidIndex(const Index& index) const {
1112 return index.page >= 0 && index.page < pagination_model_.total_pages() &&
1113 index.slot >= 0 && index.slot < tiles_per_page() &&
1114 GetModelIndexFromIndex(index) < view_model_.view_size();
1117 AppsGridView::Index AppsGridView::GetIndexOfView(
1118 const AppListItemView* view) const {
1119 const int model_index = view_model_.GetIndexOfView(view);
1120 if (model_index == -1)
1123 return GetIndexFromModelIndex(model_index);
1126 AppListItemView* AppsGridView::GetViewAtIndex(const Index& index) const {
1127 if (!IsValidIndex(index))
1130 const int model_index = GetModelIndexFromIndex(index);
1131 return GetItemViewAt(model_index);
1134 AppsGridView::Index AppsGridView::GetLastViewIndex() const {
1135 DCHECK_LT(0, view_model_.view_size());
1136 int view_index = view_model_.view_size() - 1;
1137 return Index(view_index / tiles_per_page(), view_index % tiles_per_page());
1140 void AppsGridView::MoveSelected(int page_delta,
1143 if (!selected_view_)
1144 return SetSelectedItemByIndex(Index(pagination_model_.selected_page(), 0));
1146 const Index& selected = GetIndexOfView(selected_view_);
1147 int target_slot = selected.slot + slot_x_delta + slot_y_delta * cols_;
1149 if (selected.slot % cols_ == 0 && slot_x_delta == -1) {
1150 if (selected.page > 0) {
1152 target_slot = selected.slot + cols_ - 1;
1154 target_slot = selected.slot;
1158 if (selected.slot % cols_ == cols_ - 1 && slot_x_delta == 1) {
1159 if (selected.page < pagination_model_.total_pages() - 1) {
1161 target_slot = selected.slot - cols_ + 1;
1163 target_slot = selected.slot;
1167 // Clamp the target slot to the last item if we are moving to the last page
1168 // but our target slot is past the end of the item list.
1170 selected.page + page_delta == pagination_model_.total_pages() - 1) {
1171 int last_item_slot = (view_model_.view_size() - 1) % tiles_per_page();
1172 if (last_item_slot < target_slot) {
1173 target_slot = last_item_slot;
1177 int target_page = std::min(pagination_model_.total_pages() - 1,
1178 std::max(selected.page + page_delta, 0));
1179 SetSelectedItemByIndex(Index(target_page, target_slot));
1182 void AppsGridView::CalculateIdealBounds() {
1183 // TODO(calamity): This fixes http://crbug.com/422604 on ChromeOS but it's
1184 // unclear why. This should be investigated to fix the issue on Linux Ash.
1185 if (GetContentsBounds().IsEmpty())
1188 gfx::Size grid_size = GetTileGridSize();
1190 // Page size including padding pixels. A tile.x + page_width means the same
1191 // tile slot in the next page; similarly for tile.y + page_height.
1192 const int page_width = grid_size.width() + kPagePadding;
1193 const int page_height = grid_size.height() + kPagePadding;
1195 // If there is a transition, calculates offset for current and target page.
1196 const int current_page = pagination_model_.selected_page();
1197 const PaginationModel::Transition& transition =
1198 pagination_model_.transition();
1199 const bool is_valid = pagination_model_.is_valid_page(transition.target_page);
1201 // Transition to previous page means negative offset.
1202 const int dir = transition.target_page > current_page ? -1 : 1;
1204 const int total_views =
1205 view_model_.view_size() + pulsing_blocks_model_.view_size();
1207 for (int i = 0; i < total_views; ++i) {
1208 if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_)
1211 Index view_index = GetIndexFromModelIndex(slot_index);
1213 // Leaves a blank space in the grid for the current reorder placeholder.
1214 if (reorder_placeholder_ == view_index) {
1216 view_index = GetIndexFromModelIndex(slot_index);
1219 // Decide the x or y offset for current item.
1223 if (pagination_controller_->scroll_axis() ==
1224 PaginationController::SCROLL_AXIS_HORIZONTAL) {
1225 if (view_index.page < current_page)
1226 x_offset = -page_width;
1227 else if (view_index.page > current_page)
1228 x_offset = page_width;
1231 if (view_index.page == current_page ||
1232 view_index.page == transition.target_page) {
1233 x_offset += transition.progress * page_width * dir;
1237 if (view_index.page < current_page)
1238 y_offset = -page_height;
1239 else if (view_index.page > current_page)
1240 y_offset = page_height;
1243 if (view_index.page == current_page ||
1244 view_index.page == transition.target_page) {
1245 y_offset += transition.progress * page_height * dir;
1250 const int row = view_index.slot / cols_;
1251 const int col = view_index.slot % cols_;
1252 gfx::Rect tile_slot = GetExpectedTileBounds(row, col);
1253 tile_slot.Offset(x_offset, y_offset);
1254 if (i < view_model_.view_size()) {
1255 view_model_.set_ideal_bounds(i, tile_slot);
1257 pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
1265 void AppsGridView::AnimateToIdealBounds() {
1266 const gfx::Rect visible_bounds(GetVisibleBounds());
1268 CalculateIdealBounds();
1269 for (int i = 0; i < view_model_.view_size(); ++i) {
1270 AppListItemView* view = GetItemViewAt(i);
1271 if (view == drag_view_)
1274 const gfx::Rect& target = view_model_.ideal_bounds(i);
1275 if (bounds_animator_.GetTargetBounds(view) == target)
1278 const gfx::Rect& current = view->bounds();
1279 const bool current_visible = visible_bounds.Intersects(current);
1280 const bool target_visible = visible_bounds.Intersects(target);
1281 const bool visible = current_visible || target_visible;
1283 const int y_diff = target.y() - current.y();
1284 if (visible && y_diff && y_diff % GetTotalTileSize().height() == 0) {
1285 AnimationBetweenRows(view,
1290 } else if (visible || bounds_animator_.IsAnimating(view)) {
1291 bounds_animator_.AnimateViewTo(view, target);
1292 bounds_animator_.SetAnimationDelegate(
1294 scoped_ptr<gfx::AnimationDelegate>(
1295 new ItemMoveAnimationDelegate(view)));
1297 view->SetBoundsRect(target);
1302 void AppsGridView::AnimationBetweenRows(AppListItemView* view,
1303 bool animate_current,
1304 const gfx::Rect& current,
1305 bool animate_target,
1306 const gfx::Rect& target) {
1307 // Determine page of |current| and |target|. -1 means in the left invisible
1308 // page, 0 is the center visible page and 1 means in the right invisible page.
1309 const int current_page = current.x() < 0 ? -1 :
1310 current.x() >= width() ? 1 : 0;
1311 const int target_page = target.x() < 0 ? -1 :
1312 target.x() >= width() ? 1 : 0;
1314 const int dir = current_page < target_page ||
1315 (current_page == target_page && current.y() < target.y()) ? 1 : -1;
1317 scoped_ptr<ui::Layer> layer;
1318 if (animate_current) {
1319 layer = view->RecreateLayer();
1320 layer->SuppressPaint();
1322 view->SetFillsBoundsOpaquely(false);
1323 view->layer()->SetOpacity(0.f);
1326 gfx::Size total_tile_size = GetTotalTileSize();
1327 gfx::Rect current_out(current);
1328 current_out.Offset(dir * total_tile_size.width(), 0);
1330 gfx::Rect target_in(target);
1332 target_in.Offset(-dir * total_tile_size.width(), 0);
1333 view->SetBoundsRect(target_in);
1334 bounds_animator_.AnimateViewTo(view, target);
1336 bounds_animator_.SetAnimationDelegate(
1338 scoped_ptr<gfx::AnimationDelegate>(
1339 new RowMoveAnimationDelegate(view, layer.release(), current_out)));
1342 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event,
1343 gfx::Point* drag_point) {
1344 #if defined(USE_AURA) && !defined(OS_WIN)
1345 // Use root location of |event| instead of location in |drag_view_|'s
1346 // coordinates because |drag_view_| has a scale transform and location
1347 // could have integer round error and causes jitter.
1348 *drag_point = event.root_location();
1350 // GetWidget() could be NULL for tests.
1352 aura::Window::ConvertPointToTarget(
1353 GetWidget()->GetNativeWindow()->GetRootWindow(),
1354 GetWidget()->GetNativeWindow(),
1358 views::View::ConvertPointFromWidget(this, drag_point);
1360 // For non-aura, root location is not clearly defined but |drag_view_| does
1361 // not have the scale transform. So no round error would be introduced and
1362 // it's okay to use View::ConvertPointToTarget.
1363 *drag_point = event.location();
1364 views::View::ConvertPointToTarget(drag_view_, this, drag_point);
1368 void AppsGridView::CalculateDropTarget() {
1371 gfx::Point point = drag_view_->icon()->bounds().CenterPoint();
1372 views::View::ConvertPointToTarget(drag_view_, this, &point);
1373 if (!IsPointWithinDragBuffer(point)) {
1374 // Reset the reorder target to the original position if the cursor is
1375 // outside the drag buffer.
1376 if (IsDraggingForReparentInRootLevelGridView()) {
1377 drop_attempt_ = DROP_FOR_NONE;
1381 reorder_drop_target_ = drag_view_init_index_;
1382 drop_attempt_ = DROP_FOR_REORDER;
1386 if (EnableFolderDragDropUI() &&
1387 CalculateFolderDropTarget(point, &folder_drop_target_)) {
1388 drop_attempt_ = DROP_FOR_FOLDER;
1392 drop_attempt_ = DROP_FOR_REORDER;
1393 CalculateReorderDropTarget(point, &reorder_drop_target_);
1396 bool AppsGridView::CalculateFolderDropTarget(const gfx::Point& point,
1397 Index* drop_target) const {
1398 // Folders can't be dropped into other folders.
1399 if (IsFolderItem(drag_view_->item()))
1402 // A folder drop shouldn't happen on the reorder placeholder since that would
1403 // be merging an item with itself.
1404 Index nearest_tile_index(GetNearestTileIndexForPoint(point));
1405 if (!IsValidIndex(nearest_tile_index) ||
1406 nearest_tile_index == reorder_placeholder_) {
1410 int distance_to_tile_center =
1411 (point - GetExpectedTileBounds(nearest_tile_index.slot).CenterPoint())
1413 if (distance_to_tile_center > kFolderDroppingCircleRadius)
1416 AppListItemView* target_view =
1417 GetViewDisplayedAtSlotOnCurrentPage(nearest_tile_index.slot);
1421 AppListItem* target_item = target_view->item();
1423 // Items can only be dropped into non-folders (which have no children) or
1424 // folders that have fewer than the max allowed items.
1425 // The OEM folder does not allow drag/drop of other items into it.
1426 if (target_item->ChildItemCount() >= kMaxFolderItems ||
1427 IsOEMFolderItem(target_item)) {
1431 *drop_target = nearest_tile_index;
1432 DCHECK(IsValidIndex(*drop_target));
1436 void AppsGridView::CalculateReorderDropTarget(const gfx::Point& point,
1437 Index* drop_target) const {
1438 gfx::Rect bounds = GetContentsBounds();
1439 Index grid_index = GetNearestTileIndexForPoint(point);
1440 gfx::Point reorder_placeholder_center =
1441 GetExpectedTileBounds(reorder_placeholder_.slot).CenterPoint();
1443 int x_offset_direction = 0;
1444 if (grid_index == reorder_placeholder_) {
1445 x_offset_direction = reorder_placeholder_center.x() < point.x() ? -1 : 1;
1447 x_offset_direction = reorder_placeholder_ < grid_index ? -1 : 1;
1450 gfx::Size total_tile_size = GetTotalTileSize();
1451 int row = grid_index.slot / cols_;
1453 // Offset the target column based on the direction of the target. This will
1454 // result in earlier targets getting their reorder zone shifted backwards
1455 // and later targets getting their reorder zones shifted forwards.
1457 // This makes eordering feel like the user is slotting items into the spaces
1459 int x_offset = x_offset_direction *
1460 (total_tile_size.width() - kFolderDroppingCircleRadius) / 2;
1461 int col = (point.x() - bounds.x() + x_offset) / total_tile_size.width();
1462 col = ClampToRange(col, 0, cols_ - 1);
1464 std::min(Index(pagination_model_.selected_page(), row * cols_ + col),
1465 GetLastViewIndex());
1466 DCHECK(IsValidIndex(*drop_target));
1469 void AppsGridView::OnReorderTimer() {
1470 if (drop_attempt_ == DROP_FOR_REORDER) {
1471 reorder_placeholder_ = reorder_drop_target_;
1472 AnimateToIdealBounds();
1476 void AppsGridView::OnFolderItemReparentTimer() {
1477 DCHECK(folder_delegate_);
1478 if (drag_out_of_folder_container_ && drag_view_) {
1479 bool has_native_drag = drag_and_drop_host_ != nullptr;
1481 has_native_drag = has_native_drag || synchronous_drag_;
1483 folder_delegate_->ReparentItem(
1484 drag_view_, last_drag_point_, has_native_drag);
1486 // Set the flag in the folder's grid view.
1487 dragging_for_reparent_item_ = true;
1489 // Do not observe any data change since it is going to be hidden.
1490 item_list_->RemoveObserver(this);
1495 void AppsGridView::OnFolderDroppingTimer() {
1496 if (drop_attempt_ == DROP_FOR_FOLDER)
1497 SetAsFolderDroppingTarget(folder_drop_target_, true);
1500 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer,
1501 const gfx::Point& drag_point) {
1502 if (IsUnderOEMFolder())
1505 if (IsDraggingForReparentInHiddenGridView()) {
1506 // Dispatch drag event to root level grid view for re-parenting folder
1507 // folder item purpose.
1508 DispatchDragEventForReparent(pointer, drag_point);
1512 // Regular drag and drop in a folder's grid view.
1513 folder_delegate_->UpdateFolderViewBackground(true);
1515 // Calculate if the drag_view_ is dragged out of the folder's container
1517 gfx::Rect bounds_to_folder_view = ConvertRectToParent(drag_view_->bounds());
1518 gfx::Point pt = bounds_to_folder_view.CenterPoint();
1519 bool is_item_dragged_out_of_folder =
1520 folder_delegate_->IsPointOutsideOfFolderBoundary(pt);
1521 if (is_item_dragged_out_of_folder) {
1522 if (!drag_out_of_folder_container_) {
1523 folder_item_reparent_timer_.Start(
1525 base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay),
1527 &AppsGridView::OnFolderItemReparentTimer);
1528 drag_out_of_folder_container_ = true;
1531 folder_item_reparent_timer_.Stop();
1532 drag_out_of_folder_container_ = false;
1536 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1537 return (!folder_delegate_ && dragging_for_reparent_item_);
1540 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1541 return (folder_delegate_ && dragging_for_reparent_item_);
1544 gfx::Rect AppsGridView::GetTargetIconRectInFolder(
1545 AppListItemView* drag_item_view,
1546 AppListItemView* folder_item_view) {
1547 gfx::Rect view_ideal_bounds = view_model_.ideal_bounds(
1548 view_model_.GetIndexOfView(folder_item_view));
1549 gfx::Rect icon_ideal_bounds =
1550 folder_item_view->GetIconBoundsForTargetViewBounds(view_ideal_bounds);
1551 AppListFolderItem* folder_item =
1552 static_cast<AppListFolderItem*>(folder_item_view->item());
1553 return folder_item->GetTargetIconRectInFolderForItem(
1554 drag_item_view->item(), icon_ideal_bounds);
1557 bool AppsGridView::IsUnderOEMFolder() {
1558 if (!folder_delegate_)
1561 return folder_delegate_->IsOEMFolder();
1564 void AppsGridView::DispatchDragEventForReparent(Pointer pointer,
1565 const gfx::Point& drag_point) {
1566 folder_delegate_->DispatchDragEventForReparent(pointer, drag_point);
1569 void AppsGridView::EndDragFromReparentItemInRootLevel(
1570 bool events_forwarded_to_drag_drop_host,
1572 // EndDrag was called before if |drag_view_| is NULL.
1576 DCHECK(IsDraggingForReparentInRootLevelGridView());
1577 bool cancel_reparent = cancel_drag || drop_attempt_ == DROP_FOR_NONE;
1578 if (!events_forwarded_to_drag_drop_host && !cancel_reparent) {
1579 CalculateDropTarget();
1580 if (drop_attempt_ == DROP_FOR_REORDER &&
1581 IsValidIndex(reorder_drop_target_)) {
1582 ReparentItemForReorder(drag_view_, reorder_drop_target_);
1583 } else if (drop_attempt_ == DROP_FOR_FOLDER &&
1584 IsValidIndex(folder_drop_target_)) {
1585 ReparentItemToAnotherFolder(drag_view_, folder_drop_target_);
1589 SetViewHidden(drag_view_, false /* show */, true /* no animate */);
1592 // The drag can be ended after the synchronous drag is created but before it
1594 CleanUpSynchronousDrag();
1596 SetAsFolderDroppingTarget(folder_drop_target_, false);
1597 if (cancel_reparent) {
1598 CancelFolderItemReparent(drag_view_);
1600 // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1601 // cleaning up the newly created AppListItemView, effectively claiming
1602 // ownership of the newly created drag view.
1603 drag_view_->OnDragEnded();
1607 AnimateToIdealBounds();
1609 StopPageFlipTimer();
1612 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1613 if (drag_and_drop_host_) {
1614 // If we had a drag and drop proxy icon, we delete it and make the real
1615 // item visible again.
1616 drag_and_drop_host_->DestroyDragIconProxy();
1619 // The drag can be ended after the synchronous drag is created but before it
1621 CleanUpSynchronousDrag();
1623 SetAsFolderDroppingTarget(folder_drop_target_, false);
1627 void AppsGridView::OnFolderItemRemoved() {
1628 DCHECK(folder_delegate_);
1632 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) {
1633 // When a drag and drop host is given, the item can be dragged out of the app
1634 // list window. In that case a proxy widget needs to be used.
1635 // Note: This code has very likely to be changed for Windows (non metro mode)
1636 // when a |drag_and_drop_host_| gets implemented.
1637 if (!drag_view_ || !drag_and_drop_host_)
1640 gfx::Point screen_location = grid_location;
1641 views::View::ConvertPointToScreen(this, &screen_location);
1643 // Determine the mouse offset to the center of the icon so that the drag and
1644 // drop host follows this layer.
1645 gfx::Vector2d delta = drag_view_offset_ -
1646 drag_view_->GetLocalBounds().CenterPoint();
1647 delta.set_y(delta.y() + drag_view_->title()->size().height() / 2);
1649 // We have to hide the original item since the drag and drop host will do
1650 // the OS dependent code to "lift off the dragged item".
1651 DCHECK(!IsDraggingForReparentInRootLevelGridView());
1652 drag_and_drop_host_->CreateDragIconProxy(screen_location,
1653 drag_view_->item()->icon(),
1656 kDragAndDropProxyScale);
1657 SetViewHidden(drag_view_,
1659 true /* no animation */);
1662 void AppsGridView::DispatchDragEventToDragAndDropHost(
1663 const gfx::Point& location_in_screen_coordinates) {
1664 if (!drag_view_ || !drag_and_drop_host_)
1667 if (GetLocalBounds().Contains(last_drag_point_)) {
1668 // The event was issued inside the app menu and we should get all events.
1669 if (forward_events_to_drag_and_drop_host_) {
1670 // The DnD host was previously called and needs to be informed that the
1671 // session returns to the owner.
1672 forward_events_to_drag_and_drop_host_ = false;
1673 drag_and_drop_host_->EndDrag(true);
1676 if (IsFolderItem(drag_view_->item()))
1679 // The event happened outside our app menu and we might need to dispatch.
1680 if (forward_events_to_drag_and_drop_host_) {
1681 // Dispatch since we have already started.
1682 if (!drag_and_drop_host_->Drag(location_in_screen_coordinates)) {
1683 // The host is not active any longer and we cancel the operation.
1684 forward_events_to_drag_and_drop_host_ = false;
1685 drag_and_drop_host_->EndDrag(true);
1688 if (drag_and_drop_host_->StartDrag(drag_view_->item()->id(),
1689 location_in_screen_coordinates)) {
1690 // From now on we forward the drag events.
1691 forward_events_to_drag_and_drop_host_ = true;
1692 // Any flip operations are stopped.
1693 StopPageFlipTimer();
1699 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) {
1700 if (!IsPointWithinDragBuffer(drag_point))
1701 StopPageFlipTimer();
1702 int new_page_flip_target = -1;
1704 // Drag zones are at the edges of the scroll axis.
1705 if (pagination_controller_->scroll_axis() ==
1706 PaginationController::SCROLL_AXIS_VERTICAL) {
1707 if (drag_point.y() < kPageFlipZoneSize)
1708 new_page_flip_target = pagination_model_.selected_page() - 1;
1709 else if (drag_point.y() > height() - kPageFlipZoneSize)
1710 new_page_flip_target = pagination_model_.selected_page() + 1;
1712 if (page_switcher_view_->bounds().Contains(drag_point)) {
1713 gfx::Point page_switcher_point(drag_point);
1714 views::View::ConvertPointToTarget(
1715 this, page_switcher_view_, &page_switcher_point);
1716 new_page_flip_target =
1717 page_switcher_view_->GetPageForPoint(page_switcher_point);
1720 // TODO(xiyuan): Fix this for RTL.
1721 if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize)
1722 new_page_flip_target = pagination_model_.selected_page() - 1;
1724 if (new_page_flip_target == -1 &&
1725 drag_point.x() > width() - kPageFlipZoneSize) {
1726 new_page_flip_target = pagination_model_.selected_page() + 1;
1730 if (new_page_flip_target == page_flip_target_)
1733 StopPageFlipTimer();
1734 if (pagination_model_.is_valid_page(new_page_flip_target)) {
1735 page_flip_target_ = new_page_flip_target;
1737 if (page_flip_target_ != pagination_model_.selected_page()) {
1738 page_flip_timer_.Start(FROM_HERE,
1739 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_),
1740 this, &AppsGridView::OnPageFlipTimer);
1745 void AppsGridView::OnPageFlipTimer() {
1746 DCHECK(pagination_model_.is_valid_page(page_flip_target_));
1747 pagination_model_.SelectPage(page_flip_target_, true);
1750 void AppsGridView::MoveItemInModel(AppListItemView* item_view,
1751 const Index& target) {
1752 int current_model_index = view_model_.GetIndexOfView(item_view);
1753 DCHECK_GE(current_model_index, 0);
1755 int target_model_index = GetModelIndexFromIndex(target);
1756 if (target_model_index == current_model_index)
1759 item_list_->RemoveObserver(this);
1760 item_list_->MoveItem(current_model_index, target_model_index);
1761 view_model_.Move(current_model_index, target_model_index);
1762 item_list_->AddObserver(this);
1764 if (pagination_model_.selected_page() != target.page)
1765 pagination_model_.SelectPage(target.page, false);
1768 void AppsGridView::MoveItemToFolder(AppListItemView* item_view,
1769 const Index& target) {
1770 const std::string& source_item_id = item_view->item()->id();
1771 AppListItemView* target_view =
1772 GetViewDisplayedAtSlotOnCurrentPage(target.slot);
1773 DCHECK(target_view);
1774 const std::string& target_view_item_id = target_view->item()->id();
1776 // Check that the item is not being dropped onto itself; this should not
1777 // happen, but it can if something allows multiple views to share an
1778 // item (e.g., if a folder drop does not clean up properly).
1779 DCHECK_NE(source_item_id, target_view_item_id);
1781 // Make change to data model.
1782 item_list_->RemoveObserver(this);
1783 std::string folder_item_id =
1784 model_->MergeItems(target_view_item_id, source_item_id);
1785 item_list_->AddObserver(this);
1786 if (folder_item_id.empty()) {
1787 LOG(ERROR) << "Unable to merge into item id: " << target_view_item_id;
1790 if (folder_item_id != target_view_item_id) {
1791 // New folder was created, change the view model to replace the old target
1792 // view with the new folder item view.
1793 size_t folder_item_index;
1794 if (item_list_->FindItemIndex(folder_item_id, &folder_item_index)) {
1795 int target_view_index = view_model_.GetIndexOfView(target_view);
1796 gfx::Rect target_view_bounds = target_view->bounds();
1797 DeleteItemViewAtIndex(target_view_index);
1798 AppListItemView* target_folder_view =
1799 CreateViewForItemAtIndex(folder_item_index);
1800 target_folder_view->SetBoundsRect(target_view_bounds);
1801 view_model_.Add(target_folder_view, target_view_index);
1802 AddChildView(target_folder_view);
1804 LOG(ERROR) << "Folder no longer in item_list: " << folder_item_id;
1808 // Fade out the drag_view_ and delete it when animation ends.
1809 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1810 view_model_.Remove(drag_view_index);
1811 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1812 bounds_animator_.SetAnimationDelegate(
1814 scoped_ptr<gfx::AnimationDelegate>(
1815 new ItemRemoveAnimationDelegate(drag_view_)));
1819 void AppsGridView::ReparentItemForReorder(AppListItemView* item_view,
1820 const Index& target) {
1821 item_list_->RemoveObserver(this);
1822 model_->RemoveObserver(this);
1824 AppListItem* reparent_item = item_view->item();
1825 DCHECK(reparent_item->IsInFolder());
1826 const std::string source_folder_id = reparent_item->folder_id();
1827 AppListFolderItem* source_folder =
1828 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1830 int target_model_index = GetModelIndexFromIndex(target);
1832 // Remove the source folder view if there is only 1 item in it, since the
1833 // source folder will be deleted after its only child item removed from it.
1834 if (source_folder->ChildItemCount() == 1u) {
1835 const int deleted_folder_index =
1836 view_model_.GetIndexOfView(activated_folder_item_view());
1837 DeleteItemViewAtIndex(deleted_folder_index);
1839 // Adjust |target_model_index| if it is beyond the deleted folder index.
1840 if (target_model_index > deleted_folder_index)
1841 --target_model_index;
1844 // Move the item from its parent folder to top level item list.
1845 // Must move to target_model_index, the location we expect the target item
1846 // to be, not the item location we want to insert before.
1847 int current_model_index = view_model_.GetIndexOfView(item_view);
1848 syncer::StringOrdinal target_position;
1849 if (target_model_index < static_cast<int>(item_list_->item_count()))
1850 target_position = item_list_->item_at(target_model_index)->position();
1851 model_->MoveItemToFolderAt(reparent_item, "", target_position);
1852 view_model_.Move(current_model_index, target_model_index);
1854 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1856 item_list_->AddObserver(this);
1857 model_->AddObserver(this);
1861 void AppsGridView::ReparentItemToAnotherFolder(AppListItemView* item_view,
1862 const Index& target) {
1863 DCHECK(IsDraggingForReparentInRootLevelGridView());
1865 AppListItemView* target_view =
1866 GetViewDisplayedAtSlotOnCurrentPage(target.slot);
1870 // Make change to data model.
1871 item_list_->RemoveObserver(this);
1873 AppListItem* reparent_item = item_view->item();
1874 DCHECK(reparent_item->IsInFolder());
1875 const std::string source_folder_id = reparent_item->folder_id();
1876 AppListFolderItem* source_folder =
1877 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1879 // Remove the source folder view if there is only 1 item in it, since the
1880 // source folder will be deleted after its only child item merged into the
1882 if (source_folder->ChildItemCount() == 1u)
1883 DeleteItemViewAtIndex(
1884 view_model_.GetIndexOfView(activated_folder_item_view()));
1886 AppListItem* target_item = target_view->item();
1888 // Move item to the target folder.
1889 std::string target_id_after_merge =
1890 model_->MergeItems(target_item->id(), reparent_item->id());
1891 if (target_id_after_merge.empty()) {
1892 LOG(ERROR) << "Unable to reparent to item id: " << target_item->id();
1893 item_list_->AddObserver(this);
1897 if (target_id_after_merge != target_item->id()) {
1898 // New folder was created, change the view model to replace the old target
1899 // view with the new folder item view.
1900 const std::string& new_folder_id = reparent_item->folder_id();
1901 size_t new_folder_index;
1902 if (item_list_->FindItemIndex(new_folder_id, &new_folder_index)) {
1903 int target_view_index = view_model_.GetIndexOfView(target_view);
1904 DeleteItemViewAtIndex(target_view_index);
1905 AppListItemView* new_folder_view =
1906 CreateViewForItemAtIndex(new_folder_index);
1907 view_model_.Add(new_folder_view, target_view_index);
1908 AddChildView(new_folder_view);
1910 LOG(ERROR) << "Folder no longer in item_list: " << new_folder_id;
1914 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1916 item_list_->AddObserver(this);
1918 // Fade out the drag_view_ and delete it when animation ends.
1919 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1920 view_model_.Remove(drag_view_index);
1921 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1922 bounds_animator_.SetAnimationDelegate(
1924 scoped_ptr<gfx::AnimationDelegate>(
1925 new ItemRemoveAnimationDelegate(drag_view_)));
1929 // After moving the re-parenting item out of the folder, if there is only 1 item
1930 // left, remove the last item out of the folder, delete the folder and insert it
1931 // to the data model at the same position. Make the same change to view_model_
1933 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1934 const std::string& source_folder_id) {
1935 AppListFolderItem* source_folder =
1936 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1937 if (!source_folder || source_folder->ChildItemCount() != 1u)
1940 // Delete view associated with the folder item to be removed.
1941 DeleteItemViewAtIndex(
1942 view_model_.GetIndexOfView(activated_folder_item_view()));
1944 // Now make the data change to remove the folder item in model.
1945 AppListItem* last_item = source_folder->item_list()->item_at(0);
1946 model_->MoveItemToFolderAt(last_item, "", source_folder->position());
1948 // Create a new item view for the last item in folder.
1949 size_t last_item_index;
1950 if (!item_list_->FindItemIndex(last_item->id(), &last_item_index) ||
1951 last_item_index > static_cast<size_t>(view_model_.view_size())) {
1955 AppListItemView* last_item_view = CreateViewForItemAtIndex(last_item_index);
1956 view_model_.Add(last_item_view, last_item_index);
1957 AddChildView(last_item_view);
1960 void AppsGridView::CancelFolderItemReparent(AppListItemView* drag_item_view) {
1961 // The icon of the dragged item must target to its final ideal bounds after
1962 // the animation completes.
1963 CalculateIdealBounds();
1965 gfx::Rect target_icon_rect =
1966 GetTargetIconRectInFolder(drag_item_view, activated_folder_item_view_);
1968 gfx::Rect drag_view_icon_to_grid =
1969 drag_item_view->ConvertRectToParent(drag_item_view->GetIconBounds());
1970 drag_view_icon_to_grid.ClampToCenteredSize(
1971 gfx::Size(kGridIconDimension, kGridIconDimension));
1972 TopIconAnimationView* icon_view = new TopIconAnimationView(
1973 drag_item_view->item()->icon(),
1975 false); /* animate like closing folder */
1976 AddChildView(icon_view);
1977 icon_view->SetBoundsRect(drag_view_icon_to_grid);
1978 icon_view->TransformView();
1981 void AppsGridView::CancelContextMenusOnCurrentPage() {
1982 int start = pagination_model_.selected_page() * tiles_per_page();
1983 int end = std::min(view_model_.view_size(), start + tiles_per_page());
1984 for (int i = start; i < end; ++i)
1985 GetItemViewAt(i)->CancelContextMenu();
1988 void AppsGridView::DeleteItemViewAtIndex(int index) {
1989 AppListItemView* item_view = GetItemViewAt(index);
1990 view_model_.Remove(index);
1991 if (item_view == drag_view_)
1996 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const {
1997 gfx::Rect rect(GetLocalBounds());
1998 rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx);
1999 return rect.Contains(point);
2002 void AppsGridView::ButtonPressed(views::Button* sender,
2003 const ui::Event& event) {
2007 if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName))
2009 AppListItemView* pressed_item_view = static_cast<AppListItemView*>(sender);
2012 // Always set the previous activated_folder_item_view_ to be visible. This
2013 // prevents a case where the item would remain hidden due the
2014 // |activated_folder_item_view_| changing during the animation. We only
2015 // need to track |activated_folder_item_view_| in the root level grid view.
2016 if (!folder_delegate_) {
2017 if (activated_folder_item_view_)
2018 activated_folder_item_view_->SetVisible(true);
2019 if (IsFolderItem(pressed_item_view->item()))
2020 activated_folder_item_view_ = pressed_item_view;
2022 activated_folder_item_view_ = NULL;
2024 delegate_->ActivateApp(pressed_item_view->item(), event.flags());
2028 void AppsGridView::OnListItemAdded(size_t index, AppListItem* item) {
2031 AppListItemView* view = CreateViewForItemAtIndex(index);
2032 view_model_.Add(view, index);
2036 UpdatePulsingBlockViews();
2041 void AppsGridView::OnListItemRemoved(size_t index, AppListItem* item) {
2044 DeleteItemViewAtIndex(index);
2047 UpdatePulsingBlockViews();
2052 void AppsGridView::OnListItemMoved(size_t from_index,
2054 AppListItem* item) {
2056 view_model_.Move(from_index, to_index);
2059 AnimateToIdealBounds();
2062 void AppsGridView::OnAppListItemHighlight(size_t index, bool highlight) {
2063 AppListItemView* view = GetItemViewAt(index);
2064 view->SetItemIsHighlighted(highlight);
2066 EnsureViewVisible(GetIndexFromModelIndex(index));
2069 void AppsGridView::TotalPagesChanged() {
2072 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
2074 CalculateDropTarget();
2076 MaybeStartPageFlipTimer(last_drag_point_);
2078 ClearSelectedView(selected_view_);
2083 void AppsGridView::TransitionStarted() {
2084 CancelContextMenusOnCurrentPage();
2087 void AppsGridView::TransitionChanged() {
2088 // Update layout for valid page transition only since over-scroll no longer
2089 // animates app icons.
2090 const PaginationModel::Transition& transition =
2091 pagination_model_.transition();
2092 if (pagination_model_.is_valid_page(transition.target_page))
2096 void AppsGridView::OnAppListModelStatusChanged() {
2097 UpdatePulsingBlockViews();
2102 void AppsGridView::SetViewHidden(AppListItemView* view,
2105 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
2106 animator.SetPreemptionStrategy(
2107 immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
2108 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
2109 view->layer()->SetOpacity(hide ? 0 : 1);
2112 void AppsGridView::OnImplicitAnimationsCompleted() {
2113 if (layer()->opacity() == 0.0f)
2117 bool AppsGridView::EnableFolderDragDropUI() {
2118 // Enable drag and drop folder UI only if it is at the app list root level
2119 // and the switch is on.
2120 return model_->folders_enabled() && !folder_delegate_;
2123 AppsGridView::Index AppsGridView::GetNearestTileIndexForPoint(
2124 const gfx::Point& point) const {
2125 gfx::Rect bounds = GetContentsBounds();
2126 gfx::Size total_tile_size = GetTotalTileSize();
2127 int col = ClampToRange(
2128 (point.x() - bounds.x()) / total_tile_size.width(), 0, cols_ - 1);
2129 int row = ClampToRange((point.y() - bounds.y()) / total_tile_size.height(),
2131 rows_per_page_ - 1);
2132 return Index(pagination_model_.selected_page(), row * cols_ + col);
2135 gfx::Size AppsGridView::GetTileGridSize() const {
2136 gfx::Rect bounds = GetExpectedTileBounds(0, 0);
2137 bounds.Union(GetExpectedTileBounds(rows_per_page_ - 1, cols_ - 1));
2138 if (switches::IsExperimentalAppListEnabled())
2139 bounds.Inset(-kExperimentalTileLeftRightPadding,
2140 -kExperimentalTileTopBottomPadding);
2141 return bounds.size();
2144 gfx::Rect AppsGridView::GetExpectedTileBounds(int slot) const {
2145 return GetExpectedTileBounds(slot / cols_, slot % cols_);
2148 gfx::Rect AppsGridView::GetExpectedTileBounds(int row, int col) const {
2149 gfx::Rect bounds(GetContentsBounds());
2150 gfx::Size total_tile_size = GetTotalTileSize();
2151 gfx::Rect tile_bounds(gfx::Point(bounds.x() + col * total_tile_size.width(),
2152 bounds.y() + row * total_tile_size.height()),
2154 tile_bounds.ClampToCenteredSize(GetTileViewSize());
2158 AppListItemView* AppsGridView::GetViewDisplayedAtSlotOnCurrentPage(
2163 // Calculate the original bound of the tile at |index|.
2164 int row = slot / cols_;
2165 int col = slot % cols_;
2166 gfx::Rect tile_rect = GetExpectedTileBounds(row, col);
2168 for (int i = 0; i < view_model_.view_size(); ++i) {
2169 AppListItemView* view = GetItemViewAt(i);
2170 if (view->bounds() == tile_rect && view != drag_view_)
2176 void AppsGridView::SetAsFolderDroppingTarget(const Index& target_index,
2177 bool is_target_folder) {
2178 AppListItemView* target_view =
2179 GetViewDisplayedAtSlotOnCurrentPage(target_index.slot);
2181 target_view->SetAsAttemptedFolderTarget(is_target_folder);
2184 } // namespace app_list