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

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: Rebased 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"
17 #include "ash/launcher/launcher_model.h" 18 #include "ash/launcher/launcher_model.h"
18 #include "ash/launcher/launcher_tooltip_manager.h" 19 #include "ash/launcher/launcher_tooltip_manager.h"
19 #include "ash/launcher/overflow_bubble.h" 20 #include "ash/launcher/overflow_bubble.h"
20 #include "ash/launcher/overflow_button.h" 21 #include "ash/launcher/overflow_button.h"
21 #include "ash/launcher/tabbed_launcher_button.h" 22 #include "ash/launcher/tabbed_launcher_button.h"
22 #include "ash/root_window_controller.h" 23 #include "ash/root_window_controller.h"
23 #include "ash/scoped_target_root_window.h" 24 #include "ash/scoped_target_root_window.h"
24 #include "ash/shelf/shelf_layout_manager.h" 25 #include "ash/shelf/shelf_layout_manager.h"
25 #include "ash/shelf/shelf_widget.h" 26 #include "ash/shelf/shelf_widget.h"
26 #include "ash/shell_delegate.h" 27 #include "ash/shell_delegate.h"
(...skipping 940 matching lines...) Expand 10 before | Expand all | Expand 10 after
967 view, new FadeInAnimationDelegate(view), true); 968 view, new FadeInAnimationDelegate(view), true);
968 } 969 }
969 970
970 void LauncherView::PrepareForDrag(Pointer pointer, 971 void LauncherView::PrepareForDrag(Pointer pointer,
971 const ui::LocatedEvent& event) { 972 const ui::LocatedEvent& event) {
972 DCHECK(!dragging()); 973 DCHECK(!dragging());
973 DCHECK(drag_view_); 974 DCHECK(drag_view_);
974 drag_pointer_ = pointer; 975 drag_pointer_ = pointer;
975 start_drag_index_ = view_model_->GetIndexOfView(drag_view_); 976 start_drag_index_ = view_model_->GetIndexOfView(drag_view_);
976 977
977 // If the item is no longer draggable, bail out. 978 if (start_drag_index_== -1) {
978 if (start_drag_index_ == -1 ||
979 !delegate_->IsDraggable(model_->items()[start_drag_index_])) {
980 CancelDrag(-1); 979 CancelDrag(-1);
981 return; 980 return;
982 } 981 }
982
983 // If the item is no longer draggable, bail out.
984 LauncherItemDelegate* item_delegate = delegate_->GetLauncherItemDelegate(
985 model_->items()[start_drag_index_]);
986 if (!item_delegate ||
987 !item_delegate->IsDraggable(model_->items()[start_drag_index_])) {
988 CancelDrag(-1);
989 return;
990 }
983 991
984 // Move the view to the front so that it appears on top of other views. 992 // Move the view to the front so that it appears on top of other views.
985 ReorderChildView(drag_view_, -1); 993 ReorderChildView(drag_view_, -1);
986 bounds_animator_->StopAnimatingView(drag_view_); 994 bounds_animator_->StopAnimatingView(drag_view_);
987 } 995 }
988 996
989 void LauncherView::ContinueDrag(const ui::LocatedEvent& event) { 997 void LauncherView::ContinueDrag(const ui::LocatedEvent& event) {
990 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager(); 998 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
991 999
992 // TODO: I don't think this works correctly with RTL. 1000 // TODO: I don't think this works correctly with RTL.
993 gfx::Point drag_point(event.location()); 1001 gfx::Point drag_point(event.location());
994 views::View::ConvertPointToTarget(drag_view_, this, &drag_point); 1002 views::View::ConvertPointToTarget(drag_view_, this, &drag_point);
995 int current_index = view_model_->GetIndexOfView(drag_view_); 1003 int current_index = view_model_->GetIndexOfView(drag_view_);
996 DCHECK_NE(-1, current_index); 1004 DCHECK_NE(-1, current_index);
997 1005
998 // If the item is no longer draggable, bail out. 1006 // If the item is no longer draggable, bail out.
999 if (current_index == -1 || 1007 LauncherItemDelegate* item_delegate = delegate_->GetLauncherItemDelegate(
1000 !delegate_->IsDraggable(model_->items()[current_index])) { 1008 model_->items()[current_index]);
1009 if (!item_delegate ||
1010 !item_delegate->IsDraggable(model_->items()[current_index])) {
1001 CancelDrag(-1); 1011 CancelDrag(-1);
1002 return; 1012 return;
1003 } 1013 }
1004 1014
1005 // Constrain the location to the range of valid indices for the type. 1015 // Constrain the location to the range of valid indices for the type.
1006 std::pair<int, int> indices(GetDragRange(current_index)); 1016 std::pair<int, int> indices(GetDragRange(current_index));
1007 int first_drag_index = indices.first; 1017 int first_drag_index = indices.first;
1008 int last_drag_index = indices.second; 1018 int last_drag_index = indices.second;
1009 // If the last index isn't valid, we're overflowing. Constrain to the app list 1019 // If the last index isn't valid, we're overflowing. Constrain to the app list
1010 // (which is the last visible item). 1020 // (which is the last visible item).
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
1387 } 1397 }
1388 1398
1389 void LauncherView::PointerPressedOnButton(views::View* view, 1399 void LauncherView::PointerPressedOnButton(views::View* view,
1390 Pointer pointer, 1400 Pointer pointer,
1391 const ui::LocatedEvent& event) { 1401 const ui::LocatedEvent& event) {
1392 if (drag_view_) 1402 if (drag_view_)
1393 return; 1403 return;
1394 1404
1395 tooltip_->Close(); 1405 tooltip_->Close();
1396 int index = view_model_->GetIndexOfView(view); 1406 int index = view_model_->GetIndexOfView(view);
1397 if (index == -1 || 1407 if (index == -1)
1398 view_model_->view_size() <= 1 || 1408 return;
1399 !delegate_->IsDraggable(model_->items()[index])) 1409
1410 LauncherItemDelegate* item_delegate =
1411 delegate_->GetLauncherItemDelegate(model_->items()[index]);
1412 if (view_model_->view_size() <= 1 ||
1413 !item_delegate ||
1414 !item_delegate->IsDraggable(model_->items()[index]))
1400 return; // View is being deleted or not draggable, ignore request. 1415 return; // View is being deleted or not draggable, ignore request.
1401 1416
1402 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager(); 1417 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
1403 1418
1404 drag_view_ = view; 1419 drag_view_ = view;
1405 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y()); 1420 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y());
1406 } 1421 }
1407 1422
1408 void LauncherView::PointerDraggedOnButton(views::View* view, 1423 void LauncherView::PointerDraggedOnButton(views::View* view,
1409 Pointer pointer, 1424 Pointer pointer,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1457 if (!tooltip_->IsVisible()) 1472 if (!tooltip_->IsVisible())
1458 tooltip_->StopTimer(); 1473 tooltip_->StopTimer();
1459 } 1474 }
1460 1475
1461 base::string16 LauncherView::GetAccessibleName(const views::View* view) { 1476 base::string16 LauncherView::GetAccessibleName(const views::View* view) {
1462 int view_index = view_model_->GetIndexOfView(view); 1477 int view_index = view_model_->GetIndexOfView(view);
1463 // May be -1 while in the process of animating closed. 1478 // May be -1 while in the process of animating closed.
1464 if (view_index == -1) 1479 if (view_index == -1)
1465 return base::string16(); 1480 return base::string16();
1466 1481
1467 switch (model_->items()[view_index].type) { 1482 LauncherItemDelegate* item_delegate =
1468 case TYPE_TABBED: 1483 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1469 case TYPE_APP_PANEL: 1484 DCHECK(item_delegate);
1470 case TYPE_APP_SHORTCUT: 1485 return item_delegate->GetTitle(model_->items()[view_index]);
1471 case TYPE_WINDOWED_APP:
1472 case TYPE_PLATFORM_APP:
1473 case TYPE_BROWSER_SHORTCUT:
1474 return delegate_->GetTitle(model_->items()[view_index]);
1475
1476 case TYPE_APP_LIST:
1477 return model_->status() == LauncherModel::STATUS_LOADING ?
1478 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_SYNCING_TITLE) :
1479 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE);
1480 }
1481 return base::string16();
1482 } 1486 }
1483 1487
1484 void LauncherView::ButtonPressed(views::Button* sender, 1488 void LauncherView::ButtonPressed(views::Button* sender,
1485 const ui::Event& event) { 1489 const ui::Event& event) {
1486 // Do not handle mouse release during drag. 1490 // Do not handle mouse release during drag.
1487 if (dragging()) 1491 if (dragging())
1488 return; 1492 return;
1489 1493
1490 tooltip_->Close(); 1494 tooltip_->Close();
1491 1495
(...skipping 23 matching lines...) Expand all
1515 } 1519 }
1516 1520
1517 // Collect usage statistics before we decide what to do with the click. 1521 // Collect usage statistics before we decide what to do with the click.
1518 switch (model_->items()[view_index].type) { 1522 switch (model_->items()[view_index].type) {
1519 case TYPE_APP_SHORTCUT: 1523 case TYPE_APP_SHORTCUT:
1520 case TYPE_WINDOWED_APP: 1524 case TYPE_WINDOWED_APP:
1521 case TYPE_PLATFORM_APP: 1525 case TYPE_PLATFORM_APP:
1522 case TYPE_BROWSER_SHORTCUT: 1526 case TYPE_BROWSER_SHORTCUT:
1523 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1527 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1524 UMA_LAUNCHER_CLICK_ON_APP); 1528 UMA_LAUNCHER_CLICK_ON_APP);
1525 // Fallthrough
1526 case TYPE_TABBED:
1527 case TYPE_APP_PANEL:
1528 delegate_->ItemSelected(model_->items()[view_index], event);
1529 // Don't show the menu when the user creates a new browser using ctrl
1530 // click.
1531 if (model_->items()[view_index].type != TYPE_BROWSER_SHORTCUT ||
1532 !(event.flags() & ui::EF_CONTROL_DOWN))
1533 ShowListMenuForView(model_->items()[view_index], sender, event);
1534 break; 1529 break;
1535 1530
1536 case TYPE_APP_LIST: 1531 case TYPE_APP_LIST:
1537 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1532 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1538 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON); 1533 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1539 Shell::GetInstance()->ToggleAppList(GetWidget()->GetNativeView()); 1534 break;
1535
1536 case TYPE_TABBED:
1537 case TYPE_APP_PANEL:
1540 break; 1538 break;
1541 } 1539 }
1542 } 1540 }
1541 LauncherItemDelegate* item_delegate =
1542 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1543 DCHECK(item_delegate);
1544 item_delegate->ItemSelected(model_->items()[view_index], event);
1545
1546 ShowListMenuForView(model_->items()[view_index], sender, event);
1543 } 1547 }
1544 1548
1545 bool LauncherView::ShowListMenuForView(const LauncherItem& item, 1549 bool LauncherView::ShowListMenuForView(const LauncherItem& item,
1546 views::View* source, 1550 views::View* source,
1547 const ui::Event& event) { 1551 const ui::Event& event) {
1548 scoped_ptr<ash::LauncherMenuModel> menu_model; 1552 scoped_ptr<ash::LauncherMenuModel> menu_model;
1549 menu_model.reset(delegate_->CreateApplicationMenu(item, event.flags())); 1553 LauncherItemDelegate* item_delegate =
1554 delegate_->GetLauncherItemDelegate(item);
1555 if (item_delegate)
1556 menu_model.reset(item_delegate->CreateApplicationMenu(item, event.flags()));
1557 else
1558 menu_model.reset();
1550 1559
1551 // Make sure we have a menu and it has at least two items in addition to the 1560 // Make sure we have a menu and it has at least two items in addition to the
1552 // application title and the 3 spacing separators. 1561 // application title and the 3 spacing separators.
1553 if (!menu_model.get() || menu_model->GetItemCount() <= 5) 1562 if (!menu_model.get() || menu_model->GetItemCount() <= 5)
1554 return false; 1563 return false;
1555 1564
1556 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1565 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1557 new LauncherMenuModelAdapter(menu_model.get())), 1566 new LauncherMenuModelAdapter(menu_model.get())),
1558 source, 1567 source,
1559 gfx::Point(), 1568 gfx::Point(),
1560 false, 1569 false,
1561 ui::GetMenuSourceTypeForEvent(event)); 1570 ui::GetMenuSourceTypeForEvent(event));
1562 return true; 1571 return true;
1563 } 1572 }
1564 1573
1565 void LauncherView::ShowContextMenuForView(views::View* source, 1574 void LauncherView::ShowContextMenuForView(views::View* source,
1566 const gfx::Point& point, 1575 const gfx::Point& point,
1567 ui:: MenuSourceType source_type) { 1576 ui:: MenuSourceType source_type) {
1568 int view_index = view_model_->GetIndexOfView(source); 1577 int view_index = view_model_->GetIndexOfView(source);
1578 // TODO(simon.hong81): Create LauncherContextMenu for applist in its
1579 // LauncherItemDelegate.
1569 if (view_index != -1 && 1580 if (view_index != -1 &&
1570 model_->items()[view_index].type == TYPE_APP_LIST) { 1581 model_->items()[view_index].type == TYPE_APP_LIST) {
1571 view_index = -1; 1582 view_index = -1;
1572 } 1583 }
1573 1584
1574 tooltip_->Close(); 1585 tooltip_->Close();
1575 1586
1576 if (view_index == -1) { 1587 if (view_index == -1) {
1577 Shell::GetInstance()->ShowContextMenu(point, source_type); 1588 Shell::GetInstance()->ShowContextMenu(point, source_type);
1578 return; 1589 return;
1579 } 1590 }
1580 scoped_ptr<ui::MenuModel> menu_model(delegate_->CreateContextMenu( 1591 scoped_ptr<ui::MenuModel> menu_model;
1581 model_->items()[view_index], 1592 LauncherItemDelegate* item_delegate =
1582 source->GetWidget()->GetNativeView()->GetRootWindow())); 1593 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1594 if (item_delegate) {
1595 menu_model.reset(item_delegate->CreateContextMenu(
1596 model_->items()[view_index],
1597 source->GetWidget()->GetNativeView()->GetRootWindow()));
1598 }
1583 if (!menu_model) 1599 if (!menu_model)
1584 return; 1600 return;
1585 base::AutoReset<LauncherID> reseter( 1601 base::AutoReset<LauncherID> reseter(
1586 &context_menu_id_, 1602 &context_menu_id_,
1587 view_index == -1 ? 0 : model_->items()[view_index].id); 1603 view_index == -1 ? 0 : model_->items()[view_index].id);
1588 1604
1589 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1605 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1590 new views::MenuModelAdapter(menu_model.get())), 1606 new views::MenuModelAdapter(menu_model.get())),
1591 source, 1607 source,
1592 point, 1608 point,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1706 if (view_index == -1) 1722 if (view_index == -1)
1707 return NULL; 1723 return NULL;
1708 return &(model_->items()[view_index]); 1724 return &(model_->items()[view_index]);
1709 } 1725 }
1710 1726
1711 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const { 1727 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const {
1712 if (view == GetAppListButtonView() && 1728 if (view == GetAppListButtonView() &&
1713 Shell::GetInstance()->GetAppListWindow()) 1729 Shell::GetInstance()->GetAppListWindow())
1714 return false; 1730 return false;
1715 const LauncherItem* item = LauncherItemForView(view); 1731 const LauncherItem* item = LauncherItemForView(view);
1716 return (!item || delegate_->ShouldShowTooltip(*item)); 1732 if (!item)
1733 return true;
1734 LauncherItemDelegate* item_delegate =
1735 delegate_->GetLauncherItemDelegate(*item);
1736 if (!item_delegate)
1737 return false;
1738 return item_delegate->ShouldShowTooltip(*item);
1717 } 1739 }
1718 1740
1719 } // namespace internal 1741 } // namespace internal
1720 } // namespace ash 1742 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698