Upstream version 11.39.250.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / apps_grid_view.cc
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 #include <set>
9 #include <string>
10
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"
32
33 #if defined(USE_AURA)
34 #include "ui/aura/window.h"
35 #include "ui/aura/window_event_dispatcher.h"
36 #if defined(OS_WIN)
37 #include "ui/views/win/hwnd_util.h"
38 #endif  // defined(OS_WIN)
39 #endif  // defined(USE_AURA)
40
41 #if defined(OS_WIN)
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"
50 #endif
51
52 namespace app_list {
53
54 namespace {
55
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
59 // other pages.
60 const int kDragBufferPx = 20;
61
62 // Padding space in pixels for fixed layout.
63 const int kLeftRightPadding = 23;
64 const int kTopPadding = 1;
65
66 // Padding space in pixels between pages.
67 const int kPagePadding = 40;
68
69 // Preferred tile size when showing in fixed layout.
70 const int kPreferredTileWidth = 88;
71 const int kPreferredTileHeight = 98;
72
73 const int kExperimentalPreferredTileWidth = 90;
74 const int kExperimentalPrefferedTileHeight = 90;
75
76 // Padding on each side of a tile.
77 const int kExperimentalTileLeftRightPadding = 15;
78 const int kExperimentalTileTopBottomPadding = 11;
79
80 // Width in pixels of the area on the sides that triggers a page flip.
81 const int kPageFlipZoneSize = 40;
82
83 // Delay in milliseconds to do the page flip.
84 const int kPageFlipDelayInMs = 1000;
85
86 // How many pages on either side of the selected one we prerender.
87 const int kPrerenderPages = 1;
88
89 // The drag and drop proxy should get scaled by this factor.
90 const float kDragAndDropProxyScale = 1.5f;
91
92 // Delays in milliseconds to show folder dropping preview circle.
93 const int kFolderDroppingDelay = 150;
94
95 // Delays in milliseconds to show re-order preview.
96 const int kReorderDelay = 120;
97
98 // Delays in milliseconds to show folder item reparent UI.
99 const int kFolderItemReparentDelay = 50;
100
101 // Radius of the circle, in which if entered, show folder dropping preview
102 // UI.
103 const int kFolderDroppingCircleRadius = 39;
104
105 // Returns the size of a tile view excluding its padding.
106 gfx::Size GetTileViewSize() {
107   return switches::IsExperimentalAppListEnabled()
108              ? gfx::Size(kExperimentalPreferredTileWidth,
109                          kExperimentalPrefferedTileHeight)
110              : gfx::Size(kPreferredTileWidth, kPreferredTileHeight);
111 }
112
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);
119   return size;
120 }
121
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 {
128  public:
129   RowMoveAnimationDelegate(views::View* view,
130                            ui::Layer* layer,
131                            const gfx::Rect& layer_target)
132       : view_(view),
133         layer_(layer),
134         layer_start_(layer ? layer->bounds() : gfx::Rect()),
135         layer_target_(layer_target) {
136   }
137   virtual ~RowMoveAnimationDelegate() {}
138
139   // gfx::AnimationDelegate overrides:
140   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
141     view_->layer()->SetOpacity(animation->GetCurrentValue());
142     view_->layer()->ScheduleDraw();
143
144     if (layer_) {
145       layer_->SetOpacity(1 - animation->GetCurrentValue());
146       layer_->SetBounds(animation->CurrentValueBetween(layer_start_,
147                                                        layer_target_));
148       layer_->ScheduleDraw();
149     }
150   }
151   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
152     view_->layer()->SetOpacity(1.0f);
153     view_->SchedulePaint();
154   }
155   virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
156     view_->layer()->SetOpacity(1.0f);
157     view_->SchedulePaint();
158   }
159
160  private:
161   // The view that needs to be wrapped. Owned by views hierarchy.
162   views::View* view_;
163
164   scoped_ptr<ui::Layer> layer_;
165   const gfx::Rect layer_start_;
166   const gfx::Rect layer_target_;
167
168   DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate);
169 };
170
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 {
175  public:
176   explicit ItemRemoveAnimationDelegate(views::View* view)
177       : view_(view) {
178   }
179
180   virtual ~ItemRemoveAnimationDelegate() {
181   }
182
183   // gfx::AnimationDelegate overrides:
184   virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
185     view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
186     view_->layer()->ScheduleDraw();
187   }
188
189  private:
190   scoped_ptr<views::View> view_;
191
192   DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate);
193 };
194
195 // ItemMoveAnimationDelegate observes when an item finishes animating when it is
196 // not moving between rows. This is to ensure an item is repainted for the
197 // "zoom out" case when releasing an item being dragged.
198 class ItemMoveAnimationDelegate : public gfx::AnimationDelegate {
199  public:
200   ItemMoveAnimationDelegate(views::View* view) : view_(view) {}
201
202   virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
203     view_->SchedulePaint();
204   }
205   virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
206     view_->SchedulePaint();
207   }
208
209  private:
210   views::View* view_;
211
212   DISALLOW_COPY_AND_ASSIGN(ItemMoveAnimationDelegate);
213 };
214
215 // Returns true if the |item| is an folder item.
216 bool IsFolderItem(AppListItem* item) {
217   return (item->GetItemType() == AppListFolderItem::kItemType);
218 }
219
220 bool IsOEMFolderItem(AppListItem* item) {
221   return IsFolderItem(item) &&
222          (static_cast<AppListFolderItem*>(item))->folder_type() ==
223              AppListFolderItem::FOLDER_TYPE_OEM;
224 }
225
226 int ClampToRange(int value, int min, int max) {
227   return std::min(std::max(value, min), max);
228 }
229
230 }  // namespace
231
232 #if defined(OS_WIN)
233 // Interprets drag events sent from Windows via the drag/drop API and forwards
234 // them to AppsGridView.
235 // On Windows, in order to have the OS perform the drag properly we need to
236 // provide it with a shortcut file which may or may not exist at the time the
237 // drag is started. Therefore while waiting for that shortcut to be located we
238 // just do a regular "internal" drag and transition into the synchronous drag
239 // when the shortcut is found/created. Hence a synchronous drag is an optional
240 // phase of a regular drag and non-Windows platforms drags are equivalent to a
241 // Windows drag that never enters the synchronous drag phase.
242 class SynchronousDrag : public ui::DragSourceWin {
243  public:
244   SynchronousDrag(AppsGridView* grid_view,
245                   AppListItemView* drag_view,
246                   const gfx::Point& drag_view_offset)
247       : grid_view_(grid_view),
248         drag_view_(drag_view),
249         drag_view_offset_(drag_view_offset),
250         has_shortcut_path_(false),
251         running_(false),
252         canceled_(false) {}
253
254   void set_shortcut_path(const base::FilePath& shortcut_path) {
255     has_shortcut_path_ = true;
256     shortcut_path_ = shortcut_path;
257   }
258
259   bool running() { return running_; }
260
261   bool CanRun() {
262     return has_shortcut_path_ && !running_;
263   }
264
265   void Run() {
266     DCHECK(CanRun());
267
268     // Prevent the synchronous dragger being destroyed while the drag is
269     // running.
270     scoped_refptr<SynchronousDrag> this_ref = this;
271     running_ = true;
272
273     ui::OSExchangeData data;
274     SetupExchangeData(&data);
275
276     // Hide the dragged view because the OS is going to create its own.
277     drag_view_->SetVisible(false);
278
279     // Blocks until the drag is finished. Calls into the ui::DragSourceWin
280     // methods.
281     DWORD effects;
282     DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
283                this, DROPEFFECT_MOVE | DROPEFFECT_LINK, &effects);
284
285     // If |drag_view_| is NULL the drag was ended by some reentrant code.
286     if (drag_view_) {
287       // Make the drag view visible again.
288       drag_view_->SetVisible(true);
289       drag_view_->OnSyncDragEnd();
290
291       grid_view_->EndDrag(canceled_ || !IsCursorWithinGridView());
292     }
293   }
294
295   void EndDragExternally() {
296     CancelDrag();
297     DCHECK(drag_view_);
298     drag_view_->SetVisible(true);
299     drag_view_ = NULL;
300   }
301
302  private:
303   // Overridden from ui::DragSourceWin.
304   virtual void OnDragSourceCancel() OVERRIDE {
305     canceled_ = true;
306   }
307
308   virtual void OnDragSourceDrop() OVERRIDE {
309   }
310
311   virtual void OnDragSourceMove() OVERRIDE {
312     grid_view_->UpdateDrag(AppsGridView::MOUSE, GetCursorInGridViewCoords());
313   }
314
315   void SetupExchangeData(ui::OSExchangeData* data) {
316     data->SetFilename(shortcut_path_);
317     gfx::ImageSkia image(drag_view_->GetDragImage());
318     gfx::Size image_size(image.size());
319     drag_utils::SetDragImageOnDataObject(
320         image,
321         drag_view_offset_ - drag_view_->GetDragImageOffset(),
322         data);
323   }
324
325   HWND GetGridViewHWND() {
326     return views::HWNDForView(grid_view_);
327   }
328
329   bool IsCursorWithinGridView() {
330     POINT p;
331     GetCursorPos(&p);
332     return GetGridViewHWND() == WindowFromPoint(p);
333   }
334
335   gfx::Point GetCursorInGridViewCoords() {
336     POINT p;
337     GetCursorPos(&p);
338     ScreenToClient(GetGridViewHWND(), &p);
339     gfx::Point grid_view_pt(p.x, p.y);
340     grid_view_pt = gfx::win::ScreenToDIPPoint(grid_view_pt);
341     views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt);
342     return grid_view_pt;
343   }
344
345   AppsGridView* grid_view_;
346   AppListItemView* drag_view_;
347   gfx::Point drag_view_offset_;
348   bool has_shortcut_path_;
349   base::FilePath shortcut_path_;
350   bool running_;
351   bool canceled_;
352
353   DISALLOW_COPY_AND_ASSIGN(SynchronousDrag);
354 };
355 #endif  // defined(OS_WIN)
356
357 AppsGridView::AppsGridView(AppsGridViewDelegate* delegate)
358     : model_(NULL),
359       item_list_(NULL),
360       delegate_(delegate),
361       folder_delegate_(NULL),
362       page_switcher_view_(NULL),
363       cols_(0),
364       rows_per_page_(0),
365       selected_view_(NULL),
366       drag_view_(NULL),
367       drag_start_page_(-1),
368 #if defined(OS_WIN)
369       use_synchronous_drag_(true),
370 #endif
371       drag_pointer_(NONE),
372       drop_attempt_(DROP_FOR_NONE),
373       drag_and_drop_host_(NULL),
374       forward_events_to_drag_and_drop_host_(false),
375       page_flip_target_(-1),
376       page_flip_delay_in_ms_(kPageFlipDelayInMs),
377       bounds_animator_(this),
378       activated_folder_item_view_(NULL),
379       dragging_for_reparent_item_(false) {
380   SetPaintToLayer(true);
381   // Clip any icons that are outside the grid view's bounds. These icons would
382   // otherwise be visible to the user when the grid view is off screen.
383   layer()->SetMasksToBounds(true);
384   SetFillsBoundsOpaquely(false);
385
386   pagination_model_.SetTransitionDurations(kPageTransitionDurationInMs,
387                                            kOverscrollPageTransitionDurationMs);
388
389   pagination_model_.AddObserver(this);
390   // The experimental app list transitions vertically.
391   PaginationController::ScrollAxis scroll_axis =
392       app_list::switches::IsExperimentalAppListEnabled()
393           ? PaginationController::SCROLL_AXIS_VERTICAL
394           : PaginationController::SCROLL_AXIS_HORIZONTAL;
395   pagination_controller_.reset(
396       new PaginationController(&pagination_model_, scroll_axis));
397   if (!switches::IsExperimentalAppListEnabled()) {
398     page_switcher_view_ = new PageSwitcher(&pagination_model_);
399     AddChildView(page_switcher_view_);
400   }
401 }
402
403 AppsGridView::~AppsGridView() {
404   // Coming here |drag_view_| should already be canceled since otherwise the
405   // drag would disappear after the app list got animated away and closed,
406   // which would look odd.
407   DCHECK(!drag_view_);
408   if (drag_view_)
409     EndDrag(true);
410
411   if (model_)
412     model_->RemoveObserver(this);
413   pagination_model_.RemoveObserver(this);
414
415   if (item_list_)
416     item_list_->RemoveObserver(this);
417
418   // Make sure |page_switcher_view_| is deleted before |pagination_model_|.
419   view_model_.Clear();
420   RemoveAllChildViews(true);
421 }
422
423 void AppsGridView::SetLayout(int cols, int rows_per_page) {
424   cols_ = cols;
425   rows_per_page_ = rows_per_page;
426
427   SetBorder(views::Border::CreateEmptyBorder(
428       switches::IsExperimentalAppListEnabled() ? 0 : kTopPadding,
429       kLeftRightPadding,
430       0,
431       kLeftRightPadding));
432 }
433
434 void AppsGridView::ResetForShowApps() {
435   activated_folder_item_view_ = NULL;
436   ClearDragState();
437   layer()->SetOpacity(1.0f);
438   SetVisible(true);
439   // Set all views to visible in case they weren't made visible again by an
440   // incomplete animation.
441   for (int i = 0; i < view_model_.view_size(); ++i) {
442     view_model_.view_at(i)->SetVisible(true);
443   }
444   CHECK_EQ(item_list_->item_count(),
445            static_cast<size_t>(view_model_.view_size()));
446 }
447
448 void AppsGridView::SetModel(AppListModel* model) {
449   if (model_)
450     model_->RemoveObserver(this);
451
452   model_ = model;
453   if (model_)
454     model_->AddObserver(this);
455
456   Update();
457 }
458
459 void AppsGridView::SetItemList(AppListItemList* item_list) {
460   if (item_list_)
461     item_list_->RemoveObserver(this);
462   item_list_ = item_list;
463   if (item_list_)
464     item_list_->AddObserver(this);
465   Update();
466 }
467
468 void AppsGridView::SetSelectedView(views::View* view) {
469   if (IsSelectedView(view) || IsDraggedView(view))
470     return;
471
472   Index index = GetIndexOfView(view);
473   if (IsValidIndex(index))
474     SetSelectedItemByIndex(index);
475 }
476
477 void AppsGridView::ClearSelectedView(views::View* view) {
478   if (view && IsSelectedView(view)) {
479     selected_view_->SchedulePaint();
480     selected_view_ = NULL;
481   }
482 }
483
484 void AppsGridView::ClearAnySelectedView() {
485   if (selected_view_) {
486     selected_view_->SchedulePaint();
487     selected_view_ = NULL;
488   }
489 }
490
491 bool AppsGridView::IsSelectedView(const views::View* view) const {
492   return selected_view_ == view;
493 }
494
495 void AppsGridView::EnsureViewVisible(const views::View* view) {
496   if (pagination_model_.has_transition())
497     return;
498
499   Index index = GetIndexOfView(view);
500   if (IsValidIndex(index))
501     pagination_model_.SelectPage(index.page, false);
502 }
503
504 void AppsGridView::InitiateDrag(AppListItemView* view,
505                                 Pointer pointer,
506                                 const ui::LocatedEvent& event) {
507   DCHECK(view);
508   if (drag_view_ || pulsing_blocks_model_.view_size())
509     return;
510
511   drag_view_ = view;
512   drag_view_init_index_ = GetIndexOfView(drag_view_);
513   drag_view_offset_ = event.location();
514   drag_start_page_ = pagination_model_.selected_page();
515   reorder_placeholder_ = drag_view_init_index_;
516   ExtractDragLocation(event, &drag_start_grid_view_);
517   drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
518 }
519
520 void AppsGridView::StartSettingUpSynchronousDrag() {
521 #if defined(OS_WIN)
522   if (!delegate_ || !use_synchronous_drag_)
523     return;
524
525   // Folders and downloading items can't be integrated with the OS.
526   if (IsFolderItem(drag_view_->item()) || drag_view_->item()->is_installing())
527     return;
528
529   // Favor the drag and drop host over native win32 drag. For the Win8/ash
530   // launcher we want to have ashes drag and drop over win32's.
531   if (drag_and_drop_host_)
532     return;
533
534   // Never create a second synchronous drag if the drag started in a folder.
535   if (IsDraggingForReparentInRootLevelGridView())
536     return;
537
538   synchronous_drag_ = new SynchronousDrag(this, drag_view_, drag_view_offset_);
539   delegate_->GetShortcutPathForApp(drag_view_->item()->id(),
540                                    base::Bind(&AppsGridView::OnGotShortcutPath,
541                                               base::Unretained(this),
542                                               synchronous_drag_));
543 #endif
544 }
545
546 bool AppsGridView::RunSynchronousDrag() {
547 #if defined(OS_WIN)
548   if (!synchronous_drag_)
549     return false;
550
551   if (synchronous_drag_->CanRun()) {
552     if (IsDraggingForReparentInHiddenGridView())
553       folder_delegate_->SetRootLevelDragViewVisible(false);
554     synchronous_drag_->Run();
555     synchronous_drag_ = NULL;
556     return true;
557   } else if (!synchronous_drag_->running()) {
558     // The OS drag is not ready yet. If the root grid has a drag view because
559     // a reparent has started, ensure it is visible.
560     if (IsDraggingForReparentInHiddenGridView())
561       folder_delegate_->SetRootLevelDragViewVisible(true);
562   }
563 #endif
564   return false;
565 }
566
567 void AppsGridView::CleanUpSynchronousDrag() {
568 #if defined(OS_WIN)
569   if (synchronous_drag_)
570     synchronous_drag_->EndDragExternally();
571
572   synchronous_drag_ = NULL;
573 #endif
574 }
575
576 #if defined(OS_WIN)
577 void AppsGridView::OnGotShortcutPath(
578     scoped_refptr<SynchronousDrag> synchronous_drag,
579     const base::FilePath& path) {
580   // Drag may have ended before we get the shortcut path or a new drag may have
581   // begun.
582   if (synchronous_drag_ != synchronous_drag)
583     return;
584   // Setting the shortcut path here means the next time we hit UpdateDrag()
585   // we'll enter the synchronous drag.
586   // NOTE we don't Run() the drag here because that causes animations not to
587   // update for some reason.
588   synchronous_drag_->set_shortcut_path(path);
589   DCHECK(synchronous_drag_->CanRun());
590 }
591 #endif
592
593 bool AppsGridView::UpdateDragFromItem(Pointer pointer,
594                                       const ui::LocatedEvent& event) {
595   if (!drag_view_)
596     return false;  // Drag canceled.
597
598   gfx::Point drag_point_in_grid_view;
599   ExtractDragLocation(event, &drag_point_in_grid_view);
600   UpdateDrag(pointer, drag_point_in_grid_view);
601   if (!dragging())
602     return false;
603
604   // If a drag and drop host is provided, see if the drag operation needs to be
605   // forwarded.
606   gfx::Point location_in_screen = drag_point_in_grid_view;
607   views::View::ConvertPointToScreen(this, &location_in_screen);
608   DispatchDragEventToDragAndDropHost(location_in_screen);
609   if (drag_and_drop_host_)
610     drag_and_drop_host_->UpdateDragIconProxy(location_in_screen);
611   return true;
612 }
613
614 void AppsGridView::UpdateDrag(Pointer pointer, const gfx::Point& point) {
615   if (folder_delegate_)
616     UpdateDragStateInsideFolder(pointer, point);
617
618   if (!drag_view_)
619     return;  // Drag canceled.
620
621   if (RunSynchronousDrag())
622     return;
623
624   gfx::Vector2d drag_vector(point - drag_start_grid_view_);
625   if (!dragging() && ExceededDragThreshold(drag_vector)) {
626     drag_pointer_ = pointer;
627     // Move the view to the front so that it appears on top of other views.
628     ReorderChildView(drag_view_, -1);
629     bounds_animator_.StopAnimatingView(drag_view_);
630     // Stopping the animation may have invalidated our drag view due to the
631     // view hierarchy changing.
632     if (!drag_view_)
633       return;
634
635     StartSettingUpSynchronousDrag();
636     if (!dragging_for_reparent_item_)
637       StartDragAndDropHostDrag(point);
638   }
639
640   if (drag_pointer_ != pointer)
641     return;
642
643   drag_view_->SetPosition(drag_view_start_ + drag_vector);
644
645   last_drag_point_ = point;
646   const Index last_reorder_drop_target = reorder_drop_target_;
647   const Index last_folder_drop_target = folder_drop_target_;
648   DropAttempt last_drop_attempt = drop_attempt_;
649   CalculateDropTarget();
650
651   MaybeStartPageFlipTimer(last_drag_point_);
652
653   if (page_switcher_view_) {
654     gfx::Point page_switcher_point(last_drag_point_);
655     views::View::ConvertPointToTarget(
656         this, page_switcher_view_, &page_switcher_point);
657     page_switcher_view_->UpdateUIForDragPoint(page_switcher_point);
658   }
659
660   if (last_folder_drop_target != folder_drop_target_ ||
661       last_reorder_drop_target != reorder_drop_target_ ||
662       last_drop_attempt != drop_attempt_) {
663     if (drop_attempt_ == DROP_FOR_REORDER) {
664       folder_dropping_timer_.Stop();
665       reorder_timer_.Start(FROM_HERE,
666           base::TimeDelta::FromMilliseconds(kReorderDelay),
667           this, &AppsGridView::OnReorderTimer);
668     } else if (drop_attempt_ == DROP_FOR_FOLDER) {
669       reorder_timer_.Stop();
670       folder_dropping_timer_.Start(FROM_HERE,
671           base::TimeDelta::FromMilliseconds(kFolderDroppingDelay),
672           this, &AppsGridView::OnFolderDroppingTimer);
673     }
674
675     // Reset the previous drop target.
676     SetAsFolderDroppingTarget(last_folder_drop_target, false);
677   }
678 }
679
680 void AppsGridView::EndDrag(bool cancel) {
681   // EndDrag was called before if |drag_view_| is NULL.
682   if (!drag_view_)
683     return;
684
685   // Coming here a drag and drop was in progress.
686   bool landed_in_drag_and_drop_host = forward_events_to_drag_and_drop_host_;
687   if (forward_events_to_drag_and_drop_host_) {
688     DCHECK(!IsDraggingForReparentInRootLevelGridView());
689     forward_events_to_drag_and_drop_host_ = false;
690     drag_and_drop_host_->EndDrag(cancel);
691     if (IsDraggingForReparentInHiddenGridView()) {
692       folder_delegate_->DispatchEndDragEventForReparent(
693           true /* events_forwarded_to_drag_drop_host */,
694           cancel /* cancel_drag */);
695     }
696   } else {
697     if (IsDraggingForReparentInHiddenGridView()) {
698       // Forward the EndDrag event to the root level grid view.
699       folder_delegate_->DispatchEndDragEventForReparent(
700           false /* events_forwarded_to_drag_drop_host */,
701           cancel /* cancel_drag */);
702       EndDragForReparentInHiddenFolderGridView();
703       return;
704     }
705
706     if (IsDraggingForReparentInRootLevelGridView()) {
707       // An EndDrag can be received during a reparent via a model change. This
708       // is always a cancel and needs to be forwarded to the folder.
709       DCHECK(cancel);
710       delegate_->CancelDragInActiveFolder();
711       return;
712     }
713
714     if (!cancel && dragging()) {
715       // Regular drag ending path, ie, not for reparenting.
716       CalculateDropTarget();
717       if (EnableFolderDragDropUI() && drop_attempt_ == DROP_FOR_FOLDER &&
718           IsValidIndex(folder_drop_target_)) {
719         MoveItemToFolder(drag_view_, folder_drop_target_);
720       } else if (IsValidIndex(reorder_drop_target_)) {
721         MoveItemInModel(drag_view_, reorder_drop_target_);
722       }
723     }
724   }
725
726   if (drag_and_drop_host_) {
727     // If we had a drag and drop proxy icon, we delete it and make the real
728     // item visible again.
729     drag_and_drop_host_->DestroyDragIconProxy();
730     if (landed_in_drag_and_drop_host) {
731       // Move the item directly to the target location, avoiding the "zip back"
732       // animation if the user was pinning it to the shelf.
733       int i = reorder_drop_target_.slot;
734       gfx::Rect bounds = view_model_.ideal_bounds(i);
735       drag_view_->SetBoundsRect(bounds);
736     }
737     // Fade in slowly if it landed in the shelf.
738     SetViewHidden(drag_view_,
739                   false /* show */,
740                   !landed_in_drag_and_drop_host /* animate */);
741   }
742
743   // The drag can be ended after the synchronous drag is created but before it
744   // is Run().
745   CleanUpSynchronousDrag();
746
747   SetAsFolderDroppingTarget(folder_drop_target_, false);
748   ClearDragState();
749   AnimateToIdealBounds();
750
751   StopPageFlipTimer();
752
753   // If user releases mouse inside a folder's grid view, burst the folder
754   // container ink bubble.
755   if (folder_delegate_ && !IsDraggingForReparentInHiddenGridView())
756     folder_delegate_->UpdateFolderViewBackground(false);
757 }
758
759 void AppsGridView::StopPageFlipTimer() {
760   page_flip_timer_.Stop();
761   page_flip_target_ = -1;
762 }
763
764 AppListItemView* AppsGridView::GetItemViewAt(int index) const {
765   DCHECK(index >= 0 && index < view_model_.view_size());
766   return static_cast<AppListItemView*>(view_model_.view_at(index));
767 }
768
769 void AppsGridView::SetTopItemViewsVisible(bool visible) {
770   int top_item_count = std::min(static_cast<int>(kNumFolderTopItems),
771                                 view_model_.view_size());
772   for (int i = 0; i < top_item_count; ++i)
773     GetItemViewAt(i)->icon()->SetVisible(visible);
774 }
775
776 void AppsGridView::ScheduleShowHideAnimation(bool show) {
777   // Stop any previous animation.
778   layer()->GetAnimator()->StopAnimating();
779
780   // Set initial state.
781   SetVisible(true);
782   layer()->SetOpacity(show ? 0.0f : 1.0f);
783
784   ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
785   animation.AddObserver(this);
786   animation.SetTweenType(
787       show ? kFolderFadeInTweenType : kFolderFadeOutTweenType);
788   animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
789       show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
790
791   layer()->SetOpacity(show ? 1.0f : 0.0f);
792 }
793
794 void AppsGridView::InitiateDragFromReparentItemInRootLevelGridView(
795     AppListItemView* original_drag_view,
796     const gfx::Rect& drag_view_rect,
797     const gfx::Point& drag_point) {
798   DCHECK(original_drag_view && !drag_view_);
799   DCHECK(!dragging_for_reparent_item_);
800
801   // Since the item is new, its placeholder is conceptually at the back of the
802   // entire apps grid.
803   reorder_placeholder_ = GetLastViewIndex();
804
805   // Create a new AppListItemView to duplicate the original_drag_view in the
806   // folder's grid view.
807   AppListItemView* view = new AppListItemView(this, original_drag_view->item());
808   AddChildView(view);
809   drag_view_ = view;
810   drag_view_->SetPaintToLayer(true);
811   // Note: For testing purpose, SetFillsBoundsOpaquely can be set to true to
812   // show the gray background.
813   drag_view_->SetFillsBoundsOpaquely(false);
814   drag_view_->SetBoundsRect(drag_view_rect);
815   drag_view_->SetDragUIState();  // Hide the title of the drag_view_.
816
817   // Hide the drag_view_ for drag icon proxy.
818   SetViewHidden(drag_view_,
819                 true /* hide */,
820                 true /* no animate */);
821
822   // Add drag_view_ to the end of the view_model_.
823   view_model_.Add(drag_view_, view_model_.view_size());
824
825   drag_start_page_ = pagination_model_.selected_page();
826   drag_start_grid_view_ = drag_point;
827
828   drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
829
830   // Set the flag in root level grid view.
831   dragging_for_reparent_item_ = true;
832 }
833
834 void AppsGridView::UpdateDragFromReparentItem(Pointer pointer,
835                                               const gfx::Point& drag_point) {
836   // Note that if a cancel ocurrs while reparenting, the |drag_view_| in both
837   // root and folder grid views is cleared, so the check in UpdateDragFromItem()
838   // for |drag_view_| being NULL (in the folder grid) is sufficient.
839   DCHECK(drag_view_);
840   DCHECK(IsDraggingForReparentInRootLevelGridView());
841
842   UpdateDrag(pointer, drag_point);
843 }
844
845 bool AppsGridView::IsDraggedView(const views::View* view) const {
846   return drag_view_ == view;
847 }
848
849 void AppsGridView::ClearDragState() {
850   drop_attempt_ = DROP_FOR_NONE;
851   drag_pointer_ = NONE;
852   reorder_drop_target_ = Index();
853   folder_drop_target_ = Index();
854   reorder_placeholder_ = Index();
855   drag_start_grid_view_ = gfx::Point();
856   drag_start_page_ = -1;
857   drag_view_offset_ = gfx::Point();
858
859   if (drag_view_) {
860     drag_view_->OnDragEnded();
861     if (IsDraggingForReparentInRootLevelGridView()) {
862       const int drag_view_index = view_model_.GetIndexOfView(drag_view_);
863       CHECK_EQ(view_model_.view_size() - 1, drag_view_index);
864       DeleteItemViewAtIndex(drag_view_index);
865     }
866   }
867   drag_view_ = NULL;
868   dragging_for_reparent_item_ = false;
869 }
870
871 void AppsGridView::SetDragViewVisible(bool visible) {
872   DCHECK(drag_view_);
873   SetViewHidden(drag_view_, !visible, true);
874 }
875
876 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
877     ApplicationDragAndDropHost* drag_and_drop_host) {
878   drag_and_drop_host_ = drag_and_drop_host;
879 }
880
881 void AppsGridView::Prerender() {
882   Layout();
883   int selected_page = std::max(0, pagination_model_.selected_page());
884   int start = std::max(0, (selected_page - kPrerenderPages) * tiles_per_page());
885   int end = std::min(view_model_.view_size(),
886                      (selected_page + 1 + kPrerenderPages) * tiles_per_page());
887   for (int i = start; i < end; i++) {
888     AppListItemView* v = static_cast<AppListItemView*>(view_model_.view_at(i));
889     v->Prerender();
890   }
891 }
892
893 bool AppsGridView::IsAnimatingView(views::View* view) {
894   return bounds_animator_.IsAnimating(view);
895 }
896
897 gfx::Size AppsGridView::GetPreferredSize() const {
898   const gfx::Insets insets(GetInsets());
899   int page_switcher_height = 0;
900   if (page_switcher_view_)
901     page_switcher_height = page_switcher_view_->GetPreferredSize().height();
902   gfx::Size size = GetTileGridSize();
903   size.Enlarge(insets.width(), insets.height() + page_switcher_height);
904   return size;
905 }
906
907 bool AppsGridView::GetDropFormats(
908     int* formats,
909     std::set<OSExchangeData::CustomFormat>* custom_formats) {
910   // TODO(koz): Only accept a specific drag type for app shortcuts.
911   *formats = OSExchangeData::FILE_NAME;
912   return true;
913 }
914
915 bool AppsGridView::CanDrop(const OSExchangeData& data) {
916   return true;
917 }
918
919 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent& event) {
920   return ui::DragDropTypes::DRAG_MOVE;
921 }
922
923 void AppsGridView::Layout() {
924   if (bounds_animator_.IsAnimating())
925     bounds_animator_.Cancel();
926
927   CalculateIdealBounds();
928   for (int i = 0; i < view_model_.view_size(); ++i) {
929     views::View* view = view_model_.view_at(i);
930     if (view != drag_view_)
931       view->SetBoundsRect(view_model_.ideal_bounds(i));
932   }
933   views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_);
934
935   if (page_switcher_view_) {
936     const int page_switcher_height =
937         page_switcher_view_->GetPreferredSize().height();
938     gfx::Rect rect(GetContentsBounds());
939     rect.set_y(rect.bottom() - page_switcher_height);
940     rect.set_height(page_switcher_height);
941     page_switcher_view_->SetBoundsRect(rect);
942   }
943 }
944
945 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) {
946   bool handled = false;
947   if (selected_view_)
948     handled = selected_view_->OnKeyPressed(event);
949
950   if (!handled) {
951     const int forward_dir = base::i18n::IsRTL() ? -1 : 1;
952     switch (event.key_code()) {
953       case ui::VKEY_LEFT:
954         MoveSelected(0, -forward_dir, 0);
955         return true;
956       case ui::VKEY_RIGHT:
957         MoveSelected(0, forward_dir, 0);
958         return true;
959       case ui::VKEY_UP:
960         MoveSelected(0, 0, -1);
961         return true;
962       case ui::VKEY_DOWN:
963         MoveSelected(0, 0, 1);
964         return true;
965       case ui::VKEY_PRIOR: {
966         MoveSelected(-1, 0, 0);
967         return true;
968       }
969       case ui::VKEY_NEXT: {
970         MoveSelected(1, 0, 0);
971         return true;
972       }
973       default:
974         break;
975     }
976   }
977
978   return handled;
979 }
980
981 bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) {
982   bool handled = false;
983   if (selected_view_)
984     handled = selected_view_->OnKeyReleased(event);
985
986   return handled;
987 }
988
989 bool AppsGridView::OnMouseWheel(const ui::MouseWheelEvent& event) {
990   return pagination_controller_->OnScroll(
991       gfx::Vector2d(event.x_offset(), event.y_offset()));
992 }
993
994 void AppsGridView::ViewHierarchyChanged(
995     const ViewHierarchyChangedDetails& details) {
996   if (!details.is_add && details.parent == this) {
997     // The view being delete should not have reference in |view_model_|.
998     CHECK_EQ(-1, view_model_.GetIndexOfView(details.child));
999
1000     if (selected_view_ == details.child)
1001       selected_view_ = NULL;
1002     if (activated_folder_item_view_ == details.child)
1003       activated_folder_item_view_ = NULL;
1004
1005     if (drag_view_ == details.child)
1006       EndDrag(true);
1007
1008     bounds_animator_.StopAnimatingView(details.child);
1009   }
1010 }
1011
1012 void AppsGridView::OnGestureEvent(ui::GestureEvent* event) {
1013   if (pagination_controller_->OnGestureEvent(*event, GetContentsBounds()))
1014     event->SetHandled();
1015 }
1016
1017 void AppsGridView::OnScrollEvent(ui::ScrollEvent* event) {
1018   if (event->type() == ui::ET_SCROLL_FLING_CANCEL)
1019     return;
1020
1021   gfx::Vector2dF offset(event->x_offset(), event->y_offset());
1022   if (pagination_controller_->OnScroll(gfx::ToFlooredVector2d(offset))) {
1023     event->SetHandled();
1024     event->StopPropagation();
1025   }
1026 }
1027
1028 void AppsGridView::Update() {
1029   DCHECK(!selected_view_ && !drag_view_);
1030   view_model_.Clear();
1031   if (!item_list_ || !item_list_->item_count())
1032     return;
1033   for (size_t i = 0; i < item_list_->item_count(); ++i) {
1034     views::View* view = CreateViewForItemAtIndex(i);
1035     view_model_.Add(view, i);
1036     AddChildView(view);
1037   }
1038   UpdatePaging();
1039   UpdatePulsingBlockViews();
1040   Layout();
1041   SchedulePaint();
1042 }
1043
1044 void AppsGridView::UpdatePaging() {
1045   int total_page = view_model_.view_size() && tiles_per_page()
1046                        ? (view_model_.view_size() - 1) / tiles_per_page() + 1
1047                        : 0;
1048
1049   pagination_model_.SetTotalPages(total_page);
1050 }
1051
1052 void AppsGridView::UpdatePulsingBlockViews() {
1053   const int existing_items = item_list_ ? item_list_->item_count() : 0;
1054   const int available_slots =
1055       tiles_per_page() - existing_items % tiles_per_page();
1056   const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
1057       available_slots : 0;
1058
1059   if (pulsing_blocks_model_.view_size() == desired)
1060     return;
1061
1062   while (pulsing_blocks_model_.view_size() > desired) {
1063     views::View* view = pulsing_blocks_model_.view_at(0);
1064     pulsing_blocks_model_.Remove(0);
1065     delete view;
1066   }
1067
1068   while (pulsing_blocks_model_.view_size() < desired) {
1069     views::View* view = new PulsingBlockView(GetTotalTileSize(), true);
1070     pulsing_blocks_model_.Add(view, 0);
1071     AddChildView(view);
1072   }
1073 }
1074
1075 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) {
1076   // The drag_view_ might be pending for deletion, therefore view_model_
1077   // may have one more item than item_list_.
1078   DCHECK_LE(index, item_list_->item_count());
1079   AppListItemView* view = new AppListItemView(this,
1080                                               item_list_->item_at(index));
1081   view->SetPaintToLayer(true);
1082   view->SetFillsBoundsOpaquely(false);
1083   return view;
1084 }
1085
1086 AppsGridView::Index AppsGridView::GetIndexFromModelIndex(
1087     int model_index) const {
1088   return Index(model_index / tiles_per_page(), model_index % tiles_per_page());
1089 }
1090
1091 int AppsGridView::GetModelIndexFromIndex(const Index& index) const {
1092   return index.page * tiles_per_page() + index.slot;
1093 }
1094
1095 void AppsGridView::SetSelectedItemByIndex(const Index& index) {
1096   if (GetIndexOfView(selected_view_) == index)
1097     return;
1098
1099   views::View* new_selection = GetViewAtIndex(index);
1100   if (!new_selection)
1101     return;  // Keep current selection.
1102
1103   if (selected_view_)
1104     selected_view_->SchedulePaint();
1105
1106   EnsureViewVisible(new_selection);
1107   selected_view_ = new_selection;
1108   selected_view_->SchedulePaint();
1109   selected_view_->NotifyAccessibilityEvent(
1110       ui::AX_EVENT_FOCUS, true);
1111 }
1112
1113 bool AppsGridView::IsValidIndex(const Index& index) const {
1114   return index.page >= 0 && index.page < pagination_model_.total_pages() &&
1115          index.slot >= 0 && index.slot < tiles_per_page() &&
1116          GetModelIndexFromIndex(index) < view_model_.view_size();
1117 }
1118
1119 AppsGridView::Index AppsGridView::GetIndexOfView(
1120     const views::View* view) const {
1121   const int model_index = view_model_.GetIndexOfView(view);
1122   if (model_index == -1)
1123     return Index();
1124
1125   return GetIndexFromModelIndex(model_index);
1126 }
1127
1128 views::View* AppsGridView::GetViewAtIndex(const Index& index) const {
1129   if (!IsValidIndex(index))
1130     return NULL;
1131
1132   const int model_index = GetModelIndexFromIndex(index);
1133   return view_model_.view_at(model_index);
1134 }
1135
1136 AppsGridView::Index AppsGridView::GetLastViewIndex() const {
1137   DCHECK_LT(0, view_model_.view_size());
1138   int view_index = view_model_.view_size() - 1;
1139   return Index(view_index / tiles_per_page(), view_index % tiles_per_page());
1140 }
1141
1142 void AppsGridView::MoveSelected(int page_delta,
1143                                 int slot_x_delta,
1144                                 int slot_y_delta) {
1145   if (!selected_view_)
1146     return SetSelectedItemByIndex(Index(pagination_model_.selected_page(), 0));
1147
1148   const Index& selected = GetIndexOfView(selected_view_);
1149   int target_slot = selected.slot + slot_x_delta + slot_y_delta * cols_;
1150
1151   if (selected.slot % cols_ == 0 && slot_x_delta == -1) {
1152     if (selected.page > 0) {
1153       page_delta = -1;
1154       target_slot = selected.slot + cols_ - 1;
1155     } else {
1156       target_slot = selected.slot;
1157     }
1158   }
1159
1160   if (selected.slot % cols_ == cols_ - 1 && slot_x_delta == 1) {
1161     if (selected.page < pagination_model_.total_pages() - 1) {
1162       page_delta = 1;
1163       target_slot = selected.slot - cols_ + 1;
1164     } else {
1165       target_slot = selected.slot;
1166     }
1167   }
1168
1169   // Clamp the target slot to the last item if we are moving to the last page
1170   // but our target slot is past the end of the item list.
1171   if (page_delta &&
1172       selected.page + page_delta == pagination_model_.total_pages() - 1) {
1173     int last_item_slot = (view_model_.view_size() - 1) % tiles_per_page();
1174     if (last_item_slot < target_slot) {
1175       target_slot = last_item_slot;
1176     }
1177   }
1178
1179   int target_page = std::min(pagination_model_.total_pages() - 1,
1180                              std::max(selected.page + page_delta, 0));
1181   SetSelectedItemByIndex(Index(target_page, target_slot));
1182 }
1183
1184 void AppsGridView::CalculateIdealBounds() {
1185   // TODO(calamity): This fixes http://crbug.com/422604 on ChromeOS but it's
1186   // unclear why. This should be investigated to fix the issue on Linux Ash.
1187   if (GetContentsBounds().IsEmpty())
1188     return;
1189
1190   gfx::Size grid_size = GetTileGridSize();
1191
1192   // Page size including padding pixels. A tile.x + page_width means the same
1193   // tile slot in the next page; similarly for tile.y + page_height.
1194   const int page_width = grid_size.width() + kPagePadding;
1195   const int page_height = grid_size.height() + kPagePadding;
1196
1197   // If there is a transition, calculates offset for current and target page.
1198   const int current_page = pagination_model_.selected_page();
1199   const PaginationModel::Transition& transition =
1200       pagination_model_.transition();
1201   const bool is_valid = pagination_model_.is_valid_page(transition.target_page);
1202
1203   // Transition to previous page means negative offset.
1204   const int dir = transition.target_page > current_page ? -1 : 1;
1205
1206   const int total_views =
1207       view_model_.view_size() + pulsing_blocks_model_.view_size();
1208   int slot_index = 0;
1209   for (int i = 0; i < total_views; ++i) {
1210     if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_)
1211       continue;
1212
1213     Index view_index = GetIndexFromModelIndex(slot_index);
1214
1215     // Leaves a blank space in the grid for the current reorder placeholder.
1216     if (reorder_placeholder_ == view_index) {
1217       ++slot_index;
1218       view_index = GetIndexFromModelIndex(slot_index);
1219     }
1220
1221     // Decide the x or y offset for current item.
1222     int x_offset = 0;
1223     int y_offset = 0;
1224
1225     if (pagination_controller_->scroll_axis() ==
1226         PaginationController::SCROLL_AXIS_HORIZONTAL) {
1227       if (view_index.page < current_page)
1228         x_offset = -page_width;
1229       else if (view_index.page > current_page)
1230         x_offset = page_width;
1231
1232       if (is_valid) {
1233         if (view_index.page == current_page ||
1234             view_index.page == transition.target_page) {
1235           x_offset += transition.progress * page_width * dir;
1236         }
1237       }
1238     } else {
1239       if (view_index.page < current_page)
1240         y_offset = -page_height;
1241       else if (view_index.page > current_page)
1242         y_offset = page_height;
1243
1244       if (is_valid) {
1245         if (view_index.page == current_page ||
1246             view_index.page == transition.target_page) {
1247           y_offset += transition.progress * page_height * dir;
1248         }
1249       }
1250     }
1251
1252     const int row = view_index.slot / cols_;
1253     const int col = view_index.slot % cols_;
1254     gfx::Rect tile_slot = GetExpectedTileBounds(row, col);
1255     tile_slot.Offset(x_offset, y_offset);
1256     if (i < view_model_.view_size()) {
1257       view_model_.set_ideal_bounds(i, tile_slot);
1258     } else {
1259       pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
1260                                              tile_slot);
1261     }
1262
1263     ++slot_index;
1264   }
1265 }
1266
1267 void AppsGridView::AnimateToIdealBounds() {
1268   const gfx::Rect visible_bounds(GetVisibleBounds());
1269
1270   CalculateIdealBounds();
1271   for (int i = 0; i < view_model_.view_size(); ++i) {
1272     views::View* view = view_model_.view_at(i);
1273     if (view == drag_view_)
1274       continue;
1275
1276     const gfx::Rect& target = view_model_.ideal_bounds(i);
1277     if (bounds_animator_.GetTargetBounds(view) == target)
1278       continue;
1279
1280     const gfx::Rect& current = view->bounds();
1281     const bool current_visible = visible_bounds.Intersects(current);
1282     const bool target_visible = visible_bounds.Intersects(target);
1283     const bool visible = current_visible || target_visible;
1284
1285     const int y_diff = target.y() - current.y();
1286     if (visible && y_diff && y_diff % GetTotalTileSize().height() == 0) {
1287       AnimationBetweenRows(view,
1288                            current_visible,
1289                            current,
1290                            target_visible,
1291                            target);
1292     } else if (visible || bounds_animator_.IsAnimating(view)) {
1293       bounds_animator_.AnimateViewTo(view, target);
1294       bounds_animator_.SetAnimationDelegate(
1295           view,
1296           scoped_ptr<gfx::AnimationDelegate>(
1297               new ItemMoveAnimationDelegate(view)));
1298     } else {
1299       view->SetBoundsRect(target);
1300     }
1301   }
1302 }
1303
1304 void AppsGridView::AnimationBetweenRows(views::View* view,
1305                                         bool animate_current,
1306                                         const gfx::Rect& current,
1307                                         bool animate_target,
1308                                         const gfx::Rect& target) {
1309   // Determine page of |current| and |target|. -1 means in the left invisible
1310   // page, 0 is the center visible page and 1 means in the right invisible page.
1311   const int current_page = current.x() < 0 ? -1 :
1312       current.x() >= width() ? 1 : 0;
1313   const int target_page = target.x() < 0 ? -1 :
1314       target.x() >= width() ? 1 : 0;
1315
1316   const int dir = current_page < target_page ||
1317       (current_page == target_page && current.y() < target.y()) ? 1 : -1;
1318
1319   scoped_ptr<ui::Layer> layer;
1320   if (animate_current) {
1321     layer = view->RecreateLayer();
1322     layer->SuppressPaint();
1323
1324     view->SetFillsBoundsOpaquely(false);
1325     view->layer()->SetOpacity(0.f);
1326   }
1327
1328   gfx::Size total_tile_size = GetTotalTileSize();
1329   gfx::Rect current_out(current);
1330   current_out.Offset(dir * total_tile_size.width(), 0);
1331
1332   gfx::Rect target_in(target);
1333   if (animate_target)
1334     target_in.Offset(-dir * total_tile_size.width(), 0);
1335   view->SetBoundsRect(target_in);
1336   bounds_animator_.AnimateViewTo(view, target);
1337
1338   bounds_animator_.SetAnimationDelegate(
1339       view,
1340       scoped_ptr<gfx::AnimationDelegate>(
1341           new RowMoveAnimationDelegate(view, layer.release(), current_out)));
1342 }
1343
1344 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event,
1345                                        gfx::Point* drag_point) {
1346 #if defined(USE_AURA) && !defined(OS_WIN)
1347   // Use root location of |event| instead of location in |drag_view_|'s
1348   // coordinates because |drag_view_| has a scale transform and location
1349   // could have integer round error and causes jitter.
1350   *drag_point = event.root_location();
1351
1352   // GetWidget() could be NULL for tests.
1353   if (GetWidget()) {
1354     aura::Window::ConvertPointToTarget(
1355         GetWidget()->GetNativeWindow()->GetRootWindow(),
1356         GetWidget()->GetNativeWindow(),
1357         drag_point);
1358   }
1359
1360   views::View::ConvertPointFromWidget(this, drag_point);
1361 #else
1362   // For non-aura, root location is not clearly defined but |drag_view_| does
1363   // not have the scale transform. So no round error would be introduced and
1364   // it's okay to use View::ConvertPointToTarget.
1365   *drag_point = event.location();
1366   views::View::ConvertPointToTarget(drag_view_, this, drag_point);
1367 #endif
1368 }
1369
1370 void AppsGridView::CalculateDropTarget() {
1371   DCHECK(drag_view_);
1372
1373   gfx::Point point = drag_view_->icon()->bounds().CenterPoint();
1374   views::View::ConvertPointToTarget(drag_view_, this, &point);
1375   if (!IsPointWithinDragBuffer(point)) {
1376     // Reset the reorder target to the original position if the cursor is
1377     // outside the drag buffer.
1378     if (IsDraggingForReparentInRootLevelGridView()) {
1379       drop_attempt_ = DROP_FOR_NONE;
1380       return;
1381     }
1382
1383     reorder_drop_target_ = drag_view_init_index_;
1384     drop_attempt_ = DROP_FOR_REORDER;
1385     return;
1386   }
1387
1388   if (EnableFolderDragDropUI() &&
1389       CalculateFolderDropTarget(point, &folder_drop_target_)) {
1390     drop_attempt_ = DROP_FOR_FOLDER;
1391     return;
1392   }
1393
1394   drop_attempt_ = DROP_FOR_REORDER;
1395   CalculateReorderDropTarget(point, &reorder_drop_target_);
1396 }
1397
1398 bool AppsGridView::CalculateFolderDropTarget(const gfx::Point& point,
1399                                              Index* drop_target) const {
1400   Index nearest_tile_index(GetNearestTileIndexForPoint(point));
1401   int distance_to_tile_center =
1402       (point - GetExpectedTileBounds(nearest_tile_index.slot).CenterPoint())
1403           .Length();
1404   if (nearest_tile_index != reorder_placeholder_ &&
1405       distance_to_tile_center < kFolderDroppingCircleRadius &&
1406       !IsFolderItem(drag_view_->item()) &&
1407       CanDropIntoTarget(nearest_tile_index)) {
1408     *drop_target = nearest_tile_index;
1409     DCHECK(IsValidIndex(*drop_target));
1410     return true;
1411   }
1412
1413   return false;
1414 }
1415
1416 void AppsGridView::CalculateReorderDropTarget(const gfx::Point& point,
1417                                               Index* drop_target) const {
1418   gfx::Rect bounds = GetContentsBounds();
1419   Index grid_index = GetNearestTileIndexForPoint(point);
1420   gfx::Point reorder_placeholder_center =
1421       GetExpectedTileBounds(reorder_placeholder_.slot).CenterPoint();
1422
1423   int x_offset_direction = 0;
1424   if (grid_index == reorder_placeholder_) {
1425     x_offset_direction = reorder_placeholder_center.x() < point.x() ? -1 : 1;
1426   } else {
1427     x_offset_direction = reorder_placeholder_ < grid_index ? -1 : 1;
1428   }
1429
1430   gfx::Size total_tile_size = GetTotalTileSize();
1431   int row = grid_index.slot / cols_;
1432
1433   // Offset the target column based on the direction of the target. This will
1434   // result in earlier targets getting their reorder zone shifted backwards
1435   // and later targets getting their reorder zones shifted forwards.
1436   //
1437   // This makes eordering feel like the user is slotting items into the spaces
1438   // between apps.
1439   int x_offset = x_offset_direction *
1440                  (total_tile_size.width() - kFolderDroppingCircleRadius) / 2;
1441   int col = (point.x() - bounds.x() + x_offset) / total_tile_size.width();
1442   col = ClampToRange(col, 0, cols_ - 1);
1443   *drop_target =
1444       std::min(Index(pagination_model_.selected_page(), row * cols_ + col),
1445                GetLastViewIndex());
1446   DCHECK(IsValidIndex(*drop_target));
1447 }
1448
1449 void AppsGridView::OnReorderTimer() {
1450   if (drop_attempt_ == DROP_FOR_REORDER) {
1451     reorder_placeholder_ = reorder_drop_target_;
1452     AnimateToIdealBounds();
1453   }
1454 }
1455
1456 void AppsGridView::OnFolderItemReparentTimer() {
1457   DCHECK(folder_delegate_);
1458   if (drag_out_of_folder_container_ && drag_view_) {
1459     folder_delegate_->ReparentItem(drag_view_, last_drag_point_);
1460
1461     // Set the flag in the folder's grid view.
1462     dragging_for_reparent_item_ = true;
1463
1464     // Do not observe any data change since it is going to be hidden.
1465     item_list_->RemoveObserver(this);
1466     item_list_ = NULL;
1467   }
1468 }
1469
1470 void AppsGridView::OnFolderDroppingTimer() {
1471   if (drop_attempt_ == DROP_FOR_FOLDER)
1472     SetAsFolderDroppingTarget(folder_drop_target_, true);
1473 }
1474
1475 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer,
1476                                                const gfx::Point& drag_point) {
1477   if (IsUnderOEMFolder())
1478     return;
1479
1480   if (IsDraggingForReparentInHiddenGridView()) {
1481     // Dispatch drag event to root level grid view for re-parenting folder
1482     // folder item purpose.
1483     DispatchDragEventForReparent(pointer, drag_point);
1484     return;
1485   }
1486
1487   // Regular drag and drop in a folder's grid view.
1488   folder_delegate_->UpdateFolderViewBackground(true);
1489
1490   // Calculate if the drag_view_ is dragged out of the folder's container
1491   // ink bubble.
1492   gfx::Rect bounds_to_folder_view = ConvertRectToParent(drag_view_->bounds());
1493   gfx::Point pt = bounds_to_folder_view.CenterPoint();
1494   bool is_item_dragged_out_of_folder =
1495       folder_delegate_->IsPointOutsideOfFolderBoundary(pt);
1496   if (is_item_dragged_out_of_folder) {
1497     if (!drag_out_of_folder_container_) {
1498       folder_item_reparent_timer_.Start(
1499           FROM_HERE,
1500           base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay),
1501           this,
1502           &AppsGridView::OnFolderItemReparentTimer);
1503       drag_out_of_folder_container_ = true;
1504     }
1505   } else {
1506     folder_item_reparent_timer_.Stop();
1507     drag_out_of_folder_container_ = false;
1508   }
1509 }
1510
1511 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1512   return (!folder_delegate_ && dragging_for_reparent_item_);
1513 }
1514
1515 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1516   return (folder_delegate_ && dragging_for_reparent_item_);
1517 }
1518
1519 gfx::Rect AppsGridView::GetTargetIconRectInFolder(
1520     AppListItemView* drag_item_view,
1521     AppListItemView* folder_item_view) {
1522   gfx::Rect view_ideal_bounds = view_model_.ideal_bounds(
1523       view_model_.GetIndexOfView(folder_item_view));
1524   gfx::Rect icon_ideal_bounds =
1525       folder_item_view->GetIconBoundsForTargetViewBounds(view_ideal_bounds);
1526   AppListFolderItem* folder_item =
1527       static_cast<AppListFolderItem*>(folder_item_view->item());
1528   return folder_item->GetTargetIconRectInFolderForItem(
1529       drag_item_view->item(), icon_ideal_bounds);
1530 }
1531
1532 bool AppsGridView::IsUnderOEMFolder() {
1533   if (!folder_delegate_)
1534     return false;
1535
1536   return folder_delegate_->IsOEMFolder();
1537 }
1538
1539 void AppsGridView::DispatchDragEventForReparent(Pointer pointer,
1540                                                 const gfx::Point& drag_point) {
1541   folder_delegate_->DispatchDragEventForReparent(pointer, drag_point);
1542 }
1543
1544 void AppsGridView::EndDragFromReparentItemInRootLevel(
1545     bool events_forwarded_to_drag_drop_host,
1546     bool cancel_drag) {
1547   // EndDrag was called before if |drag_view_| is NULL.
1548   if (!drag_view_)
1549     return;
1550
1551   DCHECK(IsDraggingForReparentInRootLevelGridView());
1552   bool cancel_reparent = cancel_drag || drop_attempt_ == DROP_FOR_NONE;
1553   if (!events_forwarded_to_drag_drop_host && !cancel_reparent) {
1554     CalculateDropTarget();
1555     if (drop_attempt_ == DROP_FOR_REORDER &&
1556         IsValidIndex(reorder_drop_target_)) {
1557       ReparentItemForReorder(drag_view_, reorder_drop_target_);
1558     } else if (drop_attempt_ == DROP_FOR_FOLDER &&
1559                IsValidIndex(folder_drop_target_)) {
1560       ReparentItemToAnotherFolder(drag_view_, folder_drop_target_);
1561     } else {
1562       NOTREACHED();
1563     }
1564     SetViewHidden(drag_view_, false /* show */, true /* no animate */);
1565   }
1566
1567   // The drag can be ended after the synchronous drag is created but before it
1568   // is Run().
1569   CleanUpSynchronousDrag();
1570
1571   SetAsFolderDroppingTarget(folder_drop_target_, false);
1572   if (cancel_reparent) {
1573     CancelFolderItemReparent(drag_view_);
1574   } else {
1575     // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1576     // cleaning up the newly created AppListItemView, effectively claiming
1577     // ownership of the newly created drag view.
1578     drag_view_->OnDragEnded();
1579     drag_view_ = NULL;
1580   }
1581   ClearDragState();
1582   AnimateToIdealBounds();
1583
1584   StopPageFlipTimer();
1585 }
1586
1587 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1588   if (drag_and_drop_host_) {
1589     // If we had a drag and drop proxy icon, we delete it and make the real
1590     // item visible again.
1591     drag_and_drop_host_->DestroyDragIconProxy();
1592   }
1593
1594   // The drag can be ended after the synchronous drag is created but before it
1595   // is Run().
1596   CleanUpSynchronousDrag();
1597
1598   SetAsFolderDroppingTarget(folder_drop_target_, false);
1599   ClearDragState();
1600 }
1601
1602 void AppsGridView::OnFolderItemRemoved() {
1603   DCHECK(folder_delegate_);
1604   item_list_ = NULL;
1605 }
1606
1607 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) {
1608   // When a drag and drop host is given, the item can be dragged out of the app
1609   // list window. In that case a proxy widget needs to be used.
1610   // Note: This code has very likely to be changed for Windows (non metro mode)
1611   // when a |drag_and_drop_host_| gets implemented.
1612   if (!drag_view_ || !drag_and_drop_host_)
1613     return;
1614
1615   gfx::Point screen_location = grid_location;
1616   views::View::ConvertPointToScreen(this, &screen_location);
1617
1618   // Determine the mouse offset to the center of the icon so that the drag and
1619   // drop host follows this layer.
1620   gfx::Vector2d delta = drag_view_offset_ -
1621                         drag_view_->GetLocalBounds().CenterPoint();
1622   delta.set_y(delta.y() + drag_view_->title()->size().height() / 2);
1623
1624   // We have to hide the original item since the drag and drop host will do
1625   // the OS dependent code to "lift off the dragged item".
1626   DCHECK(!IsDraggingForReparentInRootLevelGridView());
1627   drag_and_drop_host_->CreateDragIconProxy(screen_location,
1628                                            drag_view_->item()->icon(),
1629                                            drag_view_,
1630                                            delta,
1631                                            kDragAndDropProxyScale);
1632   SetViewHidden(drag_view_,
1633            true /* hide */,
1634            true /* no animation */);
1635 }
1636
1637 void AppsGridView::DispatchDragEventToDragAndDropHost(
1638     const gfx::Point& location_in_screen_coordinates) {
1639   if (!drag_view_ || !drag_and_drop_host_)
1640     return;
1641
1642   if (GetLocalBounds().Contains(last_drag_point_)) {
1643     // The event was issued inside the app menu and we should get all events.
1644     if (forward_events_to_drag_and_drop_host_) {
1645       // The DnD host was previously called and needs to be informed that the
1646       // session returns to the owner.
1647       forward_events_to_drag_and_drop_host_ = false;
1648       drag_and_drop_host_->EndDrag(true);
1649     }
1650   } else {
1651     if (IsFolderItem(drag_view_->item()))
1652       return;
1653
1654     // The event happened outside our app menu and we might need to dispatch.
1655     if (forward_events_to_drag_and_drop_host_) {
1656       // Dispatch since we have already started.
1657       if (!drag_and_drop_host_->Drag(location_in_screen_coordinates)) {
1658         // The host is not active any longer and we cancel the operation.
1659         forward_events_to_drag_and_drop_host_ = false;
1660         drag_and_drop_host_->EndDrag(true);
1661       }
1662     } else {
1663       if (drag_and_drop_host_->StartDrag(drag_view_->item()->id(),
1664                                          location_in_screen_coordinates)) {
1665         // From now on we forward the drag events.
1666         forward_events_to_drag_and_drop_host_ = true;
1667         // Any flip operations are stopped.
1668         StopPageFlipTimer();
1669       }
1670     }
1671   }
1672 }
1673
1674 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) {
1675   if (!IsPointWithinDragBuffer(drag_point))
1676     StopPageFlipTimer();
1677   int new_page_flip_target = -1;
1678
1679   // Drag zones are at the edges of the scroll axis.
1680   if (pagination_controller_->scroll_axis() ==
1681       PaginationController::SCROLL_AXIS_VERTICAL) {
1682     if (drag_point.y() < kPageFlipZoneSize)
1683       new_page_flip_target = pagination_model_.selected_page() - 1;
1684     else if (drag_point.y() > height() - kPageFlipZoneSize)
1685       new_page_flip_target = pagination_model_.selected_page() + 1;
1686   } else {
1687     if (page_switcher_view_->bounds().Contains(drag_point)) {
1688       gfx::Point page_switcher_point(drag_point);
1689       views::View::ConvertPointToTarget(
1690           this, page_switcher_view_, &page_switcher_point);
1691       new_page_flip_target =
1692           page_switcher_view_->GetPageForPoint(page_switcher_point);
1693     }
1694
1695     // TODO(xiyuan): Fix this for RTL.
1696     if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize)
1697       new_page_flip_target = pagination_model_.selected_page() - 1;
1698
1699     if (new_page_flip_target == -1 &&
1700         drag_point.x() > width() - kPageFlipZoneSize) {
1701       new_page_flip_target = pagination_model_.selected_page() + 1;
1702     }
1703   }
1704
1705   if (new_page_flip_target == page_flip_target_)
1706     return;
1707
1708   StopPageFlipTimer();
1709   if (pagination_model_.is_valid_page(new_page_flip_target)) {
1710     page_flip_target_ = new_page_flip_target;
1711
1712     if (page_flip_target_ != pagination_model_.selected_page()) {
1713       page_flip_timer_.Start(FROM_HERE,
1714           base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_),
1715           this, &AppsGridView::OnPageFlipTimer);
1716     }
1717   }
1718 }
1719
1720 void AppsGridView::OnPageFlipTimer() {
1721   DCHECK(pagination_model_.is_valid_page(page_flip_target_));
1722   pagination_model_.SelectPage(page_flip_target_, true);
1723 }
1724
1725 void AppsGridView::MoveItemInModel(views::View* item_view,
1726                                    const Index& target) {
1727   int current_model_index = view_model_.GetIndexOfView(item_view);
1728   DCHECK_GE(current_model_index, 0);
1729
1730   int target_model_index = GetModelIndexFromIndex(target);
1731   if (target_model_index == current_model_index)
1732     return;
1733
1734   item_list_->RemoveObserver(this);
1735   item_list_->MoveItem(current_model_index, target_model_index);
1736   view_model_.Move(current_model_index, target_model_index);
1737   item_list_->AddObserver(this);
1738
1739   if (pagination_model_.selected_page() != target.page)
1740     pagination_model_.SelectPage(target.page, false);
1741 }
1742
1743 void AppsGridView::MoveItemToFolder(views::View* item_view,
1744                                     const Index& target) {
1745   const std::string& source_item_id =
1746       static_cast<AppListItemView*>(item_view)->item()->id();
1747   AppListItemView* target_view =
1748       static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1749   const std::string&  target_view_item_id = target_view->item()->id();
1750
1751   // Make change to data model.
1752   item_list_->RemoveObserver(this);
1753   std::string folder_item_id =
1754       model_->MergeItems(target_view_item_id, source_item_id);
1755   item_list_->AddObserver(this);
1756   if (folder_item_id.empty()) {
1757     LOG(ERROR) << "Unable to merge into item id: " << target_view_item_id;
1758     return;
1759   }
1760   if (folder_item_id != target_view_item_id) {
1761     // New folder was created, change the view model to replace the old target
1762     // view with the new folder item view.
1763     size_t folder_item_index;
1764     if (item_list_->FindItemIndex(folder_item_id, &folder_item_index)) {
1765       int target_view_index = view_model_.GetIndexOfView(target_view);
1766       gfx::Rect target_view_bounds = target_view->bounds();
1767       DeleteItemViewAtIndex(target_view_index);
1768       views::View* target_folder_view =
1769           CreateViewForItemAtIndex(folder_item_index);
1770       target_folder_view->SetBoundsRect(target_view_bounds);
1771       view_model_.Add(target_folder_view, target_view_index);
1772       AddChildView(target_folder_view);
1773     } else {
1774       LOG(ERROR) << "Folder no longer in item_list: " << folder_item_id;
1775     }
1776   }
1777
1778   // Fade out the drag_view_ and delete it when animation ends.
1779   int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1780   view_model_.Remove(drag_view_index);
1781   bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1782   bounds_animator_.SetAnimationDelegate(
1783       drag_view_,
1784       scoped_ptr<gfx::AnimationDelegate>(
1785           new ItemRemoveAnimationDelegate(drag_view_)));
1786   UpdatePaging();
1787 }
1788
1789 void AppsGridView::ReparentItemForReorder(views::View* item_view,
1790                                           const Index& target) {
1791   item_list_->RemoveObserver(this);
1792   model_->RemoveObserver(this);
1793
1794   AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1795   DCHECK(reparent_item->IsInFolder());
1796   const std::string source_folder_id = reparent_item->folder_id();
1797   AppListFolderItem* source_folder =
1798       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1799
1800   int target_model_index = GetModelIndexFromIndex(target);
1801
1802   // Remove the source folder view if there is only 1 item in it, since the
1803   // source folder will be deleted after its only child item removed from it.
1804   if (source_folder->ChildItemCount() == 1u) {
1805     const int deleted_folder_index =
1806         view_model_.GetIndexOfView(activated_folder_item_view());
1807     DeleteItemViewAtIndex(deleted_folder_index);
1808
1809     // Adjust |target_model_index| if it is beyond the deleted folder index.
1810     if (target_model_index > deleted_folder_index)
1811       --target_model_index;
1812   }
1813
1814   // Move the item from its parent folder to top level item list.
1815   // Must move to target_model_index, the location we expect the target item
1816   // to be, not the item location we want to insert before.
1817   int current_model_index = view_model_.GetIndexOfView(item_view);
1818   syncer::StringOrdinal target_position;
1819   if (target_model_index < static_cast<int>(item_list_->item_count()))
1820     target_position = item_list_->item_at(target_model_index)->position();
1821   model_->MoveItemToFolderAt(reparent_item, "", target_position);
1822   view_model_.Move(current_model_index, target_model_index);
1823
1824   RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1825
1826   item_list_->AddObserver(this);
1827   model_->AddObserver(this);
1828   UpdatePaging();
1829 }
1830
1831 void AppsGridView::ReparentItemToAnotherFolder(views::View* item_view,
1832                                                const Index& target) {
1833   DCHECK(IsDraggingForReparentInRootLevelGridView());
1834
1835   AppListItemView* target_view =
1836       static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1837   if (!target_view)
1838     return;
1839
1840   // Make change to data model.
1841   item_list_->RemoveObserver(this);
1842
1843   AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1844   DCHECK(reparent_item->IsInFolder());
1845   const std::string source_folder_id = reparent_item->folder_id();
1846   AppListFolderItem* source_folder =
1847       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1848
1849   // Remove the source folder view if there is only 1 item in it, since the
1850   // source folder will be deleted after its only child item merged into the
1851   // target item.
1852   if (source_folder->ChildItemCount() == 1u)
1853     DeleteItemViewAtIndex(
1854         view_model_.GetIndexOfView(activated_folder_item_view()));
1855
1856   AppListItem* target_item = target_view->item();
1857
1858   // Move item to the target folder.
1859   std::string target_id_after_merge =
1860       model_->MergeItems(target_item->id(), reparent_item->id());
1861   if (target_id_after_merge.empty()) {
1862     LOG(ERROR) << "Unable to reparent to item id: " << target_item->id();
1863     item_list_->AddObserver(this);
1864     return;
1865   }
1866
1867   if (target_id_after_merge != target_item->id()) {
1868     // New folder was created, change the view model to replace the old target
1869     // view with the new folder item view.
1870     const std::string& new_folder_id = reparent_item->folder_id();
1871     size_t new_folder_index;
1872     if (item_list_->FindItemIndex(new_folder_id, &new_folder_index)) {
1873       int target_view_index = view_model_.GetIndexOfView(target_view);
1874       DeleteItemViewAtIndex(target_view_index);
1875       views::View* new_folder_view =
1876           CreateViewForItemAtIndex(new_folder_index);
1877       view_model_.Add(new_folder_view, target_view_index);
1878       AddChildView(new_folder_view);
1879     } else {
1880       LOG(ERROR) << "Folder no longer in item_list: " << new_folder_id;
1881     }
1882   }
1883
1884   RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1885
1886   item_list_->AddObserver(this);
1887
1888   // Fade out the drag_view_ and delete it when animation ends.
1889   int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1890   view_model_.Remove(drag_view_index);
1891   bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1892   bounds_animator_.SetAnimationDelegate(
1893       drag_view_,
1894       scoped_ptr<gfx::AnimationDelegate>(
1895           new ItemRemoveAnimationDelegate(drag_view_)));
1896   UpdatePaging();
1897 }
1898
1899 // After moving the re-parenting item out of the folder, if there is only 1 item
1900 // left, remove the last item out of the folder, delete the folder and insert it
1901 // to the data model at the same position. Make the same change to view_model_
1902 // accordingly.
1903 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1904     const std::string& source_folder_id) {
1905   AppListFolderItem* source_folder =
1906       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1907   if (!source_folder || source_folder->ChildItemCount() != 1u)
1908     return;
1909
1910   // Delete view associated with the folder item to be removed.
1911   DeleteItemViewAtIndex(
1912       view_model_.GetIndexOfView(activated_folder_item_view()));
1913
1914   // Now make the data change to remove the folder item in model.
1915   AppListItem* last_item = source_folder->item_list()->item_at(0);
1916   model_->MoveItemToFolderAt(last_item, "", source_folder->position());
1917
1918   // Create a new item view for the last item in folder.
1919   size_t last_item_index;
1920   if (!item_list_->FindItemIndex(last_item->id(), &last_item_index) ||
1921       last_item_index > static_cast<size_t>(view_model_.view_size())) {
1922     NOTREACHED();
1923     return;
1924   }
1925   views::View* last_item_view = CreateViewForItemAtIndex(last_item_index);
1926   view_model_.Add(last_item_view, last_item_index);
1927   AddChildView(last_item_view);
1928 }
1929
1930 void AppsGridView::CancelFolderItemReparent(AppListItemView* drag_item_view) {
1931   // The icon of the dragged item must target to its final ideal bounds after
1932   // the animation completes.
1933   CalculateIdealBounds();
1934
1935   gfx::Rect target_icon_rect =
1936       GetTargetIconRectInFolder(drag_item_view, activated_folder_item_view_);
1937
1938   gfx::Rect drag_view_icon_to_grid =
1939       drag_item_view->ConvertRectToParent(drag_item_view->GetIconBounds());
1940   drag_view_icon_to_grid.ClampToCenteredSize(
1941       gfx::Size(kGridIconDimension, kGridIconDimension));
1942   TopIconAnimationView* icon_view = new TopIconAnimationView(
1943       drag_item_view->item()->icon(),
1944       target_icon_rect,
1945       false);    /* animate like closing folder */
1946   AddChildView(icon_view);
1947   icon_view->SetBoundsRect(drag_view_icon_to_grid);
1948   icon_view->TransformView();
1949 }
1950
1951 void AppsGridView::CancelContextMenusOnCurrentPage() {
1952   int start = pagination_model_.selected_page() * tiles_per_page();
1953   int end = std::min(view_model_.view_size(), start + tiles_per_page());
1954   for (int i = start; i < end; ++i) {
1955     AppListItemView* view =
1956         static_cast<AppListItemView*>(view_model_.view_at(i));
1957     view->CancelContextMenu();
1958   }
1959 }
1960
1961 void AppsGridView::DeleteItemViewAtIndex(int index) {
1962   views::View* item_view = view_model_.view_at(index);
1963   view_model_.Remove(index);
1964   if (item_view == drag_view_)
1965     drag_view_ = NULL;
1966   delete item_view;
1967 }
1968
1969 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const {
1970   gfx::Rect rect(GetLocalBounds());
1971   rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx);
1972   return rect.Contains(point);
1973 }
1974
1975 void AppsGridView::ButtonPressed(views::Button* sender,
1976                                  const ui::Event& event) {
1977   if (dragging())
1978     return;
1979
1980   if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName))
1981     return;
1982
1983   if (delegate_) {
1984     // Always set the previous activated_folder_item_view_ to be visible. This
1985     // prevents a case where the item would remain hidden due the
1986     // |activated_folder_item_view_| changing during the animation. We only
1987     // need to track |activated_folder_item_view_| in the root level grid view.
1988     if (!folder_delegate_) {
1989       if (activated_folder_item_view_)
1990         activated_folder_item_view_->SetVisible(true);
1991       AppListItemView* pressed_item_view =
1992           static_cast<AppListItemView*>(sender);
1993       if (IsFolderItem(pressed_item_view->item()))
1994         activated_folder_item_view_ = pressed_item_view;
1995       else
1996         activated_folder_item_view_ = NULL;
1997     }
1998     delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->item(),
1999                            event.flags());
2000   }
2001 }
2002
2003 void AppsGridView::OnListItemAdded(size_t index, AppListItem* item) {
2004   EndDrag(true);
2005
2006   views::View* view = CreateViewForItemAtIndex(index);
2007   view_model_.Add(view, index);
2008   AddChildView(view);
2009
2010   UpdatePaging();
2011   UpdatePulsingBlockViews();
2012   Layout();
2013   SchedulePaint();
2014 }
2015
2016 void AppsGridView::OnListItemRemoved(size_t index, AppListItem* item) {
2017   EndDrag(true);
2018
2019   DeleteItemViewAtIndex(index);
2020
2021   UpdatePaging();
2022   UpdatePulsingBlockViews();
2023   Layout();
2024   SchedulePaint();
2025 }
2026
2027 void AppsGridView::OnListItemMoved(size_t from_index,
2028                                    size_t to_index,
2029                                    AppListItem* item) {
2030   EndDrag(true);
2031   view_model_.Move(from_index, to_index);
2032
2033   UpdatePaging();
2034   AnimateToIdealBounds();
2035 }
2036
2037 void AppsGridView::TotalPagesChanged() {
2038 }
2039
2040 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
2041   if (dragging()) {
2042     CalculateDropTarget();
2043     Layout();
2044     MaybeStartPageFlipTimer(last_drag_point_);
2045   } else {
2046     ClearSelectedView(selected_view_);
2047     Layout();
2048   }
2049 }
2050
2051 void AppsGridView::TransitionStarted() {
2052   CancelContextMenusOnCurrentPage();
2053 }
2054
2055 void AppsGridView::TransitionChanged() {
2056   // Update layout for valid page transition only since over-scroll no longer
2057   // animates app icons.
2058   const PaginationModel::Transition& transition =
2059       pagination_model_.transition();
2060   if (pagination_model_.is_valid_page(transition.target_page))
2061     Layout();
2062 }
2063
2064 void AppsGridView::OnAppListModelStatusChanged() {
2065   UpdatePulsingBlockViews();
2066   Layout();
2067   SchedulePaint();
2068 }
2069
2070 void AppsGridView::SetViewHidden(views::View* view, bool hide, bool immediate) {
2071   ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
2072   animator.SetPreemptionStrategy(
2073       immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
2074                   ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
2075   view->layer()->SetOpacity(hide ? 0 : 1);
2076 }
2077
2078 void AppsGridView::OnImplicitAnimationsCompleted() {
2079   if (layer()->opacity() == 0.0f)
2080     SetVisible(false);
2081 }
2082
2083 bool AppsGridView::EnableFolderDragDropUI() {
2084   // Enable drag and drop folder UI only if it is at the app list root level
2085   // and the switch is on.
2086   return model_->folders_enabled() && !folder_delegate_;
2087 }
2088
2089 bool AppsGridView::CanDropIntoTarget(const Index& drop_target) const {
2090   views::View* target_view = GetViewAtSlotOnCurrentPage(drop_target.slot);
2091
2092   if (!target_view)
2093     return false;
2094
2095   AppListItem* target_item =
2096       static_cast<AppListItemView*>(target_view)->item();
2097   // Items can be dropped into non-folders (which have no children) or folders
2098   // that have fewer than the max allowed items.
2099   // OEM folder does not allow to drag/drop other items in it.
2100   return target_item->ChildItemCount() < kMaxFolderItems &&
2101          !IsOEMFolderItem(target_item);
2102 }
2103
2104 AppsGridView::Index AppsGridView::GetNearestTileIndexForPoint(
2105     const gfx::Point& point) const {
2106   gfx::Rect bounds = GetContentsBounds();
2107   gfx::Size total_tile_size = GetTotalTileSize();
2108   int col = ClampToRange(
2109       (point.x() - bounds.x()) / total_tile_size.width(), 0, cols_ - 1);
2110   int row = ClampToRange((point.y() - bounds.y()) / total_tile_size.height(),
2111                          0,
2112                          rows_per_page_ - 1);
2113   return Index(pagination_model_.selected_page(), row * cols_ + col);
2114 }
2115
2116 gfx::Size AppsGridView::GetTileGridSize() const {
2117   gfx::Rect bounds = GetExpectedTileBounds(0, 0);
2118   bounds.Union(GetExpectedTileBounds(rows_per_page_ - 1, cols_ - 1));
2119   if (switches::IsExperimentalAppListEnabled())
2120     bounds.Inset(-kExperimentalTileLeftRightPadding,
2121                  -kExperimentalTileTopBottomPadding);
2122   return bounds.size();
2123 }
2124
2125 gfx::Rect AppsGridView::GetExpectedTileBounds(int slot) const {
2126   return GetExpectedTileBounds(slot / cols_, slot % cols_);
2127 }
2128
2129 gfx::Rect AppsGridView::GetExpectedTileBounds(int row, int col) const {
2130   gfx::Rect bounds(GetContentsBounds());
2131   gfx::Size total_tile_size = GetTotalTileSize();
2132   gfx::Rect tile_bounds(gfx::Point(bounds.x() + col * total_tile_size.width(),
2133                                    bounds.y() + row * total_tile_size.height()),
2134                         total_tile_size);
2135   tile_bounds.ClampToCenteredSize(GetTileViewSize());
2136   return tile_bounds;
2137 }
2138
2139 views::View* AppsGridView::GetViewAtSlotOnCurrentPage(int slot) const {
2140   if (slot < 0)
2141     return NULL;
2142
2143   // Calculate the original bound of the tile at |index|.
2144   int row = slot / cols_;
2145   int col = slot % cols_;
2146   gfx::Rect tile_rect = GetExpectedTileBounds(row, col);
2147
2148   for (int i = 0; i < view_model_.view_size(); ++i) {
2149     views::View* view = view_model_.view_at(i);
2150     if (view->bounds() == tile_rect && view != drag_view_)
2151       return view;
2152   }
2153   return NULL;
2154 }
2155
2156 void AppsGridView::SetAsFolderDroppingTarget(const Index& target_index,
2157                                              bool is_target_folder) {
2158   AppListItemView* target_view =
2159       static_cast<AppListItemView*>(
2160           GetViewAtSlotOnCurrentPage(target_index.slot));
2161   if (target_view)
2162     target_view->SetAsAttemptedFolderTarget(is_target_folder);
2163 }
2164
2165 }  // namespace app_list