shelf_test_api.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2019 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.
  4. #include "ash/public/cpp/shelf_test_api.h"
  5. #include "ash/public/cpp/shelf_ui_info.h"
  6. #include "ash/root_window_controller.h"
  7. #include "ash/shelf/home_button.h"
  8. #include "ash/shelf/hotseat_widget.h"
  9. #include "ash/shelf/scrollable_shelf_view.h"
  10. #include "ash/shelf/shelf.h"
  11. #include "ash/shelf/shelf_layout_manager.h"
  12. #include "ash/shelf/shelf_navigation_widget.h"
  13. #include "ash/shelf/shelf_widget.h"
  14. #include "ash/shell.h"
  15. #include "ui/compositor/layer_animator.h"
  16. namespace {
  17. ash::Shelf* GetShelf() {
  18. return ash::Shell::Get()->GetPrimaryRootWindowController()->shelf();
  19. }
  20. ash::ShelfWidget* GetShelfWidget() {
  21. return ash::Shell::GetRootWindowControllerWithDisplayId(
  22. display::Screen::GetScreen()->GetPrimaryDisplay().id())
  23. ->shelf()
  24. ->shelf_widget();
  25. }
  26. ash::HotseatWidget* GetHotseatWidget() {
  27. return GetShelfWidget()->hotseat_widget();
  28. }
  29. ash::ScrollableShelfView* GetScrollableShelfView() {
  30. return GetHotseatWidget()->scrollable_shelf_view();
  31. }
  32. } // namespace
  33. namespace ash {
  34. ShelfTestApi::ShelfTestApi() = default;
  35. ShelfTestApi::~ShelfTestApi() = default;
  36. bool ShelfTestApi::IsVisible() {
  37. return GetShelf()->shelf_layout_manager()->IsVisible();
  38. }
  39. bool ShelfTestApi::IsAlignmentBottomLocked() {
  40. return GetShelf()->alignment() == ShelfAlignment::kBottomLocked;
  41. }
  42. views::View* ShelfTestApi::GetHomeButton() {
  43. return GetShelfWidget()->navigation_widget()->GetHomeButton();
  44. }
  45. ScrollableShelfInfo ShelfTestApi::GetScrollableShelfInfoForState(
  46. const ShelfState& state) {
  47. const auto* scrollable_shelf_view = GetScrollableShelfView();
  48. ScrollableShelfInfo info;
  49. info.main_axis_offset =
  50. scrollable_shelf_view->CalculateMainAxisScrollDistance();
  51. info.page_offset = scrollable_shelf_view->CalculatePageScrollingOffsetInAbs(
  52. scrollable_shelf_view->layout_strategy_);
  53. info.left_arrow_bounds =
  54. scrollable_shelf_view->left_arrow()->GetBoundsInScreen();
  55. info.right_arrow_bounds =
  56. scrollable_shelf_view->right_arrow()->GetBoundsInScreen();
  57. info.is_animating = scrollable_shelf_view->during_scroll_animation_;
  58. info.is_overflow = (scrollable_shelf_view->layout_strategy_ !=
  59. ScrollableShelfView::kNotShowArrowButtons);
  60. info.is_shelf_widget_animating =
  61. GetShelfWidget()->GetLayer()->GetAnimator()->is_animating();
  62. const ShelfView* const shelf_view = scrollable_shelf_view->shelf_view_;
  63. for (int i : shelf_view->visible_views_indices()) {
  64. info.icons_bounds_in_screen.push_back(
  65. shelf_view->view_model()->view_at(i)->GetBoundsInScreen());
  66. }
  67. info.icons_under_animation = shelf_view->IsAnimating();
  68. // Calculates the target offset only when |scroll_distance| is specified.
  69. if (state.scroll_distance != 0.f) {
  70. const float target_offset =
  71. scrollable_shelf_view->CalculateTargetOffsetAfterScroll(
  72. info.main_axis_offset, state.scroll_distance);
  73. info.target_main_axis_offset = target_offset;
  74. }
  75. return info;
  76. }
  77. HotseatInfo ShelfTestApi::GetHotseatInfo() {
  78. HotseatInfo info;
  79. auto* hotseat_widget = GetHotseatWidget();
  80. info.is_animating =
  81. hotseat_widget->GetNativeView()->layer()->GetAnimator()->is_animating();
  82. info.hotseat_state = hotseat_widget->state();
  83. // Hotseat swipe can happen from the bottom center of the display.
  84. display::Display display =
  85. display::Screen::GetScreen()->GetDisplayNearestWindow(
  86. hotseat_widget->GetNativeWindow()->GetRootWindow());
  87. info.swipe_up.swipe_start_location = gfx::Point(
  88. display.bounds().CenterPoint().x(), display.bounds().bottom() - 1);
  89. // The swipe distance is small enough to avoid the window drag from shelf.
  90. const int swipe_distance = hotseat_widget->GetHotseatFullDragAmount() / 2;
  91. gfx::Point swipe_end_location = info.swipe_up.swipe_start_location;
  92. swipe_end_location.set_y(swipe_end_location.y() - swipe_distance);
  93. info.swipe_up.swipe_end_location = swipe_end_location;
  94. info.is_auto_hidden =
  95. GetShelf()->shelf_layout_manager()->is_shelf_auto_hidden();
  96. return info;
  97. }
  98. } // namespace ash