1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/app_list/views/apps_grid_view.h"
11 #include "base/guid.h"
12 #include "ui/app_list/app_list_constants.h"
13 #include "ui/app_list/app_list_folder_item.h"
14 #include "ui/app_list/app_list_item.h"
15 #include "ui/app_list/app_list_switches.h"
16 #include "ui/app_list/pagination_controller.h"
17 #include "ui/app_list/views/app_list_drag_and_drop_host.h"
18 #include "ui/app_list/views/app_list_folder_view.h"
19 #include "ui/app_list/views/app_list_item_view.h"
20 #include "ui/app_list/views/apps_grid_view_delegate.h"
21 #include "ui/app_list/views/page_switcher.h"
22 #include "ui/app_list/views/pulsing_block_view.h"
23 #include "ui/app_list/views/top_icon_animation_view.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/events/event.h"
26 #include "ui/gfx/animation/animation.h"
27 #include "ui/gfx/geometry/vector2d.h"
28 #include "ui/gfx/geometry/vector2d_conversions.h"
29 #include "ui/views/border.h"
30 #include "ui/views/view_model_utils.h"
31 #include "ui/views/widget/widget.h"
34 #include "ui/aura/window.h"
35 #include "ui/aura/window_event_dispatcher.h"
37 #include "ui/views/win/hwnd_util.h"
38 #endif // defined(OS_WIN)
39 #endif // defined(USE_AURA)
42 #include "base/command_line.h"
43 #include "base/files/file_path.h"
44 #include "base/win/shortcut.h"
45 #include "ui/base/dragdrop/drag_utils.h"
46 #include "ui/base/dragdrop/drop_target_win.h"
47 #include "ui/base/dragdrop/os_exchange_data.h"
48 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
49 #include "ui/gfx/win/dpi.h"
56 // Distance a drag needs to be from the app grid to be considered 'outside', at
57 // which point we rearrange the apps to their pre-drag configuration, as a drop
58 // then would be canceled. We have a buffer to make it easier to drag apps to
60 const int kDragBufferPx
= 20;
62 // Padding space in pixels for fixed layout.
63 const int kLeftRightPadding
= 20;
64 const int kTopPadding
= 1;
65 const int kBottomPadding
= 24;
67 // Padding space in pixels between pages.
68 const int kPagePadding
= 40;
70 // Preferred tile size when showing in fixed layout.
71 const int kPreferredTileWidth
= 88;
72 const int kPreferredTileHeight
= 98;
74 // Width in pixels of the area on the sides that triggers a page flip.
75 const int kPageFlipZoneSize
= 40;
77 // Delay in milliseconds to do the page flip.
78 const int kPageFlipDelayInMs
= 1000;
80 // How many pages on either side of the selected one we prerender.
81 const int kPrerenderPages
= 1;
83 // The drag and drop proxy should get scaled by this factor.
84 const float kDragAndDropProxyScale
= 1.5f
;
86 // Delays in milliseconds to show folder dropping preview circle.
87 const int kFolderDroppingDelay
= 150;
89 // Delays in milliseconds to show re-order preview.
90 const int kReorderDelay
= 120;
92 // Delays in milliseconds to show folder item reparent UI.
93 const int kFolderItemReparentDelay
= 50;
95 // Radius of the circle, in which if entered, show folder dropping preview
97 const int kFolderDroppingCircleRadius
= 15;
99 // RowMoveAnimationDelegate is used when moving an item into a different row.
100 // Before running the animation, the item's layer is re-created and kept in
101 // the original position, then the item is moved to just before its target
102 // position and opacity set to 0. When the animation runs, this delegate moves
103 // the layer and fades it out while fading in the item at the same time.
104 class RowMoveAnimationDelegate
: public gfx::AnimationDelegate
{
106 RowMoveAnimationDelegate(views::View
* view
,
108 const gfx::Rect
& layer_target
)
111 layer_start_(layer
? layer
->bounds() : gfx::Rect()),
112 layer_target_(layer_target
) {
114 virtual ~RowMoveAnimationDelegate() {}
116 // gfx::AnimationDelegate overrides:
117 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
{
118 view_
->layer()->SetOpacity(animation
->GetCurrentValue());
119 view_
->layer()->ScheduleDraw();
122 layer_
->SetOpacity(1 - animation
->GetCurrentValue());
123 layer_
->SetBounds(animation
->CurrentValueBetween(layer_start_
,
125 layer_
->ScheduleDraw();
128 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
{
129 view_
->layer()->SetOpacity(1.0f
);
130 view_
->SchedulePaint();
132 virtual void AnimationCanceled(const gfx::Animation
* animation
) OVERRIDE
{
133 view_
->layer()->SetOpacity(1.0f
);
134 view_
->SchedulePaint();
138 // The view that needs to be wrapped. Owned by views hierarchy.
141 scoped_ptr
<ui::Layer
> layer_
;
142 const gfx::Rect layer_start_
;
143 const gfx::Rect layer_target_
;
145 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate
);
148 // ItemRemoveAnimationDelegate is used to show animation for removing an item.
149 // This happens when user drags an item into a folder. The dragged item will
150 // be removed from the original list after it is dropped into the folder.
151 class ItemRemoveAnimationDelegate
: public gfx::AnimationDelegate
{
153 explicit ItemRemoveAnimationDelegate(views::View
* view
)
157 virtual ~ItemRemoveAnimationDelegate() {
160 // gfx::AnimationDelegate overrides:
161 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
{
162 view_
->layer()->SetOpacity(1 - animation
->GetCurrentValue());
163 view_
->layer()->ScheduleDraw();
167 scoped_ptr
<views::View
> view_
;
169 DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate
);
172 // ItemMoveAnimationDelegate observes when an item finishes animating when it is
173 // not moving between rows. This is to ensure an item is repainted for the
174 // "zoom out" case when releasing an item being dragged.
175 class ItemMoveAnimationDelegate
: public gfx::AnimationDelegate
{
177 ItemMoveAnimationDelegate(views::View
* view
) : view_(view
) {}
179 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
{
180 view_
->SchedulePaint();
182 virtual void AnimationCanceled(const gfx::Animation
* animation
) OVERRIDE
{
183 view_
->SchedulePaint();
189 DISALLOW_COPY_AND_ASSIGN(ItemMoveAnimationDelegate
);
192 // Gets the distance between the centers of the |rect_1| and |rect_2|.
193 int GetDistanceBetweenRects(gfx::Rect rect_1
,
195 return (rect_1
.CenterPoint() - rect_2
.CenterPoint()).Length();
198 // Returns true if the |item| is an folder item.
199 bool IsFolderItem(AppListItem
* item
) {
200 return (item
->GetItemType() == AppListFolderItem::kItemType
);
203 bool IsOEMFolderItem(AppListItem
* item
) {
204 return IsFolderItem(item
) &&
205 (static_cast<AppListFolderItem
*>(item
))->folder_type() ==
206 AppListFolderItem::FOLDER_TYPE_OEM
;
212 // Interprets drag events sent from Windows via the drag/drop API and forwards
213 // them to AppsGridView.
214 // On Windows, in order to have the OS perform the drag properly we need to
215 // provide it with a shortcut file which may or may not exist at the time the
216 // drag is started. Therefore while waiting for that shortcut to be located we
217 // just do a regular "internal" drag and transition into the synchronous drag
218 // when the shortcut is found/created. Hence a synchronous drag is an optional
219 // phase of a regular drag and non-Windows platforms drags are equivalent to a
220 // Windows drag that never enters the synchronous drag phase.
221 class SynchronousDrag
: public ui::DragSourceWin
{
223 SynchronousDrag(AppsGridView
* grid_view
,
224 AppListItemView
* drag_view
,
225 const gfx::Point
& drag_view_offset
)
226 : grid_view_(grid_view
),
227 drag_view_(drag_view
),
228 drag_view_offset_(drag_view_offset
),
229 has_shortcut_path_(false),
233 void set_shortcut_path(const base::FilePath
& shortcut_path
) {
234 has_shortcut_path_
= true;
235 shortcut_path_
= shortcut_path
;
238 bool running() { return running_
; }
241 return has_shortcut_path_
&& !running_
;
247 // Prevent the synchronous dragger being destroyed while the drag is
249 scoped_refptr
<SynchronousDrag
> this_ref
= this;
252 ui::OSExchangeData data
;
253 SetupExchangeData(&data
);
255 // Hide the dragged view because the OS is going to create its own.
256 drag_view_
->SetVisible(false);
258 // Blocks until the drag is finished. Calls into the ui::DragSourceWin
261 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data
),
262 this, DROPEFFECT_MOVE
| DROPEFFECT_LINK
, &effects
);
264 // If |drag_view_| is NULL the drag was ended by some reentrant code.
266 // Make the drag view visible again.
267 drag_view_
->SetVisible(true);
268 drag_view_
->OnSyncDragEnd();
270 grid_view_
->EndDrag(canceled_
|| !IsCursorWithinGridView());
274 void EndDragExternally() {
277 drag_view_
->SetVisible(true);
282 // Overridden from ui::DragSourceWin.
283 virtual void OnDragSourceCancel() OVERRIDE
{
287 virtual void OnDragSourceDrop() OVERRIDE
{
290 virtual void OnDragSourceMove() OVERRIDE
{
291 grid_view_
->UpdateDrag(AppsGridView::MOUSE
, GetCursorInGridViewCoords());
294 void SetupExchangeData(ui::OSExchangeData
* data
) {
295 data
->SetFilename(shortcut_path_
);
296 gfx::ImageSkia
image(drag_view_
->GetDragImage());
297 gfx::Size
image_size(image
.size());
298 drag_utils::SetDragImageOnDataObject(
300 drag_view_offset_
- drag_view_
->GetDragImageOffset(),
304 HWND
GetGridViewHWND() {
305 return views::HWNDForView(grid_view_
);
308 bool IsCursorWithinGridView() {
311 return GetGridViewHWND() == WindowFromPoint(p
);
314 gfx::Point
GetCursorInGridViewCoords() {
317 ScreenToClient(GetGridViewHWND(), &p
);
318 gfx::Point
grid_view_pt(p
.x
, p
.y
);
319 grid_view_pt
= gfx::win::ScreenToDIPPoint(grid_view_pt
);
320 views::View::ConvertPointFromWidget(grid_view_
, &grid_view_pt
);
324 AppsGridView
* grid_view_
;
325 AppListItemView
* drag_view_
;
326 gfx::Point drag_view_offset_
;
327 bool has_shortcut_path_
;
328 base::FilePath shortcut_path_
;
332 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag
);
334 #endif // defined(OS_WIN)
336 AppsGridView::AppsGridView(AppsGridViewDelegate
* delegate
)
340 folder_delegate_(NULL
),
341 page_switcher_view_(NULL
),
344 selected_view_(NULL
),
346 drag_start_page_(-1),
348 use_synchronous_drag_(true),
351 drop_attempt_(DROP_FOR_NONE
),
352 drag_and_drop_host_(NULL
),
353 forward_events_to_drag_and_drop_host_(false),
354 page_flip_target_(-1),
355 page_flip_delay_in_ms_(kPageFlipDelayInMs
),
356 bounds_animator_(this),
357 activated_folder_item_view_(NULL
),
358 dragging_for_reparent_item_(false) {
359 SetPaintToLayer(true);
360 // Clip any icons that are outside the grid view's bounds. These icons would
361 // otherwise be visible to the user when the grid view is off screen.
362 layer()->SetMasksToBounds(true);
363 SetFillsBoundsOpaquely(false);
365 pagination_model_
.SetTransitionDurations(kPageTransitionDurationInMs
,
366 kOverscrollPageTransitionDurationMs
);
368 pagination_model_
.AddObserver(this);
369 // The experimental app list transitions vertically.
370 PaginationController::ScrollAxis scroll_axis
=
371 app_list::switches::IsExperimentalAppListEnabled()
372 ? PaginationController::SCROLL_AXIS_VERTICAL
373 : PaginationController::SCROLL_AXIS_HORIZONTAL
;
374 pagination_controller_
.reset(
375 new PaginationController(&pagination_model_
, scroll_axis
));
376 if (!switches::IsExperimentalAppListEnabled()) {
377 page_switcher_view_
= new PageSwitcher(&pagination_model_
);
378 AddChildView(page_switcher_view_
);
382 AppsGridView::~AppsGridView() {
383 // Coming here |drag_view_| should already be canceled since otherwise the
384 // drag would disappear after the app list got animated away and closed,
385 // which would look odd.
391 model_
->RemoveObserver(this);
392 pagination_model_
.RemoveObserver(this);
395 item_list_
->RemoveObserver(this);
397 // Make sure |page_switcher_view_| is deleted before |pagination_model_|.
399 RemoveAllChildViews(true);
402 void AppsGridView::SetLayout(int cols
, int rows_per_page
) {
404 rows_per_page_
= rows_per_page
;
406 SetBorder(views::Border::CreateEmptyBorder(
407 kTopPadding
, kLeftRightPadding
, 0, kLeftRightPadding
));
410 void AppsGridView::ResetForShowApps() {
411 activated_folder_item_view_
= NULL
;
413 layer()->SetOpacity(1.0f
);
415 // Set all views to visible in case they weren't made visible again by an
416 // incomplete animation.
417 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
418 view_model_
.view_at(i
)->SetVisible(true);
420 CHECK_EQ(item_list_
->item_count(),
421 static_cast<size_t>(view_model_
.view_size()));
424 void AppsGridView::SetModel(AppListModel
* model
) {
426 model_
->RemoveObserver(this);
430 model_
->AddObserver(this);
435 void AppsGridView::SetItemList(AppListItemList
* item_list
) {
437 item_list_
->RemoveObserver(this);
438 item_list_
= item_list
;
440 item_list_
->AddObserver(this);
444 void AppsGridView::SetSelectedView(views::View
* view
) {
445 if (IsSelectedView(view
) || IsDraggedView(view
))
448 Index index
= GetIndexOfView(view
);
449 if (IsValidIndex(index
))
450 SetSelectedItemByIndex(index
);
453 void AppsGridView::ClearSelectedView(views::View
* view
) {
454 if (view
&& IsSelectedView(view
)) {
455 selected_view_
->SchedulePaint();
456 selected_view_
= NULL
;
460 void AppsGridView::ClearAnySelectedView() {
461 if (selected_view_
) {
462 selected_view_
->SchedulePaint();
463 selected_view_
= NULL
;
467 bool AppsGridView::IsSelectedView(const views::View
* view
) const {
468 return selected_view_
== view
;
471 void AppsGridView::EnsureViewVisible(const views::View
* view
) {
472 if (pagination_model_
.has_transition())
475 Index index
= GetIndexOfView(view
);
476 if (IsValidIndex(index
))
477 pagination_model_
.SelectPage(index
.page
, false);
480 void AppsGridView::InitiateDrag(AppListItemView
* view
,
482 const ui::LocatedEvent
& event
) {
484 if (drag_view_
|| pulsing_blocks_model_
.view_size())
488 drag_view_init_index_
= GetIndexOfView(drag_view_
);
489 drag_view_offset_
= event
.location();
490 drag_start_page_
= pagination_model_
.selected_page();
491 ExtractDragLocation(event
, &drag_start_grid_view_
);
492 drag_view_start_
= gfx::Point(drag_view_
->x(), drag_view_
->y());
495 void AppsGridView::StartSettingUpSynchronousDrag() {
497 if (!delegate_
|| !use_synchronous_drag_
)
500 // Folders and downloading items can't be integrated with the OS.
501 if (IsFolderItem(drag_view_
->item()) || drag_view_
->item()->is_installing())
504 // Favor the drag and drop host over native win32 drag. For the Win8/ash
505 // launcher we want to have ashes drag and drop over win32's.
506 if (drag_and_drop_host_
)
509 // Never create a second synchronous drag if the drag started in a folder.
510 if (IsDraggingForReparentInRootLevelGridView())
513 synchronous_drag_
= new SynchronousDrag(this, drag_view_
, drag_view_offset_
);
514 delegate_
->GetShortcutPathForApp(drag_view_
->item()->id(),
515 base::Bind(&AppsGridView::OnGotShortcutPath
,
516 base::Unretained(this),
521 bool AppsGridView::RunSynchronousDrag() {
523 if (!synchronous_drag_
)
526 if (synchronous_drag_
->CanRun()) {
527 if (IsDraggingForReparentInHiddenGridView())
528 folder_delegate_
->SetRootLevelDragViewVisible(false);
529 synchronous_drag_
->Run();
530 synchronous_drag_
= NULL
;
532 } else if (!synchronous_drag_
->running()) {
533 // The OS drag is not ready yet. If the root grid has a drag view because
534 // a reparent has started, ensure it is visible.
535 if (IsDraggingForReparentInHiddenGridView())
536 folder_delegate_
->SetRootLevelDragViewVisible(true);
542 void AppsGridView::CleanUpSynchronousDrag() {
544 if (synchronous_drag_
)
545 synchronous_drag_
->EndDragExternally();
547 synchronous_drag_
= NULL
;
552 void AppsGridView::OnGotShortcutPath(
553 scoped_refptr
<SynchronousDrag
> synchronous_drag
,
554 const base::FilePath
& path
) {
555 // Drag may have ended before we get the shortcut path or a new drag may have
557 if (synchronous_drag_
!= synchronous_drag
)
559 // Setting the shortcut path here means the next time we hit UpdateDrag()
560 // we'll enter the synchronous drag.
561 // NOTE we don't Run() the drag here because that causes animations not to
562 // update for some reason.
563 synchronous_drag_
->set_shortcut_path(path
);
564 DCHECK(synchronous_drag_
->CanRun());
568 bool AppsGridView::UpdateDragFromItem(Pointer pointer
,
569 const ui::LocatedEvent
& event
) {
571 return false; // Drag canceled.
573 gfx::Point drag_point_in_grid_view
;
574 ExtractDragLocation(event
, &drag_point_in_grid_view
);
575 UpdateDrag(pointer
, drag_point_in_grid_view
);
579 // If a drag and drop host is provided, see if the drag operation needs to be
581 gfx::Point location_in_screen
= drag_point_in_grid_view
;
582 views::View::ConvertPointToScreen(this, &location_in_screen
);
583 DispatchDragEventToDragAndDropHost(location_in_screen
);
584 if (drag_and_drop_host_
)
585 drag_and_drop_host_
->UpdateDragIconProxy(location_in_screen
);
589 void AppsGridView::UpdateDrag(Pointer pointer
, const gfx::Point
& point
) {
590 if (folder_delegate_
)
591 UpdateDragStateInsideFolder(pointer
, point
);
594 return; // Drag canceled.
596 if (RunSynchronousDrag())
599 gfx::Vector2d
drag_vector(point
- drag_start_grid_view_
);
600 if (!dragging() && ExceededDragThreshold(drag_vector
)) {
601 drag_pointer_
= pointer
;
602 // Move the view to the front so that it appears on top of other views.
603 ReorderChildView(drag_view_
, -1);
604 bounds_animator_
.StopAnimatingView(drag_view_
);
605 // Stopping the animation may have invalidated our drag view due to the
606 // view hierarchy changing.
610 StartSettingUpSynchronousDrag();
611 if (!dragging_for_reparent_item_
)
612 StartDragAndDropHostDrag(point
);
615 if (drag_pointer_
!= pointer
)
618 last_drag_point_
= point
;
619 const Index last_drop_target
= drop_target_
;
620 DropAttempt last_drop_attempt
= drop_attempt_
;
621 CalculateDropTarget(last_drag_point_
, false);
623 if (IsPointWithinDragBuffer(last_drag_point_
))
624 MaybeStartPageFlipTimer(last_drag_point_
);
628 if (page_switcher_view_
) {
629 gfx::Point
page_switcher_point(last_drag_point_
);
630 views::View::ConvertPointToTarget(
631 this, page_switcher_view_
, &page_switcher_point
);
632 page_switcher_view_
->UpdateUIForDragPoint(page_switcher_point
);
635 if (!EnableFolderDragDropUI()) {
636 if (last_drop_target
!= drop_target_
)
637 AnimateToIdealBounds();
638 drag_view_
->SetPosition(drag_view_start_
+ drag_vector
);
642 // Update drag with folder UI enabled.
643 if (last_drop_target
!= drop_target_
||
644 last_drop_attempt
!= drop_attempt_
) {
645 if (drop_attempt_
== DROP_FOR_REORDER
) {
646 folder_dropping_timer_
.Stop();
647 reorder_timer_
.Start(FROM_HERE
,
648 base::TimeDelta::FromMilliseconds(kReorderDelay
),
649 this, &AppsGridView::OnReorderTimer
);
650 } else if (drop_attempt_
== DROP_FOR_FOLDER
) {
651 reorder_timer_
.Stop();
652 folder_dropping_timer_
.Start(FROM_HERE
,
653 base::TimeDelta::FromMilliseconds(kFolderDroppingDelay
),
654 this, &AppsGridView::OnFolderDroppingTimer
);
657 // Reset the previous drop target.
658 SetAsFolderDroppingTarget(last_drop_target
, false);
661 drag_view_
->SetPosition(drag_view_start_
+ drag_vector
);
664 void AppsGridView::EndDrag(bool cancel
) {
665 // EndDrag was called before if |drag_view_| is NULL.
669 // Coming here a drag and drop was in progress.
670 bool landed_in_drag_and_drop_host
= forward_events_to_drag_and_drop_host_
;
671 if (forward_events_to_drag_and_drop_host_
) {
672 DCHECK(!IsDraggingForReparentInRootLevelGridView());
673 forward_events_to_drag_and_drop_host_
= false;
674 drag_and_drop_host_
->EndDrag(cancel
);
675 if (IsDraggingForReparentInHiddenGridView()) {
676 folder_delegate_
->DispatchEndDragEventForReparent(
677 true /* events_forwarded_to_drag_drop_host */,
678 cancel
/* cancel_drag */);
681 if (IsDraggingForReparentInHiddenGridView()) {
682 // Forward the EndDrag event to the root level grid view.
683 folder_delegate_
->DispatchEndDragEventForReparent(
684 false /* events_forwarded_to_drag_drop_host */,
685 cancel
/* cancel_drag */);
686 EndDragForReparentInHiddenFolderGridView();
690 if (IsDraggingForReparentInRootLevelGridView()) {
691 // An EndDrag can be received during a reparent via a model change. This
692 // is always a cancel and needs to be forwarded to the folder.
694 delegate_
->CancelDragInActiveFolder();
698 if (!cancel
&& dragging()) {
699 // Regular drag ending path, ie, not for reparenting.
700 CalculateDropTarget(last_drag_point_
, true);
701 if (IsValidIndex(drop_target_
)) {
702 if (!EnableFolderDragDropUI()) {
703 MoveItemInModel(drag_view_
, drop_target_
);
705 if (drop_attempt_
== DROP_FOR_REORDER
)
706 MoveItemInModel(drag_view_
, drop_target_
);
707 else if (drop_attempt_
== DROP_FOR_FOLDER
)
708 MoveItemToFolder(drag_view_
, drop_target_
);
714 if (drag_and_drop_host_
) {
715 // If we had a drag and drop proxy icon, we delete it and make the real
716 // item visible again.
717 drag_and_drop_host_
->DestroyDragIconProxy();
718 if (landed_in_drag_and_drop_host
) {
719 // Move the item directly to the target location, avoiding the "zip back"
720 // animation if the user was pinning it to the shelf.
721 int i
= drop_target_
.slot
;
722 gfx::Rect bounds
= view_model_
.ideal_bounds(i
);
723 drag_view_
->SetBoundsRect(bounds
);
725 // Fade in slowly if it landed in the shelf.
726 SetViewHidden(drag_view_
,
728 !landed_in_drag_and_drop_host
/* animate */);
731 // The drag can be ended after the synchronous drag is created but before it
733 CleanUpSynchronousDrag();
735 SetAsFolderDroppingTarget(drop_target_
, false);
737 AnimateToIdealBounds();
741 // If user releases mouse inside a folder's grid view, burst the folder
742 // container ink bubble.
743 if (folder_delegate_
&& !IsDraggingForReparentInHiddenGridView())
744 folder_delegate_
->UpdateFolderViewBackground(false);
747 void AppsGridView::StopPageFlipTimer() {
748 page_flip_timer_
.Stop();
749 page_flip_target_
= -1;
752 AppListItemView
* AppsGridView::GetItemViewAt(int index
) const {
753 DCHECK(index
>= 0 && index
< view_model_
.view_size());
754 return static_cast<AppListItemView
*>(view_model_
.view_at(index
));
757 void AppsGridView::SetTopItemViewsVisible(bool visible
) {
758 int top_item_count
= std::min(static_cast<int>(kNumFolderTopItems
),
759 view_model_
.view_size());
760 for (int i
= 0; i
< top_item_count
; ++i
)
761 GetItemViewAt(i
)->icon()->SetVisible(visible
);
764 void AppsGridView::ScheduleShowHideAnimation(bool show
) {
765 // Stop any previous animation.
766 layer()->GetAnimator()->StopAnimating();
768 // Set initial state.
770 layer()->SetOpacity(show
? 0.0f
: 1.0f
);
772 ui::ScopedLayerAnimationSettings
animation(layer()->GetAnimator());
773 animation
.AddObserver(this);
774 animation
.SetTweenType(
775 show
? kFolderFadeInTweenType
: kFolderFadeOutTweenType
);
776 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
777 show
? kFolderTransitionInDurationMs
: kFolderTransitionOutDurationMs
));
779 layer()->SetOpacity(show
? 1.0f
: 0.0f
);
782 void AppsGridView::InitiateDragFromReparentItemInRootLevelGridView(
783 AppListItemView
* original_drag_view
,
784 const gfx::Rect
& drag_view_rect
,
785 const gfx::Point
& drag_point
) {
786 DCHECK(original_drag_view
&& !drag_view_
);
787 DCHECK(!dragging_for_reparent_item_
);
789 // Create a new AppListItemView to duplicate the original_drag_view in the
790 // folder's grid view.
791 AppListItemView
* view
= new AppListItemView(this, original_drag_view
->item());
794 drag_view_
->SetPaintToLayer(true);
795 // Note: For testing purpose, SetFillsBoundsOpaquely can be set to true to
796 // show the gray background.
797 drag_view_
->SetFillsBoundsOpaquely(false);
798 drag_view_
->SetBoundsRect(drag_view_rect
);
799 drag_view_
->SetDragUIState(); // Hide the title of the drag_view_.
801 // Hide the drag_view_ for drag icon proxy.
802 SetViewHidden(drag_view_
,
804 true /* no animate */);
806 // Add drag_view_ to the end of the view_model_.
807 view_model_
.Add(drag_view_
, view_model_
.view_size());
809 drag_start_page_
= pagination_model_
.selected_page();
810 drag_start_grid_view_
= drag_point
;
812 drag_view_start_
= gfx::Point(drag_view_
->x(), drag_view_
->y());
814 // Set the flag in root level grid view.
815 dragging_for_reparent_item_
= true;
818 void AppsGridView::UpdateDragFromReparentItem(Pointer pointer
,
819 const gfx::Point
& drag_point
) {
820 // Note that if a cancel ocurrs while reparenting, the |drag_view_| in both
821 // root and folder grid views is cleared, so the check in UpdateDragFromItem()
822 // for |drag_view_| being NULL (in the folder grid) is sufficient.
824 DCHECK(IsDraggingForReparentInRootLevelGridView());
826 UpdateDrag(pointer
, drag_point
);
829 bool AppsGridView::IsDraggedView(const views::View
* view
) const {
830 return drag_view_
== view
;
833 void AppsGridView::ClearDragState() {
834 drop_attempt_
= DROP_FOR_NONE
;
835 drag_pointer_
= NONE
;
836 drop_target_
= Index();
837 drag_start_grid_view_
= gfx::Point();
838 drag_start_page_
= -1;
839 drag_view_offset_
= gfx::Point();
842 drag_view_
->OnDragEnded();
843 if (IsDraggingForReparentInRootLevelGridView()) {
844 const int drag_view_index
= view_model_
.GetIndexOfView(drag_view_
);
845 CHECK_EQ(view_model_
.view_size() - 1, drag_view_index
);
846 DeleteItemViewAtIndex(drag_view_index
);
850 dragging_for_reparent_item_
= false;
853 void AppsGridView::SetDragViewVisible(bool visible
) {
855 SetViewHidden(drag_view_
, !visible
, true);
858 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
859 ApplicationDragAndDropHost
* drag_and_drop_host
) {
860 drag_and_drop_host_
= drag_and_drop_host
;
863 void AppsGridView::Prerender() {
865 int selected_page
= std::max(0, pagination_model_
.selected_page());
866 int start
= std::max(0, (selected_page
- kPrerenderPages
) * tiles_per_page());
867 int end
= std::min(view_model_
.view_size(),
868 (selected_page
+ 1 + kPrerenderPages
) * tiles_per_page());
869 for (int i
= start
; i
< end
; i
++) {
870 AppListItemView
* v
= static_cast<AppListItemView
*>(view_model_
.view_at(i
));
875 bool AppsGridView::IsAnimatingView(views::View
* view
) {
876 return bounds_animator_
.IsAnimating(view
);
879 gfx::Size
AppsGridView::GetPreferredSize() const {
880 const gfx::Insets
insets(GetInsets());
881 const gfx::Size tile_size
= gfx::Size(kPreferredTileWidth
,
882 kPreferredTileHeight
);
883 int page_switcher_height
= kBottomPadding
;
884 if (page_switcher_view_
)
885 page_switcher_height
= page_switcher_view_
->GetPreferredSize().height();
887 tile_size
.width() * cols_
+ insets
.width(),
888 tile_size
.height() * rows_per_page_
+
889 page_switcher_height
+ insets
.height());
892 bool AppsGridView::GetDropFormats(
894 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
895 // TODO(koz): Only accept a specific drag type for app shortcuts.
896 *formats
= OSExchangeData::FILE_NAME
;
900 bool AppsGridView::CanDrop(const OSExchangeData
& data
) {
904 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent
& event
) {
905 return ui::DragDropTypes::DRAG_MOVE
;
908 void AppsGridView::Layout() {
909 if (bounds_animator_
.IsAnimating())
910 bounds_animator_
.Cancel();
912 CalculateIdealBounds();
913 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
914 views::View
* view
= view_model_
.view_at(i
);
915 if (view
!= drag_view_
)
916 view
->SetBoundsRect(view_model_
.ideal_bounds(i
));
918 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_
);
920 if (page_switcher_view_
) {
921 const int page_switcher_height
=
922 page_switcher_view_
->GetPreferredSize().height();
923 gfx::Rect
rect(GetContentsBounds());
924 rect
.set_y(rect
.bottom() - page_switcher_height
);
925 rect
.set_height(page_switcher_height
);
926 page_switcher_view_
->SetBoundsRect(rect
);
930 bool AppsGridView::OnKeyPressed(const ui::KeyEvent
& event
) {
931 bool handled
= false;
933 handled
= selected_view_
->OnKeyPressed(event
);
936 const int forward_dir
= base::i18n::IsRTL() ? -1 : 1;
937 switch (event
.key_code()) {
939 MoveSelected(0, -forward_dir
, 0);
942 MoveSelected(0, forward_dir
, 0);
945 MoveSelected(0, 0, -1);
948 MoveSelected(0, 0, 1);
950 case ui::VKEY_PRIOR
: {
951 MoveSelected(-1, 0, 0);
954 case ui::VKEY_NEXT
: {
955 MoveSelected(1, 0, 0);
966 bool AppsGridView::OnKeyReleased(const ui::KeyEvent
& event
) {
967 bool handled
= false;
969 handled
= selected_view_
->OnKeyReleased(event
);
974 bool AppsGridView::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
975 return pagination_controller_
->OnScroll(
976 gfx::Vector2d(event
.x_offset(), event
.y_offset()));
979 void AppsGridView::ViewHierarchyChanged(
980 const ViewHierarchyChangedDetails
& details
) {
981 if (!details
.is_add
&& details
.parent
== this) {
982 // The view being delete should not have reference in |view_model_|.
983 CHECK_EQ(-1, view_model_
.GetIndexOfView(details
.child
));
985 if (selected_view_
== details
.child
)
986 selected_view_
= NULL
;
987 if (activated_folder_item_view_
== details
.child
)
988 activated_folder_item_view_
= NULL
;
990 if (drag_view_
== details
.child
)
993 bounds_animator_
.StopAnimatingView(details
.child
);
997 void AppsGridView::OnGestureEvent(ui::GestureEvent
* event
) {
998 if (pagination_controller_
->OnGestureEvent(*event
, GetContentsBounds()))
1002 void AppsGridView::OnScrollEvent(ui::ScrollEvent
* event
) {
1003 if (event
->type() == ui::ET_SCROLL_FLING_CANCEL
)
1006 gfx::Vector2dF
offset(event
->x_offset(), event
->y_offset());
1007 if (pagination_controller_
->OnScroll(gfx::ToFlooredVector2d(offset
))) {
1008 event
->SetHandled();
1009 event
->StopPropagation();
1013 void AppsGridView::Update() {
1014 DCHECK(!selected_view_
&& !drag_view_
);
1015 view_model_
.Clear();
1016 if (!item_list_
|| !item_list_
->item_count())
1018 for (size_t i
= 0; i
< item_list_
->item_count(); ++i
) {
1019 views::View
* view
= CreateViewForItemAtIndex(i
);
1020 view_model_
.Add(view
, i
);
1024 UpdatePulsingBlockViews();
1029 void AppsGridView::UpdatePaging() {
1030 int total_page
= view_model_
.view_size() && tiles_per_page()
1031 ? (view_model_
.view_size() - 1) / tiles_per_page() + 1
1034 pagination_model_
.SetTotalPages(total_page
);
1037 void AppsGridView::UpdatePulsingBlockViews() {
1038 const int existing_items
= item_list_
? item_list_
->item_count() : 0;
1039 const int available_slots
=
1040 tiles_per_page() - existing_items
% tiles_per_page();
1041 const int desired
= model_
->status() == AppListModel::STATUS_SYNCING
?
1042 available_slots
: 0;
1044 if (pulsing_blocks_model_
.view_size() == desired
)
1047 while (pulsing_blocks_model_
.view_size() > desired
) {
1048 views::View
* view
= pulsing_blocks_model_
.view_at(0);
1049 pulsing_blocks_model_
.Remove(0);
1053 while (pulsing_blocks_model_
.view_size() < desired
) {
1054 views::View
* view
= new PulsingBlockView(
1055 gfx::Size(kPreferredTileWidth
, kPreferredTileHeight
), true);
1056 pulsing_blocks_model_
.Add(view
, 0);
1061 views::View
* AppsGridView::CreateViewForItemAtIndex(size_t index
) {
1062 // The drag_view_ might be pending for deletion, therefore view_model_
1063 // may have one more item than item_list_.
1064 DCHECK_LE(index
, item_list_
->item_count());
1065 AppListItemView
* view
= new AppListItemView(this,
1066 item_list_
->item_at(index
));
1067 view
->SetPaintToLayer(true);
1068 view
->SetFillsBoundsOpaquely(false);
1072 AppsGridView::Index
AppsGridView::GetIndexFromModelIndex(
1073 int model_index
) const {
1074 return Index(model_index
/ tiles_per_page(), model_index
% tiles_per_page());
1077 int AppsGridView::GetModelIndexFromIndex(const Index
& index
) const {
1078 return index
.page
* tiles_per_page() + index
.slot
;
1081 void AppsGridView::SetSelectedItemByIndex(const Index
& index
) {
1082 if (GetIndexOfView(selected_view_
) == index
)
1085 views::View
* new_selection
= GetViewAtIndex(index
);
1087 return; // Keep current selection.
1090 selected_view_
->SchedulePaint();
1092 EnsureViewVisible(new_selection
);
1093 selected_view_
= new_selection
;
1094 selected_view_
->SchedulePaint();
1095 selected_view_
->NotifyAccessibilityEvent(
1096 ui::AX_EVENT_FOCUS
, true);
1099 bool AppsGridView::IsValidIndex(const Index
& index
) const {
1100 return index
.page
>= 0 && index
.page
< pagination_model_
.total_pages() &&
1101 index
.slot
>= 0 && index
.slot
< tiles_per_page() &&
1102 GetModelIndexFromIndex(index
) < view_model_
.view_size();
1105 AppsGridView::Index
AppsGridView::GetIndexOfView(
1106 const views::View
* view
) const {
1107 const int model_index
= view_model_
.GetIndexOfView(view
);
1108 if (model_index
== -1)
1111 return GetIndexFromModelIndex(model_index
);
1114 views::View
* AppsGridView::GetViewAtIndex(const Index
& index
) const {
1115 if (!IsValidIndex(index
))
1118 const int model_index
= GetModelIndexFromIndex(index
);
1119 return view_model_
.view_at(model_index
);
1122 void AppsGridView::MoveSelected(int page_delta
,
1125 if (!selected_view_
)
1126 return SetSelectedItemByIndex(Index(pagination_model_
.selected_page(), 0));
1128 const Index
& selected
= GetIndexOfView(selected_view_
);
1129 int target_slot
= selected
.slot
+ slot_x_delta
+ slot_y_delta
* cols_
;
1131 if (selected
.slot
% cols_
== 0 && slot_x_delta
== -1) {
1132 if (selected
.page
> 0) {
1134 target_slot
= selected
.slot
+ cols_
- 1;
1136 target_slot
= selected
.slot
;
1140 if (selected
.slot
% cols_
== cols_
- 1 && slot_x_delta
== 1) {
1141 if (selected
.page
< pagination_model_
.total_pages() - 1) {
1143 target_slot
= selected
.slot
- cols_
+ 1;
1145 target_slot
= selected
.slot
;
1149 // Clamp the target slot to the last item if we are moving to the last page
1150 // but our target slot is past the end of the item list.
1152 selected
.page
+ page_delta
== pagination_model_
.total_pages() - 1) {
1153 int last_item_slot
= (view_model_
.view_size() - 1) % tiles_per_page();
1154 if (last_item_slot
< target_slot
) {
1155 target_slot
= last_item_slot
;
1159 int target_page
= std::min(pagination_model_
.total_pages() - 1,
1160 std::max(selected
.page
+ page_delta
, 0));
1161 SetSelectedItemByIndex(Index(target_page
, target_slot
));
1164 void AppsGridView::CalculateIdealBounds() {
1165 gfx::Rect
rect(GetContentsBounds());
1169 gfx::Size
tile_size(kPreferredTileWidth
, kPreferredTileHeight
);
1171 gfx::Rect
grid_rect(gfx::Size(tile_size
.width() * cols_
,
1172 tile_size
.height() * rows_per_page_
));
1173 grid_rect
.Intersect(rect
);
1175 // Page size including padding pixels. A tile.x + page_width means the same
1176 // tile slot in the next page; similarly for tile.y + page_height.
1177 const int page_width
= grid_rect
.width() + kPagePadding
;
1178 const int page_height
= grid_rect
.height() + kPagePadding
;
1180 // If there is a transition, calculates offset for current and target page.
1181 const int current_page
= pagination_model_
.selected_page();
1182 const PaginationModel::Transition
& transition
=
1183 pagination_model_
.transition();
1184 const bool is_valid
= pagination_model_
.is_valid_page(transition
.target_page
);
1186 // Transition to previous page means negative offset.
1187 const int dir
= transition
.target_page
> current_page
? -1 : 1;
1189 const int total_views
=
1190 view_model_
.view_size() + pulsing_blocks_model_
.view_size();
1192 for (int i
= 0; i
< total_views
; ++i
) {
1193 if (i
< view_model_
.view_size() && view_model_
.view_at(i
) == drag_view_
) {
1194 if (EnableFolderDragDropUI() && drop_attempt_
== DROP_FOR_FOLDER
)
1199 Index view_index
= GetIndexFromModelIndex(slot_index
);
1201 if (drop_target_
== view_index
) {
1202 if (EnableFolderDragDropUI() && drop_attempt_
== DROP_FOR_FOLDER
) {
1203 view_index
= GetIndexFromModelIndex(slot_index
);
1204 } else if (!EnableFolderDragDropUI() ||
1205 drop_attempt_
== DROP_FOR_REORDER
) {
1207 view_index
= GetIndexFromModelIndex(slot_index
);
1211 // Decide the x or y offset for current item.
1215 if (pagination_controller_
->scroll_axis() ==
1216 PaginationController::SCROLL_AXIS_HORIZONTAL
) {
1217 if (view_index
.page
< current_page
)
1218 x_offset
= -page_width
;
1219 else if (view_index
.page
> current_page
)
1220 x_offset
= page_width
;
1223 if (view_index
.page
== current_page
||
1224 view_index
.page
== transition
.target_page
) {
1225 x_offset
+= transition
.progress
* page_width
* dir
;
1229 if (view_index
.page
< current_page
)
1230 y_offset
= -page_height
;
1231 else if (view_index
.page
> current_page
)
1232 y_offset
= page_height
;
1235 if (view_index
.page
== current_page
||
1236 view_index
.page
== transition
.target_page
) {
1237 y_offset
+= transition
.progress
* page_height
* dir
;
1242 const int row
= view_index
.slot
/ cols_
;
1243 const int col
= view_index
.slot
% cols_
;
1244 gfx::Rect
tile_slot(
1245 gfx::Point(grid_rect
.x() + col
* tile_size
.width() + x_offset
,
1246 grid_rect
.y() + row
* tile_size
.height() + y_offset
),
1248 if (i
< view_model_
.view_size()) {
1249 view_model_
.set_ideal_bounds(i
, tile_slot
);
1251 pulsing_blocks_model_
.set_ideal_bounds(i
- view_model_
.view_size(),
1259 void AppsGridView::AnimateToIdealBounds() {
1260 const gfx::Rect
visible_bounds(GetVisibleBounds());
1262 CalculateIdealBounds();
1263 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
1264 views::View
* view
= view_model_
.view_at(i
);
1265 if (view
== drag_view_
)
1268 const gfx::Rect
& target
= view_model_
.ideal_bounds(i
);
1269 if (bounds_animator_
.GetTargetBounds(view
) == target
)
1272 const gfx::Rect
& current
= view
->bounds();
1273 const bool current_visible
= visible_bounds
.Intersects(current
);
1274 const bool target_visible
= visible_bounds
.Intersects(target
);
1275 const bool visible
= current_visible
|| target_visible
;
1277 const int y_diff
= target
.y() - current
.y();
1278 if (visible
&& y_diff
&& y_diff
% kPreferredTileHeight
== 0) {
1279 AnimationBetweenRows(view
,
1284 } else if (visible
|| bounds_animator_
.IsAnimating(view
)) {
1285 bounds_animator_
.AnimateViewTo(view
, target
);
1286 bounds_animator_
.SetAnimationDelegate(
1288 scoped_ptr
<gfx::AnimationDelegate
>(
1289 new ItemMoveAnimationDelegate(view
)));
1291 view
->SetBoundsRect(target
);
1296 void AppsGridView::AnimationBetweenRows(views::View
* view
,
1297 bool animate_current
,
1298 const gfx::Rect
& current
,
1299 bool animate_target
,
1300 const gfx::Rect
& target
) {
1301 // Determine page of |current| and |target|. -1 means in the left invisible
1302 // page, 0 is the center visible page and 1 means in the right invisible page.
1303 const int current_page
= current
.x() < 0 ? -1 :
1304 current
.x() >= width() ? 1 : 0;
1305 const int target_page
= target
.x() < 0 ? -1 :
1306 target
.x() >= width() ? 1 : 0;
1308 const int dir
= current_page
< target_page
||
1309 (current_page
== target_page
&& current
.y() < target
.y()) ? 1 : -1;
1311 scoped_ptr
<ui::Layer
> layer
;
1312 if (animate_current
) {
1313 layer
= view
->RecreateLayer();
1314 layer
->SuppressPaint();
1316 view
->SetFillsBoundsOpaquely(false);
1317 view
->layer()->SetOpacity(0.f
);
1320 gfx::Rect
current_out(current
);
1321 current_out
.Offset(dir
* kPreferredTileWidth
, 0);
1323 gfx::Rect
target_in(target
);
1325 target_in
.Offset(-dir
* kPreferredTileWidth
, 0);
1326 view
->SetBoundsRect(target_in
);
1327 bounds_animator_
.AnimateViewTo(view
, target
);
1329 bounds_animator_
.SetAnimationDelegate(
1331 scoped_ptr
<gfx::AnimationDelegate
>(
1332 new RowMoveAnimationDelegate(view
, layer
.release(), current_out
)));
1335 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent
& event
,
1336 gfx::Point
* drag_point
) {
1337 #if defined(USE_AURA) && !defined(OS_WIN)
1338 // Use root location of |event| instead of location in |drag_view_|'s
1339 // coordinates because |drag_view_| has a scale transform and location
1340 // could have integer round error and causes jitter.
1341 *drag_point
= event
.root_location();
1343 // GetWidget() could be NULL for tests.
1345 aura::Window::ConvertPointToTarget(
1346 GetWidget()->GetNativeWindow()->GetRootWindow(),
1347 GetWidget()->GetNativeWindow(),
1351 views::View::ConvertPointFromWidget(this, drag_point
);
1353 // For non-aura, root location is not clearly defined but |drag_view_| does
1354 // not have the scale transform. So no round error would be introduced and
1355 // it's okay to use View::ConvertPointToTarget.
1356 *drag_point
= event
.location();
1357 views::View::ConvertPointToTarget(drag_view_
, this, drag_point
);
1361 void AppsGridView::CalculateDropTarget(const gfx::Point
& drag_point
,
1362 bool use_page_button_hovering
) {
1363 if (EnableFolderDragDropUI()) {
1364 CalculateDropTargetWithFolderEnabled(drag_point
, use_page_button_hovering
);
1368 int current_page
= pagination_model_
.selected_page();
1369 gfx::Point
point(drag_point
);
1370 if (!IsPointWithinDragBuffer(drag_point
)) {
1371 point
= drag_start_grid_view_
;
1372 current_page
= drag_start_page_
;
1375 if (use_page_button_hovering
&& page_switcher_view_
&&
1376 page_switcher_view_
->bounds().Contains(point
)) {
1377 gfx::Point
page_switcher_point(point
);
1378 views::View::ConvertPointToTarget(this, page_switcher_view_
,
1379 &page_switcher_point
);
1380 int page
= page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1381 if (pagination_model_
.is_valid_page(page
)) {
1382 drop_target_
.page
= page
;
1383 drop_target_
.slot
= tiles_per_page() - 1;
1386 gfx::Rect
bounds(GetContentsBounds());
1387 const int drop_row
= (point
.y() - bounds
.y()) / kPreferredTileHeight
;
1388 const int drop_col
= std::min(cols_
- 1,
1389 (point
.x() - bounds
.x()) / kPreferredTileWidth
);
1391 drop_target_
.page
= current_page
;
1392 drop_target_
.slot
= std::max(0, std::min(
1393 tiles_per_page() - 1,
1394 drop_row
* cols_
+ drop_col
));
1397 // Limits to the last possible slot on last page.
1398 if (drop_target_
.page
== pagination_model_
.total_pages() - 1) {
1399 drop_target_
.slot
= std::min(
1400 (view_model_
.view_size() - 1) % tiles_per_page(),
1406 void AppsGridView::CalculateDropTargetWithFolderEnabled(
1407 const gfx::Point
& drag_point
,
1408 bool use_page_button_hovering
) {
1409 gfx::Point
point(drag_point
);
1410 if (!IsPointWithinDragBuffer(drag_point
)) {
1411 point
= drag_start_grid_view_
;
1414 if (use_page_button_hovering
&& page_switcher_view_
&&
1415 page_switcher_view_
->bounds().Contains(point
)) {
1416 gfx::Point
page_switcher_point(point
);
1417 views::View::ConvertPointToTarget(this, page_switcher_view_
,
1418 &page_switcher_point
);
1419 int page
= page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1420 if (pagination_model_
.is_valid_page(page
))
1421 drop_attempt_
= DROP_FOR_NONE
;
1424 // Try to find the nearest target for folder dropping or re-ordering.
1425 drop_target_
= GetNearestTileForDragView();
1429 void AppsGridView::OnReorderTimer() {
1430 if (drop_attempt_
== DROP_FOR_REORDER
)
1431 AnimateToIdealBounds();
1434 void AppsGridView::OnFolderItemReparentTimer() {
1435 DCHECK(folder_delegate_
);
1436 if (drag_out_of_folder_container_
&& drag_view_
) {
1437 folder_delegate_
->ReparentItem(drag_view_
, last_drag_point_
);
1439 // Set the flag in the folder's grid view.
1440 dragging_for_reparent_item_
= true;
1442 // Do not observe any data change since it is going to be hidden.
1443 item_list_
->RemoveObserver(this);
1448 void AppsGridView::OnFolderDroppingTimer() {
1449 if (drop_attempt_
== DROP_FOR_FOLDER
)
1450 SetAsFolderDroppingTarget(drop_target_
, true);
1453 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer
,
1454 const gfx::Point
& drag_point
) {
1455 if (IsUnderOEMFolder())
1458 if (IsDraggingForReparentInHiddenGridView()) {
1459 // Dispatch drag event to root level grid view for re-parenting folder
1460 // folder item purpose.
1461 DispatchDragEventForReparent(pointer
, drag_point
);
1465 // Regular drag and drop in a folder's grid view.
1466 folder_delegate_
->UpdateFolderViewBackground(true);
1468 // Calculate if the drag_view_ is dragged out of the folder's container
1470 gfx::Rect bounds_to_folder_view
= ConvertRectToParent(drag_view_
->bounds());
1471 gfx::Point pt
= bounds_to_folder_view
.CenterPoint();
1472 bool is_item_dragged_out_of_folder
=
1473 folder_delegate_
->IsPointOutsideOfFolderBoundary(pt
);
1474 if (is_item_dragged_out_of_folder
) {
1475 if (!drag_out_of_folder_container_
) {
1476 folder_item_reparent_timer_
.Start(
1478 base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay
),
1480 &AppsGridView::OnFolderItemReparentTimer
);
1481 drag_out_of_folder_container_
= true;
1484 folder_item_reparent_timer_
.Stop();
1485 drag_out_of_folder_container_
= false;
1489 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1490 return (!folder_delegate_
&& dragging_for_reparent_item_
);
1493 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1494 return (folder_delegate_
&& dragging_for_reparent_item_
);
1497 gfx::Rect
AppsGridView::GetTargetIconRectInFolder(
1498 AppListItemView
* drag_item_view
,
1499 AppListItemView
* folder_item_view
) {
1500 gfx::Rect view_ideal_bounds
= view_model_
.ideal_bounds(
1501 view_model_
.GetIndexOfView(folder_item_view
));
1502 gfx::Rect icon_ideal_bounds
=
1503 folder_item_view
->GetIconBoundsForTargetViewBounds(view_ideal_bounds
);
1504 AppListFolderItem
* folder_item
=
1505 static_cast<AppListFolderItem
*>(folder_item_view
->item());
1506 return folder_item
->GetTargetIconRectInFolderForItem(
1507 drag_item_view
->item(), icon_ideal_bounds
);
1510 bool AppsGridView::IsUnderOEMFolder() {
1511 if (!folder_delegate_
)
1514 return folder_delegate_
->IsOEMFolder();
1517 void AppsGridView::DispatchDragEventForReparent(Pointer pointer
,
1518 const gfx::Point
& drag_point
) {
1519 folder_delegate_
->DispatchDragEventForReparent(pointer
, drag_point
);
1522 void AppsGridView::EndDragFromReparentItemInRootLevel(
1523 bool events_forwarded_to_drag_drop_host
,
1525 // EndDrag was called before if |drag_view_| is NULL.
1529 DCHECK(IsDraggingForReparentInRootLevelGridView());
1530 bool cancel_reparent
= cancel_drag
|| drop_attempt_
== DROP_FOR_NONE
;
1531 if (!events_forwarded_to_drag_drop_host
&& !cancel_reparent
) {
1532 CalculateDropTarget(last_drag_point_
, true);
1533 if (IsValidIndex(drop_target_
)) {
1534 if (drop_attempt_
== DROP_FOR_REORDER
) {
1535 ReparentItemForReorder(drag_view_
, drop_target_
);
1536 } else if (drop_attempt_
== DROP_FOR_FOLDER
) {
1537 ReparentItemToAnotherFolder(drag_view_
, drop_target_
);
1540 SetViewHidden(drag_view_
, false /* show */, true /* no animate */);
1543 // The drag can be ended after the synchronous drag is created but before it
1545 CleanUpSynchronousDrag();
1547 SetAsFolderDroppingTarget(drop_target_
, false);
1548 if (cancel_reparent
) {
1549 CancelFolderItemReparent(drag_view_
);
1551 // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1552 // cleaning up the newly created AppListItemView, effectively claiming
1553 // ownership of the newly created drag view.
1554 drag_view_
->OnDragEnded();
1558 AnimateToIdealBounds();
1560 StopPageFlipTimer();
1563 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1564 if (drag_and_drop_host_
) {
1565 // If we had a drag and drop proxy icon, we delete it and make the real
1566 // item visible again.
1567 drag_and_drop_host_
->DestroyDragIconProxy();
1570 // The drag can be ended after the synchronous drag is created but before it
1572 CleanUpSynchronousDrag();
1574 SetAsFolderDroppingTarget(drop_target_
, false);
1578 void AppsGridView::OnFolderItemRemoved() {
1579 DCHECK(folder_delegate_
);
1583 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point
& grid_location
) {
1584 // When a drag and drop host is given, the item can be dragged out of the app
1585 // list window. In that case a proxy widget needs to be used.
1586 // Note: This code has very likely to be changed for Windows (non metro mode)
1587 // when a |drag_and_drop_host_| gets implemented.
1588 if (!drag_view_
|| !drag_and_drop_host_
)
1591 gfx::Point screen_location
= grid_location
;
1592 views::View::ConvertPointToScreen(this, &screen_location
);
1594 // Determine the mouse offset to the center of the icon so that the drag and
1595 // drop host follows this layer.
1596 gfx::Vector2d delta
= drag_view_offset_
-
1597 drag_view_
->GetLocalBounds().CenterPoint();
1598 delta
.set_y(delta
.y() + drag_view_
->title()->size().height() / 2);
1600 // We have to hide the original item since the drag and drop host will do
1601 // the OS dependent code to "lift off the dragged item".
1602 DCHECK(!IsDraggingForReparentInRootLevelGridView());
1603 drag_and_drop_host_
->CreateDragIconProxy(screen_location
,
1604 drag_view_
->item()->icon(),
1607 kDragAndDropProxyScale
);
1608 SetViewHidden(drag_view_
,
1610 true /* no animation */);
1613 void AppsGridView::DispatchDragEventToDragAndDropHost(
1614 const gfx::Point
& location_in_screen_coordinates
) {
1615 if (!drag_view_
|| !drag_and_drop_host_
)
1618 if (GetLocalBounds().Contains(last_drag_point_
)) {
1619 // The event was issued inside the app menu and we should get all events.
1620 if (forward_events_to_drag_and_drop_host_
) {
1621 // The DnD host was previously called and needs to be informed that the
1622 // session returns to the owner.
1623 forward_events_to_drag_and_drop_host_
= false;
1624 drag_and_drop_host_
->EndDrag(true);
1627 if (IsFolderItem(drag_view_
->item()))
1630 // The event happened outside our app menu and we might need to dispatch.
1631 if (forward_events_to_drag_and_drop_host_
) {
1632 // Dispatch since we have already started.
1633 if (!drag_and_drop_host_
->Drag(location_in_screen_coordinates
)) {
1634 // The host is not active any longer and we cancel the operation.
1635 forward_events_to_drag_and_drop_host_
= false;
1636 drag_and_drop_host_
->EndDrag(true);
1639 if (drag_and_drop_host_
->StartDrag(drag_view_
->item()->id(),
1640 location_in_screen_coordinates
)) {
1641 // From now on we forward the drag events.
1642 forward_events_to_drag_and_drop_host_
= true;
1643 // Any flip operations are stopped.
1644 StopPageFlipTimer();
1650 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point
& drag_point
) {
1651 if (!IsPointWithinDragBuffer(drag_point
))
1652 StopPageFlipTimer();
1653 int new_page_flip_target
= -1;
1655 // Drag zones are at the edges of the scroll axis.
1656 if (pagination_controller_
->scroll_axis() ==
1657 PaginationController::SCROLL_AXIS_VERTICAL
) {
1658 if (drag_point
.y() < kPageFlipZoneSize
)
1659 new_page_flip_target
= pagination_model_
.selected_page() - 1;
1660 else if (drag_point
.y() > height() - kPageFlipZoneSize
)
1661 new_page_flip_target
= pagination_model_
.selected_page() + 1;
1663 if (page_switcher_view_
->bounds().Contains(drag_point
)) {
1664 gfx::Point
page_switcher_point(drag_point
);
1665 views::View::ConvertPointToTarget(
1666 this, page_switcher_view_
, &page_switcher_point
);
1667 new_page_flip_target
=
1668 page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1671 // TODO(xiyuan): Fix this for RTL.
1672 if (new_page_flip_target
== -1 && drag_point
.x() < kPageFlipZoneSize
)
1673 new_page_flip_target
= pagination_model_
.selected_page() - 1;
1675 if (new_page_flip_target
== -1 &&
1676 drag_point
.x() > width() - kPageFlipZoneSize
) {
1677 new_page_flip_target
= pagination_model_
.selected_page() + 1;
1681 if (new_page_flip_target
== page_flip_target_
)
1684 StopPageFlipTimer();
1685 if (pagination_model_
.is_valid_page(new_page_flip_target
)) {
1686 page_flip_target_
= new_page_flip_target
;
1688 if (page_flip_target_
!= pagination_model_
.selected_page()) {
1689 page_flip_timer_
.Start(FROM_HERE
,
1690 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_
),
1691 this, &AppsGridView::OnPageFlipTimer
);
1696 void AppsGridView::OnPageFlipTimer() {
1697 DCHECK(pagination_model_
.is_valid_page(page_flip_target_
));
1698 pagination_model_
.SelectPage(page_flip_target_
, true);
1701 void AppsGridView::MoveItemInModel(views::View
* item_view
,
1702 const Index
& target
) {
1703 int current_model_index
= view_model_
.GetIndexOfView(item_view
);
1704 DCHECK_GE(current_model_index
, 0);
1706 int target_model_index
= GetModelIndexFromIndex(target
);
1707 if (target_model_index
== current_model_index
)
1710 item_list_
->RemoveObserver(this);
1711 item_list_
->MoveItem(current_model_index
, target_model_index
);
1712 view_model_
.Move(current_model_index
, target_model_index
);
1713 item_list_
->AddObserver(this);
1715 if (pagination_model_
.selected_page() != target
.page
)
1716 pagination_model_
.SelectPage(target
.page
, false);
1719 void AppsGridView::MoveItemToFolder(views::View
* item_view
,
1720 const Index
& target
) {
1721 const std::string
& source_item_id
=
1722 static_cast<AppListItemView
*>(item_view
)->item()->id();
1723 AppListItemView
* target_view
=
1724 static_cast<AppListItemView
*>(GetViewAtSlotOnCurrentPage(target
.slot
));
1725 const std::string
& target_view_item_id
= target_view
->item()->id();
1727 // Make change to data model.
1728 item_list_
->RemoveObserver(this);
1729 std::string folder_item_id
=
1730 model_
->MergeItems(target_view_item_id
, source_item_id
);
1731 item_list_
->AddObserver(this);
1732 if (folder_item_id
.empty()) {
1733 LOG(ERROR
) << "Unable to merge into item id: " << target_view_item_id
;
1736 if (folder_item_id
!= target_view_item_id
) {
1737 // New folder was created, change the view model to replace the old target
1738 // view with the new folder item view.
1739 size_t folder_item_index
;
1740 if (item_list_
->FindItemIndex(folder_item_id
, &folder_item_index
)) {
1741 int target_view_index
= view_model_
.GetIndexOfView(target_view
);
1742 gfx::Rect target_view_bounds
= target_view
->bounds();
1743 DeleteItemViewAtIndex(target_view_index
);
1744 views::View
* target_folder_view
=
1745 CreateViewForItemAtIndex(folder_item_index
);
1746 target_folder_view
->SetBoundsRect(target_view_bounds
);
1747 view_model_
.Add(target_folder_view
, target_view_index
);
1748 AddChildView(target_folder_view
);
1750 LOG(ERROR
) << "Folder no longer in item_list: " << folder_item_id
;
1754 // Fade out the drag_view_ and delete it when animation ends.
1755 int drag_view_index
= view_model_
.GetIndexOfView(drag_view_
);
1756 view_model_
.Remove(drag_view_index
);
1757 bounds_animator_
.AnimateViewTo(drag_view_
, drag_view_
->bounds());
1758 bounds_animator_
.SetAnimationDelegate(
1760 scoped_ptr
<gfx::AnimationDelegate
>(
1761 new ItemRemoveAnimationDelegate(drag_view_
)));
1765 void AppsGridView::ReparentItemForReorder(views::View
* item_view
,
1766 const Index
& target
) {
1767 item_list_
->RemoveObserver(this);
1768 model_
->RemoveObserver(this);
1770 AppListItem
* reparent_item
= static_cast<AppListItemView
*>(item_view
)->item();
1771 DCHECK(reparent_item
->IsInFolder());
1772 const std::string source_folder_id
= reparent_item
->folder_id();
1773 AppListFolderItem
* source_folder
=
1774 static_cast<AppListFolderItem
*>(item_list_
->FindItem(source_folder_id
));
1776 int target_model_index
= GetModelIndexFromIndex(target
);
1778 // Remove the source folder view if there is only 1 item in it, since the
1779 // source folder will be deleted after its only child item removed from it.
1780 if (source_folder
->ChildItemCount() == 1u) {
1781 const int deleted_folder_index
=
1782 view_model_
.GetIndexOfView(activated_folder_item_view());
1783 DeleteItemViewAtIndex(deleted_folder_index
);
1785 // Adjust |target_model_index| if it is beyond the deleted folder index.
1786 if (target_model_index
> deleted_folder_index
)
1787 --target_model_index
;
1790 // Move the item from its parent folder to top level item list.
1791 // Must move to target_model_index, the location we expect the target item
1792 // to be, not the item location we want to insert before.
1793 int current_model_index
= view_model_
.GetIndexOfView(item_view
);
1794 syncer::StringOrdinal target_position
;
1795 if (target_model_index
< static_cast<int>(item_list_
->item_count()))
1796 target_position
= item_list_
->item_at(target_model_index
)->position();
1797 model_
->MoveItemToFolderAt(reparent_item
, "", target_position
);
1798 view_model_
.Move(current_model_index
, target_model_index
);
1800 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id
);
1802 item_list_
->AddObserver(this);
1803 model_
->AddObserver(this);
1807 void AppsGridView::ReparentItemToAnotherFolder(views::View
* item_view
,
1808 const Index
& target
) {
1809 DCHECK(IsDraggingForReparentInRootLevelGridView());
1811 AppListItemView
* target_view
=
1812 static_cast<AppListItemView
*>(GetViewAtSlotOnCurrentPage(target
.slot
));
1816 // Make change to data model.
1817 item_list_
->RemoveObserver(this);
1819 AppListItem
* reparent_item
= static_cast<AppListItemView
*>(item_view
)->item();
1820 DCHECK(reparent_item
->IsInFolder());
1821 const std::string source_folder_id
= reparent_item
->folder_id();
1822 AppListFolderItem
* source_folder
=
1823 static_cast<AppListFolderItem
*>(item_list_
->FindItem(source_folder_id
));
1825 // Remove the source folder view if there is only 1 item in it, since the
1826 // source folder will be deleted after its only child item merged into the
1828 if (source_folder
->ChildItemCount() == 1u)
1829 DeleteItemViewAtIndex(
1830 view_model_
.GetIndexOfView(activated_folder_item_view()));
1832 AppListItem
* target_item
= target_view
->item();
1834 // Move item to the target folder.
1835 std::string target_id_after_merge
=
1836 model_
->MergeItems(target_item
->id(), reparent_item
->id());
1837 if (target_id_after_merge
.empty()) {
1838 LOG(ERROR
) << "Unable to reparent to item id: " << target_item
->id();
1839 item_list_
->AddObserver(this);
1843 if (target_id_after_merge
!= target_item
->id()) {
1844 // New folder was created, change the view model to replace the old target
1845 // view with the new folder item view.
1846 const std::string
& new_folder_id
= reparent_item
->folder_id();
1847 size_t new_folder_index
;
1848 if (item_list_
->FindItemIndex(new_folder_id
, &new_folder_index
)) {
1849 int target_view_index
= view_model_
.GetIndexOfView(target_view
);
1850 DeleteItemViewAtIndex(target_view_index
);
1851 views::View
* new_folder_view
=
1852 CreateViewForItemAtIndex(new_folder_index
);
1853 view_model_
.Add(new_folder_view
, target_view_index
);
1854 AddChildView(new_folder_view
);
1856 LOG(ERROR
) << "Folder no longer in item_list: " << new_folder_id
;
1860 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id
);
1862 item_list_
->AddObserver(this);
1864 // Fade out the drag_view_ and delete it when animation ends.
1865 int drag_view_index
= view_model_
.GetIndexOfView(drag_view_
);
1866 view_model_
.Remove(drag_view_index
);
1867 bounds_animator_
.AnimateViewTo(drag_view_
, drag_view_
->bounds());
1868 bounds_animator_
.SetAnimationDelegate(
1870 scoped_ptr
<gfx::AnimationDelegate
>(
1871 new ItemRemoveAnimationDelegate(drag_view_
)));
1875 // After moving the re-parenting item out of the folder, if there is only 1 item
1876 // left, remove the last item out of the folder, delete the folder and insert it
1877 // to the data model at the same position. Make the same change to view_model_
1879 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1880 const std::string
& source_folder_id
) {
1881 AppListFolderItem
* source_folder
=
1882 static_cast<AppListFolderItem
*>(item_list_
->FindItem(source_folder_id
));
1883 if (!source_folder
|| source_folder
->ChildItemCount() != 1u)
1886 // Delete view associated with the folder item to be removed.
1887 DeleteItemViewAtIndex(
1888 view_model_
.GetIndexOfView(activated_folder_item_view()));
1890 // Now make the data change to remove the folder item in model.
1891 AppListItem
* last_item
= source_folder
->item_list()->item_at(0);
1892 model_
->MoveItemToFolderAt(last_item
, "", source_folder
->position());
1894 // Create a new item view for the last item in folder.
1895 size_t last_item_index
;
1896 if (!item_list_
->FindItemIndex(last_item
->id(), &last_item_index
) ||
1897 last_item_index
> static_cast<size_t>(view_model_
.view_size())) {
1901 views::View
* last_item_view
= CreateViewForItemAtIndex(last_item_index
);
1902 view_model_
.Add(last_item_view
, last_item_index
);
1903 AddChildView(last_item_view
);
1906 void AppsGridView::CancelFolderItemReparent(AppListItemView
* drag_item_view
) {
1907 // The icon of the dragged item must target to its final ideal bounds after
1908 // the animation completes.
1909 CalculateIdealBounds();
1911 gfx::Rect target_icon_rect
=
1912 GetTargetIconRectInFolder(drag_item_view
, activated_folder_item_view_
);
1914 gfx::Rect drag_view_icon_to_grid
=
1915 drag_item_view
->ConvertRectToParent(drag_item_view
->GetIconBounds());
1916 drag_view_icon_to_grid
.ClampToCenteredSize(
1917 gfx::Size(kGridIconDimension
, kGridIconDimension
));
1918 TopIconAnimationView
* icon_view
= new TopIconAnimationView(
1919 drag_item_view
->item()->icon(),
1921 false); /* animate like closing folder */
1922 AddChildView(icon_view
);
1923 icon_view
->SetBoundsRect(drag_view_icon_to_grid
);
1924 icon_view
->TransformView();
1927 void AppsGridView::CancelContextMenusOnCurrentPage() {
1928 int start
= pagination_model_
.selected_page() * tiles_per_page();
1929 int end
= std::min(view_model_
.view_size(), start
+ tiles_per_page());
1930 for (int i
= start
; i
< end
; ++i
) {
1931 AppListItemView
* view
=
1932 static_cast<AppListItemView
*>(view_model_
.view_at(i
));
1933 view
->CancelContextMenu();
1937 void AppsGridView::DeleteItemViewAtIndex(int index
) {
1938 views::View
* item_view
= view_model_
.view_at(index
);
1939 view_model_
.Remove(index
);
1940 if (item_view
== drag_view_
)
1945 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point
& point
) const {
1946 gfx::Rect
rect(GetLocalBounds());
1947 rect
.Inset(-kDragBufferPx
, -kDragBufferPx
, -kDragBufferPx
, -kDragBufferPx
);
1948 return rect
.Contains(point
);
1951 void AppsGridView::ButtonPressed(views::Button
* sender
,
1952 const ui::Event
& event
) {
1956 if (strcmp(sender
->GetClassName(), AppListItemView::kViewClassName
))
1960 // Always set the previous activated_folder_item_view_ to be visible. This
1961 // prevents a case where the item would remain hidden due the
1962 // |activated_folder_item_view_| changing during the animation. We only
1963 // need to track |activated_folder_item_view_| in the root level grid view.
1964 if (!folder_delegate_
) {
1965 if (activated_folder_item_view_
)
1966 activated_folder_item_view_
->SetVisible(true);
1967 AppListItemView
* pressed_item_view
=
1968 static_cast<AppListItemView
*>(sender
);
1969 if (IsFolderItem(pressed_item_view
->item()))
1970 activated_folder_item_view_
= pressed_item_view
;
1972 activated_folder_item_view_
= NULL
;
1974 delegate_
->ActivateApp(static_cast<AppListItemView
*>(sender
)->item(),
1979 void AppsGridView::OnListItemAdded(size_t index
, AppListItem
* item
) {
1982 views::View
* view
= CreateViewForItemAtIndex(index
);
1983 view_model_
.Add(view
, index
);
1987 UpdatePulsingBlockViews();
1992 void AppsGridView::OnListItemRemoved(size_t index
, AppListItem
* item
) {
1995 DeleteItemViewAtIndex(index
);
1998 UpdatePulsingBlockViews();
2003 void AppsGridView::OnListItemMoved(size_t from_index
,
2005 AppListItem
* item
) {
2007 view_model_
.Move(from_index
, to_index
);
2010 AnimateToIdealBounds();
2013 void AppsGridView::TotalPagesChanged() {
2016 void AppsGridView::SelectedPageChanged(int old_selected
, int new_selected
) {
2018 CalculateDropTarget(last_drag_point_
, true);
2020 MaybeStartPageFlipTimer(last_drag_point_
);
2022 ClearSelectedView(selected_view_
);
2027 void AppsGridView::TransitionStarted() {
2028 CancelContextMenusOnCurrentPage();
2031 void AppsGridView::TransitionChanged() {
2032 // Update layout for valid page transition only since over-scroll no longer
2033 // animates app icons.
2034 const PaginationModel::Transition
& transition
=
2035 pagination_model_
.transition();
2036 if (pagination_model_
.is_valid_page(transition
.target_page
))
2040 void AppsGridView::OnAppListModelStatusChanged() {
2041 UpdatePulsingBlockViews();
2046 void AppsGridView::SetViewHidden(views::View
* view
, bool hide
, bool immediate
) {
2047 ui::ScopedLayerAnimationSettings
animator(view
->layer()->GetAnimator());
2048 animator
.SetPreemptionStrategy(
2049 immediate
? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
:
2050 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION
);
2051 view
->layer()->SetOpacity(hide
? 0 : 1);
2054 void AppsGridView::OnImplicitAnimationsCompleted() {
2055 if (layer()->opacity() == 0.0f
)
2059 bool AppsGridView::EnableFolderDragDropUI() {
2060 // Enable drag and drop folder UI only if it is at the app list root level
2061 // and the switch is on and the target folder can still accept new items.
2062 return model_
->folders_enabled() && !folder_delegate_
&&
2063 CanDropIntoTarget(drop_target_
);
2066 bool AppsGridView::CanDropIntoTarget(const Index
& drop_target
) {
2067 views::View
* target_view
= GetViewAtSlotOnCurrentPage(drop_target
.slot
);
2071 AppListItem
* target_item
=
2072 static_cast<AppListItemView
*>(target_view
)->item();
2073 // Items can be dropped into non-folders (which have no children) or folders
2074 // that have fewer than the max allowed items.
2075 // OEM folder does not allow to drag/drop other items in it.
2076 return target_item
->ChildItemCount() < kMaxFolderItems
&&
2077 !IsOEMFolderItem(target_item
);
2080 // TODO(jennyz): Optimize the calculation for finding nearest tile.
2081 AppsGridView::Index
AppsGridView::GetNearestTileForDragView() {
2083 nearest_tile
.page
= -1;
2084 nearest_tile
.slot
= -1;
2087 // Calculate the top left tile |drag_view| intersects.
2088 gfx::Point pt
= drag_view_
->bounds().origin();
2089 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
2091 // Calculate the top right tile |drag_view| intersects.
2092 pt
= drag_view_
->bounds().top_right();
2093 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
2095 // Calculate the bottom left tile |drag_view| intersects.
2096 pt
= drag_view_
->bounds().bottom_left();
2097 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
2099 // Calculate the bottom right tile |drag_view| intersects.
2100 pt
= drag_view_
->bounds().bottom_right();
2101 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
2103 const int d_folder_dropping
=
2104 kFolderDroppingCircleRadius
+ kGridIconDimension
/ 2;
2105 const int d_reorder
= kReorderDroppingCircleRadius
+ kGridIconDimension
/ 2;
2107 // If user drags an item across pages to the last page, and targets it
2108 // to the last empty slot on it, push the last item for re-ordering.
2109 if (IsLastPossibleDropTarget(nearest_tile
) && d_min
< d_reorder
) {
2110 drop_attempt_
= DROP_FOR_REORDER
;
2111 nearest_tile
.slot
= nearest_tile
.slot
- 1;
2112 return nearest_tile
;
2115 if (IsValidIndex(nearest_tile
)) {
2116 if (d_min
< d_folder_dropping
) {
2117 views::View
* target_view
= GetViewAtSlotOnCurrentPage(nearest_tile
.slot
);
2119 !IsFolderItem(static_cast<AppListItemView
*>(drag_view_
)->item())) {
2120 // If a non-folder item is dragged to the target slot with an item
2121 // sitting on it, attempt to drop the dragged item into the folder
2122 // containing the item on nearest_tile.
2123 drop_attempt_
= DROP_FOR_FOLDER
;
2124 return nearest_tile
;
2126 // If the target slot is blank, or the dragged item is a folder, attempt
2128 drop_attempt_
= DROP_FOR_REORDER
;
2129 return nearest_tile
;
2131 } else if (d_min
< d_reorder
) {
2132 // Entering the re-order circle of the slot.
2133 drop_attempt_
= DROP_FOR_REORDER
;
2134 return nearest_tile
;
2138 // If |drag_view| is not entering the re-order or fold dropping region of
2139 // any items, cancel any previous re-order or folder dropping timer, and
2141 drop_attempt_
= DROP_FOR_NONE
;
2142 reorder_timer_
.Stop();
2143 folder_dropping_timer_
.Stop();
2145 // When dragging for reparent a folder item, it should go back to its parent
2146 // folder item if there is no drop target.
2147 if (IsDraggingForReparentInRootLevelGridView()) {
2148 DCHECK(activated_folder_item_view_
);
2149 return GetIndexOfView(activated_folder_item_view_
);
2152 return GetIndexOfView(drag_view_
);
2155 void AppsGridView::CalculateNearestTileForVertex(const gfx::Point
& vertex
,
2156 Index
* nearest_tile
,
2159 gfx::Rect target_bounds
= GetTileBoundsForPoint(vertex
, &target_index
);
2161 if (target_bounds
.IsEmpty() || target_index
== *nearest_tile
)
2164 // Do not count the tile, where drag_view_ used to sit on and is still moving
2165 // on top of it, in calculating nearest tile for drag_view_.
2166 views::View
* target_view
= GetViewAtSlotOnCurrentPage(target_index
.slot
);
2167 if (target_index
== drag_view_init_index_
&& !target_view
&&
2168 !IsDraggingForReparentInRootLevelGridView()) {
2172 int d_center
= GetDistanceBetweenRects(drag_view_
->bounds(), target_bounds
);
2173 if (*d_min
< 0 || d_center
< *d_min
) {
2175 *nearest_tile
= target_index
;
2179 gfx::Rect
AppsGridView::GetTileBoundsForPoint(const gfx::Point
& point
,
2180 Index
*tile_index
) {
2181 // Check if |point| is outside of contents bounds.
2182 gfx::Rect
bounds(GetContentsBounds());
2183 if (!bounds
.Contains(point
))
2186 // Calculate which tile |point| is enclosed in.
2189 int col
= (x
- bounds
.x()) / kPreferredTileWidth
;
2190 int row
= (y
- bounds
.y()) / kPreferredTileHeight
;
2191 gfx::Rect tile_rect
= GetTileBounds(row
, col
);
2193 // Check if |point| is outside a valid item's tile.
2194 Index
index(pagination_model_
.selected_page(), row
* cols_
+ col
);
2195 *tile_index
= index
;
2199 gfx::Rect
AppsGridView::GetTileBounds(int row
, int col
) const {
2200 gfx::Rect
bounds(GetContentsBounds());
2201 gfx::Size
tile_size(kPreferredTileWidth
, kPreferredTileHeight
);
2202 gfx::Rect
grid_rect(gfx::Size(tile_size
.width() * cols_
,
2203 tile_size
.height() * rows_per_page_
));
2204 grid_rect
.Intersect(bounds
);
2205 gfx::Rect
tile_rect(
2206 gfx::Point(grid_rect
.x() + col
* tile_size
.width(),
2207 grid_rect
.y() + row
* tile_size
.height()),
2212 bool AppsGridView::IsLastPossibleDropTarget(const Index
& index
) const {
2213 int last_possible_slot
= view_model_
.view_size() % tiles_per_page();
2214 return (index
.page
== pagination_model_
.total_pages() - 1 &&
2215 index
.slot
== last_possible_slot
+ 1);
2218 views::View
* AppsGridView::GetViewAtSlotOnCurrentPage(int slot
) {
2222 // Calculate the original bound of the tile at |index|.
2223 int row
= slot
/ cols_
;
2224 int col
= slot
% cols_
;
2225 gfx::Rect tile_rect
= GetTileBounds(row
, col
);
2227 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
2228 views::View
* view
= view_model_
.view_at(i
);
2229 if (view
->bounds() == tile_rect
&& view
!= drag_view_
)
2235 void AppsGridView::SetAsFolderDroppingTarget(const Index
& target_index
,
2236 bool is_target_folder
) {
2237 AppListItemView
* target_view
=
2238 static_cast<AppListItemView
*>(
2239 GetViewAtSlotOnCurrentPage(target_index
.slot
));
2241 target_view
->SetAsAttemptedFolderTarget(is_target_folder
);
2244 } // namespace app_list