f9305d53173677b0668025368029247f54d22a54
[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   gfx::Size grid_size = GetTileGridSize();
1186
1187   // Page size including padding pixels. A tile.x + page_width means the same
1188   // tile slot in the next page; similarly for tile.y + page_height.
1189   const int page_width = grid_size.width() + kPagePadding;
1190   const int page_height = grid_size.height() + kPagePadding;
1191
1192   // If there is a transition, calculates offset for current and target page.
1193   const int current_page = pagination_model_.selected_page();
1194   const PaginationModel::Transition& transition =
1195       pagination_model_.transition();
1196   const bool is_valid = pagination_model_.is_valid_page(transition.target_page);
1197
1198   // Transition to previous page means negative offset.
1199   const int dir = transition.target_page > current_page ? -1 : 1;
1200
1201   const int total_views =
1202       view_model_.view_size() + pulsing_blocks_model_.view_size();
1203   int slot_index = 0;
1204   for (int i = 0; i < total_views; ++i) {
1205     if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_)
1206       continue;
1207
1208     Index view_index = GetIndexFromModelIndex(slot_index);
1209
1210     // Leaves a blank space in the grid for the current reorder placeholder.
1211     if (reorder_placeholder_ == view_index) {
1212       ++slot_index;
1213       view_index = GetIndexFromModelIndex(slot_index);
1214     }
1215
1216     // Decide the x or y offset for current item.
1217     int x_offset = 0;
1218     int y_offset = 0;
1219
1220     if (pagination_controller_->scroll_axis() ==
1221         PaginationController::SCROLL_AXIS_HORIZONTAL) {
1222       if (view_index.page < current_page)
1223         x_offset = -page_width;
1224       else if (view_index.page > current_page)
1225         x_offset = page_width;
1226
1227       if (is_valid) {
1228         if (view_index.page == current_page ||
1229             view_index.page == transition.target_page) {
1230           x_offset += transition.progress * page_width * dir;
1231         }
1232       }
1233     } else {
1234       if (view_index.page < current_page)
1235         y_offset = -page_height;
1236       else if (view_index.page > current_page)
1237         y_offset = page_height;
1238
1239       if (is_valid) {
1240         if (view_index.page == current_page ||
1241             view_index.page == transition.target_page) {
1242           y_offset += transition.progress * page_height * dir;
1243         }
1244       }
1245     }
1246
1247     const int row = view_index.slot / cols_;
1248     const int col = view_index.slot % cols_;
1249     gfx::Rect tile_slot = GetExpectedTileBounds(row, col);
1250     tile_slot.Offset(x_offset, y_offset);
1251     if (i < view_model_.view_size()) {
1252       view_model_.set_ideal_bounds(i, tile_slot);
1253     } else {
1254       pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
1255                                              tile_slot);
1256     }
1257
1258     ++slot_index;
1259   }
1260 }
1261
1262 void AppsGridView::AnimateToIdealBounds() {
1263   const gfx::Rect visible_bounds(GetVisibleBounds());
1264
1265   CalculateIdealBounds();
1266   for (int i = 0; i < view_model_.view_size(); ++i) {
1267     views::View* view = view_model_.view_at(i);
1268     if (view == drag_view_)
1269       continue;
1270
1271     const gfx::Rect& target = view_model_.ideal_bounds(i);
1272     if (bounds_animator_.GetTargetBounds(view) == target)
1273       continue;
1274
1275     const gfx::Rect& current = view->bounds();
1276     const bool current_visible = visible_bounds.Intersects(current);
1277     const bool target_visible = visible_bounds.Intersects(target);
1278     const bool visible = current_visible || target_visible;
1279
1280     const int y_diff = target.y() - current.y();
1281     if (visible && y_diff && y_diff % GetTotalTileSize().height() == 0) {
1282       AnimationBetweenRows(view,
1283                            current_visible,
1284                            current,
1285                            target_visible,
1286                            target);
1287     } else if (visible || bounds_animator_.IsAnimating(view)) {
1288       bounds_animator_.AnimateViewTo(view, target);
1289       bounds_animator_.SetAnimationDelegate(
1290           view,
1291           scoped_ptr<gfx::AnimationDelegate>(
1292               new ItemMoveAnimationDelegate(view)));
1293     } else {
1294       view->SetBoundsRect(target);
1295     }
1296   }
1297 }
1298
1299 void AppsGridView::AnimationBetweenRows(views::View* view,
1300                                         bool animate_current,
1301                                         const gfx::Rect& current,
1302                                         bool animate_target,
1303                                         const gfx::Rect& target) {
1304   // Determine page of |current| and |target|. -1 means in the left invisible
1305   // page, 0 is the center visible page and 1 means in the right invisible page.
1306   const int current_page = current.x() < 0 ? -1 :
1307       current.x() >= width() ? 1 : 0;
1308   const int target_page = target.x() < 0 ? -1 :
1309       target.x() >= width() ? 1 : 0;
1310
1311   const int dir = current_page < target_page ||
1312       (current_page == target_page && current.y() < target.y()) ? 1 : -1;
1313
1314   scoped_ptr<ui::Layer> layer;
1315   if (animate_current) {
1316     layer = view->RecreateLayer();
1317     layer->SuppressPaint();
1318
1319     view->SetFillsBoundsOpaquely(false);
1320     view->layer()->SetOpacity(0.f);
1321   }
1322
1323   gfx::Size total_tile_size = GetTotalTileSize();
1324   gfx::Rect current_out(current);
1325   current_out.Offset(dir * total_tile_size.width(), 0);
1326
1327   gfx::Rect target_in(target);
1328   if (animate_target)
1329     target_in.Offset(-dir * total_tile_size.width(), 0);
1330   view->SetBoundsRect(target_in);
1331   bounds_animator_.AnimateViewTo(view, target);
1332
1333   bounds_animator_.SetAnimationDelegate(
1334       view,
1335       scoped_ptr<gfx::AnimationDelegate>(
1336           new RowMoveAnimationDelegate(view, layer.release(), current_out)));
1337 }
1338
1339 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event,
1340                                        gfx::Point* drag_point) {
1341 #if defined(USE_AURA) && !defined(OS_WIN)
1342   // Use root location of |event| instead of location in |drag_view_|'s
1343   // coordinates because |drag_view_| has a scale transform and location
1344   // could have integer round error and causes jitter.
1345   *drag_point = event.root_location();
1346
1347   // GetWidget() could be NULL for tests.
1348   if (GetWidget()) {
1349     aura::Window::ConvertPointToTarget(
1350         GetWidget()->GetNativeWindow()->GetRootWindow(),
1351         GetWidget()->GetNativeWindow(),
1352         drag_point);
1353   }
1354
1355   views::View::ConvertPointFromWidget(this, drag_point);
1356 #else
1357   // For non-aura, root location is not clearly defined but |drag_view_| does
1358   // not have the scale transform. So no round error would be introduced and
1359   // it's okay to use View::ConvertPointToTarget.
1360   *drag_point = event.location();
1361   views::View::ConvertPointToTarget(drag_view_, this, drag_point);
1362 #endif
1363 }
1364
1365 void AppsGridView::CalculateDropTarget() {
1366   DCHECK(drag_view_);
1367
1368   gfx::Point point = drag_view_->icon()->bounds().CenterPoint();
1369   views::View::ConvertPointToTarget(drag_view_, this, &point);
1370   if (!IsPointWithinDragBuffer(point)) {
1371     // Reset the reorder target to the original position if the cursor is
1372     // outside the drag buffer.
1373     if (IsDraggingForReparentInRootLevelGridView()) {
1374       drop_attempt_ = DROP_FOR_NONE;
1375       return;
1376     }
1377
1378     reorder_drop_target_ = drag_view_init_index_;
1379     drop_attempt_ = DROP_FOR_REORDER;
1380     return;
1381   }
1382
1383   if (EnableFolderDragDropUI() &&
1384       CalculateFolderDropTarget(point, &folder_drop_target_)) {
1385     drop_attempt_ = DROP_FOR_FOLDER;
1386     return;
1387   }
1388
1389   drop_attempt_ = DROP_FOR_REORDER;
1390   CalculateReorderDropTarget(point, &reorder_drop_target_);
1391 }
1392
1393 bool AppsGridView::CalculateFolderDropTarget(const gfx::Point& point,
1394                                              Index* drop_target) const {
1395   Index nearest_tile_index(GetNearestTileIndexForPoint(point));
1396   int distance_to_tile_center =
1397       (point - GetExpectedTileBounds(nearest_tile_index.slot).CenterPoint())
1398           .Length();
1399   if (nearest_tile_index != reorder_placeholder_ &&
1400       distance_to_tile_center < kFolderDroppingCircleRadius &&
1401       !IsFolderItem(drag_view_->item()) &&
1402       CanDropIntoTarget(nearest_tile_index)) {
1403     *drop_target = nearest_tile_index;
1404     DCHECK(IsValidIndex(*drop_target));
1405     return true;
1406   }
1407
1408   return false;
1409 }
1410
1411 void AppsGridView::CalculateReorderDropTarget(const gfx::Point& point,
1412                                               Index* drop_target) const {
1413   gfx::Rect bounds = GetContentsBounds();
1414   Index grid_index = GetNearestTileIndexForPoint(point);
1415   gfx::Point reorder_placeholder_center =
1416       GetExpectedTileBounds(reorder_placeholder_.slot).CenterPoint();
1417
1418   int x_offset_direction = 0;
1419   if (grid_index == reorder_placeholder_) {
1420     x_offset_direction = reorder_placeholder_center.x() < point.x() ? -1 : 1;
1421   } else {
1422     x_offset_direction = reorder_placeholder_ < grid_index ? -1 : 1;
1423   }
1424
1425   gfx::Size total_tile_size = GetTotalTileSize();
1426   int row = grid_index.slot / cols_;
1427
1428   // Offset the target column based on the direction of the target. This will
1429   // result in earlier targets getting their reorder zone shifted backwards
1430   // and later targets getting their reorder zones shifted forwards.
1431   //
1432   // This makes eordering feel like the user is slotting items into the spaces
1433   // between apps.
1434   int x_offset = x_offset_direction *
1435                  (total_tile_size.width() - kFolderDroppingCircleRadius) / 2;
1436   int col = (point.x() - bounds.x() + x_offset) / total_tile_size.width();
1437   col = ClampToRange(col, 0, cols_ - 1);
1438   *drop_target =
1439       std::min(Index(pagination_model_.selected_page(), row * cols_ + col),
1440                GetLastViewIndex());
1441   DCHECK(IsValidIndex(*drop_target));
1442 }
1443
1444 void AppsGridView::OnReorderTimer() {
1445   if (drop_attempt_ == DROP_FOR_REORDER) {
1446     reorder_placeholder_ = reorder_drop_target_;
1447     AnimateToIdealBounds();
1448   }
1449 }
1450
1451 void AppsGridView::OnFolderItemReparentTimer() {
1452   DCHECK(folder_delegate_);
1453   if (drag_out_of_folder_container_ && drag_view_) {
1454     folder_delegate_->ReparentItem(drag_view_, last_drag_point_);
1455
1456     // Set the flag in the folder's grid view.
1457     dragging_for_reparent_item_ = true;
1458
1459     // Do not observe any data change since it is going to be hidden.
1460     item_list_->RemoveObserver(this);
1461     item_list_ = NULL;
1462   }
1463 }
1464
1465 void AppsGridView::OnFolderDroppingTimer() {
1466   if (drop_attempt_ == DROP_FOR_FOLDER)
1467     SetAsFolderDroppingTarget(folder_drop_target_, true);
1468 }
1469
1470 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer,
1471                                                const gfx::Point& drag_point) {
1472   if (IsUnderOEMFolder())
1473     return;
1474
1475   if (IsDraggingForReparentInHiddenGridView()) {
1476     // Dispatch drag event to root level grid view for re-parenting folder
1477     // folder item purpose.
1478     DispatchDragEventForReparent(pointer, drag_point);
1479     return;
1480   }
1481
1482   // Regular drag and drop in a folder's grid view.
1483   folder_delegate_->UpdateFolderViewBackground(true);
1484
1485   // Calculate if the drag_view_ is dragged out of the folder's container
1486   // ink bubble.
1487   gfx::Rect bounds_to_folder_view = ConvertRectToParent(drag_view_->bounds());
1488   gfx::Point pt = bounds_to_folder_view.CenterPoint();
1489   bool is_item_dragged_out_of_folder =
1490       folder_delegate_->IsPointOutsideOfFolderBoundary(pt);
1491   if (is_item_dragged_out_of_folder) {
1492     if (!drag_out_of_folder_container_) {
1493       folder_item_reparent_timer_.Start(
1494           FROM_HERE,
1495           base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay),
1496           this,
1497           &AppsGridView::OnFolderItemReparentTimer);
1498       drag_out_of_folder_container_ = true;
1499     }
1500   } else {
1501     folder_item_reparent_timer_.Stop();
1502     drag_out_of_folder_container_ = false;
1503   }
1504 }
1505
1506 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1507   return (!folder_delegate_ && dragging_for_reparent_item_);
1508 }
1509
1510 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1511   return (folder_delegate_ && dragging_for_reparent_item_);
1512 }
1513
1514 gfx::Rect AppsGridView::GetTargetIconRectInFolder(
1515     AppListItemView* drag_item_view,
1516     AppListItemView* folder_item_view) {
1517   gfx::Rect view_ideal_bounds = view_model_.ideal_bounds(
1518       view_model_.GetIndexOfView(folder_item_view));
1519   gfx::Rect icon_ideal_bounds =
1520       folder_item_view->GetIconBoundsForTargetViewBounds(view_ideal_bounds);
1521   AppListFolderItem* folder_item =
1522       static_cast<AppListFolderItem*>(folder_item_view->item());
1523   return folder_item->GetTargetIconRectInFolderForItem(
1524       drag_item_view->item(), icon_ideal_bounds);
1525 }
1526
1527 bool AppsGridView::IsUnderOEMFolder() {
1528   if (!folder_delegate_)
1529     return false;
1530
1531   return folder_delegate_->IsOEMFolder();
1532 }
1533
1534 void AppsGridView::DispatchDragEventForReparent(Pointer pointer,
1535                                                 const gfx::Point& drag_point) {
1536   folder_delegate_->DispatchDragEventForReparent(pointer, drag_point);
1537 }
1538
1539 void AppsGridView::EndDragFromReparentItemInRootLevel(
1540     bool events_forwarded_to_drag_drop_host,
1541     bool cancel_drag) {
1542   // EndDrag was called before if |drag_view_| is NULL.
1543   if (!drag_view_)
1544     return;
1545
1546   DCHECK(IsDraggingForReparentInRootLevelGridView());
1547   bool cancel_reparent = cancel_drag || drop_attempt_ == DROP_FOR_NONE;
1548   if (!events_forwarded_to_drag_drop_host && !cancel_reparent) {
1549     CalculateDropTarget();
1550     if (drop_attempt_ == DROP_FOR_REORDER &&
1551         IsValidIndex(reorder_drop_target_)) {
1552       ReparentItemForReorder(drag_view_, reorder_drop_target_);
1553     } else if (drop_attempt_ == DROP_FOR_FOLDER &&
1554                IsValidIndex(folder_drop_target_)) {
1555       ReparentItemToAnotherFolder(drag_view_, folder_drop_target_);
1556     } else {
1557       NOTREACHED();
1558     }
1559     SetViewHidden(drag_view_, false /* show */, true /* no animate */);
1560   }
1561
1562   // The drag can be ended after the synchronous drag is created but before it
1563   // is Run().
1564   CleanUpSynchronousDrag();
1565
1566   SetAsFolderDroppingTarget(folder_drop_target_, false);
1567   if (cancel_reparent) {
1568     CancelFolderItemReparent(drag_view_);
1569   } else {
1570     // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1571     // cleaning up the newly created AppListItemView, effectively claiming
1572     // ownership of the newly created drag view.
1573     drag_view_->OnDragEnded();
1574     drag_view_ = NULL;
1575   }
1576   ClearDragState();
1577   AnimateToIdealBounds();
1578
1579   StopPageFlipTimer();
1580 }
1581
1582 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1583   if (drag_and_drop_host_) {
1584     // If we had a drag and drop proxy icon, we delete it and make the real
1585     // item visible again.
1586     drag_and_drop_host_->DestroyDragIconProxy();
1587   }
1588
1589   // The drag can be ended after the synchronous drag is created but before it
1590   // is Run().
1591   CleanUpSynchronousDrag();
1592
1593   SetAsFolderDroppingTarget(folder_drop_target_, false);
1594   ClearDragState();
1595 }
1596
1597 void AppsGridView::OnFolderItemRemoved() {
1598   DCHECK(folder_delegate_);
1599   item_list_ = NULL;
1600 }
1601
1602 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) {
1603   // When a drag and drop host is given, the item can be dragged out of the app
1604   // list window. In that case a proxy widget needs to be used.
1605   // Note: This code has very likely to be changed for Windows (non metro mode)
1606   // when a |drag_and_drop_host_| gets implemented.
1607   if (!drag_view_ || !drag_and_drop_host_)
1608     return;
1609
1610   gfx::Point screen_location = grid_location;
1611   views::View::ConvertPointToScreen(this, &screen_location);
1612
1613   // Determine the mouse offset to the center of the icon so that the drag and
1614   // drop host follows this layer.
1615   gfx::Vector2d delta = drag_view_offset_ -
1616                         drag_view_->GetLocalBounds().CenterPoint();
1617   delta.set_y(delta.y() + drag_view_->title()->size().height() / 2);
1618
1619   // We have to hide the original item since the drag and drop host will do
1620   // the OS dependent code to "lift off the dragged item".
1621   DCHECK(!IsDraggingForReparentInRootLevelGridView());
1622   drag_and_drop_host_->CreateDragIconProxy(screen_location,
1623                                            drag_view_->item()->icon(),
1624                                            drag_view_,
1625                                            delta,
1626                                            kDragAndDropProxyScale);
1627   SetViewHidden(drag_view_,
1628            true /* hide */,
1629            true /* no animation */);
1630 }
1631
1632 void AppsGridView::DispatchDragEventToDragAndDropHost(
1633     const gfx::Point& location_in_screen_coordinates) {
1634   if (!drag_view_ || !drag_and_drop_host_)
1635     return;
1636
1637   if (GetLocalBounds().Contains(last_drag_point_)) {
1638     // The event was issued inside the app menu and we should get all events.
1639     if (forward_events_to_drag_and_drop_host_) {
1640       // The DnD host was previously called and needs to be informed that the
1641       // session returns to the owner.
1642       forward_events_to_drag_and_drop_host_ = false;
1643       drag_and_drop_host_->EndDrag(true);
1644     }
1645   } else {
1646     if (IsFolderItem(drag_view_->item()))
1647       return;
1648
1649     // The event happened outside our app menu and we might need to dispatch.
1650     if (forward_events_to_drag_and_drop_host_) {
1651       // Dispatch since we have already started.
1652       if (!drag_and_drop_host_->Drag(location_in_screen_coordinates)) {
1653         // The host is not active any longer and we cancel the operation.
1654         forward_events_to_drag_and_drop_host_ = false;
1655         drag_and_drop_host_->EndDrag(true);
1656       }
1657     } else {
1658       if (drag_and_drop_host_->StartDrag(drag_view_->item()->id(),
1659                                          location_in_screen_coordinates)) {
1660         // From now on we forward the drag events.
1661         forward_events_to_drag_and_drop_host_ = true;
1662         // Any flip operations are stopped.
1663         StopPageFlipTimer();
1664       }
1665     }
1666   }
1667 }
1668
1669 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) {
1670   if (!IsPointWithinDragBuffer(drag_point))
1671     StopPageFlipTimer();
1672   int new_page_flip_target = -1;
1673
1674   // Drag zones are at the edges of the scroll axis.
1675   if (pagination_controller_->scroll_axis() ==
1676       PaginationController::SCROLL_AXIS_VERTICAL) {
1677     if (drag_point.y() < kPageFlipZoneSize)
1678       new_page_flip_target = pagination_model_.selected_page() - 1;
1679     else if (drag_point.y() > height() - kPageFlipZoneSize)
1680       new_page_flip_target = pagination_model_.selected_page() + 1;
1681   } else {
1682     if (page_switcher_view_->bounds().Contains(drag_point)) {
1683       gfx::Point page_switcher_point(drag_point);
1684       views::View::ConvertPointToTarget(
1685           this, page_switcher_view_, &page_switcher_point);
1686       new_page_flip_target =
1687           page_switcher_view_->GetPageForPoint(page_switcher_point);
1688     }
1689
1690     // TODO(xiyuan): Fix this for RTL.
1691     if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize)
1692       new_page_flip_target = pagination_model_.selected_page() - 1;
1693
1694     if (new_page_flip_target == -1 &&
1695         drag_point.x() > width() - kPageFlipZoneSize) {
1696       new_page_flip_target = pagination_model_.selected_page() + 1;
1697     }
1698   }
1699
1700   if (new_page_flip_target == page_flip_target_)
1701     return;
1702
1703   StopPageFlipTimer();
1704   if (pagination_model_.is_valid_page(new_page_flip_target)) {
1705     page_flip_target_ = new_page_flip_target;
1706
1707     if (page_flip_target_ != pagination_model_.selected_page()) {
1708       page_flip_timer_.Start(FROM_HERE,
1709           base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_),
1710           this, &AppsGridView::OnPageFlipTimer);
1711     }
1712   }
1713 }
1714
1715 void AppsGridView::OnPageFlipTimer() {
1716   DCHECK(pagination_model_.is_valid_page(page_flip_target_));
1717   pagination_model_.SelectPage(page_flip_target_, true);
1718 }
1719
1720 void AppsGridView::MoveItemInModel(views::View* item_view,
1721                                    const Index& target) {
1722   int current_model_index = view_model_.GetIndexOfView(item_view);
1723   DCHECK_GE(current_model_index, 0);
1724
1725   int target_model_index = GetModelIndexFromIndex(target);
1726   if (target_model_index == current_model_index)
1727     return;
1728
1729   item_list_->RemoveObserver(this);
1730   item_list_->MoveItem(current_model_index, target_model_index);
1731   view_model_.Move(current_model_index, target_model_index);
1732   item_list_->AddObserver(this);
1733
1734   if (pagination_model_.selected_page() != target.page)
1735     pagination_model_.SelectPage(target.page, false);
1736 }
1737
1738 void AppsGridView::MoveItemToFolder(views::View* item_view,
1739                                     const Index& target) {
1740   const std::string& source_item_id =
1741       static_cast<AppListItemView*>(item_view)->item()->id();
1742   AppListItemView* target_view =
1743       static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1744   const std::string&  target_view_item_id = target_view->item()->id();
1745
1746   // Make change to data model.
1747   item_list_->RemoveObserver(this);
1748   std::string folder_item_id =
1749       model_->MergeItems(target_view_item_id, source_item_id);
1750   item_list_->AddObserver(this);
1751   if (folder_item_id.empty()) {
1752     LOG(ERROR) << "Unable to merge into item id: " << target_view_item_id;
1753     return;
1754   }
1755   if (folder_item_id != target_view_item_id) {
1756     // New folder was created, change the view model to replace the old target
1757     // view with the new folder item view.
1758     size_t folder_item_index;
1759     if (item_list_->FindItemIndex(folder_item_id, &folder_item_index)) {
1760       int target_view_index = view_model_.GetIndexOfView(target_view);
1761       gfx::Rect target_view_bounds = target_view->bounds();
1762       DeleteItemViewAtIndex(target_view_index);
1763       views::View* target_folder_view =
1764           CreateViewForItemAtIndex(folder_item_index);
1765       target_folder_view->SetBoundsRect(target_view_bounds);
1766       view_model_.Add(target_folder_view, target_view_index);
1767       AddChildView(target_folder_view);
1768     } else {
1769       LOG(ERROR) << "Folder no longer in item_list: " << folder_item_id;
1770     }
1771   }
1772
1773   // Fade out the drag_view_ and delete it when animation ends.
1774   int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1775   view_model_.Remove(drag_view_index);
1776   bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1777   bounds_animator_.SetAnimationDelegate(
1778       drag_view_,
1779       scoped_ptr<gfx::AnimationDelegate>(
1780           new ItemRemoveAnimationDelegate(drag_view_)));
1781   UpdatePaging();
1782 }
1783
1784 void AppsGridView::ReparentItemForReorder(views::View* item_view,
1785                                           const Index& target) {
1786   item_list_->RemoveObserver(this);
1787   model_->RemoveObserver(this);
1788
1789   AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1790   DCHECK(reparent_item->IsInFolder());
1791   const std::string source_folder_id = reparent_item->folder_id();
1792   AppListFolderItem* source_folder =
1793       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1794
1795   int target_model_index = GetModelIndexFromIndex(target);
1796
1797   // Remove the source folder view if there is only 1 item in it, since the
1798   // source folder will be deleted after its only child item removed from it.
1799   if (source_folder->ChildItemCount() == 1u) {
1800     const int deleted_folder_index =
1801         view_model_.GetIndexOfView(activated_folder_item_view());
1802     DeleteItemViewAtIndex(deleted_folder_index);
1803
1804     // Adjust |target_model_index| if it is beyond the deleted folder index.
1805     if (target_model_index > deleted_folder_index)
1806       --target_model_index;
1807   }
1808
1809   // Move the item from its parent folder to top level item list.
1810   // Must move to target_model_index, the location we expect the target item
1811   // to be, not the item location we want to insert before.
1812   int current_model_index = view_model_.GetIndexOfView(item_view);
1813   syncer::StringOrdinal target_position;
1814   if (target_model_index < static_cast<int>(item_list_->item_count()))
1815     target_position = item_list_->item_at(target_model_index)->position();
1816   model_->MoveItemToFolderAt(reparent_item, "", target_position);
1817   view_model_.Move(current_model_index, target_model_index);
1818
1819   RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1820
1821   item_list_->AddObserver(this);
1822   model_->AddObserver(this);
1823   UpdatePaging();
1824 }
1825
1826 void AppsGridView::ReparentItemToAnotherFolder(views::View* item_view,
1827                                                const Index& target) {
1828   DCHECK(IsDraggingForReparentInRootLevelGridView());
1829
1830   AppListItemView* target_view =
1831       static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1832   if (!target_view)
1833     return;
1834
1835   // Make change to data model.
1836   item_list_->RemoveObserver(this);
1837
1838   AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1839   DCHECK(reparent_item->IsInFolder());
1840   const std::string source_folder_id = reparent_item->folder_id();
1841   AppListFolderItem* source_folder =
1842       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1843
1844   // Remove the source folder view if there is only 1 item in it, since the
1845   // source folder will be deleted after its only child item merged into the
1846   // target item.
1847   if (source_folder->ChildItemCount() == 1u)
1848     DeleteItemViewAtIndex(
1849         view_model_.GetIndexOfView(activated_folder_item_view()));
1850
1851   AppListItem* target_item = target_view->item();
1852
1853   // Move item to the target folder.
1854   std::string target_id_after_merge =
1855       model_->MergeItems(target_item->id(), reparent_item->id());
1856   if (target_id_after_merge.empty()) {
1857     LOG(ERROR) << "Unable to reparent to item id: " << target_item->id();
1858     item_list_->AddObserver(this);
1859     return;
1860   }
1861
1862   if (target_id_after_merge != target_item->id()) {
1863     // New folder was created, change the view model to replace the old target
1864     // view with the new folder item view.
1865     const std::string& new_folder_id = reparent_item->folder_id();
1866     size_t new_folder_index;
1867     if (item_list_->FindItemIndex(new_folder_id, &new_folder_index)) {
1868       int target_view_index = view_model_.GetIndexOfView(target_view);
1869       DeleteItemViewAtIndex(target_view_index);
1870       views::View* new_folder_view =
1871           CreateViewForItemAtIndex(new_folder_index);
1872       view_model_.Add(new_folder_view, target_view_index);
1873       AddChildView(new_folder_view);
1874     } else {
1875       LOG(ERROR) << "Folder no longer in item_list: " << new_folder_id;
1876     }
1877   }
1878
1879   RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1880
1881   item_list_->AddObserver(this);
1882
1883   // Fade out the drag_view_ and delete it when animation ends.
1884   int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1885   view_model_.Remove(drag_view_index);
1886   bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1887   bounds_animator_.SetAnimationDelegate(
1888       drag_view_,
1889       scoped_ptr<gfx::AnimationDelegate>(
1890           new ItemRemoveAnimationDelegate(drag_view_)));
1891   UpdatePaging();
1892 }
1893
1894 // After moving the re-parenting item out of the folder, if there is only 1 item
1895 // left, remove the last item out of the folder, delete the folder and insert it
1896 // to the data model at the same position. Make the same change to view_model_
1897 // accordingly.
1898 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1899     const std::string& source_folder_id) {
1900   AppListFolderItem* source_folder =
1901       static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1902   if (!source_folder || source_folder->ChildItemCount() != 1u)
1903     return;
1904
1905   // Delete view associated with the folder item to be removed.
1906   DeleteItemViewAtIndex(
1907       view_model_.GetIndexOfView(activated_folder_item_view()));
1908
1909   // Now make the data change to remove the folder item in model.
1910   AppListItem* last_item = source_folder->item_list()->item_at(0);
1911   model_->MoveItemToFolderAt(last_item, "", source_folder->position());
1912
1913   // Create a new item view for the last item in folder.
1914   size_t last_item_index;
1915   if (!item_list_->FindItemIndex(last_item->id(), &last_item_index) ||
1916       last_item_index > static_cast<size_t>(view_model_.view_size())) {
1917     NOTREACHED();
1918     return;
1919   }
1920   views::View* last_item_view = CreateViewForItemAtIndex(last_item_index);
1921   view_model_.Add(last_item_view, last_item_index);
1922   AddChildView(last_item_view);
1923 }
1924
1925 void AppsGridView::CancelFolderItemReparent(AppListItemView* drag_item_view) {
1926   // The icon of the dragged item must target to its final ideal bounds after
1927   // the animation completes.
1928   CalculateIdealBounds();
1929
1930   gfx::Rect target_icon_rect =
1931       GetTargetIconRectInFolder(drag_item_view, activated_folder_item_view_);
1932
1933   gfx::Rect drag_view_icon_to_grid =
1934       drag_item_view->ConvertRectToParent(drag_item_view->GetIconBounds());
1935   drag_view_icon_to_grid.ClampToCenteredSize(
1936       gfx::Size(kGridIconDimension, kGridIconDimension));
1937   TopIconAnimationView* icon_view = new TopIconAnimationView(
1938       drag_item_view->item()->icon(),
1939       target_icon_rect,
1940       false);    /* animate like closing folder */
1941   AddChildView(icon_view);
1942   icon_view->SetBoundsRect(drag_view_icon_to_grid);
1943   icon_view->TransformView();
1944 }
1945
1946 void AppsGridView::CancelContextMenusOnCurrentPage() {
1947   int start = pagination_model_.selected_page() * tiles_per_page();
1948   int end = std::min(view_model_.view_size(), start + tiles_per_page());
1949   for (int i = start; i < end; ++i) {
1950     AppListItemView* view =
1951         static_cast<AppListItemView*>(view_model_.view_at(i));
1952     view->CancelContextMenu();
1953   }
1954 }
1955
1956 void AppsGridView::DeleteItemViewAtIndex(int index) {
1957   views::View* item_view = view_model_.view_at(index);
1958   view_model_.Remove(index);
1959   if (item_view == drag_view_)
1960     drag_view_ = NULL;
1961   delete item_view;
1962 }
1963
1964 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const {
1965   gfx::Rect rect(GetLocalBounds());
1966   rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx);
1967   return rect.Contains(point);
1968 }
1969
1970 void AppsGridView::ButtonPressed(views::Button* sender,
1971                                  const ui::Event& event) {
1972   if (dragging())
1973     return;
1974
1975   if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName))
1976     return;
1977
1978   if (delegate_) {
1979     // Always set the previous activated_folder_item_view_ to be visible. This
1980     // prevents a case where the item would remain hidden due the
1981     // |activated_folder_item_view_| changing during the animation. We only
1982     // need to track |activated_folder_item_view_| in the root level grid view.
1983     if (!folder_delegate_) {
1984       if (activated_folder_item_view_)
1985         activated_folder_item_view_->SetVisible(true);
1986       AppListItemView* pressed_item_view =
1987           static_cast<AppListItemView*>(sender);
1988       if (IsFolderItem(pressed_item_view->item()))
1989         activated_folder_item_view_ = pressed_item_view;
1990       else
1991         activated_folder_item_view_ = NULL;
1992     }
1993     delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->item(),
1994                            event.flags());
1995   }
1996 }
1997
1998 void AppsGridView::OnListItemAdded(size_t index, AppListItem* item) {
1999   EndDrag(true);
2000
2001   views::View* view = CreateViewForItemAtIndex(index);
2002   view_model_.Add(view, index);
2003   AddChildView(view);
2004
2005   UpdatePaging();
2006   UpdatePulsingBlockViews();
2007   Layout();
2008   SchedulePaint();
2009 }
2010
2011 void AppsGridView::OnListItemRemoved(size_t index, AppListItem* item) {
2012   EndDrag(true);
2013
2014   DeleteItemViewAtIndex(index);
2015
2016   UpdatePaging();
2017   UpdatePulsingBlockViews();
2018   Layout();
2019   SchedulePaint();
2020 }
2021
2022 void AppsGridView::OnListItemMoved(size_t from_index,
2023                                    size_t to_index,
2024                                    AppListItem* item) {
2025   EndDrag(true);
2026   view_model_.Move(from_index, to_index);
2027
2028   UpdatePaging();
2029   AnimateToIdealBounds();
2030 }
2031
2032 void AppsGridView::TotalPagesChanged() {
2033 }
2034
2035 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
2036   if (dragging()) {
2037     CalculateDropTarget();
2038     Layout();
2039     MaybeStartPageFlipTimer(last_drag_point_);
2040   } else {
2041     ClearSelectedView(selected_view_);
2042     Layout();
2043   }
2044 }
2045
2046 void AppsGridView::TransitionStarted() {
2047   CancelContextMenusOnCurrentPage();
2048 }
2049
2050 void AppsGridView::TransitionChanged() {
2051   // Update layout for valid page transition only since over-scroll no longer
2052   // animates app icons.
2053   const PaginationModel::Transition& transition =
2054       pagination_model_.transition();
2055   if (pagination_model_.is_valid_page(transition.target_page))
2056     Layout();
2057 }
2058
2059 void AppsGridView::OnAppListModelStatusChanged() {
2060   UpdatePulsingBlockViews();
2061   Layout();
2062   SchedulePaint();
2063 }
2064
2065 void AppsGridView::SetViewHidden(views::View* view, bool hide, bool immediate) {
2066   ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
2067   animator.SetPreemptionStrategy(
2068       immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
2069                   ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
2070   view->layer()->SetOpacity(hide ? 0 : 1);
2071 }
2072
2073 void AppsGridView::OnImplicitAnimationsCompleted() {
2074   if (layer()->opacity() == 0.0f)
2075     SetVisible(false);
2076 }
2077
2078 bool AppsGridView::EnableFolderDragDropUI() {
2079   // Enable drag and drop folder UI only if it is at the app list root level
2080   // and the switch is on.
2081   return model_->folders_enabled() && !folder_delegate_;
2082 }
2083
2084 bool AppsGridView::CanDropIntoTarget(const Index& drop_target) const {
2085   views::View* target_view = GetViewAtIndex(drop_target);
2086   if (!target_view)
2087     return false;
2088
2089   AppListItem* target_item =
2090       static_cast<AppListItemView*>(target_view)->item();
2091   // Items can be dropped into non-folders (which have no children) or folders
2092   // that have fewer than the max allowed items.
2093   // OEM folder does not allow to drag/drop other items in it.
2094   return target_item->ChildItemCount() < kMaxFolderItems &&
2095          !IsOEMFolderItem(target_item);
2096 }
2097
2098 AppsGridView::Index AppsGridView::GetNearestTileIndexForPoint(
2099     const gfx::Point& point) const {
2100   gfx::Rect bounds = GetContentsBounds();
2101   gfx::Size total_tile_size = GetTotalTileSize();
2102   int col = ClampToRange(
2103       (point.x() - bounds.x()) / total_tile_size.width(), 0, cols_ - 1);
2104   int row = ClampToRange((point.y() - bounds.y()) / total_tile_size.height(),
2105                          0,
2106                          rows_per_page_ - 1);
2107   return Index(pagination_model_.selected_page(), row * cols_ + col);
2108 }
2109
2110 gfx::Size AppsGridView::GetTileGridSize() const {
2111   gfx::Rect bounds = GetExpectedTileBounds(0, 0);
2112   bounds.Union(GetExpectedTileBounds(rows_per_page_ - 1, cols_ - 1));
2113   if (switches::IsExperimentalAppListEnabled())
2114     bounds.Inset(-kExperimentalTileLeftRightPadding,
2115                  -kExperimentalTileTopBottomPadding);
2116   return bounds.size();
2117 }
2118
2119 gfx::Rect AppsGridView::GetExpectedTileBounds(int slot) const {
2120   return GetExpectedTileBounds(slot / cols_, slot % cols_);
2121 }
2122
2123 gfx::Rect AppsGridView::GetExpectedTileBounds(int row, int col) const {
2124   gfx::Rect bounds(GetContentsBounds());
2125   gfx::Size total_tile_size = GetTotalTileSize();
2126   gfx::Rect tile_bounds(gfx::Point(bounds.x() + col * total_tile_size.width(),
2127                                    bounds.y() + row * total_tile_size.height()),
2128                         total_tile_size);
2129   tile_bounds.ClampToCenteredSize(GetTileViewSize());
2130   return tile_bounds;
2131 }
2132
2133 views::View* AppsGridView::GetViewAtSlotOnCurrentPage(int slot) {
2134   if (slot < 0)
2135     return NULL;
2136
2137   // Calculate the original bound of the tile at |index|.
2138   int row = slot / cols_;
2139   int col = slot % cols_;
2140   gfx::Rect tile_rect = GetExpectedTileBounds(row, col);
2141
2142   for (int i = 0; i < view_model_.view_size(); ++i) {
2143     views::View* view = view_model_.view_at(i);
2144     if (view->bounds() == tile_rect && view != drag_view_)
2145       return view;
2146   }
2147   return NULL;
2148 }
2149
2150 void AppsGridView::SetAsFolderDroppingTarget(const Index& target_index,
2151                                              bool is_target_folder) {
2152   AppListItemView* target_view =
2153       static_cast<AppListItemView*>(
2154           GetViewAtSlotOnCurrentPage(target_index.slot));
2155   if (target_view)
2156     target_view->SetAsAttemptedFolderTarget(is_target_folder);
2157 }
2158
2159 }  // namespace app_list