Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: ash/launcher/launcher_view.cc

Issue 22429004: Refactor LauncherDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add LauncherItemDelegateManager Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/launcher/launcher_view.h" 5 #include "ash/launcher/launcher_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "ash/ash_constants.h" 9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h" 10 #include "ash/ash_switches.h"
11 #include "ash/drag_drop/drag_image_view.h" 11 #include "ash/drag_drop/drag_image_view.h"
12 #include "ash/launcher/alternate_app_list_button.h" 12 #include "ash/launcher/alternate_app_list_button.h"
13 #include "ash/launcher/app_list_button.h" 13 #include "ash/launcher/app_list_button.h"
14 #include "ash/launcher/launcher_button.h" 14 #include "ash/launcher/launcher_button.h"
15 #include "ash/launcher/launcher_delegate.h" 15 #include "ash/launcher/launcher_delegate.h"
16 #include "ash/launcher/launcher_icon_observer.h" 16 #include "ash/launcher/launcher_icon_observer.h"
17 #include "ash/launcher/launcher_item_delegate.h"
18 #include "ash/launcher/launcher_item_delegate_manager.h"
17 #include "ash/launcher/launcher_model.h" 19 #include "ash/launcher/launcher_model.h"
18 #include "ash/launcher/launcher_tooltip_manager.h" 20 #include "ash/launcher/launcher_tooltip_manager.h"
19 #include "ash/launcher/overflow_bubble.h" 21 #include "ash/launcher/overflow_bubble.h"
20 #include "ash/launcher/overflow_button.h" 22 #include "ash/launcher/overflow_button.h"
21 #include "ash/launcher/tabbed_launcher_button.h" 23 #include "ash/launcher/tabbed_launcher_button.h"
22 #include "ash/root_window_controller.h" 24 #include "ash/root_window_controller.h"
23 #include "ash/scoped_target_root_window.h" 25 #include "ash/scoped_target_root_window.h"
24 #include "ash/shelf/shelf_layout_manager.h" 26 #include "ash/shelf/shelf_layout_manager.h"
25 #include "ash/shelf/shelf_widget.h" 27 #include "ash/shelf/shelf_widget.h"
26 #include "ash/shell_delegate.h" 28 #include "ash/shell_delegate.h"
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 drag_offset_(0), 403 drag_offset_(0),
402 start_drag_index_(-1), 404 start_drag_index_(-1),
403 context_menu_id_(0), 405 context_menu_id_(0),
404 leading_inset_(kDefaultLeadingInset), 406 leading_inset_(kDefaultLeadingInset),
405 cancelling_drag_model_changed_(false), 407 cancelling_drag_model_changed_(false),
406 last_hidden_index_(0), 408 last_hidden_index_(0),
407 closing_event_time_(base::TimeDelta()), 409 closing_event_time_(base::TimeDelta()),
408 got_deleted_(NULL), 410 got_deleted_(NULL),
409 drag_and_drop_item_pinned_(false), 411 drag_and_drop_item_pinned_(false),
410 drag_and_drop_launcher_id_(0), 412 drag_and_drop_launcher_id_(0),
411 dragged_off_shelf_(false) { 413 dragged_off_shelf_(false),
414 item_manager_(Shell::GetInstance()->launcher_item_delegate_manager()) {
412 DCHECK(model_); 415 DCHECK(model_);
413 bounds_animator_.reset(new views::BoundsAnimator(this)); 416 bounds_animator_.reset(new views::BoundsAnimator(this));
414 bounds_animator_->AddObserver(this); 417 bounds_animator_->AddObserver(this);
415 set_context_menu_controller(this); 418 set_context_menu_controller(this);
416 focus_search_.reset(new LauncherFocusSearch(view_model_.get())); 419 focus_search_.reset(new LauncherFocusSearch(view_model_.get()));
417 tooltip_.reset(new LauncherTooltipManager( 420 tooltip_.reset(new LauncherTooltipManager(
418 shelf_layout_manager, this)); 421 shelf_layout_manager, this));
419 } 422 }
420 423
421 LauncherView::~LauncherView() { 424 LauncherView::~LauncherView() {
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
974 view, new FadeInAnimationDelegate(view), true); 977 view, new FadeInAnimationDelegate(view), true);
975 } 978 }
976 979
977 void LauncherView::PrepareForDrag(Pointer pointer, 980 void LauncherView::PrepareForDrag(Pointer pointer,
978 const ui::LocatedEvent& event) { 981 const ui::LocatedEvent& event) {
979 DCHECK(!dragging()); 982 DCHECK(!dragging());
980 DCHECK(drag_view_); 983 DCHECK(drag_view_);
981 drag_pointer_ = pointer; 984 drag_pointer_ = pointer;
982 start_drag_index_ = view_model_->GetIndexOfView(drag_view_); 985 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
983 986
984 // If the item is no longer draggable, bail out. 987 if (start_drag_index_== -1) {
985 if (start_drag_index_ == -1 ||
986 !delegate_->IsDraggable(model_->items()[start_drag_index_])) {
987 CancelDrag(-1); 988 CancelDrag(-1);
988 return; 989 return;
989 } 990 }
991
992 // If the item is no longer draggable, bail out.
993 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
994 model_->items()[start_drag_index_].type);
995 if (!item_delegate->IsDraggable(model_->items()[start_drag_index_])) {
996 CancelDrag(-1);
997 return;
998 }
990 999
991 // Move the view to the front so that it appears on top of other views. 1000 // Move the view to the front so that it appears on top of other views.
992 ReorderChildView(drag_view_, -1); 1001 ReorderChildView(drag_view_, -1);
993 bounds_animator_->StopAnimatingView(drag_view_); 1002 bounds_animator_->StopAnimatingView(drag_view_);
994 } 1003 }
995 1004
996 void LauncherView::ContinueDrag(const ui::LocatedEvent& event) { 1005 void LauncherView::ContinueDrag(const ui::LocatedEvent& event) {
997 // Due to a syncing operation the application might have been removed. 1006 // Due to a syncing operation the application might have been removed.
998 // Bail if it is gone. 1007 // Bail if it is gone.
999 int current_index = view_model_->GetIndexOfView(drag_view_); 1008 int current_index = view_model_->GetIndexOfView(drag_view_);
1000 DCHECK_NE(-1, current_index); 1009 DCHECK_NE(-1, current_index);
1001 if (current_index == -1 || 1010
1002 !delegate_->IsDraggable(model_->items()[current_index])) { 1011 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
1012 model_->items()[current_index].type);
1013 if (!item_delegate->IsDraggable(model_->items()[current_index])) {
1003 CancelDrag(-1); 1014 CancelDrag(-1);
1004 return; 1015 return;
1005 } 1016 }
1006 1017
1007 // If this is not a drag and drop host operation, check if the item got 1018 // If this is not a drag and drop host operation, check if the item got
1008 // ripped off the shelf - if it did we are done. 1019 // ripped off the shelf - if it did we are done.
1009 if (!drag_and_drop_launcher_id_ && ash::switches::UseDragOffShelf()) { 1020 if (!drag_and_drop_launcher_id_ && ash::switches::UseDragOffShelf()) {
1010 if (HandleRipOffDrag(event)) 1021 if (HandleRipOffDrag(event))
1011 return; 1022 return;
1012 // The rip off handler could have changed the location of the item. 1023 // The rip off handler could have changed the location of the item.
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after
1489 } 1500 }
1490 1501
1491 void LauncherView::PointerPressedOnButton(views::View* view, 1502 void LauncherView::PointerPressedOnButton(views::View* view,
1492 Pointer pointer, 1503 Pointer pointer,
1493 const ui::LocatedEvent& event) { 1504 const ui::LocatedEvent& event) {
1494 if (drag_view_) 1505 if (drag_view_)
1495 return; 1506 return;
1496 1507
1497 tooltip_->Close(); 1508 tooltip_->Close();
1498 int index = view_model_->GetIndexOfView(view); 1509 int index = view_model_->GetIndexOfView(view);
1499 if (index == -1 || 1510 if (index == -1)
1500 view_model_->view_size() <= 1 || 1511 return;
1501 !delegate_->IsDraggable(model_->items()[index])) 1512
1513 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
1514 model_->items()[index].type);
1515 if (view_model_->view_size() <= 1 ||
1516 !item_delegate->IsDraggable(model_->items()[index]))
1502 return; // View is being deleted or not draggable, ignore request. 1517 return; // View is being deleted or not draggable, ignore request.
1503 1518
1504 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager(); 1519 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
1505 1520
1506 drag_view_ = view; 1521 drag_view_ = view;
1507 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y()); 1522 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y());
1508 } 1523 }
1509 1524
1510 void LauncherView::PointerDraggedOnButton(views::View* view, 1525 void LauncherView::PointerDraggedOnButton(views::View* view,
1511 Pointer pointer, 1526 Pointer pointer,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1560 if (!tooltip_->IsVisible()) 1575 if (!tooltip_->IsVisible())
1561 tooltip_->StopTimer(); 1576 tooltip_->StopTimer();
1562 } 1577 }
1563 1578
1564 base::string16 LauncherView::GetAccessibleName(const views::View* view) { 1579 base::string16 LauncherView::GetAccessibleName(const views::View* view) {
1565 int view_index = view_model_->GetIndexOfView(view); 1580 int view_index = view_model_->GetIndexOfView(view);
1566 // May be -1 while in the process of animating closed. 1581 // May be -1 while in the process of animating closed.
1567 if (view_index == -1) 1582 if (view_index == -1)
1568 return base::string16(); 1583 return base::string16();
1569 1584
1570 switch (model_->items()[view_index].type) { 1585 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
1571 case TYPE_TABBED: 1586 model_->items()[view_index].type);
1572 case TYPE_APP_PANEL: 1587 return item_delegate->GetTitle(model_->items()[view_index]);
1573 case TYPE_APP_SHORTCUT:
1574 case TYPE_WINDOWED_APP:
1575 case TYPE_PLATFORM_APP:
1576 case TYPE_BROWSER_SHORTCUT:
1577 return delegate_->GetTitle(model_->items()[view_index]);
1578
1579 case TYPE_APP_LIST:
1580 return model_->status() == LauncherModel::STATUS_LOADING ?
1581 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_SYNCING_TITLE) :
1582 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE);
1583 }
1584 return base::string16();
1585 } 1588 }
1586 1589
1587 void LauncherView::ButtonPressed(views::Button* sender, 1590 void LauncherView::ButtonPressed(views::Button* sender,
1588 const ui::Event& event) { 1591 const ui::Event& event) {
1589 // Do not handle mouse release during drag. 1592 // Do not handle mouse release during drag.
1590 if (dragging()) 1593 if (dragging())
1591 return; 1594 return;
1592 1595
1593 tooltip_->Close(); 1596 tooltip_->Close();
1594 1597
(...skipping 23 matching lines...) Expand all
1618 } 1621 }
1619 1622
1620 // Collect usage statistics before we decide what to do with the click. 1623 // Collect usage statistics before we decide what to do with the click.
1621 switch (model_->items()[view_index].type) { 1624 switch (model_->items()[view_index].type) {
1622 case TYPE_APP_SHORTCUT: 1625 case TYPE_APP_SHORTCUT:
1623 case TYPE_WINDOWED_APP: 1626 case TYPE_WINDOWED_APP:
1624 case TYPE_PLATFORM_APP: 1627 case TYPE_PLATFORM_APP:
1625 case TYPE_BROWSER_SHORTCUT: 1628 case TYPE_BROWSER_SHORTCUT:
1626 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1629 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1627 UMA_LAUNCHER_CLICK_ON_APP); 1630 UMA_LAUNCHER_CLICK_ON_APP);
1628 // Fallthrough
1629 case TYPE_TABBED:
1630 case TYPE_APP_PANEL:
1631 delegate_->ItemSelected(model_->items()[view_index], event);
1632 // Don't show the menu when the user creates a new browser using ctrl
1633 // click.
1634 if (model_->items()[view_index].type != TYPE_BROWSER_SHORTCUT ||
1635 !(event.flags() & ui::EF_CONTROL_DOWN))
1636 ShowListMenuForView(model_->items()[view_index], sender, event);
1637 break; 1631 break;
1638 1632
1639 case TYPE_APP_LIST: 1633 case TYPE_APP_LIST:
1640 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1634 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1641 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON); 1635 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1642 Shell::GetInstance()->ToggleAppList(GetWidget()->GetNativeView()); 1636 break;
1637
1638 case TYPE_TABBED:
1639 case TYPE_APP_PANEL:
1643 break; 1640 break;
1644 } 1641 }
1645 } 1642 }
1643 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
1644 model_->items()[view_index].type);
1645 item_delegate->ItemSelected(model_->items()[view_index], event);
1646
1647 ShowListMenuForView(model_->items()[view_index], sender, event);
1646 } 1648 }
1647 1649
1648 bool LauncherView::ShowListMenuForView(const LauncherItem& item, 1650 bool LauncherView::ShowListMenuForView(const LauncherItem& item,
1649 views::View* source, 1651 views::View* source,
1650 const ui::Event& event) { 1652 const ui::Event& event) {
1651 scoped_ptr<ash::LauncherMenuModel> menu_model; 1653 scoped_ptr<ash::LauncherMenuModel> menu_model;
1652 menu_model.reset(delegate_->CreateApplicationMenu(item, event.flags())); 1654 LauncherItemDelegate* item_delegate =
1655 item_manager_->GetLauncherItemDelegate(item.type);
1656 menu_model.reset(item_delegate->CreateApplicationMenu(item, event.flags()));
1653 1657
1654 // Make sure we have a menu and it has at least two items in addition to the 1658 // Make sure we have a menu and it has at least two items in addition to the
1655 // application title and the 3 spacing separators. 1659 // application title and the 3 spacing separators.
1656 if (!menu_model.get() || menu_model->GetItemCount() <= 5) 1660 if (!menu_model.get() || menu_model->GetItemCount() <= 5)
1657 return false; 1661 return false;
1658 1662
1659 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1663 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1660 new LauncherMenuModelAdapter(menu_model.get())), 1664 new LauncherMenuModelAdapter(menu_model.get())),
1661 source, 1665 source,
1662 gfx::Point(), 1666 gfx::Point(),
1663 false, 1667 false,
1664 ui::GetMenuSourceTypeForEvent(event)); 1668 ui::GetMenuSourceTypeForEvent(event));
1665 return true; 1669 return true;
1666 } 1670 }
1667 1671
1668 void LauncherView::ShowContextMenuForView(views::View* source, 1672 void LauncherView::ShowContextMenuForView(views::View* source,
1669 const gfx::Point& point, 1673 const gfx::Point& point,
1670 ui:: MenuSourceType source_type) { 1674 ui:: MenuSourceType source_type) {
1671 int view_index = view_model_->GetIndexOfView(source); 1675 int view_index = view_model_->GetIndexOfView(source);
1676 // TODO(simon.hong81): Create LauncherContextMenu for applist in its
1677 // LauncherItemDelegate.
1672 if (view_index != -1 && 1678 if (view_index != -1 &&
1673 model_->items()[view_index].type == TYPE_APP_LIST) { 1679 model_->items()[view_index].type == TYPE_APP_LIST) {
1674 view_index = -1; 1680 view_index = -1;
1675 } 1681 }
1676 1682
1677 tooltip_->Close(); 1683 tooltip_->Close();
1678 1684
1679 if (view_index == -1) { 1685 if (view_index == -1) {
1680 Shell::GetInstance()->ShowContextMenu(point, source_type); 1686 Shell::GetInstance()->ShowContextMenu(point, source_type);
1681 return; 1687 return;
1682 } 1688 }
1683 scoped_ptr<ui::MenuModel> menu_model(delegate_->CreateContextMenu( 1689 scoped_ptr<ui::MenuModel> menu_model;
1684 model_->items()[view_index], 1690 LauncherItemDelegate* item_delegate = item_manager_->GetLauncherItemDelegate(
1685 source->GetWidget()->GetNativeView()->GetRootWindow())); 1691 model_->items()[view_index].type);
1692 if (item_delegate) {
1693 menu_model.reset(item_delegate->CreateContextMenu(
1694 model_->items()[view_index],
1695 source->GetWidget()->GetNativeView()->GetRootWindow()));
1696 }
1686 if (!menu_model) 1697 if (!menu_model)
1687 return; 1698 return;
1688 base::AutoReset<LauncherID> reseter( 1699 base::AutoReset<LauncherID> reseter(
1689 &context_menu_id_, 1700 &context_menu_id_,
1690 view_index == -1 ? 0 : model_->items()[view_index].id); 1701 view_index == -1 ? 0 : model_->items()[view_index].id);
1691 1702
1692 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1703 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1693 new views::MenuModelAdapter(menu_model.get())), 1704 new views::MenuModelAdapter(menu_model.get())),
1694 source, 1705 source,
1695 point, 1706 point,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1809 if (view_index == -1) 1820 if (view_index == -1)
1810 return NULL; 1821 return NULL;
1811 return &(model_->items()[view_index]); 1822 return &(model_->items()[view_index]);
1812 } 1823 }
1813 1824
1814 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const { 1825 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const {
1815 if (view == GetAppListButtonView() && 1826 if (view == GetAppListButtonView() &&
1816 Shell::GetInstance()->GetAppListWindow()) 1827 Shell::GetInstance()->GetAppListWindow())
1817 return false; 1828 return false;
1818 const LauncherItem* item = LauncherItemForView(view); 1829 const LauncherItem* item = LauncherItemForView(view);
1819 return (!item || delegate_->ShouldShowTooltip(*item)); 1830 if (!item)
1831 return true;
1832 LauncherItemDelegate* item_delegate =
1833 item_manager_->GetLauncherItemDelegate(item->type);
sky 2013/08/27 00:03:47 You don't NULL every where. How come?
simonhong_ 2013/08/27 01:05:29 GetLauncherItemDelegate() don't return NULL. This
1834 if (!item_delegate)
1835 return false;
1836 return item_delegate->ShouldShowTooltip(*item);
1820 } 1837 }
1821 1838
1822 int LauncherView::CalculateShelfDistance(const gfx::Point& coordinate) const { 1839 int LauncherView::CalculateShelfDistance(const gfx::Point& coordinate) const {
1823 ShelfWidget* shelf = RootWindowController::ForLauncher( 1840 ShelfWidget* shelf = RootWindowController::ForLauncher(
1824 GetWidget()->GetNativeView())->shelf(); 1841 GetWidget()->GetNativeView())->shelf();
1825 ash::ShelfAlignment align = shelf->GetAlignment(); 1842 ash::ShelfAlignment align = shelf->GetAlignment();
1826 const gfx::Rect bounds = GetBoundsInScreen(); 1843 const gfx::Rect bounds = GetBoundsInScreen();
1827 int distance = 0; 1844 int distance = 0;
1828 switch (align) { 1845 switch (align) {
1829 case ash::SHELF_ALIGNMENT_BOTTOM: 1846 case ash::SHELF_ALIGNMENT_BOTTOM:
1830 distance = bounds.y() - coordinate.y(); 1847 distance = bounds.y() - coordinate.y();
1831 break; 1848 break;
1832 case ash::SHELF_ALIGNMENT_LEFT: 1849 case ash::SHELF_ALIGNMENT_LEFT:
1833 distance = coordinate.x() - bounds.right(); 1850 distance = coordinate.x() - bounds.right();
1834 break; 1851 break;
1835 case ash::SHELF_ALIGNMENT_RIGHT: 1852 case ash::SHELF_ALIGNMENT_RIGHT:
1836 distance = bounds.x() - coordinate.x(); 1853 distance = bounds.x() - coordinate.x();
1837 break; 1854 break;
1838 case ash::SHELF_ALIGNMENT_TOP: 1855 case ash::SHELF_ALIGNMENT_TOP:
1839 distance = coordinate.y() - bounds.bottom(); 1856 distance = coordinate.y() - bounds.bottom();
1840 break; 1857 break;
1841 } 1858 }
1842 return distance > 0 ? distance : 0; 1859 return distance > 0 ? distance : 0;
1843 } 1860 }
1844 1861
1845 } // namespace internal 1862 } // namespace internal
1846 } // namespace ash 1863 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698