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 "content/public/browser/web_contents.h"
13 #include "ui/app_list/app_list_constants.h"
14 #include "ui/app_list/app_list_folder_item.h"
15 #include "ui/app_list/app_list_item.h"
16 #include "ui/app_list/app_list_switches.h"
17 #include "ui/app_list/pagination_model.h"
18 #include "ui/app_list/views/app_list_drag_and_drop_host.h"
19 #include "ui/app_list/views/app_list_folder_view.h"
20 #include "ui/app_list/views/app_list_item_view.h"
21 #include "ui/app_list/views/apps_grid_view_delegate.h"
22 #include "ui/app_list/views/page_switcher.h"
23 #include "ui/app_list/views/pulsing_block_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/views/border.h"
28 #include "ui/views/controls/webview/webview.h"
29 #include "ui/views/view_model_utils.h"
30 #include "ui/views/widget/widget.h"
33 #include "ui/aura/root_window.h"
34 #include "ui/aura/window.h"
36 #include "ui/views/win/hwnd_util.h"
37 #endif // defined(OS_WIN)
38 #endif // defined(USE_AURA)
41 #include "base/command_line.h"
42 #include "base/files/file_path.h"
43 #include "base/win/shortcut.h"
44 #include "ui/base/dragdrop/drag_utils.h"
45 #include "ui/base/dragdrop/drop_target_win.h"
46 #include "ui/base/dragdrop/os_exchange_data.h"
47 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
54 // Distance a drag needs to be from the app grid to be considered 'outside', at
55 // which point we rearrange the apps to their pre-drag configuration, as a drop
56 // then would be canceled. We have a buffer to make it easier to drag apps to
58 const int kDragBufferPx
= 20;
60 // Padding space in pixels for fixed layout.
61 const int kLeftRightPadding
= 20;
62 const int kTopPadding
= 1;
64 // Padding space in pixels between pages.
65 const int kPagePadding
= 40;
67 // Preferred tile size when showing in fixed layout.
68 const int kPreferredTileWidth
= 88;
69 const int kPreferredTileHeight
= 98;
71 // Width in pixels of the area on the sides that triggers a page flip.
72 const int kPageFlipZoneSize
= 40;
74 // Delay in milliseconds to do the page flip.
75 const int kPageFlipDelayInMs
= 1000;
77 // How many pages on either side of the selected one we prerender.
78 const int kPrerenderPages
= 1;
80 // The drag and drop proxy should get scaled by this factor.
81 const float kDragAndDropProxyScale
= 1.5f
;
83 // Delays in milliseconds to show folder dropping preview circle.
84 const int kFolderDroppingDelay
= 250;
86 // Delays in milliseconds to show re-order preview.
87 const int kReorderDelay
= 50;
89 // Radius of the circle, in which if entered, show folder dropping preview
91 const int kFolderDroppingCircleRadius
= 15;
93 // Radius of the circle, in which if entered, show re-order preview.
94 const int kReorderDroppingCircleRadius
= 30;
96 // Max items allowed in a folder.
97 size_t kMaxFolderItems
= 16;
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
105 : public views::BoundsAnimator::OwnedAnimationDelegate
{
107 RowMoveAnimationDelegate(views::View
* view
,
109 const gfx::Rect
& layer_target
)
112 layer_start_(layer
? layer
->bounds() : gfx::Rect()),
113 layer_target_(layer_target
) {
115 virtual ~RowMoveAnimationDelegate() {}
117 // gfx::AnimationDelegate overrides:
118 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
{
119 view_
->layer()->SetOpacity(animation
->GetCurrentValue());
120 view_
->layer()->ScheduleDraw();
123 layer_
->SetOpacity(1 - animation
->GetCurrentValue());
124 layer_
->SetBounds(animation
->CurrentValueBetween(layer_start_
,
126 layer_
->ScheduleDraw();
129 virtual void AnimationEnded(const gfx::Animation
* animation
) OVERRIDE
{
130 view_
->layer()->SetOpacity(1.0f
);
131 view_
->layer()->ScheduleDraw();
133 virtual void AnimationCanceled(const gfx::Animation
* animation
) OVERRIDE
{
134 view_
->layer()->SetOpacity(1.0f
);
135 view_
->layer()->ScheduleDraw();
139 // The view that needs to be wrapped. Owned by views hierarchy.
142 scoped_ptr
<ui::Layer
> layer_
;
143 const gfx::Rect layer_start_
;
144 const gfx::Rect layer_target_
;
146 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate
);
149 // ItemRemoveAnimationDelegate is used to show animation for removing an item.
150 // This happens when user drags an item into a folder. The dragged item will
151 // be removed from the original list after it is dropped into the folder.
152 class ItemRemoveAnimationDelegate
153 : public views::BoundsAnimator::OwnedAnimationDelegate
{
155 explicit ItemRemoveAnimationDelegate(views::View
* view
)
159 virtual ~ItemRemoveAnimationDelegate() {
162 // gfx::AnimationDelegate overrides:
163 virtual void AnimationProgressed(const gfx::Animation
* animation
) OVERRIDE
{
164 view_
->layer()->SetOpacity(1 - animation
->GetCurrentValue());
165 view_
->layer()->ScheduleDraw();
169 scoped_ptr
<views::View
> view_
;
171 DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate
);
174 // Gets the distance between the centers of the |rect_1| and |rect_2|.
175 int GetDistanceBetweenRects(gfx::Rect rect_1
,
177 return (rect_1
.CenterPoint() - rect_2
.CenterPoint()).Length();
180 // Returns true if the |item| is an folder item.
181 bool IsFolderItem(AppListItem
* item
) {
182 return (item
->GetItemType() == AppListFolderItem::kItemType
);
188 // Interprets drag events sent from Windows via the drag/drop API and forwards
189 // them to AppsGridView.
190 // On Windows, in order to have the OS perform the drag properly we need to
191 // provide it with a shortcut file which may or may not exist at the time the
192 // drag is started. Therefore while waiting for that shortcut to be located we
193 // just do a regular "internal" drag and transition into the synchronous drag
194 // when the shortcut is found/created. Hence a synchronous drag is an optional
195 // phase of a regular drag and non-Windows platforms drags are equivalent to a
196 // Windows drag that never enters the synchronous drag phase.
197 class SynchronousDrag
: public ui::DragSourceWin
{
199 SynchronousDrag(AppsGridView
* grid_view
,
200 AppListItemView
* drag_view
,
201 const gfx::Point
& drag_view_offset
)
202 : grid_view_(grid_view
),
203 drag_view_(drag_view
),
204 drag_view_offset_(drag_view_offset
),
205 has_shortcut_path_(false),
209 void set_shortcut_path(const base::FilePath
& shortcut_path
) {
210 has_shortcut_path_
= true;
211 shortcut_path_
= shortcut_path
;
215 return has_shortcut_path_
&& !running_
;
222 ui::OSExchangeData data
;
223 SetupExchangeData(&data
);
225 // Hide the dragged view because the OS is going to create its own.
226 const gfx::Size drag_view_size
= drag_view_
->size();
227 drag_view_
->SetSize(gfx::Size(0, 0));
229 // Blocks until the drag is finished. Calls into the ui::DragSourceWin
232 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data
),
233 this, DROPEFFECT_MOVE
| DROPEFFECT_LINK
, &effects
);
235 // Restore the dragged view to its original size.
236 drag_view_
->SetSize(drag_view_size
);
237 drag_view_
->OnSyncDragEnd();
239 grid_view_
->EndDrag(canceled_
|| !IsCursorWithinGridView());
243 // Overridden from ui::DragSourceWin.
244 virtual void OnDragSourceCancel() OVERRIDE
{
248 virtual void OnDragSourceDrop() OVERRIDE
{
251 virtual void OnDragSourceMove() OVERRIDE
{
252 grid_view_
->UpdateDrag(AppsGridView::MOUSE
, GetCursorInGridViewCoords());
255 void SetupExchangeData(ui::OSExchangeData
* data
) {
256 data
->SetFilename(shortcut_path_
);
257 gfx::ImageSkia
image(drag_view_
->GetDragImage());
258 gfx::Size
image_size(image
.size());
259 drag_utils::SetDragImageOnDataObject(
262 drag_view_offset_
- drag_view_
->GetDragImageOffset(),
266 HWND
GetGridViewHWND() {
267 return views::HWNDForView(grid_view_
);
270 bool IsCursorWithinGridView() {
273 return GetGridViewHWND() == WindowFromPoint(p
);
276 gfx::Point
GetCursorInGridViewCoords() {
279 ScreenToClient(GetGridViewHWND(), &p
);
280 gfx::Point
grid_view_pt(p
.x
, p
.y
);
281 views::View::ConvertPointFromWidget(grid_view_
, &grid_view_pt
);
285 AppsGridView
* grid_view_
;
286 AppListItemView
* drag_view_
;
287 gfx::Point drag_view_offset_
;
288 bool has_shortcut_path_
;
289 base::FilePath shortcut_path_
;
293 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag
);
295 #endif // defined(OS_WIN)
297 AppsGridView::AppsGridView(AppsGridViewDelegate
* delegate
,
298 PaginationModel
* pagination_model
,
299 content::WebContents
* start_page_contents
)
303 pagination_model_(pagination_model
),
304 page_switcher_view_(new PageSwitcher(pagination_model
)),
305 start_page_view_(NULL
),
308 selected_view_(NULL
),
310 drag_start_page_(-1),
312 drop_attempt_(DROP_FOR_NONE
),
313 drag_and_drop_host_(NULL
),
314 forward_events_to_drag_and_drop_host_(false),
315 page_flip_target_(-1),
316 page_flip_delay_in_ms_(kPageFlipDelayInMs
),
317 bounds_animator_(this),
318 is_root_level_(true),
319 activated_item_view_(NULL
) {
320 SetPaintToLayer(true);
321 SetFillsBoundsOpaquely(false);
323 pagination_model_
->AddObserver(this);
324 AddChildView(page_switcher_view_
);
326 if (start_page_contents
) {
328 new views::WebView(start_page_contents
->GetBrowserContext());
329 start_page_view_
->SetWebContents(start_page_contents
);
330 AddChildView(start_page_view_
);
334 AppsGridView::~AppsGridView() {
335 // Coming here |drag_view_| should already be canceled since otherwise the
336 // drag would disappear after the app list got animated away and closed,
337 // which would look odd.
343 model_
->RemoveObserver(this);
344 pagination_model_
->RemoveObserver(this);
347 item_list_
->RemoveObserver(this);
350 void AppsGridView::SetLayout(int icon_size
, int cols
, int rows_per_page
) {
351 icon_size_
.SetSize(icon_size
, icon_size
);
353 rows_per_page_
= rows_per_page
;
355 SetBorder(views::Border::CreateEmptyBorder(
356 kTopPadding
, kLeftRightPadding
, 0, kLeftRightPadding
));
359 void AppsGridView::SetModel(AppListModel
* model
) {
361 model_
->RemoveObserver(this);
365 model_
->AddObserver(this);
370 void AppsGridView::SetItemList(AppListItemList
* item_list
) {
372 item_list_
->RemoveObserver(this);
373 item_list_
= item_list
;
375 item_list_
->AddObserver(this);
379 void AppsGridView::SetSelectedView(views::View
* view
) {
380 if (IsSelectedView(view
) || IsDraggedView(view
))
383 Index index
= GetIndexOfView(view
);
384 if (IsValidIndex(index
))
385 SetSelectedItemByIndex(index
);
388 void AppsGridView::ClearSelectedView(views::View
* view
) {
389 if (view
&& IsSelectedView(view
)) {
390 selected_view_
->SchedulePaint();
391 selected_view_
= NULL
;
395 void AppsGridView::ClearAnySelectedView() {
396 if (selected_view_
) {
397 selected_view_
->SchedulePaint();
398 selected_view_
= NULL
;
402 bool AppsGridView::IsSelectedView(const views::View
* view
) const {
403 return selected_view_
== view
;
406 void AppsGridView::EnsureViewVisible(const views::View
* view
) {
407 if (pagination_model_
->has_transition())
410 Index index
= GetIndexOfView(view
);
411 if (IsValidIndex(index
))
412 pagination_model_
->SelectPage(index
.page
, false);
415 void AppsGridView::InitiateDrag(AppListItemView
* view
,
417 const ui::LocatedEvent
& event
) {
419 if (drag_view_
|| pulsing_blocks_model_
.view_size())
423 drag_view_offset_
= event
.location();
424 drag_start_page_
= pagination_model_
->selected_page();
425 ExtractDragLocation(event
, &drag_start_grid_view_
);
426 drag_view_start_
= gfx::Point(drag_view_
->x(), drag_view_
->y());
429 void AppsGridView::OnGotShortcutPath(const base::FilePath
& path
) {
431 // Drag may have ended before we get the shortcut path.
432 if (!synchronous_drag_
)
434 // Setting the shortcut path here means the next time we hit UpdateDrag()
435 // we'll enter the synchronous drag.
436 // NOTE we don't Run() the drag here because that causes animations not to
437 // update for some reason.
438 synchronous_drag_
->set_shortcut_path(path
);
439 DCHECK(synchronous_drag_
->CanRun());
443 void AppsGridView::StartSettingUpSynchronousDrag() {
448 // Favor the drag and drop host over native win32 drag. For the Win8/ash
449 // launcher we want to have ashes drag and drop over win32's.
450 if (drag_and_drop_host_
)
453 delegate_
->GetShortcutPathForApp(
454 drag_view_
->item()->id(),
455 base::Bind(&AppsGridView::OnGotShortcutPath
, base::Unretained(this)));
456 synchronous_drag_
= new SynchronousDrag(this, drag_view_
, drag_view_offset_
);
460 bool AppsGridView::RunSynchronousDrag() {
462 if (synchronous_drag_
&& synchronous_drag_
->CanRun()) {
463 synchronous_drag_
->Run();
464 synchronous_drag_
= NULL
;
471 void AppsGridView::CleanUpSynchronousDrag() {
473 synchronous_drag_
= NULL
;
477 void AppsGridView::UpdateDragFromItem(Pointer pointer
,
478 const ui::LocatedEvent
& event
) {
481 gfx::Point drag_point_in_grid_view
;
482 ExtractDragLocation(event
, &drag_point_in_grid_view
);
483 UpdateDrag(pointer
, drag_point_in_grid_view
);
487 // If a drag and drop host is provided, see if the drag operation needs to be
489 gfx::Point location_in_screen
= drag_point_in_grid_view
;
490 views::View::ConvertPointToScreen(this, &location_in_screen
);
491 DispatchDragEventToDragAndDropHost(location_in_screen
);
492 if (drag_and_drop_host_
)
493 drag_and_drop_host_
->UpdateDragIconProxy(location_in_screen
);
496 void AppsGridView::UpdateDrag(Pointer pointer
, const gfx::Point
& point
) {
497 // EndDrag was called before if |drag_view_| is NULL.
501 if (RunSynchronousDrag())
504 gfx::Vector2d
drag_vector(point
- drag_start_grid_view_
);
505 if (!dragging() && ExceededDragThreshold(drag_vector
)) {
506 drag_pointer_
= pointer
;
507 // Move the view to the front so that it appears on top of other views.
508 ReorderChildView(drag_view_
, -1);
509 bounds_animator_
.StopAnimatingView(drag_view_
);
510 StartSettingUpSynchronousDrag();
511 StartDragAndDropHostDrag(point
);
514 if (drag_pointer_
!= pointer
)
517 last_drag_point_
= point
;
518 const Index last_drop_target
= drop_target_
;
519 DropAttempt last_drop_attempt
= drop_attempt_
;
520 CalculateDropTarget(last_drag_point_
, false);
522 if (IsPointWithinDragBuffer(last_drag_point_
))
523 MaybeStartPageFlipTimer(last_drag_point_
);
527 gfx::Point
page_switcher_point(last_drag_point_
);
528 views::View::ConvertPointToTarget(this, page_switcher_view_
,
529 &page_switcher_point
);
530 page_switcher_view_
->UpdateUIForDragPoint(page_switcher_point
);
532 if (!EnableFolderDragDropUI()) {
533 if (last_drop_target
!= drop_target_
)
534 AnimateToIdealBounds();
535 drag_view_
->SetPosition(drag_view_start_
+ drag_vector
);
539 // Update drag with folder UI enabled.
540 if (last_drop_target
!= drop_target_
||
541 last_drop_attempt
!= drop_attempt_
) {
542 if (drop_attempt_
== DROP_FOR_REORDER
) {
543 folder_dropping_timer_
.Stop();
544 reorder_timer_
.Start(FROM_HERE
,
545 base::TimeDelta::FromMilliseconds(kReorderDelay
),
546 this, &AppsGridView::OnReorderTimer
);
547 } else if (drop_attempt_
== DROP_FOR_FOLDER
) {
548 reorder_timer_
.Stop();
549 folder_dropping_timer_
.Start(FROM_HERE
,
550 base::TimeDelta::FromMilliseconds(kFolderDroppingDelay
),
551 this, &AppsGridView::OnFolderDroppingTimer
);
554 // Reset the previous drop target.
555 SetAsFolderDroppingTarget(last_drop_target
, false);
558 drag_view_
->SetPosition(drag_view_start_
+ drag_vector
);
561 void AppsGridView::EndDrag(bool cancel
) {
562 // EndDrag was called before if |drag_view_| is NULL.
565 // Coming here a drag and drop was in progress.
566 bool landed_in_drag_and_drop_host
= forward_events_to_drag_and_drop_host_
;
567 if (forward_events_to_drag_and_drop_host_
) {
568 forward_events_to_drag_and_drop_host_
= false;
569 drag_and_drop_host_
->EndDrag(cancel
);
570 } else if (!cancel
&& dragging()) {
571 CalculateDropTarget(last_drag_point_
, true);
572 if (IsValidIndex(drop_target_
)) {
573 if (!EnableFolderDragDropUI()) {
574 MoveItemInModel(drag_view_
, drop_target_
);
576 if (drop_attempt_
== DROP_FOR_REORDER
)
577 MoveItemInModel(drag_view_
, drop_target_
);
578 else if (drop_attempt_
== DROP_FOR_FOLDER
)
579 MoveItemToFolder(drag_view_
, drop_target_
);
584 if (drag_and_drop_host_
) {
585 // If we had a drag and drop proxy icon, we delete it and make the real
586 // item visible again.
587 drag_and_drop_host_
->DestroyDragIconProxy();
588 if (landed_in_drag_and_drop_host
) {
589 // Move the item directly to the target location, avoiding the "zip back"
590 // animation if the user was pinning it to the shelf.
591 int i
= drop_target_
.slot
;
592 gfx::Rect bounds
= view_model_
.ideal_bounds(i
);
593 drag_view_
->SetBoundsRect(bounds
);
595 // Fade in slowly if it landed in the shelf.
596 SetViewHidden(drag_view_
,
598 !landed_in_drag_and_drop_host
/* animate */);
601 // The drag can be ended after the synchronous drag is created but before it
603 CleanUpSynchronousDrag();
605 SetAsFolderDroppingTarget(drop_target_
, false);
606 drop_attempt_
= DROP_FOR_NONE
;
607 drag_pointer_
= NONE
;
608 drop_target_
= Index();
609 drag_view_
->OnDragEnded();
611 drag_start_grid_view_
= gfx::Point();
612 drag_start_page_
= -1;
613 drag_view_offset_
= gfx::Point();
614 AnimateToIdealBounds();
619 void AppsGridView::StopPageFlipTimer() {
620 page_flip_timer_
.Stop();
621 page_flip_target_
= -1;
624 AppListItemView
* AppsGridView::GetItemViewAt(int index
) const {
625 DCHECK(index
>= 0 && index
< view_model_
.view_size());
626 return static_cast<AppListItemView
*>(view_model_
.view_at(index
));
629 void AppsGridView::SetTopItemViewsVisible(bool visible
) {
630 int top_item_count
= std::min(static_cast<int>(kNumFolderTopItems
),
631 view_model_
.view_size());
632 for (int i
= 0; i
< top_item_count
; ++i
)
633 GetItemViewAt(i
)->SetVisible(visible
);
636 void AppsGridView::ScheduleShowHideAnimation(bool show
) {
637 // Stop any previous animation.
638 layer()->GetAnimator()->StopAnimating();
640 // Set initial state.
642 layer()->SetOpacity(show
? 0.0f
: 1.0f
);
644 ui::ScopedLayerAnimationSettings
animation(layer()->GetAnimator());
645 animation
.AddObserver(this);
646 animation
.SetTweenType(gfx::Tween::EASE_IN_2
);
647 animation
.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
648 show
? kFolderTransitionInDurationMs
: kFolderTransitionOutDurationMs
));
650 layer()->SetOpacity(show
? 1.0f
: 0.0f
);
653 bool AppsGridView::IsDraggedView(const views::View
* view
) const {
654 return drag_view_
== view
;
657 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
658 ApplicationDragAndDropHost
* drag_and_drop_host
) {
659 drag_and_drop_host_
= drag_and_drop_host
;
662 void AppsGridView::Prerender(int page_index
) {
664 int start
= std::max(0, (page_index
- kPrerenderPages
) * tiles_per_page());
665 int end
= std::min(view_model_
.view_size(),
666 (page_index
+ 1 + kPrerenderPages
) * tiles_per_page());
667 for (int i
= start
; i
< end
; i
++) {
668 AppListItemView
* v
= static_cast<AppListItemView
*>(view_model_
.view_at(i
));
673 gfx::Size
AppsGridView::GetPreferredSize() {
674 const gfx::Insets
insets(GetInsets());
675 const gfx::Size tile_size
= gfx::Size(kPreferredTileWidth
,
676 kPreferredTileHeight
);
677 const int page_switcher_height
=
678 page_switcher_view_
->GetPreferredSize().height();
680 tile_size
.width() * cols_
+ insets
.width(),
681 tile_size
.height() * rows_per_page_
+
682 page_switcher_height
+ insets
.height());
685 bool AppsGridView::GetDropFormats(
687 std::set
<OSExchangeData::CustomFormat
>* custom_formats
) {
688 // TODO(koz): Only accept a specific drag type for app shortcuts.
689 *formats
= OSExchangeData::FILE_NAME
;
693 bool AppsGridView::CanDrop(const OSExchangeData
& data
) {
697 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent
& event
) {
698 return ui::DragDropTypes::DRAG_MOVE
;
701 void AppsGridView::Layout() {
702 if (bounds_animator_
.IsAnimating())
703 bounds_animator_
.Cancel();
705 CalculateIdealBounds();
706 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
707 views::View
* view
= view_model_
.view_at(i
);
708 if (view
!= drag_view_
)
709 view
->SetBoundsRect(view_model_
.ideal_bounds(i
));
711 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_
);
713 const int page_switcher_height
=
714 page_switcher_view_
->GetPreferredSize().height();
715 gfx::Rect
rect(GetContentsBounds());
716 rect
.set_y(rect
.bottom() - page_switcher_height
);
717 rect
.set_height(page_switcher_height
);
718 page_switcher_view_
->SetBoundsRect(rect
);
723 bool AppsGridView::OnKeyPressed(const ui::KeyEvent
& event
) {
724 bool handled
= false;
726 handled
= selected_view_
->OnKeyPressed(event
);
729 const int forward_dir
= base::i18n::IsRTL() ? -1 : 1;
730 switch (event
.key_code()) {
732 MoveSelected(0, -forward_dir
, 0);
735 MoveSelected(0, forward_dir
, 0);
738 MoveSelected(0, 0, -1);
741 MoveSelected(0, 0, 1);
743 case ui::VKEY_PRIOR
: {
744 MoveSelected(-1, 0, 0);
747 case ui::VKEY_NEXT
: {
748 MoveSelected(1, 0, 0);
759 bool AppsGridView::OnKeyReleased(const ui::KeyEvent
& event
) {
760 bool handled
= false;
762 handled
= selected_view_
->OnKeyReleased(event
);
767 void AppsGridView::ViewHierarchyChanged(
768 const ViewHierarchyChangedDetails
& details
) {
769 if (!details
.is_add
&& details
.parent
== this) {
770 if (selected_view_
== details
.child
)
771 selected_view_
= NULL
;
773 if (drag_view_
== details
.child
)
776 bounds_animator_
.StopAnimatingView(details
.child
);
780 void AppsGridView::Update() {
781 DCHECK(!selected_view_
&& !drag_view_
);
783 if (!item_list_
|| !item_list_
->item_count())
785 for (size_t i
= 0; i
< item_list_
->item_count(); ++i
) {
786 views::View
* view
= CreateViewForItemAtIndex(i
);
787 view_model_
.Add(view
, i
);
791 UpdatePulsingBlockViews();
796 void AppsGridView::UpdatePaging() {
797 int total_page
= start_page_view_
? 1 : 0;
798 if (view_model_
.view_size() && tiles_per_page())
799 total_page
+= (view_model_
.view_size() - 1) / tiles_per_page() + 1;
801 pagination_model_
->SetTotalPages(total_page
);
804 void AppsGridView::UpdatePulsingBlockViews() {
805 const int existing_items
= item_list_
? item_list_
->item_count() : 0;
806 const int available_slots
=
807 tiles_per_page() - existing_items
% tiles_per_page();
808 const int desired
= model_
->status() == AppListModel::STATUS_SYNCING
?
811 if (pulsing_blocks_model_
.view_size() == desired
)
814 while (pulsing_blocks_model_
.view_size() > desired
) {
815 views::View
* view
= pulsing_blocks_model_
.view_at(0);
816 pulsing_blocks_model_
.Remove(0);
820 while (pulsing_blocks_model_
.view_size() < desired
) {
821 views::View
* view
= new PulsingBlockView(
822 gfx::Size(kPreferredTileWidth
, kPreferredTileHeight
), true);
823 pulsing_blocks_model_
.Add(view
, 0);
828 views::View
* AppsGridView::CreateViewForItemAtIndex(size_t index
) {
829 // The drag_view_ might be pending for deletion, therefore view_model_
830 // may have one more item than item_list_.
831 DCHECK_LE(index
, item_list_
->item_count());
832 AppListItemView
* view
= new AppListItemView(this,
833 item_list_
->item_at(index
));
834 view
->SetIconSize(icon_size_
);
835 #if defined(USE_AURA)
836 view
->SetPaintToLayer(true);
837 view
->SetFillsBoundsOpaquely(false);
842 AppsGridView::Index
AppsGridView::GetIndexFromModelIndex(
843 int model_index
) const {
844 int page
= model_index
/ tiles_per_page();
845 if (start_page_view_
)
848 return Index(page
, model_index
% tiles_per_page());
851 int AppsGridView::GetModelIndexFromIndex(const Index
& index
) const {
852 int model_index
= index
.page
* tiles_per_page() + index
.slot
;
853 if (start_page_view_
)
854 model_index
-= tiles_per_page();
859 void AppsGridView::SetSelectedItemByIndex(const Index
& index
) {
860 if (GetIndexOfView(selected_view_
) == index
)
863 views::View
* new_selection
= GetViewAtIndex(index
);
865 return; // Keep current selection.
868 selected_view_
->SchedulePaint();
870 EnsureViewVisible(new_selection
);
871 selected_view_
= new_selection
;
872 selected_view_
->SchedulePaint();
873 selected_view_
->NotifyAccessibilityEvent(
874 ui::AccessibilityTypes::EVENT_FOCUS
, true);
877 bool AppsGridView::IsValidIndex(const Index
& index
) const {
878 const int item_page_start
= start_page_view_
? 1 : 0;
879 return index
.page
>= item_page_start
&&
880 index
.page
< pagination_model_
->total_pages() &&
882 index
.slot
< tiles_per_page() &&
883 GetModelIndexFromIndex(index
) < view_model_
.view_size();
886 AppsGridView::Index
AppsGridView::GetIndexOfView(
887 const views::View
* view
) const {
888 const int model_index
= view_model_
.GetIndexOfView(view
);
889 if (model_index
== -1)
892 return GetIndexFromModelIndex(model_index
);
895 views::View
* AppsGridView::GetViewAtIndex(const Index
& index
) const {
896 if (!IsValidIndex(index
))
899 const int model_index
= GetModelIndexFromIndex(index
);
900 return view_model_
.view_at(model_index
);
903 void AppsGridView::MoveSelected(int page_delta
,
907 return SetSelectedItemByIndex(Index(pagination_model_
->selected_page(), 0));
909 const Index
& selected
= GetIndexOfView(selected_view_
);
910 int target_slot
= selected
.slot
+ slot_x_delta
+ slot_y_delta
* cols_
;
912 if (selected
.slot
% cols_
== 0 && slot_x_delta
== -1) {
913 if (selected
.page
> 0) {
915 target_slot
= selected
.slot
+ cols_
- 1;
917 target_slot
= selected
.slot
;
921 if (selected
.slot
% cols_
== cols_
- 1 && slot_x_delta
== 1) {
922 if (selected
.page
< pagination_model_
->total_pages() - 1) {
924 target_slot
= selected
.slot
- cols_
+ 1;
926 target_slot
= selected
.slot
;
930 // Clamp the target slot to the last item if we are moving to the last page
931 // but our target slot is past the end of the item list.
933 selected
.page
+ page_delta
== pagination_model_
->total_pages() - 1) {
934 int last_item_slot
= (view_model_
.view_size() - 1) % tiles_per_page();
935 if (last_item_slot
< target_slot
) {
936 target_slot
= last_item_slot
;
940 int target_page
= std::min(pagination_model_
->total_pages() - 1,
941 std::max(selected
.page
+ page_delta
, 0));
942 SetSelectedItemByIndex(Index(target_page
, target_slot
));
945 void AppsGridView::CalculateIdealBounds() {
946 gfx::Rect
rect(GetContentsBounds());
950 gfx::Size
tile_size(kPreferredTileWidth
, kPreferredTileHeight
);
952 gfx::Rect
grid_rect(gfx::Size(tile_size
.width() * cols_
,
953 tile_size
.height() * rows_per_page_
));
954 grid_rect
.Intersect(rect
);
956 // Page width including padding pixels. A tile.x + page_width means the same
957 // tile slot in the next page.
958 const int page_width
= grid_rect
.width() + kPagePadding
;
960 // If there is a transition, calculates offset for current and target page.
961 const int current_page
= pagination_model_
->selected_page();
962 const PaginationModel::Transition
& transition
=
963 pagination_model_
->transition();
964 const bool is_valid
=
965 pagination_model_
->is_valid_page(transition
.target_page
);
967 // Transition to right means negative offset.
968 const int dir
= transition
.target_page
> current_page
? -1 : 1;
969 const int transition_offset
= is_valid
?
970 transition
.progress
* page_width
* dir
: 0;
972 const int total_views
=
973 view_model_
.view_size() + pulsing_blocks_model_
.view_size();
975 for (int i
= 0; i
< total_views
; ++i
) {
976 if (i
< view_model_
.view_size() && view_model_
.view_at(i
) == drag_view_
) {
977 if (EnableFolderDragDropUI() && drop_attempt_
== DROP_FOR_FOLDER
)
982 Index view_index
= GetIndexFromModelIndex(slot_index
);
984 if (drop_target_
== view_index
) {
985 if (EnableFolderDragDropUI() && drop_attempt_
== DROP_FOR_FOLDER
) {
986 view_index
= GetIndexFromModelIndex(slot_index
);
989 view_index
= GetIndexFromModelIndex(slot_index
);
993 // Decides an x_offset for current item.
995 if (view_index
.page
< current_page
)
996 x_offset
= -page_width
;
997 else if (view_index
.page
> current_page
)
998 x_offset
= page_width
;
1001 if (view_index
.page
== current_page
||
1002 view_index
.page
== transition
.target_page
) {
1003 x_offset
+= transition_offset
;
1007 const int row
= view_index
.slot
/ cols_
;
1008 const int col
= view_index
.slot
% cols_
;
1009 gfx::Rect
tile_slot(
1010 gfx::Point(grid_rect
.x() + col
* tile_size
.width() + x_offset
,
1011 grid_rect
.y() + row
* tile_size
.height()),
1013 if (i
< view_model_
.view_size()) {
1014 view_model_
.set_ideal_bounds(i
, tile_slot
);
1016 pulsing_blocks_model_
.set_ideal_bounds(i
- view_model_
.view_size(),
1024 void AppsGridView::AnimateToIdealBounds() {
1025 const gfx::Rect
visible_bounds(GetVisibleBounds());
1027 CalculateIdealBounds();
1028 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
1029 views::View
* view
= view_model_
.view_at(i
);
1030 if (view
== drag_view_
)
1033 const gfx::Rect
& target
= view_model_
.ideal_bounds(i
);
1034 if (bounds_animator_
.GetTargetBounds(view
) == target
)
1037 const gfx::Rect
& current
= view
->bounds();
1038 const bool current_visible
= visible_bounds
.Intersects(current
);
1039 const bool target_visible
= visible_bounds
.Intersects(target
);
1040 const bool visible
= current_visible
|| target_visible
;
1042 const int y_diff
= target
.y() - current
.y();
1043 if (visible
&& y_diff
&& y_diff
% kPreferredTileHeight
== 0) {
1044 AnimationBetweenRows(view
,
1050 bounds_animator_
.AnimateViewTo(view
, target
);
1055 void AppsGridView::AnimationBetweenRows(views::View
* view
,
1056 bool animate_current
,
1057 const gfx::Rect
& current
,
1058 bool animate_target
,
1059 const gfx::Rect
& target
) {
1060 // Determine page of |current| and |target|. -1 means in the left invisible
1061 // page, 0 is the center visible page and 1 means in the right invisible page.
1062 const int current_page
= current
.x() < 0 ? -1 :
1063 current
.x() >= width() ? 1 : 0;
1064 const int target_page
= target
.x() < 0 ? -1 :
1065 target
.x() >= width() ? 1 : 0;
1067 const int dir
= current_page
< target_page
||
1068 (current_page
== target_page
&& current
.y() < target
.y()) ? 1 : -1;
1070 #if defined(USE_AURA)
1071 scoped_ptr
<ui::Layer
> layer
;
1072 if (animate_current
) {
1073 layer
.reset(view
->RecreateLayer());
1074 layer
->SuppressPaint();
1076 view
->SetFillsBoundsOpaquely(false);
1077 view
->layer()->SetOpacity(0.f
);
1080 gfx::Rect
current_out(current
);
1081 current_out
.Offset(dir
* kPreferredTileWidth
, 0);
1084 gfx::Rect
target_in(target
);
1086 target_in
.Offset(-dir
* kPreferredTileWidth
, 0);
1087 view
->SetBoundsRect(target_in
);
1088 bounds_animator_
.AnimateViewTo(view
, target
);
1090 #if defined(USE_AURA)
1091 bounds_animator_
.SetAnimationDelegate(
1093 new RowMoveAnimationDelegate(view
, layer
.release(), current_out
),
1098 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent
& event
,
1099 gfx::Point
* drag_point
) {
1100 #if defined(USE_AURA) && !defined(OS_WIN)
1101 // Use root location of |event| instead of location in |drag_view_|'s
1102 // coordinates because |drag_view_| has a scale transform and location
1103 // could have integer round error and causes jitter.
1104 *drag_point
= event
.root_location();
1106 // GetWidget() could be NULL for tests.
1108 aura::Window::ConvertPointToTarget(
1109 GetWidget()->GetNativeWindow()->GetRootWindow(),
1110 GetWidget()->GetNativeWindow(),
1114 views::View::ConvertPointFromWidget(this, drag_point
);
1116 // For non-aura, root location is not clearly defined but |drag_view_| does
1117 // not have the scale transform. So no round error would be introduced and
1118 // it's okay to use View::ConvertPointToTarget.
1119 *drag_point
= event
.location();
1120 views::View::ConvertPointToTarget(drag_view_
, this, drag_point
);
1124 void AppsGridView::CalculateDropTarget(const gfx::Point
& drag_point
,
1125 bool use_page_button_hovering
) {
1126 if (EnableFolderDragDropUI()) {
1127 CalculateDropTargetWithFolderEnabled(drag_point
, use_page_button_hovering
);
1131 int current_page
= pagination_model_
->selected_page();
1132 gfx::Point
point(drag_point
);
1133 if (!IsPointWithinDragBuffer(drag_point
)) {
1134 point
= drag_start_grid_view_
;
1135 current_page
= drag_start_page_
;
1138 if (use_page_button_hovering
&&
1139 page_switcher_view_
->bounds().Contains(point
)) {
1140 gfx::Point
page_switcher_point(point
);
1141 views::View::ConvertPointToTarget(this, page_switcher_view_
,
1142 &page_switcher_point
);
1143 int page
= page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1144 if (pagination_model_
->is_valid_page(page
)) {
1145 drop_target_
.page
= page
;
1146 drop_target_
.slot
= tiles_per_page() - 1;
1149 gfx::Rect
bounds(GetContentsBounds());
1150 const int drop_row
= (point
.y() - bounds
.y()) / kPreferredTileHeight
;
1151 const int drop_col
= std::min(cols_
- 1,
1152 (point
.x() - bounds
.x()) / kPreferredTileWidth
);
1154 drop_target_
.page
= current_page
;
1155 drop_target_
.slot
= std::max(0, std::min(
1156 tiles_per_page() - 1,
1157 drop_row
* cols_
+ drop_col
));
1160 // Limits to the last possible slot on last page.
1161 if (drop_target_
.page
== pagination_model_
->total_pages() - 1) {
1162 drop_target_
.slot
= std::min(
1163 (view_model_
.view_size() - 1) % tiles_per_page(),
1169 void AppsGridView::CalculateDropTargetWithFolderEnabled(
1170 const gfx::Point
& drag_point
,
1171 bool use_page_button_hovering
) {
1172 gfx::Point
point(drag_point
);
1173 if (!IsPointWithinDragBuffer(drag_point
)) {
1174 point
= drag_start_grid_view_
;
1177 if (use_page_button_hovering
&&
1178 page_switcher_view_
->bounds().Contains(point
)) {
1179 gfx::Point
page_switcher_point(point
);
1180 views::View::ConvertPointToTarget(this, page_switcher_view_
,
1181 &page_switcher_point
);
1182 int page
= page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1183 if (pagination_model_
->is_valid_page(page
)) {
1184 drop_target_
.page
= page
;
1185 drop_target_
.slot
= tiles_per_page() - 1;
1187 if (drop_target_
.page
== pagination_model_
->total_pages() - 1) {
1188 drop_target_
.slot
= std::min(
1189 (view_model_
.view_size() - 1) % tiles_per_page(),
1192 drop_attempt_
= DROP_FOR_REORDER
;
1195 // Try to find the nearest target for folder dropping or re-ordering.
1196 drop_target_
= GetNearestTileForDragView();
1200 void AppsGridView::OnReorderTimer() {
1201 if (drop_attempt_
== DROP_FOR_REORDER
)
1202 AnimateToIdealBounds();
1205 void AppsGridView::OnFolderDroppingTimer() {
1206 if (drop_attempt_
== DROP_FOR_FOLDER
)
1207 SetAsFolderDroppingTarget(drop_target_
, true);
1210 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point
& grid_location
) {
1211 // When a drag and drop host is given, the item can be dragged out of the app
1212 // list window. In that case a proxy widget needs to be used.
1213 // Note: This code has very likely to be changed for Windows (non metro mode)
1214 // when a |drag_and_drop_host_| gets implemented.
1215 if (!drag_view_
|| !drag_and_drop_host_
)
1218 gfx::Point screen_location
= grid_location
;
1219 views::View::ConvertPointToScreen(this, &screen_location
);
1221 // Determine the mouse offset to the center of the icon so that the drag and
1222 // drop host follows this layer.
1223 gfx::Vector2d delta
= drag_view_offset_
-
1224 drag_view_
->GetLocalBounds().CenterPoint();
1225 delta
.set_y(delta
.y() + drag_view_
->title()->size().height() / 2);
1227 // We have to hide the original item since the drag and drop host will do
1228 // the OS dependent code to "lift off the dragged item".
1229 drag_and_drop_host_
->CreateDragIconProxy(screen_location
,
1230 drag_view_
->item()->icon(),
1233 kDragAndDropProxyScale
);
1234 SetViewHidden(drag_view_
,
1236 true /* no animation */);
1239 void AppsGridView::DispatchDragEventToDragAndDropHost(
1240 const gfx::Point
& location_in_screen_coordinates
) {
1241 if (!drag_view_
|| !drag_and_drop_host_
)
1243 if (bounds().Contains(last_drag_point_
)) {
1244 // The event was issued inside the app menu and we should get all events.
1245 if (forward_events_to_drag_and_drop_host_
) {
1246 // The DnD host was previously called and needs to be informed that the
1247 // session returns to the owner.
1248 forward_events_to_drag_and_drop_host_
= false;
1249 drag_and_drop_host_
->EndDrag(true);
1252 // The event happened outside our app menu and we might need to dispatch.
1253 if (forward_events_to_drag_and_drop_host_
) {
1254 // Dispatch since we have already started.
1255 if (!drag_and_drop_host_
->Drag(location_in_screen_coordinates
)) {
1256 // The host is not active any longer and we cancel the operation.
1257 forward_events_to_drag_and_drop_host_
= false;
1258 drag_and_drop_host_
->EndDrag(true);
1261 if (drag_and_drop_host_
->StartDrag(drag_view_
->item()->id(),
1262 location_in_screen_coordinates
)) {
1263 // From now on we forward the drag events.
1264 forward_events_to_drag_and_drop_host_
= true;
1265 // Any flip operations are stopped.
1266 StopPageFlipTimer();
1272 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point
& drag_point
) {
1273 if (!IsPointWithinDragBuffer(drag_point
))
1274 StopPageFlipTimer();
1275 int new_page_flip_target
= -1;
1277 if (page_switcher_view_
->bounds().Contains(drag_point
)) {
1278 gfx::Point
page_switcher_point(drag_point
);
1279 views::View::ConvertPointToTarget(this, page_switcher_view_
,
1280 &page_switcher_point
);
1281 new_page_flip_target
=
1282 page_switcher_view_
->GetPageForPoint(page_switcher_point
);
1285 // TODO(xiyuan): Fix this for RTL.
1286 if (new_page_flip_target
== -1 && drag_point
.x() < kPageFlipZoneSize
)
1287 new_page_flip_target
= pagination_model_
->selected_page() - 1;
1289 if (new_page_flip_target
== -1 &&
1290 drag_point
.x() > width() - kPageFlipZoneSize
) {
1291 new_page_flip_target
= pagination_model_
->selected_page() + 1;
1294 if (new_page_flip_target
== page_flip_target_
)
1297 StopPageFlipTimer();
1298 if (pagination_model_
->is_valid_page(new_page_flip_target
)) {
1299 page_flip_target_
= new_page_flip_target
;
1301 if (page_flip_target_
!= pagination_model_
->selected_page()) {
1302 page_flip_timer_
.Start(FROM_HERE
,
1303 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_
),
1304 this, &AppsGridView::OnPageFlipTimer
);
1309 void AppsGridView::OnPageFlipTimer() {
1310 DCHECK(pagination_model_
->is_valid_page(page_flip_target_
));
1311 pagination_model_
->SelectPage(page_flip_target_
, true);
1314 void AppsGridView::MoveItemInModel(views::View
* item_view
,
1315 const Index
& target
) {
1316 int current_model_index
= view_model_
.GetIndexOfView(item_view
);
1317 DCHECK_GE(current_model_index
, 0);
1319 int target_model_index
= GetModelIndexFromIndex(target
);
1320 if (target_model_index
== current_model_index
)
1323 item_list_
->RemoveObserver(this);
1324 item_list_
->MoveItem(current_model_index
, target_model_index
);
1325 view_model_
.Move(current_model_index
, target_model_index
);
1326 item_list_
->AddObserver(this);
1328 if (pagination_model_
->selected_page() != target
.page
)
1329 pagination_model_
->SelectPage(target
.page
, false);
1332 void AppsGridView::MoveItemToFolder(views::View
* item_view
,
1333 const Index
& target
) {
1334 const std::string
& source_id
=
1335 static_cast<AppListItemView
*>(item_view
)->item()->id();
1336 AppListItemView
* target_view
=
1337 static_cast<AppListItemView
*>(GetViewAtSlotOnCurrentPage(target
.slot
));
1338 const std::string
& target_id
= target_view
->item()->id();
1340 // Make change to data model.
1341 item_list_
->RemoveObserver(this);
1342 std::string folder_id
= model_
->MergeItems(target_id
, source_id
);
1343 item_list_
->AddObserver(this);
1345 if (folder_id
!= target_id
) {
1346 // New folder was created, change the view model to replace the old target
1347 // view with the new folder item view.
1348 size_t folder_index
;
1349 if (item_list_
->FindItemIndex(folder_id
, &folder_index
)) {
1350 int target_index
= view_model_
.GetIndexOfView(target_view
);
1351 view_model_
.Remove(target_index
);
1353 views::View
* target_folder_view
= CreateViewForItemAtIndex(folder_index
);
1354 view_model_
.Add(target_folder_view
, target_index
);
1355 AddChildView(target_folder_view
);
1357 LOG(ERROR
) << "Folder no longer in item_list: " << folder_id
;
1361 // Fade out the drag_view_ and delete it when animation ends.
1362 int drag_view_index
= view_model_
.GetIndexOfView(drag_view_
);
1363 view_model_
.Remove(drag_view_index
);
1364 bounds_animator_
.AnimateViewTo(drag_view_
, drag_view_
->bounds());
1365 bounds_animator_
.SetAnimationDelegate(
1366 drag_view_
, new ItemRemoveAnimationDelegate(drag_view_
), true);
1371 void AppsGridView::CancelContextMenusOnCurrentPage() {
1372 int start
= pagination_model_
->selected_page() * tiles_per_page();
1373 int end
= std::min(view_model_
.view_size(), start
+ tiles_per_page());
1374 for (int i
= start
; i
< end
; ++i
) {
1375 AppListItemView
* view
=
1376 static_cast<AppListItemView
*>(view_model_
.view_at(i
));
1377 view
->CancelContextMenu();
1381 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point
& point
) const {
1382 gfx::Rect
rect(GetLocalBounds());
1383 rect
.Inset(-kDragBufferPx
, -kDragBufferPx
, -kDragBufferPx
, -kDragBufferPx
);
1384 return rect
.Contains(point
);
1387 void AppsGridView::ButtonPressed(views::Button
* sender
,
1388 const ui::Event
& event
) {
1392 if (strcmp(sender
->GetClassName(), AppListItemView::kViewClassName
))
1396 activated_item_view_
= static_cast<AppListItemView
*>(sender
);
1397 delegate_
->ActivateApp(static_cast<AppListItemView
*>(sender
)->item(),
1402 void AppsGridView::LayoutStartPage() {
1403 if (!start_page_view_
)
1406 gfx::Rect
start_page_bounds(GetLocalBounds());
1407 start_page_bounds
.set_height(start_page_bounds
.height() -
1408 page_switcher_view_
->height());
1410 const int page_width
= width() + kPagePadding
;
1411 const int current_page
= pagination_model_
->selected_page();
1412 if (current_page
> 0)
1413 start_page_bounds
.Offset(-page_width
, 0);
1415 const PaginationModel::Transition
& transition
=
1416 pagination_model_
->transition();
1417 if (current_page
== 0 || transition
.target_page
== 0) {
1418 const int dir
= transition
.target_page
> current_page
? -1 : 1;
1419 start_page_bounds
.Offset(transition
.progress
* page_width
* dir
, 0);
1422 start_page_view_
->SetBoundsRect(start_page_bounds
);
1425 void AppsGridView::OnListItemAdded(size_t index
, AppListItem
* item
) {
1428 views::View
* view
= CreateViewForItemAtIndex(index
);
1429 view_model_
.Add(view
, index
);
1433 UpdatePulsingBlockViews();
1438 void AppsGridView::OnListItemRemoved(size_t index
, AppListItem
* item
) {
1441 views::View
* view
= view_model_
.view_at(index
);
1442 view_model_
.Remove(index
);
1446 UpdatePulsingBlockViews();
1451 void AppsGridView::OnListItemMoved(size_t from_index
,
1453 AppListItem
* item
) {
1455 view_model_
.Move(from_index
, to_index
);
1458 AnimateToIdealBounds();
1461 void AppsGridView::TotalPagesChanged() {
1464 void AppsGridView::SelectedPageChanged(int old_selected
, int new_selected
) {
1466 CalculateDropTarget(last_drag_point_
, true);
1468 MaybeStartPageFlipTimer(last_drag_point_
);
1470 ClearSelectedView(selected_view_
);
1475 void AppsGridView::TransitionStarted() {
1476 CancelContextMenusOnCurrentPage();
1479 void AppsGridView::TransitionChanged() {
1480 // Update layout for valid page transition only since over-scroll no longer
1481 // animates app icons.
1482 const PaginationModel::Transition
& transition
=
1483 pagination_model_
->transition();
1484 if (pagination_model_
->is_valid_page(transition
.target_page
))
1488 void AppsGridView::OnAppListModelStatusChanged() {
1489 UpdatePulsingBlockViews();
1494 void AppsGridView::SetViewHidden(views::View
* view
, bool hide
, bool immediate
) {
1495 #if defined(USE_AURA)
1496 ui::ScopedLayerAnimationSettings
animator(view
->layer()->GetAnimator());
1497 animator
.SetPreemptionStrategy(
1498 immediate
? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET
:
1499 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION
);
1500 view
->layer()->SetOpacity(hide
? 0 : 1);
1504 void AppsGridView::OnImplicitAnimationsCompleted() {
1505 if (layer()->opacity() == 0.0f
)
1509 bool AppsGridView::EnableFolderDragDropUI() {
1510 // Enable drag and drop folder UI only if it is at the app list root level
1511 // and the switch is on and the target folder can still accept new items.
1512 return switches::IsFolderUIEnabled() && is_root_level_
&&
1513 CanDropIntoTarget(drop_target_
);
1516 bool AppsGridView::CanDropIntoTarget(const Index
& drop_target
) {
1517 views::View
* target_view
= GetViewAtSlotOnCurrentPage(drop_target
.slot
);
1521 AppListItem
* target_item
=
1522 static_cast<AppListItemView
*>(target_view
)->item();
1523 // Items can be dropped into non-folders (which have no children) or folders
1524 // that have fewer than the max allowed items.
1525 return target_item
->ChildItemCount() < kMaxFolderItems
;
1528 // TODO(jennyz): Optimize the calculation for finding nearest tile.
1529 AppsGridView::Index
AppsGridView::GetNearestTileForDragView() {
1531 nearest_tile
.page
= -1;
1532 nearest_tile
.slot
= -1;
1535 // Calculate the top left tile |drag_view| intersects.
1536 gfx::Point pt
= drag_view_
->bounds().origin();
1537 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
1539 // Calculate the top right tile |drag_view| intersects.
1540 pt
= drag_view_
->bounds().top_right();
1541 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
1543 // Calculate the bottom left tile |drag_view| intersects.
1544 pt
= drag_view_
->bounds().bottom_left();
1545 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
1547 // Calculate the bottom right tile |drag_view| intersects.
1548 pt
= drag_view_
->bounds().bottom_right();
1549 CalculateNearestTileForVertex(pt
, &nearest_tile
, &d_min
);
1551 const int d_folder_dropping
=
1552 kFolderDroppingCircleRadius
+ kPreferredIconDimension
/ 2;
1553 const int d_reorder
=
1554 kReorderDroppingCircleRadius
+ kPreferredIconDimension
/ 2;
1556 if (IsValidIndex(nearest_tile
)) {
1557 if (d_min
< d_folder_dropping
) {
1558 views::View
* target_view
= GetViewAtSlotOnCurrentPage(nearest_tile
.slot
);
1560 !IsFolderItem(static_cast<AppListItemView
*>(drag_view_
)->item())) {
1561 // If a non-folder item is dragged to the target slot with an item
1562 // sitting on it, attempt to drop the dragged item into the folder
1563 // containing the item on nearest_tile.
1564 drop_attempt_
= DROP_FOR_FOLDER
;
1565 return nearest_tile
;
1567 // If the target slot is blank, or the dragged item is a folder, attempt
1569 drop_attempt_
= DROP_FOR_REORDER
;
1570 return nearest_tile
;
1572 } else if (d_min
< d_reorder
) {
1573 // Entering the re-order circle of the slot.
1574 drop_attempt_
= DROP_FOR_REORDER
;
1575 return nearest_tile
;
1579 // If |drag_view| is not entering the re-order or fold dropping region of
1580 // any items, cancel any previous re-order or folder dropping timer, and
1582 drop_attempt_
= DROP_FOR_NONE
;
1583 reorder_timer_
.Stop();
1584 folder_dropping_timer_
.Stop();
1585 return GetIndexOfView(drag_view_
);
1588 void AppsGridView::CalculateNearestTileForVertex(const gfx::Point
& vertex
,
1589 Index
* nearest_tile
,
1592 gfx::Rect target_bounds
= GetTileBoundsForPoint(vertex
, &target_index
);
1594 if (target_bounds
.IsEmpty() || target_index
== *nearest_tile
)
1597 int d_center
= GetDistanceBetweenRects(drag_view_
->bounds(), target_bounds
);
1598 if (*d_min
< 0 || d_center
< *d_min
) {
1600 *nearest_tile
= target_index
;
1604 gfx::Rect
AppsGridView::GetTileBoundsForPoint(const gfx::Point
& point
,
1605 Index
*tile_index
) {
1606 // Check if |point| is outside of contents bounds.
1607 gfx::Rect
bounds(GetContentsBounds());
1608 if (!bounds
.Contains(point
))
1611 // Calculate which tile |point| is enclosed in.
1614 int col
= (x
- bounds
.x()) / kPreferredTileWidth
;
1615 int row
= (y
- bounds
.y()) / kPreferredTileHeight
;
1616 gfx::Rect tile_rect
= GetTileBounds(row
, col
);
1618 // Check if |point| is outside a valid item's tile.
1619 Index
index(pagination_model_
->selected_page(), row
* cols_
+ col
);
1620 if (!IsValidIndex(index
))
1623 // |point| is inside of the valid item's tile.
1624 *tile_index
= index
;
1628 gfx::Rect
AppsGridView::GetTileBounds(int row
, int col
) const {
1629 gfx::Rect
bounds(GetContentsBounds());
1630 gfx::Size
tile_size(kPreferredTileWidth
, kPreferredTileHeight
);
1631 gfx::Rect
grid_rect(gfx::Size(tile_size
.width() * cols_
,
1632 tile_size
.height() * rows_per_page_
));
1633 grid_rect
.Intersect(bounds
);
1634 gfx::Rect
tile_rect(
1635 gfx::Point(grid_rect
.x() + col
* tile_size
.width(),
1636 grid_rect
.y() + row
* tile_size
.height()),
1641 views::View
* AppsGridView::GetViewAtSlotOnCurrentPage(int slot
) {
1645 // Calculate the original bound of the tile at |index|.
1646 int row
= slot
/ cols_
;
1647 int col
= slot
% cols_
;
1648 gfx::Rect tile_rect
= GetTileBounds(row
, col
);
1650 for (int i
= 0; i
< view_model_
.view_size(); ++i
) {
1651 views::View
* view
= view_model_
.view_at(i
);
1652 if (view
->bounds() == tile_rect
)
1658 void AppsGridView::SetAsFolderDroppingTarget(const Index
& target_index
,
1659 bool is_target_folder
) {
1660 AppListItemView
* target_view
=
1661 static_cast<AppListItemView
*>(
1662 GetViewAtSlotOnCurrentPage(target_index
.slot
));
1664 target_view
->SetAsAttemptedFolderTarget(is_target_folder
);
1667 } // namespace app_list