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

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: fix bugs 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 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1382 } 1392 }
1383 1393
1384 void LauncherView::PointerPressedOnButton(views::View* view, 1394 void LauncherView::PointerPressedOnButton(views::View* view,
1385 Pointer pointer, 1395 Pointer pointer,
1386 const ui::LocatedEvent& event) { 1396 const ui::LocatedEvent& event) {
1387 if (drag_view_) 1397 if (drag_view_)
1388 return; 1398 return;
1389 1399
1390 tooltip_->Close(); 1400 tooltip_->Close();
1391 int index = view_model_->GetIndexOfView(view); 1401 int index = view_model_->GetIndexOfView(view);
1392 if (index == -1 || 1402 if (index == -1)
1393 view_model_->view_size() <= 1 || 1403 return;
1394 !delegate_->IsDraggable(model_->items()[index])) 1404
1405 LauncherItemDelegate* item_delegate =
1406 delegate_->GetLauncherItemDelegate(model_->items()[index]);
1407 if (view_model_->view_size() <= 1 ||
1408 !item_delegate ||
1409 !item_delegate->IsDraggable(model_->items()[index]))
1395 return; // View is being deleted or not draggable, ignore request. 1410 return; // View is being deleted or not draggable, ignore request.
1396 1411
1397 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager(); 1412 ShelfLayoutManager* shelf = tooltip_->shelf_layout_manager();
1398 1413
1399 drag_view_ = view; 1414 drag_view_ = view;
1400 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y()); 1415 drag_offset_ = shelf->PrimaryAxisValue(event.x(), event.y());
1401 } 1416 }
1402 1417
1403 void LauncherView::PointerDraggedOnButton(views::View* view, 1418 void LauncherView::PointerDraggedOnButton(views::View* view,
1404 Pointer pointer, 1419 Pointer pointer,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
1452 if (!tooltip_->IsVisible()) 1467 if (!tooltip_->IsVisible())
1453 tooltip_->StopTimer(); 1468 tooltip_->StopTimer();
1454 } 1469 }
1455 1470
1456 base::string16 LauncherView::GetAccessibleName(const views::View* view) { 1471 base::string16 LauncherView::GetAccessibleName(const views::View* view) {
1457 int view_index = view_model_->GetIndexOfView(view); 1472 int view_index = view_model_->GetIndexOfView(view);
1458 // May be -1 while in the process of animating closed. 1473 // May be -1 while in the process of animating closed.
1459 if (view_index == -1) 1474 if (view_index == -1)
1460 return base::string16(); 1475 return base::string16();
1461 1476
1462 switch (model_->items()[view_index].type) { 1477 LauncherItemDelegate* item_delegate =
1463 case TYPE_TABBED: 1478 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1464 case TYPE_APP_PANEL: 1479 DCHECK(item_delegate);
1465 case TYPE_APP_SHORTCUT: 1480 return item_delegate->GetTitle(model_->items()[view_index]);
1466 case TYPE_WINDOWED_APP:
1467 case TYPE_PLATFORM_APP:
1468 case TYPE_BROWSER_SHORTCUT:
1469 return delegate_->GetTitle(model_->items()[view_index]);
1470
1471 case TYPE_APP_LIST:
1472 return model_->status() == LauncherModel::STATUS_LOADING ?
1473 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_SYNCING_TITLE) :
1474 l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE);
1475 }
1476 return base::string16();
1477 } 1481 }
1478 1482
1479 void LauncherView::ButtonPressed(views::Button* sender, 1483 void LauncherView::ButtonPressed(views::Button* sender,
1480 const ui::Event& event) { 1484 const ui::Event& event) {
1481 // Do not handle mouse release during drag. 1485 // Do not handle mouse release during drag.
1482 if (dragging()) 1486 if (dragging())
1483 return; 1487 return;
1484 1488
1485 tooltip_->Close(); 1489 tooltip_->Close();
1486 1490
(...skipping 23 matching lines...) Expand all
1510 } 1514 }
1511 1515
1512 // Collect usage statistics before we decide what to do with the click. 1516 // Collect usage statistics before we decide what to do with the click.
1513 switch (model_->items()[view_index].type) { 1517 switch (model_->items()[view_index].type) {
1514 case TYPE_APP_SHORTCUT: 1518 case TYPE_APP_SHORTCUT:
1515 case TYPE_WINDOWED_APP: 1519 case TYPE_WINDOWED_APP:
1516 case TYPE_PLATFORM_APP: 1520 case TYPE_PLATFORM_APP:
1517 case TYPE_BROWSER_SHORTCUT: 1521 case TYPE_BROWSER_SHORTCUT:
1518 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1522 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1519 UMA_LAUNCHER_CLICK_ON_APP); 1523 UMA_LAUNCHER_CLICK_ON_APP);
1520 // Fallthrough
1521 case TYPE_TABBED:
1522 case TYPE_APP_PANEL:
1523 delegate_->ItemSelected(model_->items()[view_index], event);
1524 break; 1524 break;
1525 1525
1526 case TYPE_APP_LIST: 1526 case TYPE_APP_LIST:
1527 Shell::GetInstance()->delegate()->RecordUserMetricsAction( 1527 Shell::GetInstance()->delegate()->RecordUserMetricsAction(
1528 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON); 1528 UMA_LAUNCHER_CLICK_ON_APPLIST_BUTTON);
1529 Shell::GetInstance()->ToggleAppList(GetWidget()->GetNativeView()); 1529 break;
1530
1531 case TYPE_TABBED:
1532 case TYPE_APP_PANEL:
1530 break; 1533 break;
1531 } 1534 }
1532 } 1535 }
1536 LauncherItemDelegate* item_delegate =
1537 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1538 DCHECK(item_delegate);
1539 item_delegate->ItemSelected(model_->items()[view_index], event);
1533 1540
1534 if (model_->items()[view_index].type != TYPE_APP_LIST) 1541 ShowListMenuForView(model_->items()[view_index], sender, event);
1535 ShowListMenuForView(model_->items()[view_index], sender, event);
1536 } 1542 }
1537 1543
1538 bool LauncherView::ShowListMenuForView(const LauncherItem& item, 1544 bool LauncherView::ShowListMenuForView(const LauncherItem& item,
1539 views::View* source, 1545 views::View* source,
1540 const ui::Event& event) { 1546 const ui::Event& event) {
1541 scoped_ptr<ash::LauncherMenuModel> menu_model; 1547 scoped_ptr<ash::LauncherMenuModel> menu_model;
1542 menu_model.reset(delegate_->CreateApplicationMenu(item, event.flags())); 1548 LauncherItemDelegate* item_delegate =
1549 delegate_->GetLauncherItemDelegate(item);
1550 if (item_delegate)
1551 menu_model.reset(item_delegate->CreateApplicationMenu(item, event.flags()));
1552 else
1553 menu_model.reset();
1543 1554
1544 // Make sure we have a menu and it has at least two items in addition to the 1555 // Make sure we have a menu and it has at least two items in addition to the
1545 // application title and the 3 spacing separators. 1556 // application title and the 3 spacing separators.
1546 if (!menu_model.get() || menu_model->GetItemCount() <= 5) 1557 if (!menu_model.get() || menu_model->GetItemCount() <= 5)
1547 return false; 1558 return false;
1548 1559
1549 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1560 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1550 new LauncherMenuModelAdapter(menu_model.get())), 1561 new LauncherMenuModelAdapter(menu_model.get())),
1551 source, 1562 source,
1552 gfx::Point(), 1563 gfx::Point(),
1553 false, 1564 false,
1554 ui::GetMenuSourceTypeForEvent(event)); 1565 ui::GetMenuSourceTypeForEvent(event));
1555 return true; 1566 return true;
1556 } 1567 }
1557 1568
1558 void LauncherView::ShowContextMenuForView(views::View* source, 1569 void LauncherView::ShowContextMenuForView(views::View* source,
1559 const gfx::Point& point, 1570 const gfx::Point& point,
1560 ui:: MenuSourceType source_type) { 1571 ui:: MenuSourceType source_type) {
1561 int view_index = view_model_->GetIndexOfView(source); 1572 int view_index = view_model_->GetIndexOfView(source);
1573 // TODO: Create LauncherContextMenu for applist in its LauncherItemDelegate.
1562 if (view_index != -1 && 1574 if (view_index != -1 &&
1563 model_->items()[view_index].type == TYPE_APP_LIST) { 1575 model_->items()[view_index].type == TYPE_APP_LIST) {
1564 view_index = -1; 1576 view_index = -1;
1565 } 1577 }
1566 1578
1567 tooltip_->Close(); 1579 tooltip_->Close();
1568 1580
1569 if (view_index == -1) { 1581 if (view_index == -1) {
1570 Shell::GetInstance()->ShowContextMenu(point, source_type); 1582 Shell::GetInstance()->ShowContextMenu(point, source_type);
1571 return; 1583 return;
1572 } 1584 }
1573 scoped_ptr<ui::MenuModel> menu_model(delegate_->CreateContextMenu( 1585 scoped_ptr<ui::MenuModel> menu_model;
1574 model_->items()[view_index], 1586 LauncherItemDelegate* item_delegate =
1575 source->GetWidget()->GetNativeView()->GetRootWindow())); 1587 delegate_->GetLauncherItemDelegate(model_->items()[view_index]);
1588 if (item_delegate) {
1589 menu_model.reset(item_delegate->CreateContextMenu(
1590 model_->items()[view_index],
1591 source->GetWidget()->GetNativeView()->GetRootWindow()));
1592 }
1576 if (!menu_model) 1593 if (!menu_model)
1577 return; 1594 return;
1578 base::AutoReset<LauncherID> reseter( 1595 base::AutoReset<LauncherID> reseter(
1579 &context_menu_id_, 1596 &context_menu_id_,
1580 view_index == -1 ? 0 : model_->items()[view_index].id); 1597 view_index == -1 ? 0 : model_->items()[view_index].id);
1581 1598
1582 ShowMenu(scoped_ptr<views::MenuModelAdapter>( 1599 ShowMenu(scoped_ptr<views::MenuModelAdapter>(
1583 new views::MenuModelAdapter(menu_model.get())), 1600 new views::MenuModelAdapter(menu_model.get())),
1584 source, 1601 source,
1585 point, 1602 point,
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1699 if (view_index == -1) 1716 if (view_index == -1)
1700 return NULL; 1717 return NULL;
1701 return &(model_->items()[view_index]); 1718 return &(model_->items()[view_index]);
1702 } 1719 }
1703 1720
1704 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const { 1721 bool LauncherView::ShouldShowTooltipForView(const views::View* view) const {
1705 if (view == GetAppListButtonView() && 1722 if (view == GetAppListButtonView() &&
1706 Shell::GetInstance()->GetAppListWindow()) 1723 Shell::GetInstance()->GetAppListWindow())
1707 return false; 1724 return false;
1708 const LauncherItem* item = LauncherItemForView(view); 1725 const LauncherItem* item = LauncherItemForView(view);
1709 return (!item || delegate_->ShouldShowTooltip(*item)); 1726 if (!item)
1727 return true;
1728 LauncherItemDelegate* item_delegate =
1729 delegate_->GetLauncherItemDelegate(*item);
1730 if (!item_delegate)
1731 return false;
1732 return item_delegate->ShouldShowTooltip(*item);
1710 } 1733 }
1711 1734
1712 } // namespace internal 1735 } // namespace internal
1713 } // namespace ash 1736 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698