Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / app_list / views / apps_grid_view.cc
blobeab0e5231f8d05b0c7e8213b8253c096df998c3b
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/app_list/views/apps_grid_view.h"
7 #include <algorithm>
8 #include <set>
9 #include <string>
11 #include "base/guid.h"
12 #include "ui/app_list/app_list_constants.h"
13 #include "ui/app_list/app_list_folder_item.h"
14 #include "ui/app_list/app_list_item.h"
15 #include "ui/app_list/app_list_switches.h"
16 #include "ui/app_list/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"
33 #if defined(USE_AURA)
34 #include "ui/aura/window.h"
35 #include "ui/aura/window_event_dispatcher.h"
36 #if defined(OS_WIN)
37 #include "ui/views/win/hwnd_util.h"
38 #endif // defined(OS_WIN)
39 #endif // defined(USE_AURA)
41 #if defined(OS_WIN)
42 #include "base/command_line.h"
43 #include "base/files/file_path.h"
44 #include "base/win/shortcut.h"
45 #include "ui/base/dragdrop/drag_utils.h"
46 #include "ui/base/dragdrop/drop_target_win.h"
47 #include "ui/base/dragdrop/os_exchange_data.h"
48 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
49 #include "ui/gfx/win/dpi.h"
50 #endif
52 namespace app_list {
54 namespace {
56 // Distance a drag needs to be from the app grid to be considered 'outside', at
57 // which point we rearrange the apps to their pre-drag configuration, as a drop
58 // then would be canceled. We have a buffer to make it easier to drag apps to
59 // other pages.
60 const int kDragBufferPx = 20;
62 // Padding space in pixels for fixed layout.
63 const int kBottomPadding = 2;
64 const int kLeftRightPadding = 24;
66 // Padding space in pixels between pages.
67 const int kPagePadding = 40;
69 // Preferred tile size when showing in fixed layout.
70 const int kPreferredTileWidth = 88;
71 const int kPreferredTileHeight = 98;
73 const int kExperimentalPreferredTileWidth = 100;
74 const int kExperimentalPreferredTileHeight = 100;
76 // Padding on each side of a tile.
77 const int kExperimentalTileLeftRightPadding = 10;
78 const int kExperimentalTileBottomPadding = 6;
79 const int kExperimentalTileTopPadding = 6;
81 // Width in pixels of the area on the sides that triggers a page flip.
82 const int kPageFlipZoneSize = 40;
84 // Delay in milliseconds to do the page flip.
85 const int kPageFlipDelayInMs = 1000;
87 // How many pages on either side of the selected one we prerender. Currently 0
88 // to test impact of prerendering on UI jank for http://crbug.com/440224. Was 1.
89 const int kPrerenderPages = 0;
91 // The drag and drop proxy should get scaled by this factor.
92 const float kDragAndDropProxyScale = 1.5f;
94 // Delays in milliseconds to show folder dropping preview circle.
95 const int kFolderDroppingDelay = 150;
97 // Delays in milliseconds to show re-order preview.
98 const int kReorderDelay = 120;
100 // Delays in milliseconds to show folder item reparent UI.
101 const int kFolderItemReparentDelay = 50;
103 // Radius of the circle, in which if entered, show folder dropping preview
104 // UI.
105 const int kFolderDroppingCircleRadius = 39;
107 // Returns the size of a tile view excluding its padding.
108 gfx::Size GetTileViewSize() {
109 return switches::IsExperimentalAppListEnabled()
110 ? gfx::Size(kExperimentalPreferredTileWidth,
111 kExperimentalPreferredTileHeight)
112 : gfx::Size(kPreferredTileWidth, kPreferredTileHeight);
115 // Returns the padding around a tile view.
116 gfx::Insets GetTilePadding() {
117 if (!switches::IsExperimentalAppListEnabled())
118 return gfx::Insets();
120 return gfx::Insets(
121 -kExperimentalTileTopPadding, -kExperimentalTileLeftRightPadding,
122 -kExperimentalTileBottomPadding, -kExperimentalTileLeftRightPadding);
125 // RowMoveAnimationDelegate is used when moving an item into a different row.
126 // Before running the animation, the item's layer is re-created and kept in
127 // the original position, then the item is moved to just before its target
128 // position and opacity set to 0. When the animation runs, this delegate moves
129 // the layer and fades it out while fading in the item at the same time.
130 class RowMoveAnimationDelegate : public gfx::AnimationDelegate {
131 public:
132 RowMoveAnimationDelegate(views::View* view,
133 ui::Layer* layer,
134 const gfx::Rect& layer_target)
135 : view_(view),
136 layer_(layer),
137 layer_start_(layer ? layer->bounds() : gfx::Rect()),
138 layer_target_(layer_target) {
140 ~RowMoveAnimationDelegate() override {}
142 // gfx::AnimationDelegate overrides:
143 void AnimationProgressed(const gfx::Animation* animation) override {
144 view_->layer()->SetOpacity(animation->GetCurrentValue());
145 view_->layer()->ScheduleDraw();
147 if (layer_) {
148 layer_->SetOpacity(1 - animation->GetCurrentValue());
149 layer_->SetBounds(animation->CurrentValueBetween(layer_start_,
150 layer_target_));
151 layer_->ScheduleDraw();
154 void AnimationEnded(const gfx::Animation* animation) override {
155 view_->layer()->SetOpacity(1.0f);
156 view_->SchedulePaint();
158 void AnimationCanceled(const gfx::Animation* animation) override {
159 view_->layer()->SetOpacity(1.0f);
160 view_->SchedulePaint();
163 private:
164 // The view that needs to be wrapped. Owned by views hierarchy.
165 views::View* view_;
167 scoped_ptr<ui::Layer> layer_;
168 const gfx::Rect layer_start_;
169 const gfx::Rect layer_target_;
171 DISALLOW_COPY_AND_ASSIGN(RowMoveAnimationDelegate);
174 // ItemRemoveAnimationDelegate is used to show animation for removing an item.
175 // This happens when user drags an item into a folder. The dragged item will
176 // be removed from the original list after it is dropped into the folder.
177 class ItemRemoveAnimationDelegate : public gfx::AnimationDelegate {
178 public:
179 explicit ItemRemoveAnimationDelegate(views::View* view)
180 : view_(view) {
183 ~ItemRemoveAnimationDelegate() override {}
185 // gfx::AnimationDelegate overrides:
186 void AnimationProgressed(const gfx::Animation* animation) override {
187 view_->layer()->SetOpacity(1 - animation->GetCurrentValue());
188 view_->layer()->ScheduleDraw();
191 private:
192 scoped_ptr<views::View> view_;
194 DISALLOW_COPY_AND_ASSIGN(ItemRemoveAnimationDelegate);
197 // ItemMoveAnimationDelegate observes when an item finishes animating when it is
198 // not moving between rows. This is to ensure an item is repainted for the
199 // "zoom out" case when releasing an item being dragged.
200 class ItemMoveAnimationDelegate : public gfx::AnimationDelegate {
201 public:
202 explicit ItemMoveAnimationDelegate(views::View* view) : view_(view) {}
204 void AnimationEnded(const gfx::Animation* animation) override {
205 view_->SchedulePaint();
207 void AnimationCanceled(const gfx::Animation* animation) override {
208 view_->SchedulePaint();
211 private:
212 views::View* view_;
214 DISALLOW_COPY_AND_ASSIGN(ItemMoveAnimationDelegate);
217 // Returns true if the |item| is a folder item.
218 bool IsFolderItem(AppListItem* item) {
219 return (item->GetItemType() == AppListFolderItem::kItemType);
222 bool IsOEMFolderItem(AppListItem* item) {
223 return IsFolderItem(item) &&
224 (static_cast<AppListFolderItem*>(item))->folder_type() ==
225 AppListFolderItem::FOLDER_TYPE_OEM;
228 int ClampToRange(int value, int min, int max) {
229 return std::min(std::max(value, min), max);
232 } // namespace
234 #if defined(OS_WIN)
235 // Interprets drag events sent from Windows via the drag/drop API and forwards
236 // them to AppsGridView.
237 // On Windows, in order to have the OS perform the drag properly we need to
238 // provide it with a shortcut file which may or may not exist at the time the
239 // drag is started. Therefore while waiting for that shortcut to be located we
240 // just do a regular "internal" drag and transition into the synchronous drag
241 // when the shortcut is found/created. Hence a synchronous drag is an optional
242 // phase of a regular drag and non-Windows platforms drags are equivalent to a
243 // Windows drag that never enters the synchronous drag phase.
244 class SynchronousDrag : public ui::DragSourceWin {
245 public:
246 SynchronousDrag(AppsGridView* grid_view,
247 AppListItemView* drag_view,
248 const gfx::Point& drag_view_offset)
249 : grid_view_(grid_view),
250 drag_view_(drag_view),
251 drag_view_offset_(drag_view_offset),
252 has_shortcut_path_(false),
253 running_(false),
254 canceled_(false) {}
256 void set_shortcut_path(const base::FilePath& shortcut_path) {
257 has_shortcut_path_ = true;
258 shortcut_path_ = shortcut_path;
261 bool running() { return running_; }
263 bool CanRun() {
264 return has_shortcut_path_ && !running_;
267 void Run() {
268 DCHECK(CanRun());
270 // Prevent the synchronous dragger being destroyed while the drag is
271 // running.
272 Microsoft::WRL::ComPtr<SynchronousDrag> this_ref = this;
273 running_ = true;
275 ui::OSExchangeData data;
276 SetupExchangeData(&data);
278 // Hide the dragged view because the OS is going to create its own.
279 drag_view_->SetVisible(false);
281 // Blocks until the drag is finished. Calls into the ui::DragSourceWin
282 // methods.
283 DWORD effects;
284 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
285 this, DROPEFFECT_MOVE | DROPEFFECT_LINK, &effects);
287 // If |drag_view_| is NULL the drag was ended by some reentrant code.
288 if (drag_view_) {
289 // Make the drag view visible again.
290 drag_view_->SetVisible(true);
291 drag_view_->OnSyncDragEnd();
293 grid_view_->EndDrag(canceled_ || !IsCursorWithinGridView());
297 void EndDragExternally() {
298 CancelDrag();
299 DCHECK(drag_view_);
300 drag_view_->SetVisible(true);
301 drag_view_ = NULL;
304 private:
305 // Overridden from ui::DragSourceWin.
306 void OnDragSourceCancel() override { canceled_ = true; }
308 void OnDragSourceDrop() override {}
310 void OnDragSourceMove() override {
311 grid_view_->UpdateDrag(AppsGridView::MOUSE, GetCursorInGridViewCoords());
314 void SetupExchangeData(ui::OSExchangeData* data) {
315 data->SetFilename(shortcut_path_);
316 gfx::ImageSkia image(drag_view_->GetDragImage());
317 gfx::Size image_size(image.size());
318 drag_utils::SetDragImageOnDataObject(
319 image,
320 drag_view_offset_ - drag_view_->GetDragImageOffset(),
321 data);
324 HWND GetGridViewHWND() {
325 return views::HWNDForView(grid_view_);
328 bool IsCursorWithinGridView() {
329 POINT p;
330 GetCursorPos(&p);
331 return GetGridViewHWND() == WindowFromPoint(p);
334 gfx::Point GetCursorInGridViewCoords() {
335 POINT p;
336 GetCursorPos(&p);
337 ScreenToClient(GetGridViewHWND(), &p);
338 gfx::Point grid_view_pt(p.x, p.y);
339 grid_view_pt = gfx::win::ScreenToDIPPoint(grid_view_pt);
340 views::View::ConvertPointFromWidget(grid_view_, &grid_view_pt);
341 return grid_view_pt;
344 AppsGridView* grid_view_;
345 AppListItemView* drag_view_;
346 gfx::Point drag_view_offset_;
347 bool has_shortcut_path_;
348 base::FilePath shortcut_path_;
349 bool running_;
350 bool canceled_;
352 DISALLOW_COPY_AND_ASSIGN(SynchronousDrag);
354 #endif // defined(OS_WIN)
356 AppsGridView::AppsGridView(AppsGridViewDelegate* delegate)
357 : model_(NULL),
358 item_list_(NULL),
359 delegate_(delegate),
360 folder_delegate_(NULL),
361 page_switcher_view_(NULL),
362 cols_(0),
363 rows_per_page_(0),
364 selected_view_(NULL),
365 drag_view_(NULL),
366 drag_start_page_(-1),
367 #if defined(OS_WIN)
368 use_synchronous_drag_(true),
369 #endif
370 drag_pointer_(NONE),
371 drop_attempt_(DROP_FOR_NONE),
372 drag_and_drop_host_(NULL),
373 forward_events_to_drag_and_drop_host_(false),
374 page_flip_target_(-1),
375 page_flip_delay_in_ms_(kPageFlipDelayInMs),
376 bounds_animator_(this),
377 activated_folder_item_view_(NULL),
378 dragging_for_reparent_item_(false) {
379 SetPaintToLayer(true);
380 // Clip any icons that are outside the grid view's bounds. These icons would
381 // otherwise be visible to the user when the grid view is off screen.
382 layer()->SetMasksToBounds(true);
383 SetFillsBoundsOpaquely(false);
385 pagination_model_.SetTransitionDurations(kPageTransitionDurationInMs,
386 kOverscrollPageTransitionDurationMs);
388 pagination_model_.AddObserver(this);
389 pagination_controller_.reset(new PaginationController(
390 &pagination_model_, PaginationController::SCROLL_AXIS_HORIZONTAL));
391 page_switcher_view_ = new PageSwitcher(&pagination_model_);
392 AddChildView(page_switcher_view_);
395 AppsGridView::~AppsGridView() {
396 // Coming here |drag_view_| should already be canceled since otherwise the
397 // drag would disappear after the app list got animated away and closed,
398 // which would look odd.
399 DCHECK(!drag_view_);
400 if (drag_view_)
401 EndDrag(true);
403 if (model_)
404 model_->RemoveObserver(this);
405 pagination_model_.RemoveObserver(this);
407 if (item_list_)
408 item_list_->RemoveObserver(this);
410 // Make sure |page_switcher_view_| is deleted before |pagination_model_|.
411 view_model_.Clear();
412 RemoveAllChildViews(true);
415 void AppsGridView::SetLayout(int cols, int rows_per_page) {
416 cols_ = cols;
417 rows_per_page_ = rows_per_page;
419 if (switches::IsExperimentalAppListEnabled()) {
420 SetBorder(views::Border::CreateEmptyBorder(
421 0, kExperimentalAppsGridPadding, 0, kExperimentalAppsGridPadding));
422 } else {
423 SetBorder(views::Border::CreateEmptyBorder(
424 0, kLeftRightPadding, kBottomPadding, kLeftRightPadding));
428 // static
429 gfx::Size AppsGridView::GetTotalTileSize() {
430 gfx::Rect rect(GetTileViewSize());
431 rect.Inset(GetTilePadding());
432 return rect.size();
435 void AppsGridView::ResetForShowApps() {
436 activated_folder_item_view_ = NULL;
437 ClearDragState();
438 layer()->SetOpacity(1.0f);
439 SetVisible(true);
440 // Set all views to visible in case they weren't made visible again by an
441 // incomplete animation.
442 for (int i = 0; i < view_model_.view_size(); ++i) {
443 view_model_.view_at(i)->SetVisible(true);
445 CHECK_EQ(item_list_->item_count(),
446 static_cast<size_t>(view_model_.view_size()));
449 void AppsGridView::SetModel(AppListModel* model) {
450 if (model_)
451 model_->RemoveObserver(this);
453 model_ = model;
454 if (model_)
455 model_->AddObserver(this);
457 Update();
460 void AppsGridView::SetItemList(AppListItemList* item_list) {
461 if (item_list_)
462 item_list_->RemoveObserver(this);
463 item_list_ = item_list;
464 if (item_list_)
465 item_list_->AddObserver(this);
466 Update();
469 void AppsGridView::SetSelectedView(AppListItemView* view) {
470 if (IsSelectedView(view) || IsDraggedView(view))
471 return;
473 Index index = GetIndexOfView(view);
474 if (IsValidIndex(index))
475 SetSelectedItemByIndex(index);
478 void AppsGridView::ClearSelectedView(AppListItemView* view) {
479 if (view && IsSelectedView(view)) {
480 selected_view_->SchedulePaint();
481 selected_view_ = NULL;
485 void AppsGridView::ClearAnySelectedView() {
486 if (selected_view_) {
487 selected_view_->SchedulePaint();
488 selected_view_ = NULL;
492 bool AppsGridView::IsSelectedView(const AppListItemView* view) const {
493 return selected_view_ == view;
496 void AppsGridView::InitiateDrag(AppListItemView* view,
497 Pointer pointer,
498 const ui::LocatedEvent& event) {
499 DCHECK(view);
500 if (drag_view_ || pulsing_blocks_model_.view_size())
501 return;
503 drag_view_ = view;
504 drag_view_init_index_ = GetIndexOfView(drag_view_);
505 drag_view_offset_ = event.location();
506 drag_start_page_ = pagination_model_.selected_page();
507 reorder_placeholder_ = drag_view_init_index_;
508 ExtractDragLocation(event, &drag_start_grid_view_);
509 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
512 void AppsGridView::StartSettingUpSynchronousDrag() {
513 #if defined(OS_WIN)
514 if (!delegate_ || !use_synchronous_drag_)
515 return;
517 // Folders and downloading items can't be integrated with the OS.
518 if (IsFolderItem(drag_view_->item()) || drag_view_->item()->is_installing())
519 return;
521 // Favor the drag and drop host over native win32 drag. For the Win8/ash
522 // launcher we want to have ashes drag and drop over win32's.
523 if (drag_and_drop_host_)
524 return;
526 // Never create a second synchronous drag if the drag started in a folder.
527 if (IsDraggingForReparentInRootLevelGridView())
528 return;
530 synchronous_drag_ = Microsoft::WRL::Make<SynchronousDrag>(this, drag_view_,
531 drag_view_offset_);
532 delegate_->GetShortcutPathForApp(drag_view_->item()->id(),
533 base::Bind(&AppsGridView::OnGotShortcutPath,
534 base::Unretained(this),
535 synchronous_drag_));
536 #endif
539 bool AppsGridView::RunSynchronousDrag() {
540 #if defined(OS_WIN)
541 if (!synchronous_drag_.Get())
542 return false;
544 if (synchronous_drag_->CanRun()) {
545 if (IsDraggingForReparentInHiddenGridView())
546 folder_delegate_->SetRootLevelDragViewVisible(false);
547 synchronous_drag_->Run();
548 synchronous_drag_ = nullptr;
549 return true;
550 } else if (!synchronous_drag_->running()) {
551 // The OS drag is not ready yet. If the root grid has a drag view because
552 // a reparent has started, ensure it is visible.
553 if (IsDraggingForReparentInHiddenGridView())
554 folder_delegate_->SetRootLevelDragViewVisible(true);
556 #endif
557 return false;
560 void AppsGridView::CleanUpSynchronousDrag() {
561 #if defined(OS_WIN)
562 if (synchronous_drag_.Get())
563 synchronous_drag_->EndDragExternally();
565 synchronous_drag_ = nullptr;
566 #endif
569 #if defined(OS_WIN)
570 void AppsGridView::OnGotShortcutPath(
571 Microsoft::WRL::ComPtr<SynchronousDrag> synchronous_drag,
572 const base::FilePath& path) {
573 // Drag may have ended before we get the shortcut path or a new drag may have
574 // begun.
575 if (synchronous_drag_ != synchronous_drag)
576 return;
577 // Setting the shortcut path here means the next time we hit UpdateDrag()
578 // we'll enter the synchronous drag.
579 // NOTE we don't Run() the drag here because that causes animations not to
580 // update for some reason.
581 synchronous_drag_->set_shortcut_path(path);
582 DCHECK(synchronous_drag_->CanRun());
584 #endif
586 bool AppsGridView::UpdateDragFromItem(Pointer pointer,
587 const ui::LocatedEvent& event) {
588 if (!drag_view_)
589 return false; // Drag canceled.
591 gfx::Point drag_point_in_grid_view;
592 ExtractDragLocation(event, &drag_point_in_grid_view);
593 UpdateDrag(pointer, drag_point_in_grid_view);
594 if (!dragging())
595 return false;
597 // If a drag and drop host is provided, see if the drag operation needs to be
598 // forwarded.
599 gfx::Point location_in_screen = drag_point_in_grid_view;
600 views::View::ConvertPointToScreen(this, &location_in_screen);
601 DispatchDragEventToDragAndDropHost(location_in_screen);
602 if (drag_and_drop_host_)
603 drag_and_drop_host_->UpdateDragIconProxy(location_in_screen);
604 return true;
607 void AppsGridView::UpdateDrag(Pointer pointer, const gfx::Point& point) {
608 if (folder_delegate_)
609 UpdateDragStateInsideFolder(pointer, point);
611 if (!drag_view_)
612 return; // Drag canceled.
614 if (RunSynchronousDrag())
615 return;
617 gfx::Vector2d drag_vector(point - drag_start_grid_view_);
618 if (!dragging() && ExceededDragThreshold(drag_vector)) {
619 drag_pointer_ = pointer;
620 // Move the view to the front so that it appears on top of other views.
621 ReorderChildView(drag_view_, -1);
622 bounds_animator_.StopAnimatingView(drag_view_);
623 // Stopping the animation may have invalidated our drag view due to the
624 // view hierarchy changing.
625 if (!drag_view_)
626 return;
628 StartSettingUpSynchronousDrag();
629 if (!dragging_for_reparent_item_)
630 StartDragAndDropHostDrag(point);
633 if (drag_pointer_ != pointer)
634 return;
636 drag_view_->SetPosition(drag_view_start_ + drag_vector);
638 last_drag_point_ = point;
639 const Index last_reorder_drop_target = reorder_drop_target_;
640 const Index last_folder_drop_target = folder_drop_target_;
641 DropAttempt last_drop_attempt = drop_attempt_;
642 CalculateDropTarget();
644 MaybeStartPageFlipTimer(last_drag_point_);
646 gfx::Point page_switcher_point(last_drag_point_);
647 views::View::ConvertPointToTarget(this, page_switcher_view_,
648 &page_switcher_point);
649 page_switcher_view_->UpdateUIForDragPoint(page_switcher_point);
651 if (last_folder_drop_target != folder_drop_target_ ||
652 last_reorder_drop_target != reorder_drop_target_ ||
653 last_drop_attempt != drop_attempt_) {
654 if (drop_attempt_ == DROP_FOR_REORDER) {
655 folder_dropping_timer_.Stop();
656 reorder_timer_.Start(FROM_HERE,
657 base::TimeDelta::FromMilliseconds(kReorderDelay),
658 this, &AppsGridView::OnReorderTimer);
659 } else if (drop_attempt_ == DROP_FOR_FOLDER) {
660 reorder_timer_.Stop();
661 folder_dropping_timer_.Start(FROM_HERE,
662 base::TimeDelta::FromMilliseconds(kFolderDroppingDelay),
663 this, &AppsGridView::OnFolderDroppingTimer);
666 // Reset the previous drop target.
667 SetAsFolderDroppingTarget(last_folder_drop_target, false);
671 void AppsGridView::EndDrag(bool cancel) {
672 // EndDrag was called before if |drag_view_| is NULL.
673 if (!drag_view_)
674 return;
676 // Coming here a drag and drop was in progress.
677 bool landed_in_drag_and_drop_host = forward_events_to_drag_and_drop_host_;
678 if (forward_events_to_drag_and_drop_host_) {
679 DCHECK(!IsDraggingForReparentInRootLevelGridView());
680 forward_events_to_drag_and_drop_host_ = false;
681 drag_and_drop_host_->EndDrag(cancel);
682 if (IsDraggingForReparentInHiddenGridView()) {
683 folder_delegate_->DispatchEndDragEventForReparent(
684 true /* events_forwarded_to_drag_drop_host */,
685 cancel /* cancel_drag */);
687 } else {
688 if (IsDraggingForReparentInHiddenGridView()) {
689 // Forward the EndDrag event to the root level grid view.
690 folder_delegate_->DispatchEndDragEventForReparent(
691 false /* events_forwarded_to_drag_drop_host */,
692 cancel /* cancel_drag */);
693 EndDragForReparentInHiddenFolderGridView();
694 return;
697 if (IsDraggingForReparentInRootLevelGridView()) {
698 // An EndDrag can be received during a reparent via a model change. This
699 // is always a cancel and needs to be forwarded to the folder.
700 DCHECK(cancel);
701 delegate_->CancelDragInActiveFolder();
702 return;
705 if (!cancel && dragging()) {
706 // Regular drag ending path, ie, not for reparenting.
707 CalculateDropTarget();
708 if (EnableFolderDragDropUI() && drop_attempt_ == DROP_FOR_FOLDER &&
709 IsValidIndex(folder_drop_target_)) {
710 MoveItemToFolder(drag_view_, folder_drop_target_);
711 } else if (IsValidIndex(reorder_drop_target_)) {
712 MoveItemInModel(drag_view_, reorder_drop_target_);
717 if (drag_and_drop_host_) {
718 // If we had a drag and drop proxy icon, we delete it and make the real
719 // item visible again.
720 drag_and_drop_host_->DestroyDragIconProxy();
721 // Issue 439055: MoveItemToFolder() can sometimes delete |drag_view_|
722 if (drag_view_) {
723 if (landed_in_drag_and_drop_host) {
724 // Move the item directly to the target location, avoiding the
725 // "zip back" animation if the user was pinning it to the shelf.
726 int i = reorder_drop_target_.slot;
727 gfx::Rect bounds = view_model_.ideal_bounds(i);
728 drag_view_->SetBoundsRect(bounds);
730 // Fade in slowly if it landed in the shelf.
731 SetViewHidden(drag_view_, false /* show */,
732 !landed_in_drag_and_drop_host /* animate */);
736 // The drag can be ended after the synchronous drag is created but before it
737 // is Run().
738 CleanUpSynchronousDrag();
740 SetAsFolderDroppingTarget(folder_drop_target_, false);
741 ClearDragState();
742 AnimateToIdealBounds();
744 StopPageFlipTimer();
746 // If user releases mouse inside a folder's grid view, burst the folder
747 // container ink bubble.
748 if (folder_delegate_ && !IsDraggingForReparentInHiddenGridView())
749 folder_delegate_->UpdateFolderViewBackground(false);
752 void AppsGridView::StopPageFlipTimer() {
753 page_flip_timer_.Stop();
754 page_flip_target_ = -1;
757 AppListItemView* AppsGridView::GetItemViewAt(int index) const {
758 return view_model_.view_at(index);
761 void AppsGridView::SetTopItemViewsVisible(bool visible) {
762 int top_item_count = std::min(static_cast<int>(kNumFolderTopItems),
763 view_model_.view_size());
764 for (int i = 0; i < top_item_count; ++i)
765 GetItemViewAt(i)->icon()->SetVisible(visible);
768 void AppsGridView::ScheduleShowHideAnimation(bool show) {
769 // Stop any previous animation.
770 layer()->GetAnimator()->StopAnimating();
772 // Set initial state.
773 SetVisible(true);
774 layer()->SetOpacity(show ? 0.0f : 1.0f);
776 ui::ScopedLayerAnimationSettings animation(layer()->GetAnimator());
777 animation.AddObserver(this);
778 animation.SetTweenType(
779 show ? kFolderFadeInTweenType : kFolderFadeOutTweenType);
780 animation.SetTransitionDuration(base::TimeDelta::FromMilliseconds(
781 show ? kFolderTransitionInDurationMs : kFolderTransitionOutDurationMs));
783 layer()->SetOpacity(show ? 1.0f : 0.0f);
786 void AppsGridView::InitiateDragFromReparentItemInRootLevelGridView(
787 AppListItemView* original_drag_view,
788 const gfx::Rect& drag_view_rect,
789 const gfx::Point& drag_point,
790 bool has_native_drag) {
791 DCHECK(original_drag_view && !drag_view_);
792 DCHECK(!dragging_for_reparent_item_);
794 // Since the item is new, its placeholder is conceptually at the back of the
795 // entire apps grid.
796 reorder_placeholder_ = GetLastViewIndex();
798 // Create a new AppListItemView to duplicate the original_drag_view in the
799 // folder's grid view.
800 AppListItemView* view = new AppListItemView(this, original_drag_view->item());
801 AddChildView(view);
802 drag_view_ = view;
803 drag_view_->SetPaintToLayer(true);
804 // Note: For testing purpose, SetFillsBoundsOpaquely can be set to true to
805 // show the gray background.
806 drag_view_->SetFillsBoundsOpaquely(false);
807 drag_view_->SetBoundsRect(drag_view_rect);
808 drag_view_->SetDragUIState(); // Hide the title of the drag_view_.
810 // Hide the drag_view_ for drag icon proxy when a native drag is responsible
811 // for showing the icon.
812 if (has_native_drag)
813 SetViewHidden(drag_view_, true /* hide */, true /* no animate */);
815 // Add drag_view_ to the end of the view_model_.
816 view_model_.Add(drag_view_, view_model_.view_size());
818 drag_start_page_ = pagination_model_.selected_page();
819 drag_start_grid_view_ = drag_point;
821 drag_view_start_ = gfx::Point(drag_view_->x(), drag_view_->y());
823 // Set the flag in root level grid view.
824 dragging_for_reparent_item_ = true;
827 void AppsGridView::UpdateDragFromReparentItem(Pointer pointer,
828 const gfx::Point& drag_point) {
829 // Note that if a cancel ocurrs while reparenting, the |drag_view_| in both
830 // root and folder grid views is cleared, so the check in UpdateDragFromItem()
831 // for |drag_view_| being NULL (in the folder grid) is sufficient.
832 DCHECK(drag_view_);
833 DCHECK(IsDraggingForReparentInRootLevelGridView());
835 UpdateDrag(pointer, drag_point);
838 bool AppsGridView::IsDraggedView(const AppListItemView* view) const {
839 return drag_view_ == view;
842 void AppsGridView::ClearDragState() {
843 drop_attempt_ = DROP_FOR_NONE;
844 drag_pointer_ = NONE;
845 reorder_drop_target_ = Index();
846 folder_drop_target_ = Index();
847 reorder_placeholder_ = Index();
848 drag_start_grid_view_ = gfx::Point();
849 drag_start_page_ = -1;
850 drag_view_offset_ = gfx::Point();
852 if (drag_view_) {
853 drag_view_->OnDragEnded();
854 if (IsDraggingForReparentInRootLevelGridView()) {
855 const int drag_view_index = view_model_.GetIndexOfView(drag_view_);
856 CHECK_EQ(view_model_.view_size() - 1, drag_view_index);
857 DeleteItemViewAtIndex(drag_view_index);
860 drag_view_ = NULL;
861 dragging_for_reparent_item_ = false;
864 void AppsGridView::SetDragViewVisible(bool visible) {
865 DCHECK(drag_view_);
866 SetViewHidden(drag_view_, !visible, true);
869 void AppsGridView::SetDragAndDropHostOfCurrentAppList(
870 ApplicationDragAndDropHost* drag_and_drop_host) {
871 drag_and_drop_host_ = drag_and_drop_host;
874 void AppsGridView::Prerender() {
875 Layout();
876 int selected_page = std::max(0, pagination_model_.selected_page());
877 int start = std::max(0, (selected_page - kPrerenderPages) * tiles_per_page());
878 int end = std::min(view_model_.view_size(),
879 (selected_page + 1 + kPrerenderPages) * tiles_per_page());
880 for (int i = start; i < end; i++)
881 GetItemViewAt(i)->Prerender();
884 bool AppsGridView::IsAnimatingView(AppListItemView* view) {
885 return bounds_animator_.IsAnimating(view);
888 gfx::Size AppsGridView::GetPreferredSize() const {
889 const gfx::Insets insets(GetInsets());
890 int page_switcher_height = page_switcher_view_->GetPreferredSize().height();
891 gfx::Size size = GetTileGridSize();
892 size.Enlarge(insets.width(), insets.height() + page_switcher_height);
893 return size;
896 bool AppsGridView::GetDropFormats(
897 int* formats,
898 std::set<OSExchangeData::CustomFormat>* custom_formats) {
899 // TODO(koz): Only accept a specific drag type for app shortcuts.
900 *formats = OSExchangeData::FILE_NAME;
901 return true;
904 bool AppsGridView::CanDrop(const OSExchangeData& data) {
905 return true;
908 int AppsGridView::OnDragUpdated(const ui::DropTargetEvent& event) {
909 return ui::DragDropTypes::DRAG_MOVE;
912 void AppsGridView::Layout() {
913 if (bounds_animator_.IsAnimating())
914 bounds_animator_.Cancel();
916 CalculateIdealBounds();
917 for (int i = 0; i < view_model_.view_size(); ++i) {
918 AppListItemView* view = GetItemViewAt(i);
919 if (view != drag_view_)
920 view->SetBoundsRect(view_model_.ideal_bounds(i));
922 views::ViewModelUtils::SetViewBoundsToIdealBounds(pulsing_blocks_model_);
924 const int page_switcher_height =
925 page_switcher_view_->GetPreferredSize().height();
926 gfx::Rect rect(GetContentsBounds());
927 rect.set_y(rect.bottom() - page_switcher_height);
928 rect.set_height(page_switcher_height);
929 page_switcher_view_->SetBoundsRect(rect);
932 bool AppsGridView::OnKeyPressed(const ui::KeyEvent& event) {
933 bool handled = false;
934 if (selected_view_)
935 handled = static_cast<views::View*>(selected_view_)->OnKeyPressed(event);
937 if (!handled) {
938 const int forward_dir = base::i18n::IsRTL() ? -1 : 1;
939 switch (event.key_code()) {
940 case ui::VKEY_LEFT:
941 MoveSelected(0, -forward_dir, 0);
942 return true;
943 case ui::VKEY_RIGHT:
944 MoveSelected(0, forward_dir, 0);
945 return true;
946 case ui::VKEY_UP:
947 if (selected_view_) // Don't initiate selection with UP
948 MoveSelected(0, 0, -1);
949 return true;
950 case ui::VKEY_DOWN:
951 MoveSelected(0, 0, 1);
952 return true;
953 case ui::VKEY_PRIOR: {
954 MoveSelected(-1, 0, 0);
955 return true;
957 case ui::VKEY_NEXT: {
958 MoveSelected(1, 0, 0);
959 return true;
961 case ui::VKEY_TAB: {
962 if (event.IsShiftDown()) {
963 ClearAnySelectedView(); // ContentsView will move focus back.
964 } else {
965 MoveSelected(0, 0, 0); // Ensure but don't change selection.
966 handled = true; // TABing internally doesn't move focus.
968 break;
970 default:
971 break;
975 return handled;
978 bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) {
979 bool handled = false;
980 if (selected_view_)
981 handled = selected_view_->OnKeyReleased(event);
983 return handled;
986 bool AppsGridView::OnMouseWheel(const ui::MouseWheelEvent& event) {
987 return pagination_controller_->OnScroll(
988 gfx::Vector2d(event.x_offset(), event.y_offset()));
991 void AppsGridView::ViewHierarchyChanged(
992 const ViewHierarchyChangedDetails& details) {
993 if (!details.is_add && details.parent == this) {
994 // The view being delete should not have reference in |view_model_|.
995 CHECK_EQ(-1, view_model_.GetIndexOfView(details.child));
997 if (selected_view_ == details.child)
998 selected_view_ = NULL;
999 if (activated_folder_item_view_ == details.child)
1000 activated_folder_item_view_ = NULL;
1002 if (drag_view_ == details.child)
1003 EndDrag(true);
1005 bounds_animator_.StopAnimatingView(details.child);
1009 void AppsGridView::OnGestureEvent(ui::GestureEvent* event) {
1010 if (pagination_controller_->OnGestureEvent(*event, GetContentsBounds()))
1011 event->SetHandled();
1014 void AppsGridView::OnScrollEvent(ui::ScrollEvent* event) {
1015 if (event->type() == ui::ET_SCROLL_FLING_CANCEL)
1016 return;
1018 gfx::Vector2dF offset(event->x_offset(), event->y_offset());
1019 if (pagination_controller_->OnScroll(gfx::ToFlooredVector2d(offset))) {
1020 event->SetHandled();
1021 event->StopPropagation();
1025 void AppsGridView::Update() {
1026 DCHECK(!selected_view_ && !drag_view_);
1027 view_model_.Clear();
1028 if (!item_list_ || !item_list_->item_count())
1029 return;
1030 for (size_t i = 0; i < item_list_->item_count(); ++i) {
1031 AppListItemView* view = CreateViewForItemAtIndex(i);
1032 view_model_.Add(view, i);
1033 AddChildView(view);
1035 UpdatePaging();
1036 UpdatePulsingBlockViews();
1037 Layout();
1038 SchedulePaint();
1041 void AppsGridView::UpdatePaging() {
1042 int total_page = view_model_.view_size() && tiles_per_page()
1043 ? (view_model_.view_size() - 1) / tiles_per_page() + 1
1044 : 0;
1046 pagination_model_.SetTotalPages(total_page);
1049 void AppsGridView::UpdatePulsingBlockViews() {
1050 const int existing_items = item_list_ ? item_list_->item_count() : 0;
1051 const int available_slots =
1052 tiles_per_page() - existing_items % tiles_per_page();
1053 const int desired = model_->status() == AppListModel::STATUS_SYNCING ?
1054 available_slots : 0;
1056 if (pulsing_blocks_model_.view_size() == desired)
1057 return;
1059 while (pulsing_blocks_model_.view_size() > desired) {
1060 PulsingBlockView* view = pulsing_blocks_model_.view_at(0);
1061 pulsing_blocks_model_.Remove(0);
1062 delete view;
1065 while (pulsing_blocks_model_.view_size() < desired) {
1066 PulsingBlockView* view = new PulsingBlockView(GetTotalTileSize(), true);
1067 pulsing_blocks_model_.Add(view, 0);
1068 AddChildView(view);
1072 AppListItemView* AppsGridView::CreateViewForItemAtIndex(size_t index) {
1073 // The drag_view_ might be pending for deletion, therefore view_model_
1074 // may have one more item than item_list_.
1075 DCHECK_LE(index, item_list_->item_count());
1076 AppListItemView* view = new AppListItemView(this,
1077 item_list_->item_at(index));
1078 view->SetPaintToLayer(true);
1079 view->SetFillsBoundsOpaquely(false);
1080 return view;
1083 AppsGridView::Index AppsGridView::GetIndexFromModelIndex(
1084 int model_index) const {
1085 return Index(model_index / tiles_per_page(), model_index % tiles_per_page());
1088 int AppsGridView::GetModelIndexFromIndex(const Index& index) const {
1089 return index.page * tiles_per_page() + index.slot;
1092 void AppsGridView::EnsureViewVisible(const Index& index) {
1093 if (pagination_model_.has_transition())
1094 return;
1096 if (IsValidIndex(index))
1097 pagination_model_.SelectPage(index.page, false);
1100 void AppsGridView::SetSelectedItemByIndex(const Index& index) {
1101 if (GetIndexOfView(selected_view_) == index)
1102 return;
1104 AppListItemView* new_selection = GetViewAtIndex(index);
1105 if (!new_selection)
1106 return; // Keep current selection.
1108 if (selected_view_)
1109 selected_view_->SchedulePaint();
1111 EnsureViewVisible(index);
1112 selected_view_ = new_selection;
1113 selected_view_->SetTitleSubpixelAA();
1114 selected_view_->SchedulePaint();
1115 selected_view_->NotifyAccessibilityEvent(
1116 ui::AX_EVENT_FOCUS, true);
1119 bool AppsGridView::IsValidIndex(const Index& index) const {
1120 return index.page >= 0 && index.page < pagination_model_.total_pages() &&
1121 index.slot >= 0 && index.slot < tiles_per_page() &&
1122 GetModelIndexFromIndex(index) < view_model_.view_size();
1125 AppsGridView::Index AppsGridView::GetIndexOfView(
1126 const AppListItemView* view) const {
1127 const int model_index = view_model_.GetIndexOfView(view);
1128 if (model_index == -1)
1129 return Index();
1131 return GetIndexFromModelIndex(model_index);
1134 AppListItemView* AppsGridView::GetViewAtIndex(const Index& index) const {
1135 if (!IsValidIndex(index))
1136 return NULL;
1138 const int model_index = GetModelIndexFromIndex(index);
1139 return GetItemViewAt(model_index);
1142 AppsGridView::Index AppsGridView::GetLastViewIndex() const {
1143 DCHECK_LT(0, view_model_.view_size());
1144 int view_index = view_model_.view_size() - 1;
1145 return Index(view_index / tiles_per_page(), view_index % tiles_per_page());
1148 void AppsGridView::MoveSelected(int page_delta,
1149 int slot_x_delta,
1150 int slot_y_delta) {
1151 if (!selected_view_)
1152 return SetSelectedItemByIndex(Index(pagination_model_.selected_page(), 0));
1154 const Index& selected = GetIndexOfView(selected_view_);
1155 int target_slot = selected.slot + slot_x_delta + slot_y_delta * cols_;
1157 if (selected.slot % cols_ == 0 && slot_x_delta == -1) {
1158 if (selected.page > 0) {
1159 page_delta = -1;
1160 target_slot = selected.slot + cols_ - 1;
1161 } else {
1162 target_slot = selected.slot;
1166 if (selected.slot % cols_ == cols_ - 1 && slot_x_delta == 1) {
1167 if (selected.page < pagination_model_.total_pages() - 1) {
1168 page_delta = 1;
1169 target_slot = selected.slot - cols_ + 1;
1170 } else {
1171 target_slot = selected.slot;
1175 // Clamp the target slot to the last item if we are moving to the last page
1176 // but our target slot is past the end of the item list.
1177 if (page_delta &&
1178 selected.page + page_delta == pagination_model_.total_pages() - 1) {
1179 int last_item_slot = (view_model_.view_size() - 1) % tiles_per_page();
1180 if (last_item_slot < target_slot) {
1181 target_slot = last_item_slot;
1185 int target_page = std::min(pagination_model_.total_pages() - 1,
1186 std::max(selected.page + page_delta, 0));
1187 SetSelectedItemByIndex(Index(target_page, target_slot));
1190 void AppsGridView::CalculateIdealBounds() {
1191 gfx::Size grid_size = GetTileGridSize();
1193 // Page size including padding pixels. A tile.x + page_width means the same
1194 // tile slot in the next page; similarly for tile.y + page_height.
1195 const int page_width = grid_size.width() + kPagePadding;
1196 const int page_height = grid_size.height() + kPagePadding;
1198 // If there is a transition, calculates offset for current and target page.
1199 const int current_page = pagination_model_.selected_page();
1200 const PaginationModel::Transition& transition =
1201 pagination_model_.transition();
1202 const bool is_valid = pagination_model_.is_valid_page(transition.target_page);
1204 // Transition to previous page means negative offset.
1205 const int dir = transition.target_page > current_page ? -1 : 1;
1207 const int total_views =
1208 view_model_.view_size() + pulsing_blocks_model_.view_size();
1209 int slot_index = 0;
1210 for (int i = 0; i < total_views; ++i) {
1211 if (i < view_model_.view_size() && view_model_.view_at(i) == drag_view_)
1212 continue;
1214 Index view_index = GetIndexFromModelIndex(slot_index);
1216 // Leaves a blank space in the grid for the current reorder placeholder.
1217 if (reorder_placeholder_ == view_index) {
1218 ++slot_index;
1219 view_index = GetIndexFromModelIndex(slot_index);
1222 // Decide the x or y offset for current item.
1223 int x_offset = 0;
1224 int y_offset = 0;
1226 if (pagination_controller_->scroll_axis() ==
1227 PaginationController::SCROLL_AXIS_HORIZONTAL) {
1228 if (view_index.page < current_page)
1229 x_offset = -page_width;
1230 else if (view_index.page > current_page)
1231 x_offset = page_width;
1233 if (is_valid) {
1234 if (view_index.page == current_page ||
1235 view_index.page == transition.target_page) {
1236 x_offset += transition.progress * page_width * dir;
1239 } else {
1240 if (view_index.page < current_page)
1241 y_offset = -page_height;
1242 else if (view_index.page > current_page)
1243 y_offset = page_height;
1245 if (is_valid) {
1246 if (view_index.page == current_page ||
1247 view_index.page == transition.target_page) {
1248 y_offset += transition.progress * page_height * dir;
1253 const int row = view_index.slot / cols_;
1254 const int col = view_index.slot % cols_;
1255 gfx::Rect tile_slot = GetExpectedTileBounds(row, col);
1256 tile_slot.Offset(x_offset, y_offset);
1257 if (i < view_model_.view_size()) {
1258 view_model_.set_ideal_bounds(i, tile_slot);
1259 } else {
1260 pulsing_blocks_model_.set_ideal_bounds(i - view_model_.view_size(),
1261 tile_slot);
1264 ++slot_index;
1268 void AppsGridView::AnimateToIdealBounds() {
1269 const gfx::Rect visible_bounds(GetVisibleBounds());
1271 CalculateIdealBounds();
1272 for (int i = 0; i < view_model_.view_size(); ++i) {
1273 AppListItemView* view = GetItemViewAt(i);
1274 if (view == drag_view_)
1275 continue;
1277 const gfx::Rect& target = view_model_.ideal_bounds(i);
1278 if (bounds_animator_.GetTargetBounds(view) == target)
1279 continue;
1281 const gfx::Rect& current = view->bounds();
1282 const bool current_visible = visible_bounds.Intersects(current);
1283 const bool target_visible = visible_bounds.Intersects(target);
1284 const bool visible = current_visible || target_visible;
1286 const int y_diff = target.y() - current.y();
1287 if (visible && y_diff && y_diff % GetTotalTileSize().height() == 0) {
1288 AnimationBetweenRows(view,
1289 current_visible,
1290 current,
1291 target_visible,
1292 target);
1293 } else if (visible || bounds_animator_.IsAnimating(view)) {
1294 bounds_animator_.AnimateViewTo(view, target);
1295 bounds_animator_.SetAnimationDelegate(
1296 view,
1297 scoped_ptr<gfx::AnimationDelegate>(
1298 new ItemMoveAnimationDelegate(view)));
1299 } else {
1300 view->SetBoundsRect(target);
1305 void AppsGridView::AnimationBetweenRows(AppListItemView* view,
1306 bool animate_current,
1307 const gfx::Rect& current,
1308 bool animate_target,
1309 const gfx::Rect& target) {
1310 // Determine page of |current| and |target|. -1 means in the left invisible
1311 // page, 0 is the center visible page and 1 means in the right invisible page.
1312 const int current_page = current.x() < 0 ? -1 :
1313 current.x() >= width() ? 1 : 0;
1314 const int target_page = target.x() < 0 ? -1 :
1315 target.x() >= width() ? 1 : 0;
1317 const int dir = current_page < target_page ||
1318 (current_page == target_page && current.y() < target.y()) ? 1 : -1;
1320 scoped_ptr<ui::Layer> layer;
1321 if (animate_current) {
1322 layer = view->RecreateLayer();
1323 layer->SuppressPaint();
1325 view->SetFillsBoundsOpaquely(false);
1326 view->layer()->SetOpacity(0.f);
1329 gfx::Size total_tile_size = GetTotalTileSize();
1330 gfx::Rect current_out(current);
1331 current_out.Offset(dir * total_tile_size.width(), 0);
1333 gfx::Rect target_in(target);
1334 if (animate_target)
1335 target_in.Offset(-dir * total_tile_size.width(), 0);
1336 view->SetBoundsRect(target_in);
1337 bounds_animator_.AnimateViewTo(view, target);
1339 bounds_animator_.SetAnimationDelegate(
1340 view,
1341 scoped_ptr<gfx::AnimationDelegate>(
1342 new RowMoveAnimationDelegate(view, layer.release(), current_out)));
1345 void AppsGridView::ExtractDragLocation(const ui::LocatedEvent& event,
1346 gfx::Point* drag_point) {
1347 // Use root location of |event| instead of location in |drag_view_|'s
1348 // coordinates because |drag_view_| has a scale transform and location
1349 // could have integer round error and causes jitter.
1350 *drag_point = event.root_location();
1352 #if defined(USE_AURA)
1353 // GetWidget() could be NULL for tests.
1354 if (GetWidget()) {
1355 aura::Window::ConvertPointToTarget(
1356 GetWidget()->GetNativeWindow()->GetRootWindow(),
1357 GetWidget()->GetNativeWindow(),
1358 drag_point);
1360 #endif
1362 views::View::ConvertPointFromWidget(this, drag_point);
1365 void AppsGridView::CalculateDropTarget() {
1366 DCHECK(drag_view_);
1368 gfx::Point point = drag_view_->icon()->bounds().CenterPoint();
1369 views::View::ConvertPointToTarget(drag_view_, this, &point);
1370 if (!IsPointWithinDragBuffer(point)) {
1371 // Reset the reorder target to the original position if the cursor is
1372 // outside the drag buffer.
1373 if (IsDraggingForReparentInRootLevelGridView()) {
1374 drop_attempt_ = DROP_FOR_NONE;
1375 return;
1378 reorder_drop_target_ = drag_view_init_index_;
1379 drop_attempt_ = DROP_FOR_REORDER;
1380 return;
1383 if (EnableFolderDragDropUI() &&
1384 CalculateFolderDropTarget(point, &folder_drop_target_)) {
1385 drop_attempt_ = DROP_FOR_FOLDER;
1386 return;
1389 drop_attempt_ = DROP_FOR_REORDER;
1390 CalculateReorderDropTarget(point, &reorder_drop_target_);
1393 bool AppsGridView::CalculateFolderDropTarget(const gfx::Point& point,
1394 Index* drop_target) const {
1395 // Folders can't be dropped into other folders.
1396 if (IsFolderItem(drag_view_->item()))
1397 return false;
1399 // A folder drop shouldn't happen on the reorder placeholder since that would
1400 // be merging an item with itself.
1401 Index nearest_tile_index(GetNearestTileIndexForPoint(point));
1402 if (!IsValidIndex(nearest_tile_index) ||
1403 nearest_tile_index == reorder_placeholder_) {
1404 return false;
1407 int distance_to_tile_center =
1408 (point - GetExpectedTileBounds(nearest_tile_index.slot).CenterPoint())
1409 .Length();
1410 if (distance_to_tile_center > kFolderDroppingCircleRadius)
1411 return false;
1413 AppListItemView* target_view =
1414 GetViewDisplayedAtSlotOnCurrentPage(nearest_tile_index.slot);
1415 if (!target_view)
1416 return false;
1418 AppListItem* target_item = target_view->item();
1420 // Items can only be dropped into non-folders (which have no children) or
1421 // folders that have fewer than the max allowed items.
1422 // The OEM folder does not allow drag/drop of other items into it.
1423 if (target_item->ChildItemCount() >= kMaxFolderItems ||
1424 IsOEMFolderItem(target_item)) {
1425 return false;
1428 *drop_target = nearest_tile_index;
1429 DCHECK(IsValidIndex(*drop_target));
1430 return true;
1433 void AppsGridView::CalculateReorderDropTarget(const gfx::Point& point,
1434 Index* drop_target) const {
1435 gfx::Rect bounds = GetContentsBounds();
1436 Index grid_index = GetNearestTileIndexForPoint(point);
1437 gfx::Point reorder_placeholder_center =
1438 GetExpectedTileBounds(reorder_placeholder_.slot).CenterPoint();
1440 int x_offset_direction = 0;
1441 if (grid_index == reorder_placeholder_) {
1442 x_offset_direction = reorder_placeholder_center.x() < point.x() ? -1 : 1;
1443 } else {
1444 x_offset_direction = reorder_placeholder_ < grid_index ? -1 : 1;
1447 gfx::Size total_tile_size = GetTotalTileSize();
1448 int row = grid_index.slot / cols_;
1450 // Offset the target column based on the direction of the target. This will
1451 // result in earlier targets getting their reorder zone shifted backwards
1452 // and later targets getting their reorder zones shifted forwards.
1454 // This makes eordering feel like the user is slotting items into the spaces
1455 // between apps.
1456 int x_offset = x_offset_direction *
1457 (total_tile_size.width() - kFolderDroppingCircleRadius) / 2;
1458 int col = (point.x() - bounds.x() + x_offset) / total_tile_size.width();
1459 col = ClampToRange(col, 0, cols_ - 1);
1460 *drop_target =
1461 std::min(Index(pagination_model_.selected_page(), row * cols_ + col),
1462 GetLastViewIndex());
1463 DCHECK(IsValidIndex(*drop_target));
1466 void AppsGridView::OnReorderTimer() {
1467 if (drop_attempt_ == DROP_FOR_REORDER) {
1468 reorder_placeholder_ = reorder_drop_target_;
1469 AnimateToIdealBounds();
1473 void AppsGridView::OnFolderItemReparentTimer() {
1474 DCHECK(folder_delegate_);
1475 if (drag_out_of_folder_container_ && drag_view_) {
1476 bool has_native_drag = drag_and_drop_host_ != nullptr;
1477 #if defined(OS_WIN)
1478 has_native_drag = has_native_drag || synchronous_drag_.Get();
1479 #endif
1480 folder_delegate_->ReparentItem(
1481 drag_view_, last_drag_point_, has_native_drag);
1483 // Set the flag in the folder's grid view.
1484 dragging_for_reparent_item_ = true;
1486 // Do not observe any data change since it is going to be hidden.
1487 item_list_->RemoveObserver(this);
1488 item_list_ = NULL;
1492 void AppsGridView::OnFolderDroppingTimer() {
1493 if (drop_attempt_ == DROP_FOR_FOLDER)
1494 SetAsFolderDroppingTarget(folder_drop_target_, true);
1497 void AppsGridView::UpdateDragStateInsideFolder(Pointer pointer,
1498 const gfx::Point& drag_point) {
1499 if (IsUnderOEMFolder())
1500 return;
1502 if (IsDraggingForReparentInHiddenGridView()) {
1503 // Dispatch drag event to root level grid view for re-parenting folder
1504 // folder item purpose.
1505 DispatchDragEventForReparent(pointer, drag_point);
1506 return;
1509 // Regular drag and drop in a folder's grid view.
1510 folder_delegate_->UpdateFolderViewBackground(true);
1512 // Calculate if the drag_view_ is dragged out of the folder's container
1513 // ink bubble.
1514 gfx::Rect bounds_to_folder_view = ConvertRectToParent(drag_view_->bounds());
1515 gfx::Point pt = bounds_to_folder_view.CenterPoint();
1516 bool is_item_dragged_out_of_folder =
1517 folder_delegate_->IsPointOutsideOfFolderBoundary(pt);
1518 if (is_item_dragged_out_of_folder) {
1519 if (!drag_out_of_folder_container_) {
1520 folder_item_reparent_timer_.Start(
1521 FROM_HERE,
1522 base::TimeDelta::FromMilliseconds(kFolderItemReparentDelay),
1523 this,
1524 &AppsGridView::OnFolderItemReparentTimer);
1525 drag_out_of_folder_container_ = true;
1527 } else {
1528 folder_item_reparent_timer_.Stop();
1529 drag_out_of_folder_container_ = false;
1533 bool AppsGridView::IsDraggingForReparentInRootLevelGridView() const {
1534 return (!folder_delegate_ && dragging_for_reparent_item_);
1537 bool AppsGridView::IsDraggingForReparentInHiddenGridView() const {
1538 return (folder_delegate_ && dragging_for_reparent_item_);
1541 gfx::Rect AppsGridView::GetTargetIconRectInFolder(
1542 AppListItemView* drag_item_view,
1543 AppListItemView* folder_item_view) {
1544 gfx::Rect view_ideal_bounds = view_model_.ideal_bounds(
1545 view_model_.GetIndexOfView(folder_item_view));
1546 gfx::Rect icon_ideal_bounds =
1547 folder_item_view->GetIconBoundsForTargetViewBounds(view_ideal_bounds);
1548 AppListFolderItem* folder_item =
1549 static_cast<AppListFolderItem*>(folder_item_view->item());
1550 return folder_item->GetTargetIconRectInFolderForItem(
1551 drag_item_view->item(), icon_ideal_bounds);
1554 bool AppsGridView::IsUnderOEMFolder() {
1555 if (!folder_delegate_)
1556 return false;
1558 return folder_delegate_->IsOEMFolder();
1561 void AppsGridView::DispatchDragEventForReparent(Pointer pointer,
1562 const gfx::Point& drag_point) {
1563 folder_delegate_->DispatchDragEventForReparent(pointer, drag_point);
1566 void AppsGridView::EndDragFromReparentItemInRootLevel(
1567 bool events_forwarded_to_drag_drop_host,
1568 bool cancel_drag) {
1569 // EndDrag was called before if |drag_view_| is NULL.
1570 if (!drag_view_)
1571 return;
1573 DCHECK(IsDraggingForReparentInRootLevelGridView());
1574 bool cancel_reparent = cancel_drag || drop_attempt_ == DROP_FOR_NONE;
1575 if (!events_forwarded_to_drag_drop_host && !cancel_reparent) {
1576 CalculateDropTarget();
1577 if (drop_attempt_ == DROP_FOR_REORDER &&
1578 IsValidIndex(reorder_drop_target_)) {
1579 ReparentItemForReorder(drag_view_, reorder_drop_target_);
1580 } else if (drop_attempt_ == DROP_FOR_FOLDER &&
1581 IsValidIndex(folder_drop_target_)) {
1582 cancel_reparent =
1583 !ReparentItemToAnotherFolder(drag_view_, folder_drop_target_);
1584 } else {
1585 NOTREACHED();
1587 SetViewHidden(drag_view_, false /* show */, true /* no animate */);
1590 // The drag can be ended after the synchronous drag is created but before it
1591 // is Run().
1592 CleanUpSynchronousDrag();
1594 SetAsFolderDroppingTarget(folder_drop_target_, false);
1595 if (cancel_reparent) {
1596 CancelFolderItemReparent(drag_view_);
1597 } else {
1598 // By setting |drag_view_| to NULL here, we prevent ClearDragState() from
1599 // cleaning up the newly created AppListItemView, effectively claiming
1600 // ownership of the newly created drag view.
1601 drag_view_->OnDragEnded();
1602 drag_view_ = NULL;
1604 ClearDragState();
1605 AnimateToIdealBounds();
1607 StopPageFlipTimer();
1610 void AppsGridView::EndDragForReparentInHiddenFolderGridView() {
1611 if (drag_and_drop_host_) {
1612 // If we had a drag and drop proxy icon, we delete it and make the real
1613 // item visible again.
1614 drag_and_drop_host_->DestroyDragIconProxy();
1617 // The drag can be ended after the synchronous drag is created but before it
1618 // is Run().
1619 CleanUpSynchronousDrag();
1621 SetAsFolderDroppingTarget(folder_drop_target_, false);
1622 ClearDragState();
1625 void AppsGridView::OnFolderItemRemoved() {
1626 DCHECK(folder_delegate_);
1627 if (item_list_)
1628 item_list_->RemoveObserver(this);
1629 item_list_ = nullptr;
1632 void AppsGridView::StartDragAndDropHostDrag(const gfx::Point& grid_location) {
1633 // When a drag and drop host is given, the item can be dragged out of the app
1634 // list window. In that case a proxy widget needs to be used.
1635 // Note: This code has very likely to be changed for Windows (non metro mode)
1636 // when a |drag_and_drop_host_| gets implemented.
1637 if (!drag_view_ || !drag_and_drop_host_)
1638 return;
1640 gfx::Point screen_location = grid_location;
1641 views::View::ConvertPointToScreen(this, &screen_location);
1643 // Determine the mouse offset to the center of the icon so that the drag and
1644 // drop host follows this layer.
1645 gfx::Vector2d delta = drag_view_offset_ -
1646 drag_view_->GetLocalBounds().CenterPoint();
1647 delta.set_y(delta.y() + drag_view_->title()->size().height() / 2);
1649 // We have to hide the original item since the drag and drop host will do
1650 // the OS dependent code to "lift off the dragged item".
1651 DCHECK(!IsDraggingForReparentInRootLevelGridView());
1652 drag_and_drop_host_->CreateDragIconProxy(screen_location,
1653 drag_view_->item()->icon(),
1654 drag_view_,
1655 delta,
1656 kDragAndDropProxyScale);
1657 SetViewHidden(drag_view_,
1658 true /* hide */,
1659 true /* no animation */);
1662 void AppsGridView::DispatchDragEventToDragAndDropHost(
1663 const gfx::Point& location_in_screen_coordinates) {
1664 if (!drag_view_ || !drag_and_drop_host_)
1665 return;
1667 if (GetLocalBounds().Contains(last_drag_point_)) {
1668 // The event was issued inside the app menu and we should get all events.
1669 if (forward_events_to_drag_and_drop_host_) {
1670 // The DnD host was previously called and needs to be informed that the
1671 // session returns to the owner.
1672 forward_events_to_drag_and_drop_host_ = false;
1673 drag_and_drop_host_->EndDrag(true);
1675 } else {
1676 if (IsFolderItem(drag_view_->item()))
1677 return;
1679 // The event happened outside our app menu and we might need to dispatch.
1680 if (forward_events_to_drag_and_drop_host_) {
1681 // Dispatch since we have already started.
1682 if (!drag_and_drop_host_->Drag(location_in_screen_coordinates)) {
1683 // The host is not active any longer and we cancel the operation.
1684 forward_events_to_drag_and_drop_host_ = false;
1685 drag_and_drop_host_->EndDrag(true);
1687 } else {
1688 if (drag_and_drop_host_->StartDrag(drag_view_->item()->id(),
1689 location_in_screen_coordinates)) {
1690 // From now on we forward the drag events.
1691 forward_events_to_drag_and_drop_host_ = true;
1692 // Any flip operations are stopped.
1693 StopPageFlipTimer();
1699 void AppsGridView::MaybeStartPageFlipTimer(const gfx::Point& drag_point) {
1700 if (!IsPointWithinDragBuffer(drag_point))
1701 StopPageFlipTimer();
1702 int new_page_flip_target = -1;
1704 // Drag zones are at the edges of the scroll axis.
1705 if (pagination_controller_->scroll_axis() ==
1706 PaginationController::SCROLL_AXIS_VERTICAL) {
1707 if (drag_point.y() < kPageFlipZoneSize)
1708 new_page_flip_target = pagination_model_.selected_page() - 1;
1709 else if (drag_point.y() > height() - kPageFlipZoneSize)
1710 new_page_flip_target = pagination_model_.selected_page() + 1;
1711 } else {
1712 if (page_switcher_view_->bounds().Contains(drag_point)) {
1713 gfx::Point page_switcher_point(drag_point);
1714 views::View::ConvertPointToTarget(
1715 this, page_switcher_view_, &page_switcher_point);
1716 new_page_flip_target =
1717 page_switcher_view_->GetPageForPoint(page_switcher_point);
1720 // TODO(xiyuan): Fix this for RTL.
1721 if (new_page_flip_target == -1 && drag_point.x() < kPageFlipZoneSize)
1722 new_page_flip_target = pagination_model_.selected_page() - 1;
1724 if (new_page_flip_target == -1 &&
1725 drag_point.x() > width() - kPageFlipZoneSize) {
1726 new_page_flip_target = pagination_model_.selected_page() + 1;
1730 if (new_page_flip_target == page_flip_target_)
1731 return;
1733 StopPageFlipTimer();
1734 if (pagination_model_.is_valid_page(new_page_flip_target)) {
1735 page_flip_target_ = new_page_flip_target;
1737 if (page_flip_target_ != pagination_model_.selected_page()) {
1738 page_flip_timer_.Start(FROM_HERE,
1739 base::TimeDelta::FromMilliseconds(page_flip_delay_in_ms_),
1740 this, &AppsGridView::OnPageFlipTimer);
1745 void AppsGridView::OnPageFlipTimer() {
1746 DCHECK(pagination_model_.is_valid_page(page_flip_target_));
1747 pagination_model_.SelectPage(page_flip_target_, true);
1750 void AppsGridView::MoveItemInModel(AppListItemView* item_view,
1751 const Index& target) {
1752 int current_model_index = view_model_.GetIndexOfView(item_view);
1753 DCHECK_GE(current_model_index, 0);
1755 int target_model_index = GetModelIndexFromIndex(target);
1756 if (target_model_index == current_model_index)
1757 return;
1759 item_list_->RemoveObserver(this);
1760 item_list_->MoveItem(current_model_index, target_model_index);
1761 view_model_.Move(current_model_index, target_model_index);
1762 item_list_->AddObserver(this);
1764 if (pagination_model_.selected_page() != target.page)
1765 pagination_model_.SelectPage(target.page, false);
1768 void AppsGridView::MoveItemToFolder(AppListItemView* item_view,
1769 const Index& target) {
1770 const std::string& source_item_id = item_view->item()->id();
1771 AppListItemView* target_view =
1772 GetViewDisplayedAtSlotOnCurrentPage(target.slot);
1773 DCHECK(target_view);
1774 const std::string& target_view_item_id = target_view->item()->id();
1776 // Check that the item is not being dropped onto itself; this should not
1777 // happen, but it can if something allows multiple views to share an
1778 // item (e.g., if a folder drop does not clean up properly).
1779 DCHECK_NE(source_item_id, target_view_item_id);
1781 // Make change to data model.
1782 item_list_->RemoveObserver(this);
1783 std::string folder_item_id =
1784 model_->MergeItems(target_view_item_id, source_item_id);
1785 item_list_->AddObserver(this);
1786 if (folder_item_id.empty()) {
1787 LOG(ERROR) << "Unable to merge into item id: " << target_view_item_id;
1788 return;
1790 if (folder_item_id != target_view_item_id) {
1791 // New folder was created, change the view model to replace the old target
1792 // view with the new folder item view.
1793 size_t folder_item_index;
1794 if (item_list_->FindItemIndex(folder_item_id, &folder_item_index)) {
1795 int target_view_index = view_model_.GetIndexOfView(target_view);
1796 gfx::Rect target_view_bounds = target_view->bounds();
1797 DeleteItemViewAtIndex(target_view_index);
1798 AppListItemView* target_folder_view =
1799 CreateViewForItemAtIndex(folder_item_index);
1800 target_folder_view->SetBoundsRect(target_view_bounds);
1801 view_model_.Add(target_folder_view, target_view_index);
1802 AddChildView(target_folder_view);
1803 } else {
1804 LOG(ERROR) << "Folder no longer in item_list: " << folder_item_id;
1808 // Fade out the drag_view_ and delete it when animation ends.
1809 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1810 view_model_.Remove(drag_view_index);
1811 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1812 bounds_animator_.SetAnimationDelegate(
1813 drag_view_,
1814 scoped_ptr<gfx::AnimationDelegate>(
1815 new ItemRemoveAnimationDelegate(drag_view_)));
1816 UpdatePaging();
1819 void AppsGridView::ReparentItemForReorder(AppListItemView* item_view,
1820 const Index& target) {
1821 item_list_->RemoveObserver(this);
1822 model_->RemoveObserver(this);
1824 AppListItem* reparent_item = item_view->item();
1825 DCHECK(reparent_item->IsInFolder());
1826 const std::string source_folder_id = reparent_item->folder_id();
1827 AppListFolderItem* source_folder =
1828 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1830 int target_model_index = GetModelIndexFromIndex(target);
1832 // Remove the source folder view if there is only 1 item in it, since the
1833 // source folder will be deleted after its only child item removed from it.
1834 if (source_folder->ChildItemCount() == 1u) {
1835 const int deleted_folder_index =
1836 view_model_.GetIndexOfView(activated_folder_item_view());
1837 DeleteItemViewAtIndex(deleted_folder_index);
1839 // Adjust |target_model_index| if it is beyond the deleted folder index.
1840 if (target_model_index > deleted_folder_index)
1841 --target_model_index;
1844 // Move the item from its parent folder to top level item list.
1845 // Must move to target_model_index, the location we expect the target item
1846 // to be, not the item location we want to insert before.
1847 int current_model_index = view_model_.GetIndexOfView(item_view);
1848 syncer::StringOrdinal target_position;
1849 if (target_model_index < static_cast<int>(item_list_->item_count()))
1850 target_position = item_list_->item_at(target_model_index)->position();
1851 model_->MoveItemToFolderAt(reparent_item, "", target_position);
1852 view_model_.Move(current_model_index, target_model_index);
1854 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1856 item_list_->AddObserver(this);
1857 model_->AddObserver(this);
1858 UpdatePaging();
1861 bool AppsGridView::ReparentItemToAnotherFolder(AppListItemView* item_view,
1862 const Index& target) {
1863 DCHECK(IsDraggingForReparentInRootLevelGridView());
1865 AppListItemView* target_view =
1866 GetViewDisplayedAtSlotOnCurrentPage(target.slot);
1867 if (!target_view)
1868 return false;
1870 AppListItem* reparent_item = item_view->item();
1871 DCHECK(reparent_item->IsInFolder());
1872 const std::string source_folder_id = reparent_item->folder_id();
1873 AppListFolderItem* source_folder =
1874 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1876 AppListItem* target_item = target_view->item();
1878 // An app is being reparented to its original folder. Just cancel the
1879 // reparent.
1880 if (target_item->id() == reparent_item->folder_id())
1881 return false;
1883 // Make change to data model.
1884 item_list_->RemoveObserver(this);
1886 // Remove the source folder view if there is only 1 item in it, since the
1887 // source folder will be deleted after its only child item merged into the
1888 // target item.
1889 if (source_folder->ChildItemCount() == 1u)
1890 DeleteItemViewAtIndex(
1891 view_model_.GetIndexOfView(activated_folder_item_view()));
1893 // Move item to the target folder.
1894 std::string target_id_after_merge =
1895 model_->MergeItems(target_item->id(), reparent_item->id());
1896 if (target_id_after_merge.empty()) {
1897 LOG(ERROR) << "Unable to reparent to item id: " << target_item->id();
1898 item_list_->AddObserver(this);
1899 return false;
1902 if (target_id_after_merge != target_item->id()) {
1903 // New folder was created, change the view model to replace the old target
1904 // view with the new folder item view.
1905 const std::string& new_folder_id = reparent_item->folder_id();
1906 size_t new_folder_index;
1907 if (item_list_->FindItemIndex(new_folder_id, &new_folder_index)) {
1908 int target_view_index = view_model_.GetIndexOfView(target_view);
1909 DeleteItemViewAtIndex(target_view_index);
1910 AppListItemView* new_folder_view =
1911 CreateViewForItemAtIndex(new_folder_index);
1912 view_model_.Add(new_folder_view, target_view_index);
1913 AddChildView(new_folder_view);
1914 } else {
1915 LOG(ERROR) << "Folder no longer in item_list: " << new_folder_id;
1919 RemoveLastItemFromReparentItemFolderIfNecessary(source_folder_id);
1921 item_list_->AddObserver(this);
1923 // Fade out the drag_view_ and delete it when animation ends.
1924 int drag_view_index = view_model_.GetIndexOfView(drag_view_);
1925 view_model_.Remove(drag_view_index);
1926 bounds_animator_.AnimateViewTo(drag_view_, drag_view_->bounds());
1927 bounds_animator_.SetAnimationDelegate(
1928 drag_view_,
1929 scoped_ptr<gfx::AnimationDelegate>(
1930 new ItemRemoveAnimationDelegate(drag_view_)));
1931 UpdatePaging();
1933 return true;
1936 // After moving the re-parenting item out of the folder, if there is only 1 item
1937 // left, remove the last item out of the folder, delete the folder and insert it
1938 // to the data model at the same position. Make the same change to view_model_
1939 // accordingly.
1940 void AppsGridView::RemoveLastItemFromReparentItemFolderIfNecessary(
1941 const std::string& source_folder_id) {
1942 AppListFolderItem* source_folder =
1943 static_cast<AppListFolderItem*>(item_list_->FindItem(source_folder_id));
1944 if (!source_folder || source_folder->ChildItemCount() != 1u)
1945 return;
1947 // Delete view associated with the folder item to be removed.
1948 DeleteItemViewAtIndex(
1949 view_model_.GetIndexOfView(activated_folder_item_view()));
1951 // Now make the data change to remove the folder item in model.
1952 AppListItem* last_item = source_folder->item_list()->item_at(0);
1953 model_->MoveItemToFolderAt(last_item, "", source_folder->position());
1955 // Create a new item view for the last item in folder.
1956 size_t last_item_index;
1957 if (!item_list_->FindItemIndex(last_item->id(), &last_item_index) ||
1958 last_item_index > static_cast<size_t>(view_model_.view_size())) {
1959 NOTREACHED();
1960 return;
1962 AppListItemView* last_item_view = CreateViewForItemAtIndex(last_item_index);
1963 view_model_.Add(last_item_view, last_item_index);
1964 AddChildView(last_item_view);
1967 void AppsGridView::CancelFolderItemReparent(AppListItemView* drag_item_view) {
1968 // The icon of the dragged item must target to its final ideal bounds after
1969 // the animation completes.
1970 CalculateIdealBounds();
1972 gfx::Rect target_icon_rect =
1973 GetTargetIconRectInFolder(drag_item_view, activated_folder_item_view_);
1975 gfx::Rect drag_view_icon_to_grid =
1976 drag_item_view->ConvertRectToParent(drag_item_view->GetIconBounds());
1977 drag_view_icon_to_grid.ClampToCenteredSize(
1978 gfx::Size(kGridIconDimension, kGridIconDimension));
1979 TopIconAnimationView* icon_view = new TopIconAnimationView(
1980 drag_item_view->item()->icon(),
1981 target_icon_rect,
1982 false); /* animate like closing folder */
1983 AddChildView(icon_view);
1984 icon_view->SetBoundsRect(drag_view_icon_to_grid);
1985 icon_view->TransformView();
1988 void AppsGridView::CancelContextMenusOnCurrentPage() {
1989 int start = pagination_model_.selected_page() * tiles_per_page();
1990 int end = std::min(view_model_.view_size(), start + tiles_per_page());
1991 for (int i = start; i < end; ++i)
1992 GetItemViewAt(i)->CancelContextMenu();
1995 void AppsGridView::DeleteItemViewAtIndex(int index) {
1996 AppListItemView* item_view = GetItemViewAt(index);
1997 view_model_.Remove(index);
1998 if (item_view == drag_view_)
1999 drag_view_ = NULL;
2000 delete item_view;
2003 bool AppsGridView::IsPointWithinDragBuffer(const gfx::Point& point) const {
2004 gfx::Rect rect(GetLocalBounds());
2005 rect.Inset(-kDragBufferPx, -kDragBufferPx, -kDragBufferPx, -kDragBufferPx);
2006 return rect.Contains(point);
2009 void AppsGridView::ButtonPressed(views::Button* sender,
2010 const ui::Event& event) {
2011 if (dragging())
2012 return;
2014 if (strcmp(sender->GetClassName(), AppListItemView::kViewClassName))
2015 return;
2016 AppListItemView* pressed_item_view = static_cast<AppListItemView*>(sender);
2018 if (delegate_) {
2019 // Always set the previous activated_folder_item_view_ to be visible. This
2020 // prevents a case where the item would remain hidden due the
2021 // |activated_folder_item_view_| changing during the animation. We only
2022 // need to track |activated_folder_item_view_| in the root level grid view.
2023 if (!folder_delegate_) {
2024 if (activated_folder_item_view_)
2025 activated_folder_item_view_->SetVisible(true);
2026 if (IsFolderItem(pressed_item_view->item()))
2027 activated_folder_item_view_ = pressed_item_view;
2028 else
2029 activated_folder_item_view_ = NULL;
2031 delegate_->ActivateApp(pressed_item_view->item(), event.flags());
2035 void AppsGridView::OnListItemAdded(size_t index, AppListItem* item) {
2036 EndDrag(true);
2038 AppListItemView* view = CreateViewForItemAtIndex(index);
2039 view_model_.Add(view, index);
2040 AddChildView(view);
2042 UpdatePaging();
2043 UpdatePulsingBlockViews();
2044 Layout();
2045 SchedulePaint();
2048 void AppsGridView::OnListItemRemoved(size_t index, AppListItem* item) {
2049 EndDrag(true);
2051 DeleteItemViewAtIndex(index);
2053 UpdatePaging();
2054 UpdatePulsingBlockViews();
2055 Layout();
2056 SchedulePaint();
2059 void AppsGridView::OnListItemMoved(size_t from_index,
2060 size_t to_index,
2061 AppListItem* item) {
2062 EndDrag(true);
2063 view_model_.Move(from_index, to_index);
2065 UpdatePaging();
2066 AnimateToIdealBounds();
2069 void AppsGridView::OnAppListItemHighlight(size_t index, bool highlight) {
2070 AppListItemView* view = GetItemViewAt(index);
2071 view->SetItemIsHighlighted(highlight);
2072 if (highlight)
2073 EnsureViewVisible(GetIndexFromModelIndex(index));
2076 void AppsGridView::TotalPagesChanged() {
2079 void AppsGridView::SelectedPageChanged(int old_selected, int new_selected) {
2080 if (dragging()) {
2081 CalculateDropTarget();
2082 Layout();
2083 MaybeStartPageFlipTimer(last_drag_point_);
2084 } else {
2085 ClearSelectedView(selected_view_);
2086 Layout();
2090 void AppsGridView::TransitionStarted() {
2091 CancelContextMenusOnCurrentPage();
2094 void AppsGridView::TransitionChanged() {
2095 // Update layout for valid page transition only since over-scroll no longer
2096 // animates app icons.
2097 const PaginationModel::Transition& transition =
2098 pagination_model_.transition();
2099 if (pagination_model_.is_valid_page(transition.target_page))
2100 Layout();
2103 void AppsGridView::OnAppListModelStatusChanged() {
2104 UpdatePulsingBlockViews();
2105 Layout();
2106 SchedulePaint();
2109 void AppsGridView::SetViewHidden(AppListItemView* view,
2110 bool hide,
2111 bool immediate) {
2112 ui::ScopedLayerAnimationSettings animator(view->layer()->GetAnimator());
2113 animator.SetPreemptionStrategy(
2114 immediate ? ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET :
2115 ui::LayerAnimator::BLEND_WITH_CURRENT_ANIMATION);
2116 view->layer()->SetOpacity(hide ? 0 : 1);
2119 void AppsGridView::OnImplicitAnimationsCompleted() {
2120 if (layer()->opacity() == 0.0f)
2121 SetVisible(false);
2124 bool AppsGridView::EnableFolderDragDropUI() {
2125 // Enable drag and drop folder UI only if it is at the app list root level
2126 // and the switch is on.
2127 return model_->folders_enabled() && !folder_delegate_;
2130 AppsGridView::Index AppsGridView::GetNearestTileIndexForPoint(
2131 const gfx::Point& point) const {
2132 gfx::Rect bounds = GetContentsBounds();
2133 gfx::Size total_tile_size = GetTotalTileSize();
2134 int col = ClampToRange(
2135 (point.x() - bounds.x()) / total_tile_size.width(), 0, cols_ - 1);
2136 int row = ClampToRange((point.y() - bounds.y()) / total_tile_size.height(),
2138 rows_per_page_ - 1);
2139 return Index(pagination_model_.selected_page(), row * cols_ + col);
2142 gfx::Size AppsGridView::GetTileGridSize() const {
2143 gfx::Rect bounds = GetExpectedTileBounds(0, 0);
2144 bounds.Union(GetExpectedTileBounds(rows_per_page_ - 1, cols_ - 1));
2145 bounds.Inset(GetTilePadding());
2146 return bounds.size();
2149 gfx::Rect AppsGridView::GetExpectedTileBounds(int slot) const {
2150 return GetExpectedTileBounds(slot / cols_, slot % cols_);
2153 gfx::Rect AppsGridView::GetExpectedTileBounds(int row, int col) const {
2154 gfx::Rect bounds(GetContentsBounds());
2155 gfx::Size total_tile_size = GetTotalTileSize();
2156 gfx::Rect tile_bounds(gfx::Point(bounds.x() + col * total_tile_size.width(),
2157 bounds.y() + row * total_tile_size.height()),
2158 total_tile_size);
2159 tile_bounds.Inset(-GetTilePadding());
2160 return tile_bounds;
2163 AppListItemView* AppsGridView::GetViewDisplayedAtSlotOnCurrentPage(
2164 int slot) const {
2165 if (slot < 0)
2166 return NULL;
2168 // Calculate the original bound of the tile at |index|.
2169 int row = slot / cols_;
2170 int col = slot % cols_;
2171 gfx::Rect tile_rect = GetExpectedTileBounds(row, col);
2173 for (int i = 0; i < view_model_.view_size(); ++i) {
2174 AppListItemView* view = GetItemViewAt(i);
2175 if (view->bounds() == tile_rect && view != drag_view_)
2176 return view;
2178 return NULL;
2181 void AppsGridView::SetAsFolderDroppingTarget(const Index& target_index,
2182 bool is_target_folder) {
2183 AppListItemView* target_view =
2184 GetViewDisplayedAtSlotOnCurrentPage(target_index.slot);
2185 if (target_view)
2186 target_view->SetAsAttemptedFolderTarget(is_target_folder);
2189 } // namespace app_list