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