Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / app_list / views / apps_grid_view.cc
blobf150eb6b6802d80162520ddfe390c0bbcac19581
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/app_list/views/apps_grid_view.h"
7 #include <algorithm>
8 #include <set>
9 #include <string>
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/views/app_list_drag_and_drop_host.h"
17 #include "ui/app_list/views/app_list_folder_view.h"
18 #include "ui/app_list/views/app_list_item_view.h"
19 #include "ui/app_list/views/apps_grid_view_delegate.h"
20 #include "ui/app_list/views/page_switcher.h"
21 #include "ui/app_list/views/pulsing_block_view.h"
22 #include "ui/app_list/views/top_icon_animation_view.h"
23 #include "ui/compositor/scoped_layer_animation_settings.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/animation/animation.h"
26 #include "ui/views/border.h"
27 #include "ui/views/view_model_utils.h"
28 #include "ui/views/widget/widget.h"
30 #if defined(USE_AURA)
31 #include "ui/aura/window.h"
32 #include "ui/aura/window_event_dispatcher.h"
33 #if defined(OS_WIN)
34 #include "ui/views/win/hwnd_util.h"
35 #endif // defined(OS_WIN)
36 #endif // defined(USE_AURA)
38 #if defined(OS_WIN)
39 #include "base/command_line.h"
40 #include "base/files/file_path.h"
41 #include "base/win/shortcut.h"
42 #include "ui/base/dragdrop/drag_utils.h"
43 #include "ui/base/dragdrop/drop_target_win.h"
44 #include "ui/base/dragdrop/os_exchange_data.h"
45 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
46 #include "ui/gfx/win/dpi.h"
47 #endif
49 namespace app_list {
51 namespace {
53 // Distance a drag needs to be from the app grid to be considered 'outside', at
54 // which point we rearrange the apps to their pre-drag configuration, as a drop
55 // then would be canceled. We have a buffer to make it easier to drag apps to
56 // other pages.
57 const int kDragBufferPx = 20;
59 // Padding space in pixels for fixed layout.
60 const int kLeftRightPadding = 20;
61 const int kTopPadding = 1;
63 // Padding space in pixels between pages.
64 const int kPagePadding = 40;
66 // Preferred tile size when showing in fixed layout.
67 const int kPreferredTileWidth = 88;
68 const int kPreferredTileHeight = 98;
70 // Width in pixels of the area on the sides that triggers a page flip.
71 const int kPageFlipZoneSize = 40;
73 // Delay in milliseconds to do the page flip.
74 const int kPageFlipDelayInMs = 1000;
76 // How many pages on either side of the selected one we prerender.
77 const int kPrerenderPages = 1;
79 // The drag and drop proxy should get scaled by this factor.
80 const float kDragAndDropProxyScale = 1.5f;
82 // Delays in milliseconds to show folder dropping preview circle.
83 const int kFolderDroppingDelay = 150;
85 // Delays in milliseconds to show re-order preview.
86 const int kReorderDelay = 120;
88 // Delays in milliseconds to show folder item reparent UI.
89 const int kFolderItemReparentDelay = 50;
91 // Radius of the circle, in which if entered, show folder dropping preview
92 // UI.
93 const int kFolderDroppingCircleRadius = 15;
96 // RowMoveAnimationDelegate is used when moving an item into a different row.
97 // Before running the animation, the item's layer is re-created and kept in
98 // the original position, then the item is moved to just before its target
99 // position and opacity set to 0. When the animation runs, this delegate moves
100 // the layer and fades it out while fading in the item at the same time.
101 class RowMoveAnimationDelegate : public gfx::AnimationDelegate {
102 public:
103 RowMoveAnimationDelegate(views::View* view,
104 ui::Layer* layer,
105 const gfx::Rect& layer_target)
106 : view_(view),
107 layer_(layer),
108 layer_start_(layer ? layer->bounds() : gfx::Rect()),
109 layer_target_(layer_target) {
111 virtual ~RowMoveAnimationDelegate() {}
113 // gfx::AnimationDelegate overrides:
114 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
115 view_->layer()->SetOpacity(animation->GetCurrentValue());
116 view_->layer()->ScheduleDraw();
118 if (layer_) {
119 layer_->SetOpacity(1 - animation->GetCurrentValue());
120 layer_->SetBounds(animation->CurrentValueBetween(layer_start_,
121 layer_target_));
122 layer_->ScheduleDraw();
125 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
126 view_->layer()->SetOpacity(1.0f);
127 view_->SchedulePaint();
129 virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
130 view_->layer()->SetOpacity(1.0f);
131 view_->SchedulePaint();
134 private:
135 // The view that needs to be wrapped. Owned by views hierarchy.
136 views::View* view_;
138 scoped_ptr<ui::Layer> layer_;
139 const gfx::Rect layer_start_;
140 const gfx::Rect layer_target_;
142 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate);
145 // ItemRemoveAnimationDelegate is used to show animation for removing an item.
146 // This happens when user drags an item into a folder. The dragged item will
147 // be removed from the original list after it is dropped into the folder.
148 class ItemRemoveAnimationDelegate : public gfx::AnimationDelegate {
149 public:
150 explicit ItemRemoveAnimationDelegate(views::View* view)
151 : view_(view) {
154 virtual ~ItemRemoveAnimationDelegate() {
157 // gfx::AnimationDelegate overrides:
158 virtual void AnimationProgressed(const gfx::Animation* animation) OVERRIDE {
159 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
160 view_->layer()->ScheduleDraw();
163 private:
164 scoped_ptr<views::View> view_;
166 DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate);
169 // ItemMoveAnimationDelegate observes when an item finishes animating when it is
170 // not moving between rows. This is to ensure an item is repainted for the
171 // "zoom out" case when releasing an item being dragged.
172 class ItemMoveAnimationDelegate : public gfx::AnimationDelegate {
173 public:
174 ItemMoveAnimationDelegate(views::View* view) : view_(view) {}
176 virtual void AnimationEnded(const gfx::Animation* animation) OVERRIDE {
177 view_->SchedulePaint();
179 virtual void AnimationCanceled(const gfx::Animation* animation) OVERRIDE {
180 view_->SchedulePaint();
183 private:
184 views::View* view_;
186 DISALLOW_COPY_AND_ASSIGN(ItemMoveAnimationDelegate);
189 // Gets the distance between the centers of the |rect_1| and |rect_2|.
190 int GetDistanceBetweenRects(gfx::Rect rect_1,
191 gfx::Rect rect_2) {
192 return (rect_1.CenterPoint() - rect_2.CenterPoint()).Length();
195 // Returns true if the |item| is an folder item.
196 bool IsFolderItem(AppListItem* item) {
197 return (item->GetItemType() == AppListFolderItem::kItemType);
200 bool IsOEMFolderItem(AppListItem* item) {
201 return IsFolderItem(item) &&
202 (static_cast<AppListFolderItem*>(item))->folder_type() ==
203 AppListFolderItem::FOLDER_TYPE_OEM;
206 } // namespace
208 #if defined(OS_WIN)
209 // Interprets drag events sent from Windows via the drag/drop API and forwards
210 // them to AppsGridView.
211 // On Windows, in order to have the OS perform the drag properly we need to
212 // provide it with a shortcut file which may or may not exist at the time the
213 // drag is started. Therefore while waiting for that shortcut to be located we
214 // just do a regular "internal" drag and transition into the synchronous drag
215 // when the shortcut is found/created. Hence a synchronous drag is an optional
216 // phase of a regular drag and non-Windows platforms drags are equivalent to a
217 // Windows drag that never enters the synchronous drag phase.
218 class SynchronousDrag : public ui::DragSourceWin {
219 public:
220 SynchronousDrag(AppsGridView* grid_view,
221 AppListItemView* drag_view,
222 const gfx::Point& drag_view_offset)
223 : grid_view_(grid_view),
224 drag_view_(drag_view),
225 drag_view_offset_(drag_view_offset),
226 has_shortcut_path_(false),
227 running_(false),
228 canceled_(false) {}
230 void set_shortcut_path(const base::FilePath& shortcut_path) {
231 has_shortcut_path_ = true;
232 shortcut_path_ = shortcut_path;
235 bool running() { return running_; }
237 bool CanRun() {
238 return has_shortcut_path_ && !running_;
241 void Run() {
242 DCHECK(CanRun());
244 // Prevent the synchronous dragger being destroyed while the drag is
245 // running.
246 scoped_refptr<SynchronousDrag> this_ref = this;
247 running_ = true;
249 ui::OSExchangeData data;
250 SetupExchangeData(&data);
252 // Hide the dragged view because the OS is going to create its own.
253 drag_view_->SetVisible(false);
255 // Blocks until the drag is finished. Calls into the ui::DragSourceWin
256 // methods.
257 DWORD effects;
258 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
259 this, DROPEFFECT_MOVE | DROPEFFECT_LINK, &effects);
261 // If |drag_view_| is NULL the drag was ended by some reentrant code.
262 if (drag_view_) {
263 // Make the drag view visible again.
264 drag_view_->SetVisible(true);
265 drag_view_->OnSyncDragEnd();
267 grid_view_->EndDrag(canceled_ || !IsCursorWithinGridView());
271 void EndDragExternally() {
272 CancelDrag();
273 DCHECK(drag_view_);
274 drag_view_->SetVisible(true);
275 drag_view_ = NULL;
278 private:
279 // Overridden from ui::DragSourceWin.
280 virtual void OnDragSourceCancel() OVERRIDE {
281 canceled_ = true;
284 virtual void OnDragSourceDrop() OVERRIDE {
287 virtual void OnDragSourceMove() OVERRIDE {
288 grid_view_->UpdateDrag(AppsGridView::MOUSE, GetCursorInGridViewCoords());
291 void SetupExchangeData(ui::OSExchangeData* data) {
292 data->SetFilename(shortcut_path_);
293 gfx::ImageSkia image(drag_view_->GetDragImage());
294 gfx::Size image_size(image.size());
295 drag_utils::SetDragImageOnDataObject(
296 image,
297 drag_view_offset_ - drag_view_->GetDragImageOffset(),
298 data);
301 HWND GetGridViewHWND() {
302 return views::HWNDForView(grid_view_);
305 bool IsCursorWithinGridView() {
306 POINT p;
307 GetCursorPos(&p);
308 return GetGridViewHWND() == WindowFromPoint(p);
311 gfx::Point GetCursorInGridViewCoords() {
312 POINT p;
313 GetCursorPos(&p);
314 ScreenToClient(GetGridViewHWND(), &p);
315 gfx::Point grid_view_pt(p.x, p.y);
316 grid_view_pt = gfx::win::ScreenToDIPPoint(grid_view_pt);
317 views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt);
318 return grid_view_pt;
321 AppsGridView* grid_view_;
322 AppListItemView* drag_view_;
323 gfx::Point drag_view_offset_;
324 bool has_shortcut_path_;
325 base::FilePath shortcut_path_;
326 bool running_;
327 bool canceled_;
329 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag);
331 #endif // defined(OS_WIN)
333 AppsGridView::AppsGridView(AppsGridViewDelegate* delegate)
334 : model_(NULL),
335 item_list_(NULL),
336 delegate_(delegate),
337 folder_delegate_(NULL),
338 page_switcher_view_(NULL),
339 cols_(0),
340 rows_per_page_(0),
341 selected_view_(NULL),
342 drag_view_(NULL),
343 drag_start_page_(-1),
344 #if defined(OS_WIN)
345 use_synchronous_drag_(true),
346 #endif
347 drag_pointer_(NONE),
348 drop_attempt_(DROP_FOR_NONE),
349 drag_and_drop_host_(NULL),
350 forward_events_to_drag_and_drop_host_(false),
351 page_flip_target_(-1),
352 page_flip_delay_in_ms_(kPageFlipDelayInMs),
353 bounds_animator_(this),
354 activated_folder_item_view_(NULL),
355 dragging_for_reparent_item_(false) {
356 SetPaintToLayer(true);
357 // Clip any icons that are outside the grid view's bounds. These icons would
358 // otherwise be visible to the user when the grid view is off screen.
359 layer()->SetMasksToBounds(true);
360 SetFillsBoundsOpaquely(false);
362 pagination_model_.SetTransitionDurations(kPageTransitionDurationInMs,
363 kOverscrollPageTransitionDurationMs);
365 pagination_model_.AddObserver(this);
366 page_switcher_view_ = new PageSwitcher(&pagination_model_);
367 AddChildView(page_switcher_view_);
370 AppsGridView::~AppsGridView() {
371 // Coming here |drag_view_| should already be canceled since otherwise the
372 // drag would disappear after the app list got animated away and closed,
373 // which would look odd.
374 DCHECK(!drag_view_);
375 if (drag_view_)
376 EndDrag(true);
378 if (model_)
379 model_->RemoveObserver(this);
380 pagination_model_.RemoveObserver(this);
382 if (item_list_)
383 item_list_->RemoveObserver(this);
385 // Make sure |page_switcher_view_| is deleted before |pagination_model_|.
386 view_model_.Clear();
387 RemoveAllChildViews(true);
390 void AppsGridView::SetLayout(int cols, int rows_per_page) {
391 cols_ = cols;
392 rows_per_page_ = rows_per_page;
394 SetBorder(views::Border::CreateEmptyBorder(
395 kTopPadding, kLeftRightPadding, 0, kLeftRightPadding));
398 void AppsGridView::ResetForShowApps() {
399 activated_folder_item_view_ = NULL;
400 ClearDragState();
401 layer()->SetOpacity(1.0f);
402 SetVisible(true);
403 // Set all views to visible in case they weren't made visible again by an
404 // incomplete animation.
405 for (int i = 0; i < view_model_.view_size(); ++i) {
406 view_model_.view_at(i)->SetVisible(true);
408 CHECK_EQ(item_list_->item_count(),
409 static_cast<size_t>(view_model_.view_size()));
412 void AppsGridView::SetModel(AppListModel* model) {
413 if (model_)
414 model_->RemoveObserver(this);
416 model_ = model;
417 if (model_)
418 model_->AddObserver(this);
420 Update();
423 void AppsGridView::SetItemList(AppListItemList* item_list) {
424 if (item_list_)
425 item_list_->RemoveObserver(this);
426 item_list_ = item_list;
427 if (item_list_)
428 item_list_->AddObserver(this);
429 Update();
432 void AppsGridView::SetSelectedView(views::View* view) {
433 if (IsSelectedView(view) || IsDraggedView(view))
434 return;
436 Index index = GetIndexOfView(view);
437 if (IsValidIndex(index))
438 SetSelectedItemByIndex(index);
441 void AppsGridView::ClearSelectedView(views::View* view) {
442 if (view && IsSelectedView(view)) {
443 selected_view_->SchedulePaint();
444 selected_view_ = NULL;
448 void AppsGridView::ClearAnySelectedView() {
449 if (selected_view_) {
450 selected_view_->SchedulePaint();
451 selected_view_ = NULL;
455 bool AppsGridView::IsSelectedView(const views::View* view) const {
456 return selected_view_ == view;
459 void AppsGridView::EnsureViewVisible(const views::View* view) {
460 if (pagination_model_.has_transition())
461 return;
463 Index index = GetIndexOfView(view);
464 if (IsValidIndex(index))
465 pagination_model_.SelectPage(index.page, false);
468 void AppsGridView::InitiateDrag(AppListItemView* view,
469 Pointer pointer,
470 const ui::LocatedEvent& event) {
471 DCHECK(view);
472 if (drag_view_ || pulsing_blocks_model_.view_size())
473 return;
475 drag_view_ = view;
476 drag_view_init_index_ = GetIndexOfView(drag_view_);
477 drag_view_offset_ = event.location();
478 drag_start_page_ = pagination_model_.selected_page();
479 ExtractDragLocation(event, &drag_start_grid_view_);
480 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
483 void AppsGridView::StartSettingUpSynchronousDrag() {
484 #if defined(OS_WIN)
485 if (!delegate_ || !use_synchronous_drag_)
486 return;
488 // Folders and downloading items can't be integrated with the OS.
489 if (IsFolderItem(drag_view_->item()) || drag_view_->item()->is_installing())
490 return;
492 // Favor the drag and drop host over native win32 drag. For the Win8/ash
493 // launcher we want to have ashes drag and drop over win32's.
494 if (drag_and_drop_host_)
495 return;
497 // Never create a second synchronous drag if the drag started in a folder.
498 if (IsDraggingForReparentInRootLevelGridView())
499 return;
501 synchronous_drag_ = new SynchronousDrag(this, drag_view_, drag_view_offset_);
502 delegate_->GetShortcutPathForApp(drag_view_->item()->id(),
503 base::Bind(&AppsGridView::OnGotShortcutPath,
504 base::Unretained(this),
505 synchronous_drag_));
506 #endif
509 bool AppsGridView::RunSynchronousDrag() {
510 #if defined(OS_WIN)
511 if (!synchronous_drag_)
512 return false;
514 if (synchronous_drag_->CanRun()) {
515 if (IsDraggingForReparentInHiddenGridView())
516 folder_delegate_->SetRootLevelDragViewVisible(false);
517 synchronous_drag_->Run();
518 synchronous_drag_ = NULL;
519 return true;
520 } else if (!synchronous_drag_->running()) {
521 // The OS drag is not ready yet. If the root grid has a drag view because
522 // a reparent has started, ensure it is visible.
523 if (IsDraggingForReparentInHiddenGridView())
524 folder_delegate_->SetRootLevelDragViewVisible(true);
526 #endif
527 return false;
530 void AppsGridView::CleanUpSynchronousDrag() {
531 #if defined(OS_WIN)
532 if (synchronous_drag_)
533 synchronous_drag_->EndDragExternally();
535 synchronous_drag_ = NULL;
536 #endif
539 #if defined(OS_WIN)
540 void AppsGridView::OnGotShortcutPath(
541 scoped_refptr<SynchronousDrag> synchronous_drag,
542 const base::FilePath& path) {
543 // Drag may have ended before we get the shortcut path or a new drag may have
544 // begun.
545 if (synchronous_drag_ != synchronous_drag)
546 return;
547 // Setting the shortcut path here means the next time we hit UpdateDrag()
548 // we'll enter the synchronous drag.
549 // NOTE we don't Run() the drag here because that causes animations not to
550 // update for some reason.
551 synchronous_drag_->set_shortcut_path(path);
552 DCHECK(synchronous_drag_->CanRun());
554 #endif
556 bool AppsGridView::UpdateDragFromItem(Pointer pointer,
557 const ui::LocatedEvent& event) {
558 if (!drag_view_)
559 return false; // Drag canceled.
561 gfx::Point drag_point_in_grid_view;
562 ExtractDragLocation(event, &drag_point_in_grid_view);
563 UpdateDrag(pointer, drag_point_in_grid_view);
564 if (!dragging())
565 return false;
567 // If a drag and drop host is provided, see if the drag operation needs to be
568 // forwarded.
569 gfx::Point location_in_screen = drag_point_in_grid_view;
570 views::View::ConvertPointToScreen(this, &location_in_screen);
571 DispatchDragEventToDragAndDropHost(location_in_screen);
572 if (drag_and_drop_host_)
573 drag_and_drop_host_->UpdateDragIconProxy(location_in_screen);
574 return true;
577 void AppsGridView::UpdateDrag(Pointer pointer, const gfx::Point& point) {
578 if (folder_delegate_)
579 UpdateDragStateInsideFolder(pointer, point);
581 if (!drag_view_)
582 return; // Drag canceled.
584 if (RunSynchronousDrag())
585 return;
587 gfx::Vector2d drag_vector(point - drag_start_grid_view_);
588 if (!dragging() && ExceededDragThreshold(drag_vector)) {
589 drag_pointer_ = pointer;
590 // Move the view to the front so that it appears on top of other views.
591 ReorderChildView(drag_view_, -1);
592 bounds_animator_.StopAnimatingView(drag_view_);
593 // Stopping the animation may have invalidated our drag view due to the
594 // view hierarchy changing.
595 if (!drag_view_)
596 return;
598 StartSettingUpSynchronousDrag();
599 if (!dragging_for_reparent_item_)
600 StartDragAndDropHostDrag(point);
603 if (drag_pointer_ != pointer)
604 return;
606 last_drag_point_ = point;
607 const Index last_drop_target = drop_target_;
608 DropAttempt last_drop_attempt = drop_attempt_;
609 CalculateDropTarget(last_drag_point_, false);
611 if (IsPointWithinDragBuffer(last_drag_point_))
612 MaybeStartPageFlipTimer(last_drag_point_);
613 else
614 StopPageFlipTimer();
616 gfx::Point page_switcher_point(last_drag_point_);
617 views::View::ConvertPointToTarget(this, page_switcher_view_,
618 &page_switcher_point);
619 page_switcher_view_->UpdateUIForDragPoint(page_switcher_point);
621 if (!EnableFolderDragDropUI()) {
622 if (last_drop_target != drop_target_)
623 AnimateToIdealBounds();
624 drag_view_->SetPosition(drag_view_start_ + drag_vector);
625 return;
628 // Update drag with folder UI enabled.
629 if (last_drop_target != drop_target_ ||
630 last_drop_attempt != drop_attempt_) {
631 if (drop_attempt_ == DROP_FOR_REORDER) {
632 folder_dropping_timer_.Stop();
633 reorder_timer_.Start(FROM_HERE,
634 base::TimeDelta::FromMilliseconds(kReorderDelay),
635 this, &AppsGridView::OnReorderTimer);
636 } else if (drop_attempt_ == DROP_FOR_FOLDER) {
637 reorder_timer_.Stop();
638 folder_dropping_timer_.Start(FROM_HERE,
639 base::TimeDelta::FromMilliseconds(kFolderDroppingDelay),
640 this, &AppsGridView::OnFolderDroppingTimer);
643 // Reset the previous drop target.
644 SetAsFolderDroppingTarget(last_drop_target, false);
647 drag_view_->SetPosition(drag_view_start_ + drag_vector);
650 void AppsGridView::EndDrag(bool cancel) {
651 // EndDrag was called before if |drag_view_| is NULL.
652 if (!drag_view_)
653 return;
655 // Coming here a drag and drop was in progress.
656 bool landed_in_drag_and_drop_host = forward_events_to_drag_and_drop_host_;
657 if (forward_events_to_drag_and_drop_host_) {
658 DCHECK(!IsDraggingForReparentInRootLevelGridView());
659 forward_events_to_drag_and_drop_host_ = false;
660 drag_and_drop_host_->EndDrag(cancel);
661 if (IsDraggingForReparentInHiddenGridView()) {
662 folder_delegate_->DispatchEndDragEventForReparent(
663 true /* events_forwarded_to_drag_drop_host */,
664 cancel /* cancel_drag */);
666 } else {
667 if (IsDraggingForReparentInHiddenGridView()) {
668 // Forward the EndDrag event to the root level grid view.
669 folder_delegate_->DispatchEndDragEventForReparent(
670 false /* events_forwarded_to_drag_drop_host */,
671 cancel /* cancel_drag */);
672 EndDragForReparentInHiddenFolderGridView();
673 return;
676 if (IsDraggingForReparentInRootLevelGridView()) {
677 // An EndDrag can be received during a reparent via a model change. This
678 // is always a cancel and needs to be forwarded to the folder.
679 DCHECK(cancel);
680 delegate_->CancelDragInActiveFolder();
681 return;
684 if (!cancel && dragging()) {
685 // Regular drag ending path, ie, not for reparenting.
686 CalculateDropTarget(last_drag_point_, true);
687 if (IsValidIndex(drop_target_)) {
688 if (!EnableFolderDragDropUI()) {
689 MoveItemInModel(drag_view_, drop_target_);
690 } else {
691 if (drop_attempt_ == DROP_FOR_REORDER)
692 MoveItemInModel(drag_view_, drop_target_);
693 else if (drop_attempt_ == DROP_FOR_FOLDER)
694 MoveItemToFolder(drag_view_, drop_target_);
700 if (drag_and_drop_host_) {
701 // If we had a drag and drop proxy icon, we delete it and make the real
702 // item visible again.
703 drag_and_drop_host_->DestroyDragIconProxy();
704 if (landed_in_drag_and_drop_host) {
705 // Move the item directly to the target location, avoiding the "zip back"
706 // animation if the user was pinning it to the shelf.
707 int i = drop_target_.slot;
708 gfx::Rect bounds = view_model_.ideal_bounds(i);
709 drag_view_->SetBoundsRect(bounds);
711 // Fade in slowly if it landed in the shelf.
712 SetViewHidden(drag_view_,
713 false /* show */,
714 !landed_in_drag_and_drop_host /* animate */);
717 // The drag can be ended after the synchronous drag is created but before it
718 // is Run().
719 CleanUpSynchronousDrag();
721 SetAsFolderDroppingTarget(drop_target_, false);
722 ClearDragState();
723 AnimateToIdealBounds();
725 StopPageFlipTimer();
727 // If user releases mouse inside a folder's grid view, burst the folder
728 // container ink bubble.
729 if (folder_delegate_ && !IsDraggingForReparentInHiddenGridView())
730 folder_delegate_->UpdateFolderViewBackground(false);
733 void AppsGridView::StopPageFlipTimer() {
734 page_flip_timer_.Stop();
735 page_flip_target_ = -1;
738 AppListItemView* AppsGridView::GetItemViewAt(int index) const {
739 DCHECK(index >= 0 && index < view_model_.view_size());
740 return static_cast<AppListItemView*>(view_model_.view_at(index));
743 void AppsGridView::SetTopItemViewsVisible(bool visible) {
744 int top_item_count = std::min(static_cast<int>(kNumFolderTopItems),
745 view_model_.view_size());
746 for (int i = 0; i < top_item_count; ++i)
747 GetItemViewAt(i)->icon()->SetVisible(visible);
750 void AppsGridView::ScheduleShowHideAnimation(bool show) {
751 // Stop any previous animation.
752 layer()->GetAnimator()->StopAnimating();
754 // Set initial state.
755 SetVisible(true);
756 layer()->SetOpacity(show ? 0.0f : 1.0f);
758 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
759 animation.AddObserver(this);
760 animation.SetTweenType(
761 show ? kFolderFadeInTweenType : kFolderFadeOutTweenType);
762 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
763 show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
765 layer()->SetOpacity(show ? 1.0f : 0.0f);
768 void AppsGridView::InitiateDragFromReparentItemInRootLevelGridView(
769 AppListItemView* original_drag_view,
770 const gfx::Rect& drag_view_rect,
771 const gfx::Point& drag_point) {
772 DCHECK(original_drag_view && !drag_view_);
773 DCHECK(!dragging_for_reparent_item_);
775 // Create a new AppListItemView to duplicate the original_drag_view in the
776 // folder's grid view.
777 AppListItemView* view = new AppListItemView(this, original_drag_view->item());
778 AddChildView(view);
779 drag_view_ = view;
780 drag_view_->SetPaintToLayer(true);
781 // Note: For testing purpose, SetFillsBoundsOpaquely can be set to true to
782 // show the gray background.
783 drag_view_->SetFillsBoundsOpaquely(false);
784 drag_view_->SetBoundsRect(drag_view_rect);
785 drag_view_->SetDragUIState(); // Hide the title of the drag_view_.
787 // Hide the drag_view_ for drag icon proxy.
788 SetViewHidden(drag_view_,
789 true /* hide */,
790 true /* no animate */);
792 // Add drag_view_ to the end of the view_model_.
793 view_model_.Add(drag_view_, view_model_.view_size());
795 drag_start_page_ = pagination_model_.selected_page();
796 drag_start_grid_view_ = drag_point;
798 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
800 // Set the flag in root level grid view.
801 dragging_for_reparent_item_ = true;
804 void AppsGridView::UpdateDragFromReparentItem(Pointer pointer,
805 const gfx::Point& drag_point) {
806 // Note that if a cancel ocurrs while reparenting, the |drag_view_| in both
807 // root and folder grid views is cleared, so the check in UpdateDragFromItem()
808 // for |drag_view_| being NULL (in the folder grid) is sufficient.
809 DCHECK(drag_view_);
810 DCHECK(IsDraggingForReparentInRootLevelGridView());
812 UpdateDrag(pointer, drag_point);
815 bool AppsGridView::IsDraggedView(const views::View* view) const {
816 return drag_view_ == view;
819 void AppsGridView::ClearDragState() {
820 drop_attempt_ = DROP_FOR_NONE;
821 drag_pointer_ = NONE;
822 drop_target_ = Index();
823 drag_start_grid_view_ = gfx::Point();
824 drag_start_page_ = -1;
825 drag_view_offset_ = gfx::Point();
827 if (drag_view_) {
828 drag_view_->OnDragEnded();
829 if (IsDraggingForReparentInRootLevelGridView()) {
830 const int drag_view_index = view_model_.GetIndexOfView(drag_view_);
831 CHECK_EQ(view_model_.view_size() - 1, drag_view_index);
832 DeleteItemViewAtIndex(drag_view_index);
835 drag_view_ = NULL;
836 dragging_for_reparent_item_ = false;
839 void AppsGridView::SetDragViewVisible(bool visible) {
840 DCHECK(drag_view_);
841 SetViewHidden(drag_view_, !visible, true);
844 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
845 ApplicationDragAndDropHost* drag_and_drop_host) {
846 drag_and_drop_host_ = drag_and_drop_host;
849 void AppsGridView::Prerender(int page_index) {
850 Layout();
851 int start = std::max(0, (page_index - kPrerenderPages) * tiles_per_page());
852 int end = std::min(view_model_.view_size(),
853 (page_index + 1 + kPrerenderPages) * tiles_per_page());
854 for (int i = start; i < end; i++) {
855 AppListItemView* v = static_cast<AppListItemView*>(view_model_.view_at(i));
856 v->Prerender();
860 bool AppsGridView::IsAnimatingView(views::View* view) {
861 return bounds_animator_.IsAnimating(view);
864 gfx::Size AppsGridView::GetPreferredSize() const {
865 const gfx::Insets insets(GetInsets());
866 const gfx::Size tile_size = gfx::Size(kPreferredTileWidth,
867 kPreferredTileHeight);
868 const int page_switcher_height =
869 page_switcher_view_->GetPreferredSize().height();
870 return gfx::Size(
871 tile_size.width() * cols_ + insets.width(),
872 tile_size.height() * rows_per_page_ +
873 page_switcher_height + insets.height());
876 bool AppsGridView::GetDropFormats(
877 int* formats,
878 std::set<OSExchangeData::CustomFormat>* custom_formats) {
879 // TODO(koz): Only accept a specific drag type for app shortcuts.
880 *formats = OSExchangeData::FILE_NAME;
881 return true;
884 bool AppsGridView::CanDrop(const OSExchangeData& data) {
885 return true;
888 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent& event) {
889 return ui::DragDropTypes::DRAG_MOVE;
892 void AppsGridView::Layout() {
893 if (bounds_animator_.IsAnimating())
894 bounds_animator_.Cancel();
896 CalculateIdealBounds();
897 for (int i = 0; i < view_model_.view_size(); ++i) {
898 views::View* view = view_model_.view_at(i);
899 if (view != drag_view_)
900 view->SetBoundsRect(view_model_.ideal_bounds(i));
902 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_);
904 const int page_switcher_height =
905 page_switcher_view_->GetPreferredSize().height();
906 gfx::Rect rect(GetContentsBounds());
907 rect.set_y(rect.bottom() - page_switcher_height);
908 rect.set_height(page_switcher_height);
909 page_switcher_view_->SetBoundsRect(rect);
912 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) {
913 bool handled = false;
914 if (selected_view_)
915 handled = selected_view_->OnKeyPressed(event);
917 if (!handled) {
918 const int forward_dir = base::i18n::IsRTL() ? -1 : 1;
919 switch (event.key_code()) {
920 case ui::VKEY_LEFT:
921 MoveSelected(0, -forward_dir, 0);
922 return true;
923 case ui::VKEY_RIGHT:
924 MoveSelected(0, forward_dir, 0);
925 return true;
926 case ui::VKEY_UP:
927 MoveSelected(0, 0, -1);
928 return true;
929 case ui::VKEY_DOWN:
930 MoveSelected(0, 0, 1);
931 return true;
932 case ui::VKEY_PRIOR: {
933 MoveSelected(-1, 0, 0);
934 return true;
936 case ui::VKEY_NEXT: {
937 MoveSelected(1, 0, 0);
938 return true;
940 default:
941 break;
945 return handled;
948 bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) {
949 bool handled = false;
950 if (selected_view_)
951 handled = selected_view_->OnKeyReleased(event);
953 return handled;
956 void AppsGridView::ViewHierarchyChanged(
957 const ViewHierarchyChangedDetails& details) {
958 if (!details.is_add && details.parent == this) {
959 // The view being delete should not have reference in |view_model_|.
960 CHECK_EQ(-1, view_model_.GetIndexOfView(details.child));
962 if (selected_view_ == details.child)
963 selected_view_ = NULL;
964 if (activated_folder_item_view_ == details.child)
965 activated_folder_item_view_ = NULL;
967 if (drag_view_ == details.child)
968 EndDrag(true);
970 bounds_animator_.StopAnimatingView(details.child);
974 void AppsGridView::Update() {
975 DCHECK(!selected_view_ && !drag_view_);
976 view_model_.Clear();
977 if (!item_list_ || !item_list_->item_count())
978 return;
979 for (size_t i = 0; i < item_list_->item_count(); ++i) {
980 views::View* view = CreateViewForItemAtIndex(i);
981 view_model_.Add(view, i);
982 AddChildView(view);
984 UpdatePaging();
985 UpdatePulsingBlockViews();
986 Layout();
987 SchedulePaint();
990 void AppsGridView::UpdatePaging() {
991 int total_page = view_model_.view_size() && tiles_per_page()
992 ? (view_model_.view_size() - 1) / tiles_per_page() + 1
993 : 0;
995 pagination_model_.SetTotalPages(total_page);
998 void AppsGridView::UpdatePulsingBlockViews() {
999 const int existing_items = item_list_ ? item_list_->item_count() : 0;
1000 const int available_slots =
1001 tiles_per_page() - existing_items % tiles_per_page();
1002 const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
1003 available_slots : 0;
1005 if (pulsing_blocks_model_.view_size() == desired)
1006 return;
1008 while (pulsing_blocks_model_.view_size() > desired) {
1009 views::View* view = pulsing_blocks_model_.view_at(0);
1010 pulsing_blocks_model_.Remove(0);
1011 delete view;
1014 while (pulsing_blocks_model_.view_size() < desired) {
1015 views::View* view = new PulsingBlockView(
1016 gfx::Size(kPreferredTileWidth, kPreferredTileHeight), true);
1017 pulsing_blocks_model_.Add(view, 0);
1018 AddChildView(view);
1022 views::View* AppsGridView::CreateViewForItemAtIndex(size_t index) {
1023 // The drag_view_ might be pending for deletion, therefore view_model_
1024 // may have one more item than item_list_.
1025 DCHECK_LE(index, item_list_->item_count());
1026 AppListItemView* view = new AppListItemView(this,
1027 item_list_->item_at(index));
1028 view->SetPaintToLayer(true);
1029 view->SetFillsBoundsOpaquely(false);
1030 return view;
1033 AppsGridView::Index AppsGridView::GetIndexFromModelIndex(
1034 int model_index) const {
1035 return Index(model_index / tiles_per_page(), model_index % tiles_per_page());
1038 int AppsGridView::GetModelIndexFromIndex(const Index& index) const {
1039 return index.page * tiles_per_page() + index.slot;
1042 void AppsGridView::SetSelectedItemByIndex(const Index& index) {
1043 if (GetIndexOfView(selected_view_) == index)
1044 return;
1046 views::View* new_selection = GetViewAtIndex(index);
1047 if (!new_selection)
1048 return; // Keep current selection.
1050 if (selected_view_)
1051 selected_view_->SchedulePaint();
1053 EnsureViewVisible(new_selection);
1054 selected_view_ = new_selection;
1055 selected_view_->SchedulePaint();
1056 selected_view_->NotifyAccessibilityEvent(
1057 ui::AX_EVENT_FOCUS, true);
1060 bool AppsGridView::IsValidIndex(const Index& index) const {
1061 return index.page >= 0 && index.page < pagination_model_.total_pages() &&
1062 index.slot >= 0 && index.slot < tiles_per_page() &&
1063 GetModelIndexFromIndex(index) < view_model_.view_size();
1066 AppsGridView::Index AppsGridView::GetIndexOfView(
1067 const views::View* view) const {
1068 const int model_index = view_model_.GetIndexOfView(view);
1069 if (model_index == -1)
1070 return Index();
1072 return GetIndexFromModelIndex(model_index);
1075 views::View* AppsGridView::GetViewAtIndex(const Index& index) const {
1076 if (!IsValidIndex(index))
1077 return NULL;
1079 const int model_index = GetModelIndexFromIndex(index);
1080 return view_model_.view_at(model_index);
1083 void AppsGridView::MoveSelected(int page_delta,
1084 int slot_x_delta,
1085 int slot_y_delta) {
1086 if (!selected_view_)
1087 return SetSelectedItemByIndex(Index(pagination_model_.selected_page(), 0));
1089 const Index& selected = GetIndexOfView(selected_view_);
1090 int target_slot = selected.slot + slot_x_delta + slot_y_delta * cols_;
1092 if (selected.slot % cols_ == 0 && slot_x_delta == -1) {
1093 if (selected.page > 0) {
1094 page_delta = -1;
1095 target_slot = selected.slot + cols_ - 1;
1096 } else {
1097 target_slot = selected.slot;
1101 if (selected.slot % cols_ == cols_ - 1 && slot_x_delta == 1) {
1102 if (selected.page < pagination_model_.total_pages() - 1) {
1103 page_delta = 1;
1104 target_slot = selected.slot - cols_ + 1;
1105 } else {
1106 target_slot = selected.slot;
1110 // Clamp the target slot to the last item if we are moving to the last page
1111 // but our target slot is past the end of the item list.
1112 if (page_delta &&
1113 selected.page + page_delta == pagination_model_.total_pages() - 1) {
1114 int last_item_slot = (view_model_.view_size() - 1) % tiles_per_page();
1115 if (last_item_slot < target_slot) {
1116 target_slot = last_item_slot;
1120 int target_page = std::min(pagination_model_.total_pages() - 1,
1121 std::max(selected.page + page_delta, 0));
1122 SetSelectedItemByIndex(Index(target_page, target_slot));
1125 void AppsGridView::CalculateIdealBounds() {
1126 gfx::Rect rect(GetContentsBounds());
1127 if (rect.IsEmpty())
1128 return;
1130 gfx::Size tile_size(kPreferredTileWidth, kPreferredTileHeight);
1132 gfx::Rect grid_rect(gfx::Size(tile_size.width() * cols_,
1133 tile_size.height() * rows_per_page_));
1134 grid_rect.Intersect(rect);
1136 // Page width including padding pixels. A tile.x + page_width means the same
1137 // tile slot in the next page.
1138 const int page_width = grid_rect.width() + kPagePadding;
1140 // If there is a transition, calculates offset for current and target page.
1141 const int current_page = pagination_model_.selected_page();
1142 const PaginationModel::Transition& transition =
1143 pagination_model_.transition();
1144 const bool is_valid = pagination_model_.is_valid_page(transition.target_page);
1146 // Transition to right means negative offset.
1147 const int dir = transition.target_page > current_page ? -1 : 1;
1148 const int transition_offset = is_valid ?
1149 transition.progress * page_width * dir : 0;
1151 const int total_views =
1152 view_model_.view_size() + pulsing_blocks_model_.view_size();
1153 int slot_index = 0;
1154 for (int i = 0; i < total_views; ++i) {
1155 if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_) {
1156 if (EnableFolderDragDropUI() && drop_attempt_ == DROP_FOR_FOLDER)
1157 ++slot_index;
1158 continue;
1161 Index view_index = GetIndexFromModelIndex(slot_index);
1163 if (drop_target_ == view_index) {
1164 if (EnableFolderDragDropUI() && drop_attempt_ == DROP_FOR_FOLDER) {
1165 view_index = GetIndexFromModelIndex(slot_index);
1166 } else if (!EnableFolderDragDropUI() ||
1167 drop_attempt_ == DROP_FOR_REORDER) {
1168 ++slot_index;
1169 view_index = GetIndexFromModelIndex(slot_index);
1173 // Decides an x_offset for current item.
1174 int x_offset = 0;
1175 if (view_index.page < current_page)
1176 x_offset = -page_width;
1177 else if (view_index.page > current_page)
1178 x_offset = page_width;
1180 if (is_valid) {
1181 if (view_index.page == current_page ||
1182 view_index.page == transition.target_page) {
1183 x_offset += transition_offset;
1187 const int row = view_index.slot / cols_;
1188 const int col = view_index.slot % cols_;
1189 gfx::Rect tile_slot(
1190 gfx::Point(grid_rect.x() + col * tile_size.width() + x_offset,
1191 grid_rect.y() + row * tile_size.height()),
1192 tile_size);
1193 if (i < view_model_.view_size()) {
1194 view_model_.set_ideal_bounds(i, tile_slot);
1195 } else {
1196 pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
1197 tile_slot);
1200 ++slot_index;
1204 void AppsGridView::AnimateToIdealBounds() {
1205 const gfx::Rect visible_bounds(GetVisibleBounds());
1207 CalculateIdealBounds();
1208 for (int i = 0; i < view_model_.view_size(); ++i) {
1209 views::View* view = view_model_.view_at(i);
1210 if (view == drag_view_)
1211 continue;
1213 const gfx::Rect& target = view_model_.ideal_bounds(i);
1214 if (bounds_animator_.GetTargetBounds(view) == target)
1215 continue;
1217 const gfx::Rect& current = view->bounds();
1218 const bool current_visible = visible_bounds.Intersects(current);
1219 const bool target_visible = visible_bounds.Intersects(target);
1220 const bool visible = current_visible || target_visible;
1222 const int y_diff = target.y() - current.y();
1223 if (visible && y_diff && y_diff % kPreferredTileHeight == 0) {
1224 AnimationBetweenRows(view,
1225 current_visible,
1226 current,
1227 target_visible,
1228 target);
1229 } else if (visible || bounds_animator_.IsAnimating(view)) {
1230 bounds_animator_.AnimateViewTo(view, target);
1231 bounds_animator_.SetAnimationDelegate(
1232 view,
1233 scoped_ptr<gfx::AnimationDelegate>(
1234 new ItemMoveAnimationDelegate(view)));
1235 } else {
1236 view->SetBoundsRect(target);
1241 void AppsGridView::AnimationBetweenRows(views::View* view,
1242 bool animate_current,
1243 const gfx::Rect& current,
1244 bool animate_target,
1245 const gfx::Rect& target) {
1246 // Determine page of |current| and |target|. -1 means in the left invisible
1247 // page, 0 is the center visible page and 1 means in the right invisible page.
1248 const int current_page = current.x() < 0 ? -1 :
1249 current.x() >= width() ? 1 : 0;
1250 const int target_page = target.x() < 0 ? -1 :
1251 target.x() >= width() ? 1 : 0;
1253 const int dir = current_page < target_page ||
1254 (current_page == target_page && current.y() < target.y()) ? 1 : -1;
1256 scoped_ptr<ui::Layer> layer;
1257 if (animate_current) {
1258 layer = view->RecreateLayer();
1259 layer->SuppressPaint();
1261 view->SetFillsBoundsOpaquely(false);
1262 view->layer()->SetOpacity(0.f);
1265 gfx::Rect current_out(current);
1266 current_out.Offset(dir * kPreferredTileWidth, 0);
1268 gfx::Rect target_in(target);
1269 if (animate_target)
1270 target_in.Offset(-dir * kPreferredTileWidth, 0);
1271 view->SetBoundsRect(target_in);
1272 bounds_animator_.AnimateViewTo(view, target);
1274 bounds_animator_.SetAnimationDelegate(
1275 view,
1276 scoped_ptr<gfx::AnimationDelegate>(
1277 new RowMoveAnimationDelegate(view, layer.release(), current_out)));
1280 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event,
1281 gfx::Point* drag_point) {
1282 #if defined(USE_AURA) && !defined(OS_WIN)
1283 // Use root location of |event| instead of location in |drag_view_|'s
1284 // coordinates because |drag_view_| has a scale transform and location
1285 // could have integer round error and causes jitter.
1286 *drag_point = event.root_location();
1288 // GetWidget() could be NULL for tests.
1289 if (GetWidget()) {
1290 aura::Window::ConvertPointToTarget(
1291 GetWidget()->GetNativeWindow()->GetRootWindow(),
1292 GetWidget()->GetNativeWindow(),
1293 drag_point);
1296 views::View::ConvertPointFromWidget(this, drag_point);
1297 #else
1298 // For non-aura, root location is not clearly defined but |drag_view_| does
1299 // not have the scale transform. So no round error would be introduced and
1300 // it's okay to use View::ConvertPointToTarget.
1301 *drag_point = event.location();
1302 views::View::ConvertPointToTarget(drag_view_, this, drag_point);
1303 #endif
1306 void AppsGridView::CalculateDropTarget(const gfx::Point& drag_point,
1307 bool use_page_button_hovering) {
1308 if (EnableFolderDragDropUI()) {
1309 CalculateDropTargetWithFolderEnabled(drag_point, use_page_button_hovering);
1310 return;
1313 int current_page = pagination_model_.selected_page();
1314 gfx::Point point(drag_point);
1315 if (!IsPointWithinDragBuffer(drag_point)) {
1316 point = drag_start_grid_view_;
1317 current_page = drag_start_page_;
1320 if (use_page_button_hovering &&
1321 page_switcher_view_->bounds().Contains(point)) {
1322 gfx::Point page_switcher_point(point);
1323 views::View::ConvertPointToTarget(this, page_switcher_view_,
1324 &page_switcher_point);
1325 int page = page_switcher_view_->GetPageForPoint(page_switcher_point);
1326 if (pagination_model_.is_valid_page(page)) {
1327 drop_target_.page = page;
1328 drop_target_.slot = tiles_per_page() - 1;
1330 } else {
1331 gfx::Rect bounds(GetContentsBounds());
1332 const int drop_row = (point.y() - bounds.y()) / kPreferredTileHeight;
1333 const int drop_col = std::min(cols_ - 1,
1334 (point.x() - bounds.x()) / kPreferredTileWidth);
1336 drop_target_.page = current_page;
1337 drop_target_.slot = std::max(0, std::min(
1338 tiles_per_page() - 1,
1339 drop_row * cols_ + drop_col));
1342 // Limits to the last possible slot on last page.
1343 if (drop_target_.page == pagination_model_.total_pages() - 1) {
1344 drop_target_.slot = std::min(
1345 (view_model_.view_size() - 1) % tiles_per_page(),
1346 drop_target_.slot);
1351 void AppsGridView::CalculateDropTargetWithFolderEnabled(
1352 const gfx::Point& drag_point,
1353 bool use_page_button_hovering) {
1354 gfx::Point point(drag_point);
1355 if (!IsPointWithinDragBuffer(drag_point)) {
1356 point = drag_start_grid_view_;
1359 if (use_page_button_hovering &&
1360 page_switcher_view_->bounds().Contains(point)) {
1361 gfx::Point page_switcher_point(point);
1362 views::View::ConvertPointToTarget(this, page_switcher_view_,
1363 &page_switcher_point);
1364 int page = page_switcher_view_->GetPageForPoint(page_switcher_point);
1365 if (pagination_model_.is_valid_page(page))
1366 drop_attempt_ = DROP_FOR_NONE;
1367 } else {
1368 DCHECK(drag_view_);
1369 // Try to find the nearest target for folder dropping or re-ordering.
1370 drop_target_ = GetNearestTileForDragView();
1374 void AppsGridView::OnReorderTimer() {
1375 if (drop_attempt_ == DROP_FOR_REORDER)
1376 AnimateToIdealBounds();
1379 void AppsGridView::OnFolderItemReparentTimer() {
1380 DCHECK(folder_delegate_);
1381 if (drag_out_of_folder_container_ && drag_view_) {
1382 folder_delegate_->ReparentItem(drag_view_, last_drag_point_);
1384 // Set the flag in the folder's grid view.
1385 dragging_for_reparent_item_ = true;
1387 // Do not observe any data change since it is going to be hidden.
1388 item_list_->RemoveObserver(this);
1389 item_list_ = NULL;
1393 void AppsGridView::OnFolderDroppingTimer() {
1394 if (drop_attempt_ == DROP_FOR_FOLDER)
1395 SetAsFolderDroppingTarget(drop_target_, true);
1398 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer,
1399 const gfx::Point& drag_point) {
1400 if (IsUnderOEMFolder())
1401 return;
1403 if (IsDraggingForReparentInHiddenGridView()) {
1404 // Dispatch drag event to root level grid view for re-parenting folder
1405 // folder item purpose.
1406 DispatchDragEventForReparent(pointer, drag_point);
1407 return;
1410 // Regular drag and drop in a folder's grid view.
1411 folder_delegate_->UpdateFolderViewBackground(true);
1413 // Calculate if the drag_view_ is dragged out of the folder's container
1414 // ink bubble.
1415 gfx::Rect bounds_to_folder_view = ConvertRectToParent(drag_view_->bounds());
1416 gfx::Point pt = bounds_to_folder_view.CenterPoint();
1417 bool is_item_dragged_out_of_folder =
1418 folder_delegate_->IsPointOutsideOfFolderBoundary(pt);
1419 if (is_item_dragged_out_of_folder) {
1420 if (!drag_out_of_folder_container_) {
1421 folder_item_reparent_timer_.Start(
1422 FROM_HERE,
1423 base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay),
1424 this,
1425 &AppsGridView::OnFolderItemReparentTimer);
1426 drag_out_of_folder_container_ = true;
1428 } else {
1429 folder_item_reparent_timer_.Stop();
1430 drag_out_of_folder_container_ = false;
1434 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1435 return (!folder_delegate_ && dragging_for_reparent_item_);
1438 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1439 return (folder_delegate_ && dragging_for_reparent_item_);
1442 gfx::Rect AppsGridView::GetTargetIconRectInFolder(
1443 AppListItemView* drag_item_view,
1444 AppListItemView* folder_item_view) {
1445 gfx::Rect view_ideal_bounds = view_model_.ideal_bounds(
1446 view_model_.GetIndexOfView(folder_item_view));
1447 gfx::Rect icon_ideal_bounds =
1448 folder_item_view->GetIconBoundsForTargetViewBounds(view_ideal_bounds);
1449 AppListFolderItem* folder_item =
1450 static_cast<AppListFolderItem*>(folder_item_view->item());
1451 return folder_item->GetTargetIconRectInFolderForItem(
1452 drag_item_view->item(), icon_ideal_bounds);
1455 bool AppsGridView::IsUnderOEMFolder() {
1456 if (!folder_delegate_)
1457 return false;
1459 return folder_delegate_->IsOEMFolder();
1462 void AppsGridView::DispatchDragEventForReparent(Pointer pointer,
1463 const gfx::Point& drag_point) {
1464 folder_delegate_->DispatchDragEventForReparent(pointer, drag_point);
1467 void AppsGridView::EndDragFromReparentItemInRootLevel(
1468 bool events_forwarded_to_drag_drop_host,
1469 bool cancel_drag) {
1470 // EndDrag was called before if |drag_view_| is NULL.
1471 if (!drag_view_)
1472 return;
1474 DCHECK(IsDraggingForReparentInRootLevelGridView());
1475 bool cancel_reparent = cancel_drag || drop_attempt_ == DROP_FOR_NONE;
1476 if (!events_forwarded_to_drag_drop_host && !cancel_reparent) {
1477 CalculateDropTarget(last_drag_point_, true);
1478 if (IsValidIndex(drop_target_)) {
1479 if (drop_attempt_ == DROP_FOR_REORDER) {
1480 ReparentItemForReorder(drag_view_, drop_target_);
1481 } else if (drop_attempt_ == DROP_FOR_FOLDER) {
1482 ReparentItemToAnotherFolder(drag_view_, drop_target_);
1485 SetViewHidden(drag_view_, false /* show */, true /* no animate */);
1488 // The drag can be ended after the synchronous drag is created but before it
1489 // is Run().
1490 CleanUpSynchronousDrag();
1492 SetAsFolderDroppingTarget(drop_target_, false);
1493 if (cancel_reparent) {
1494 CancelFolderItemReparent(drag_view_);
1495 } else {
1496 // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1497 // cleaning up the newly created AppListItemView, effectively claiming
1498 // ownership of the newly created drag view.
1499 drag_view_->OnDragEnded();
1500 drag_view_ = NULL;
1502 ClearDragState();
1503 AnimateToIdealBounds();
1505 StopPageFlipTimer();
1508 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1509 if (drag_and_drop_host_) {
1510 // If we had a drag and drop proxy icon, we delete it and make the real
1511 // item visible again.
1512 drag_and_drop_host_->DestroyDragIconProxy();
1515 // The drag can be ended after the synchronous drag is created but before it
1516 // is Run().
1517 CleanUpSynchronousDrag();
1519 SetAsFolderDroppingTarget(drop_target_, false);
1520 ClearDragState();
1523 void AppsGridView::OnFolderItemRemoved() {
1524 DCHECK(folder_delegate_);
1525 item_list_ = NULL;
1528 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) {
1529 // When a drag and drop host is given, the item can be dragged out of the app
1530 // list window. In that case a proxy widget needs to be used.
1531 // Note: This code has very likely to be changed for Windows (non metro mode)
1532 // when a |drag_and_drop_host_| gets implemented.
1533 if (!drag_view_ || !drag_and_drop_host_)
1534 return;
1536 gfx::Point screen_location = grid_location;
1537 views::View::ConvertPointToScreen(this, &screen_location);
1539 // Determine the mouse offset to the center of the icon so that the drag and
1540 // drop host follows this layer.
1541 gfx::Vector2d delta = drag_view_offset_ -
1542 drag_view_->GetLocalBounds().CenterPoint();
1543 delta.set_y(delta.y() + drag_view_->title()->size().height() / 2);
1545 // We have to hide the original item since the drag and drop host will do
1546 // the OS dependent code to "lift off the dragged item".
1547 DCHECK(!IsDraggingForReparentInRootLevelGridView());
1548 drag_and_drop_host_->CreateDragIconProxy(screen_location,
1549 drag_view_->item()->icon(),
1550 drag_view_,
1551 delta,
1552 kDragAndDropProxyScale);
1553 SetViewHidden(drag_view_,
1554 true /* hide */,
1555 true /* no animation */);
1558 void AppsGridView::DispatchDragEventToDragAndDropHost(
1559 const gfx::Point& location_in_screen_coordinates) {
1560 if (!drag_view_ || !drag_and_drop_host_)
1561 return;
1563 if (GetLocalBounds().Contains(last_drag_point_)) {
1564 // The event was issued inside the app menu and we should get all events.
1565 if (forward_events_to_drag_and_drop_host_) {
1566 // The DnD host was previously called and needs to be informed that the
1567 // session returns to the owner.
1568 forward_events_to_drag_and_drop_host_ = false;
1569 drag_and_drop_host_->EndDrag(true);
1571 } else {
1572 if (IsFolderItem(drag_view_->item()))
1573 return;
1575 // The event happened outside our app menu and we might need to dispatch.
1576 if (forward_events_to_drag_and_drop_host_) {
1577 // Dispatch since we have already started.
1578 if (!drag_and_drop_host_->Drag(location_in_screen_coordinates)) {
1579 // The host is not active any longer and we cancel the operation.
1580 forward_events_to_drag_and_drop_host_ = false;
1581 drag_and_drop_host_->EndDrag(true);
1583 } else {
1584 if (drag_and_drop_host_->StartDrag(drag_view_->item()->id(),
1585 location_in_screen_coordinates)) {
1586 // From now on we forward the drag events.
1587 forward_events_to_drag_and_drop_host_ = true;
1588 // Any flip operations are stopped.
1589 StopPageFlipTimer();
1595 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) {
1596 if (!IsPointWithinDragBuffer(drag_point))
1597 StopPageFlipTimer();
1598 int new_page_flip_target = -1;
1600 if (page_switcher_view_->bounds().Contains(drag_point)) {
1601 gfx::Point page_switcher_point(drag_point);
1602 views::View::ConvertPointToTarget(this, page_switcher_view_,
1603 &page_switcher_point);
1604 new_page_flip_target =
1605 page_switcher_view_->GetPageForPoint(page_switcher_point);
1608 // TODO(xiyuan): Fix this for RTL.
1609 if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize)
1610 new_page_flip_target = pagination_model_.selected_page() - 1;
1612 if (new_page_flip_target == -1 &&
1613 drag_point.x() > width() - kPageFlipZoneSize) {
1614 new_page_flip_target = pagination_model_.selected_page() + 1;
1617 if (new_page_flip_target == page_flip_target_)
1618 return;
1620 StopPageFlipTimer();
1621 if (pagination_model_.is_valid_page(new_page_flip_target)) {
1622 page_flip_target_ = new_page_flip_target;
1624 if (page_flip_target_ != pagination_model_.selected_page()) {
1625 page_flip_timer_.Start(FROM_HERE,
1626 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_),
1627 this, &AppsGridView::OnPageFlipTimer);
1632 void AppsGridView::OnPageFlipTimer() {
1633 DCHECK(pagination_model_.is_valid_page(page_flip_target_));
1634 pagination_model_.SelectPage(page_flip_target_, true);
1637 void AppsGridView::MoveItemInModel(views::View* item_view,
1638 const Index& target) {
1639 int current_model_index = view_model_.GetIndexOfView(item_view);
1640 DCHECK_GE(current_model_index, 0);
1642 int target_model_index = GetModelIndexFromIndex(target);
1643 if (target_model_index == current_model_index)
1644 return;
1646 item_list_->RemoveObserver(this);
1647 item_list_->MoveItem(current_model_index, target_model_index);
1648 view_model_.Move(current_model_index, target_model_index);
1649 item_list_->AddObserver(this);
1651 if (pagination_model_.selected_page() != target.page)
1652 pagination_model_.SelectPage(target.page, false);
1655 void AppsGridView::MoveItemToFolder(views::View* item_view,
1656 const Index& target) {
1657 const std::string& source_item_id =
1658 static_cast<AppListItemView*>(item_view)->item()->id();
1659 AppListItemView* target_view =
1660 static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1661 const std::string& target_view_item_id = target_view->item()->id();
1663 // Make change to data model.
1664 item_list_->RemoveObserver(this);
1665 std::string folder_item_id =
1666 model_->MergeItems(target_view_item_id, source_item_id);
1667 item_list_->AddObserver(this);
1668 if (folder_item_id.empty()) {
1669 LOG(ERROR) << "Unable to merge into item id: " << target_view_item_id;
1670 return;
1672 if (folder_item_id != target_view_item_id) {
1673 // New folder was created, change the view model to replace the old target
1674 // view with the new folder item view.
1675 size_t folder_item_index;
1676 if (item_list_->FindItemIndex(folder_item_id, &folder_item_index)) {
1677 int target_view_index = view_model_.GetIndexOfView(target_view);
1678 gfx::Rect target_view_bounds = target_view->bounds();
1679 DeleteItemViewAtIndex(target_view_index);
1680 views::View* target_folder_view =
1681 CreateViewForItemAtIndex(folder_item_index);
1682 target_folder_view->SetBoundsRect(target_view_bounds);
1683 view_model_.Add(target_folder_view, target_view_index);
1684 AddChildView(target_folder_view);
1685 } else {
1686 LOG(ERROR) << "Folder no longer in item_list: " << folder_item_id;
1690 // Fade out the drag_view_ and delete it when animation ends.
1691 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1692 view_model_.Remove(drag_view_index);
1693 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1694 bounds_animator_.SetAnimationDelegate(
1695 drag_view_,
1696 scoped_ptr<gfx::AnimationDelegate>(
1697 new ItemRemoveAnimationDelegate(drag_view_)));
1698 UpdatePaging();
1701 void AppsGridView::ReparentItemForReorder(views::View* item_view,
1702 const Index& target) {
1703 item_list_->RemoveObserver(this);
1704 model_->RemoveObserver(this);
1706 AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1707 DCHECK(reparent_item->IsInFolder());
1708 const std::string source_folder_id = reparent_item->folder_id();
1709 AppListFolderItem* source_folder =
1710 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1712 int target_model_index = GetModelIndexFromIndex(target);
1714 // Remove the source folder view if there is only 1 item in it, since the
1715 // source folder will be deleted after its only child item removed from it.
1716 if (source_folder->ChildItemCount() == 1u) {
1717 const int deleted_folder_index =
1718 view_model_.GetIndexOfView(activated_folder_item_view());
1719 DeleteItemViewAtIndex(deleted_folder_index);
1721 // Adjust |target_model_index| if it is beyond the deleted folder index.
1722 if (target_model_index > deleted_folder_index)
1723 --target_model_index;
1726 // Move the item from its parent folder to top level item list.
1727 // Must move to target_model_index, the location we expect the target item
1728 // to be, not the item location we want to insert before.
1729 int current_model_index = view_model_.GetIndexOfView(item_view);
1730 syncer::StringOrdinal target_position;
1731 if (target_model_index < static_cast<int>(item_list_->item_count()))
1732 target_position = item_list_->item_at(target_model_index)->position();
1733 model_->MoveItemToFolderAt(reparent_item, "", target_position);
1734 view_model_.Move(current_model_index, target_model_index);
1736 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1738 item_list_->AddObserver(this);
1739 model_->AddObserver(this);
1740 UpdatePaging();
1743 void AppsGridView::ReparentItemToAnotherFolder(views::View* item_view,
1744 const Index& target) {
1745 DCHECK(IsDraggingForReparentInRootLevelGridView());
1747 AppListItemView* target_view =
1748 static_cast<AppListItemView*>(GetViewAtSlotOnCurrentPage(target.slot));
1749 if (!target_view)
1750 return;
1752 // Make change to data model.
1753 item_list_->RemoveObserver(this);
1755 AppListItem* reparent_item = static_cast<AppListItemView*>(item_view)->item();
1756 DCHECK(reparent_item->IsInFolder());
1757 const std::string source_folder_id = reparent_item->folder_id();
1758 AppListFolderItem* source_folder =
1759 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1761 // Remove the source folder view if there is only 1 item in it, since the
1762 // source folder will be deleted after its only child item merged into the
1763 // target item.
1764 if (source_folder->ChildItemCount() == 1u)
1765 DeleteItemViewAtIndex(
1766 view_model_.GetIndexOfView(activated_folder_item_view()));
1768 AppListItem* target_item = target_view->item();
1770 // Move item to the target folder.
1771 std::string target_id_after_merge =
1772 model_->MergeItems(target_item->id(), reparent_item->id());
1773 if (target_id_after_merge.empty()) {
1774 LOG(ERROR) << "Unable to reparent to item id: " << target_item->id();
1775 item_list_->AddObserver(this);
1776 return;
1779 if (target_id_after_merge != target_item->id()) {
1780 // New folder was created, change the view model to replace the old target
1781 // view with the new folder item view.
1782 const std::string& new_folder_id = reparent_item->folder_id();
1783 size_t new_folder_index;
1784 if (item_list_->FindItemIndex(new_folder_id, &new_folder_index)) {
1785 int target_view_index = view_model_.GetIndexOfView(target_view);
1786 DeleteItemViewAtIndex(target_view_index);
1787 views::View* new_folder_view =
1788 CreateViewForItemAtIndex(new_folder_index);
1789 view_model_.Add(new_folder_view, target_view_index);
1790 AddChildView(new_folder_view);
1791 } else {
1792 LOG(ERROR) << "Folder no longer in item_list: " << new_folder_id;
1796 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1798 item_list_->AddObserver(this);
1800 // Fade out the drag_view_ and delete it when animation ends.
1801 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1802 view_model_.Remove(drag_view_index);
1803 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1804 bounds_animator_.SetAnimationDelegate(
1805 drag_view_,
1806 scoped_ptr<gfx::AnimationDelegate>(
1807 new ItemRemoveAnimationDelegate(drag_view_)));
1808 UpdatePaging();
1811 // After moving the re-parenting item out of the folder, if there is only 1 item
1812 // left, remove the last item out of the folder, delete the folder and insert it
1813 // to the data model at the same position. Make the same change to view_model_
1814 // accordingly.
1815 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1816 const std::string& source_folder_id) {
1817 AppListFolderItem* source_folder =
1818 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1819 if (!source_folder || source_folder->ChildItemCount() != 1u)
1820 return;
1822 // Delete view associated with the folder item to be removed.
1823 DeleteItemViewAtIndex(
1824 view_model_.GetIndexOfView(activated_folder_item_view()));
1826 // Now make the data change to remove the folder item in model.
1827 AppListItem* last_item = source_folder->item_list()->item_at(0);
1828 model_->MoveItemToFolderAt(last_item, "", source_folder->position());
1830 // Create a new item view for the last item in folder.
1831 size_t last_item_index;
1832 if (!item_list_->FindItemIndex(last_item->id(), &last_item_index) ||
1833 last_item_index > static_cast<size_t>(view_model_.view_size())) {
1834 NOTREACHED();
1835 return;
1837 views::View* last_item_view = CreateViewForItemAtIndex(last_item_index);
1838 view_model_.Add(last_item_view, last_item_index);
1839 AddChildView(last_item_view);
1842 void AppsGridView::CancelFolderItemReparent(AppListItemView* drag_item_view) {
1843 // The icon of the dragged item must target to its final ideal bounds after
1844 // the animation completes.
1845 CalculateIdealBounds();
1847 gfx::Rect target_icon_rect =
1848 GetTargetIconRectInFolder(drag_item_view, activated_folder_item_view_);
1850 gfx::Rect drag_view_icon_to_grid =
1851 drag_item_view->ConvertRectToParent(drag_item_view->GetIconBounds());
1852 drag_view_icon_to_grid.ClampToCenteredSize(
1853 gfx::Size(kGridIconDimension, kGridIconDimension));
1854 TopIconAnimationView* icon_view = new TopIconAnimationView(
1855 drag_item_view->item()->icon(),
1856 target_icon_rect,
1857 false); /* animate like closing folder */
1858 AddChildView(icon_view);
1859 icon_view->SetBoundsRect(drag_view_icon_to_grid);
1860 icon_view->TransformView();
1863 void AppsGridView::CancelContextMenusOnCurrentPage() {
1864 int start = pagination_model_.selected_page() * tiles_per_page();
1865 int end = std::min(view_model_.view_size(), start + tiles_per_page());
1866 for (int i = start; i < end; ++i) {
1867 AppListItemView* view =
1868 static_cast<AppListItemView*>(view_model_.view_at(i));
1869 view->CancelContextMenu();
1873 void AppsGridView::DeleteItemViewAtIndex(int index) {
1874 views::View* item_view = view_model_.view_at(index);
1875 view_model_.Remove(index);
1876 if (item_view == drag_view_)
1877 drag_view_ = NULL;
1878 delete item_view;
1881 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const {
1882 gfx::Rect rect(GetLocalBounds());
1883 rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx);
1884 return rect.Contains(point);
1887 void AppsGridView::ButtonPressed(views::Button* sender,
1888 const ui::Event& event) {
1889 if (dragging())
1890 return;
1892 if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName))
1893 return;
1895 if (delegate_) {
1896 // Always set the previous activated_folder_item_view_ to be visible. This
1897 // prevents a case where the item would remain hidden due the
1898 // |activated_folder_item_view_| changing during the animation. We only
1899 // need to track |activated_folder_item_view_| in the root level grid view.
1900 if (!folder_delegate_) {
1901 if (activated_folder_item_view_)
1902 activated_folder_item_view_->SetVisible(true);
1903 AppListItemView* pressed_item_view =
1904 static_cast<AppListItemView*>(sender);
1905 if (IsFolderItem(pressed_item_view->item()))
1906 activated_folder_item_view_ = pressed_item_view;
1907 else
1908 activated_folder_item_view_ = NULL;
1910 delegate_->ActivateApp(static_cast<AppListItemView*>(sender)->item(),
1911 event.flags());
1915 void AppsGridView::OnListItemAdded(size_t index, AppListItem* item) {
1916 EndDrag(true);
1918 views::View* view = CreateViewForItemAtIndex(index);
1919 view_model_.Add(view, index);
1920 AddChildView(view);
1922 UpdatePaging();
1923 UpdatePulsingBlockViews();
1924 Layout();
1925 SchedulePaint();
1928 void AppsGridView::OnListItemRemoved(size_t index, AppListItem* item) {
1929 EndDrag(true);
1931 DeleteItemViewAtIndex(index);
1933 UpdatePaging();
1934 UpdatePulsingBlockViews();
1935 Layout();
1936 SchedulePaint();
1939 void AppsGridView::OnListItemMoved(size_t from_index,
1940 size_t to_index,
1941 AppListItem* item) {
1942 EndDrag(true);
1943 view_model_.Move(from_index, to_index);
1945 UpdatePaging();
1946 AnimateToIdealBounds();
1949 void AppsGridView::TotalPagesChanged() {
1952 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
1953 if (dragging()) {
1954 CalculateDropTarget(last_drag_point_, true);
1955 Layout();
1956 MaybeStartPageFlipTimer(last_drag_point_);
1957 } else {
1958 ClearSelectedView(selected_view_);
1959 Layout();
1963 void AppsGridView::TransitionStarted() {
1964 CancelContextMenusOnCurrentPage();
1967 void AppsGridView::TransitionChanged() {
1968 // Update layout for valid page transition only since over-scroll no longer
1969 // animates app icons.
1970 const PaginationModel::Transition& transition =
1971 pagination_model_.transition();
1972 if (pagination_model_.is_valid_page(transition.target_page))
1973 Layout();
1976 void AppsGridView::OnAppListModelStatusChanged() {
1977 UpdatePulsingBlockViews();
1978 Layout();
1979 SchedulePaint();
1982 void AppsGridView::SetViewHidden(views::View* view, bool hide, bool immediate) {
1983 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
1984 animator.SetPreemptionStrategy(
1985 immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
1986 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
1987 view->layer()->SetOpacity(hide ? 0 : 1);
1990 void AppsGridView::OnImplicitAnimationsCompleted() {
1991 if (layer()->opacity() == 0.0f)
1992 SetVisible(false);
1995 bool AppsGridView::EnableFolderDragDropUI() {
1996 // Enable drag and drop folder UI only if it is at the app list root level
1997 // and the switch is on and the target folder can still accept new items.
1998 return model_->folders_enabled() && !folder_delegate_ &&
1999 CanDropIntoTarget(drop_target_);
2002 bool AppsGridView::CanDropIntoTarget(const Index& drop_target) {
2003 views::View* target_view = GetViewAtSlotOnCurrentPage(drop_target.slot);
2004 if (!target_view)
2005 return true;
2007 AppListItem* target_item =
2008 static_cast<AppListItemView*>(target_view)->item();
2009 // Items can be dropped into non-folders (which have no children) or folders
2010 // that have fewer than the max allowed items.
2011 // OEM folder does not allow to drag/drop other items in it.
2012 return target_item->ChildItemCount() < kMaxFolderItems &&
2013 !IsOEMFolderItem(target_item);
2016 // TODO(jennyz): Optimize the calculation for finding nearest tile.
2017 AppsGridView::Index AppsGridView::GetNearestTileForDragView() {
2018 Index nearest_tile;
2019 nearest_tile.page = -1;
2020 nearest_tile.slot = -1;
2021 int d_min = -1;
2023 // Calculate the top left tile |drag_view| intersects.
2024 gfx::Point pt = drag_view_->bounds().origin();
2025 CalculateNearestTileForVertex(pt, &nearest_tile, &d_min);
2027 // Calculate the top right tile |drag_view| intersects.
2028 pt = drag_view_->bounds().top_right();
2029 CalculateNearestTileForVertex(pt, &nearest_tile, &d_min);
2031 // Calculate the bottom left tile |drag_view| intersects.
2032 pt = drag_view_->bounds().bottom_left();
2033 CalculateNearestTileForVertex(pt, &nearest_tile, &d_min);
2035 // Calculate the bottom right tile |drag_view| intersects.
2036 pt = drag_view_->bounds().bottom_right();
2037 CalculateNearestTileForVertex(pt, &nearest_tile, &d_min);
2039 const int d_folder_dropping =
2040 kFolderDroppingCircleRadius + kGridIconDimension / 2;
2041 const int d_reorder = kReorderDroppingCircleRadius + kGridIconDimension / 2;
2043 // If user drags an item across pages to the last page, and targets it
2044 // to the last empty slot on it, push the last item for re-ordering.
2045 if (IsLastPossibleDropTarget(nearest_tile) && d_min < d_reorder) {
2046 drop_attempt_ = DROP_FOR_REORDER;
2047 nearest_tile.slot = nearest_tile.slot - 1;
2048 return nearest_tile;
2051 if (IsValidIndex(nearest_tile)) {
2052 if (d_min < d_folder_dropping) {
2053 views::View* target_view = GetViewAtSlotOnCurrentPage(nearest_tile.slot);
2054 if (target_view &&
2055 !IsFolderItem(static_cast<AppListItemView*>(drag_view_)->item())) {
2056 // If a non-folder item is dragged to the target slot with an item
2057 // sitting on it, attempt to drop the dragged item into the folder
2058 // containing the item on nearest_tile.
2059 drop_attempt_ = DROP_FOR_FOLDER;
2060 return nearest_tile;
2061 } else {
2062 // If the target slot is blank, or the dragged item is a folder, attempt
2063 // to re-order.
2064 drop_attempt_ = DROP_FOR_REORDER;
2065 return nearest_tile;
2067 } else if (d_min < d_reorder) {
2068 // Entering the re-order circle of the slot.
2069 drop_attempt_ = DROP_FOR_REORDER;
2070 return nearest_tile;
2074 // If |drag_view| is not entering the re-order or fold dropping region of
2075 // any items, cancel any previous re-order or folder dropping timer, and
2076 // return itself.
2077 drop_attempt_ = DROP_FOR_NONE;
2078 reorder_timer_.Stop();
2079 folder_dropping_timer_.Stop();
2081 // When dragging for reparent a folder item, it should go back to its parent
2082 // folder item if there is no drop target.
2083 if (IsDraggingForReparentInRootLevelGridView()) {
2084 DCHECK(activated_folder_item_view_);
2085 return GetIndexOfView(activated_folder_item_view_);
2088 return GetIndexOfView(drag_view_);
2091 void AppsGridView::CalculateNearestTileForVertex(const gfx::Point& vertex,
2092 Index* nearest_tile,
2093 int* d_min) {
2094 Index target_index;
2095 gfx::Rect target_bounds = GetTileBoundsForPoint(vertex, &target_index);
2097 if (target_bounds.IsEmpty() || target_index == *nearest_tile)
2098 return;
2100 // Do not count the tile, where drag_view_ used to sit on and is still moving
2101 // on top of it, in calculating nearest tile for drag_view_.
2102 views::View* target_view = GetViewAtSlotOnCurrentPage(target_index.slot);
2103 if (target_index == drag_view_init_index_ && !target_view &&
2104 !IsDraggingForReparentInRootLevelGridView()) {
2105 return;
2108 int d_center = GetDistanceBetweenRects(drag_view_->bounds(), target_bounds);
2109 if (*d_min < 0 || d_center < *d_min) {
2110 *d_min = d_center;
2111 *nearest_tile = target_index;
2115 gfx::Rect AppsGridView::GetTileBoundsForPoint(const gfx::Point& point,
2116 Index *tile_index) {
2117 // Check if |point| is outside of contents bounds.
2118 gfx::Rect bounds(GetContentsBounds());
2119 if (!bounds.Contains(point))
2120 return gfx::Rect();
2122 // Calculate which tile |point| is enclosed in.
2123 int x = point.x();
2124 int y = point.y();
2125 int col = (x - bounds.x()) / kPreferredTileWidth;
2126 int row = (y - bounds.y()) / kPreferredTileHeight;
2127 gfx::Rect tile_rect = GetTileBounds(row, col);
2129 // Check if |point| is outside a valid item's tile.
2130 Index index(pagination_model_.selected_page(), row * cols_ + col);
2131 *tile_index = index;
2132 return tile_rect;
2135 gfx::Rect AppsGridView::GetTileBounds(int row, int col) const {
2136 gfx::Rect bounds(GetContentsBounds());
2137 gfx::Size tile_size(kPreferredTileWidth, kPreferredTileHeight);
2138 gfx::Rect grid_rect(gfx::Size(tile_size.width() * cols_,
2139 tile_size.height() * rows_per_page_));
2140 grid_rect.Intersect(bounds);
2141 gfx::Rect tile_rect(
2142 gfx::Point(grid_rect.x() + col * tile_size.width(),
2143 grid_rect.y() + row * tile_size.height()),
2144 tile_size);
2145 return tile_rect;
2148 bool AppsGridView::IsLastPossibleDropTarget(const Index& index) const {
2149 int last_possible_slot = view_model_.view_size() % tiles_per_page();
2150 return (index.page == pagination_model_.total_pages() - 1 &&
2151 index.slot == last_possible_slot + 1);
2154 views::View* AppsGridView::GetViewAtSlotOnCurrentPage(int slot) {
2155 if (slot < 0)
2156 return NULL;
2158 // Calculate the original bound of the tile at |index|.
2159 int row = slot / cols_;
2160 int col = slot % cols_;
2161 gfx::Rect tile_rect = GetTileBounds(row, col);
2163 for (int i = 0; i < view_model_.view_size(); ++i) {
2164 views::View* view = view_model_.view_at(i);
2165 if (view->bounds() == tile_rect && view != drag_view_)
2166 return view;
2168 return NULL;
2171 void AppsGridView::SetAsFolderDroppingTarget(const Index& target_index,
2172 bool is_target_folder) {
2173 AppListItemView* target_view =
2174 static_cast<AppListItemView*>(
2175 GetViewAtSlotOnCurrentPage(target_index.slot));
2176 if (target_view)
2177 target_view->SetAsAttemptedFolderTarget(is_target_folder);
2180 } // namespace app_list