app_list_bubble_presenter.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2021 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. #ifndef ASH_APP_LIST_APP_LIST_BUBBLE_PRESENTER_H_
  5. #define ASH_APP_LIST_APP_LIST_BUBBLE_PRESENTER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "ash/ash_export.h"
  9. #include "ash/public/cpp/app_list/app_list_types.h"
  10. #include "ash/public/cpp/shelf_types.h"
  11. #include "ash/shelf/shelf.h"
  12. #include "ash/shelf/shelf_observer.h"
  13. #include "base/memory/weak_ptr.h"
  14. #include "base/scoped_observation.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. #include "ui/aura/client/focus_change_observer.h"
  17. #include "ui/display/display_observer.h"
  18. #include "ui/views/widget/widget_observer.h"
  19. namespace aura {
  20. class Window;
  21. } // namespace aura
  22. namespace ash {
  23. class AppListBubbleEventFilter;
  24. class AppListBubbleView;
  25. class AppListControllerImpl;
  26. enum class AppListSortOrder;
  27. // Manages the UI for the bubble launcher used in clamshell mode. Handles
  28. // showing and hiding the UI, as well as bounds computations. Only one bubble
  29. // can be visible at a time, across all displays.
  30. class ASH_EXPORT AppListBubblePresenter
  31. : public views::WidgetObserver,
  32. public aura::client::FocusChangeObserver,
  33. public display::DisplayObserver,
  34. public ShelfObserver {
  35. public:
  36. explicit AppListBubblePresenter(AppListControllerImpl* controller);
  37. AppListBubblePresenter(const AppListBubblePresenter&) = delete;
  38. AppListBubblePresenter& operator=(const AppListBubblePresenter&) = delete;
  39. ~AppListBubblePresenter() override;
  40. // Closes the bubble if it is open and prepares for shutdown.
  41. void Shutdown();
  42. // Shows the bubble on the display with `display_id`. The bubble is shown
  43. // asynchronously (after a delay) because the continue suggestions need to be
  44. // refreshed before the bubble views can be created and animated. This delay
  45. // is skipped in unit tests (see TestAppListClient) for convenience. Larger
  46. // tests (e.g. browser_tests) may need to wait for the window to open.
  47. void Show(int64_t display_id);
  48. // Shows or hides the bubble on the display with `display_id`. Returns the
  49. // appropriate ShelfAction to indicate whether the bubble was shown or hidden.
  50. ShelfAction Toggle(int64_t display_id);
  51. // Closes and destroys the bubble.
  52. void Dismiss();
  53. // Returns the bubble window or nullptr if it is not open.
  54. aura::Window* GetWindow() const;
  55. // Returns true if the bubble is showing on any display.
  56. bool IsShowing() const;
  57. // Returns true if the assistant page is showing.
  58. bool IsShowingEmbeddedAssistantUI() const;
  59. // Switches to the assistant page. Requires the bubble to be open.
  60. void ShowEmbeddedAssistantUI();
  61. // Updates the continue section visibility based on user preference.
  62. void UpdateContinueSectionVisibility();
  63. // Handles `AppListController::UpdateAppListWithNewSortingOrder()` for the
  64. // bubble launcher.
  65. void UpdateForNewSortingOrder(
  66. const absl::optional<AppListSortOrder>& new_order,
  67. bool animate,
  68. base::OnceClosure update_position_closure);
  69. // views::WidgetObserver:
  70. void OnWidgetDestroying(views::Widget* widget) override;
  71. // aura::client::FocusChangeObserver:
  72. void OnWindowFocused(aura::Window* gained_focus,
  73. aura::Window* lost_focus) override;
  74. // DisplayObserver:
  75. void OnDisplayMetricsChanged(const display::Display& display,
  76. uint32_t changed_metrics) override;
  77. // ShelfObserver:
  78. void OnShelfShuttingDown() override;
  79. views::Widget* bubble_widget_for_test() { return bubble_widget_; }
  80. AppListBubbleView* bubble_view_for_test() { return bubble_view_; }
  81. private:
  82. // Callback for zero state search update. Builds the bubble widget and views
  83. // on display `display_id` and triggers the show animation.
  84. void OnZeroStateSearchDone(int64_t display_id);
  85. // Callback for AppListBubbleEventFilter, used to notify this of presses
  86. // outside the bubble.
  87. void OnPressOutsideBubble();
  88. // Gets the display id for the display `bubble_widget_` is shown on. Returns
  89. // kInvalidDisplayId if not shown.
  90. int64_t GetDisplayId() const;
  91. // Callback for the hide animation.
  92. void OnHideAnimationEnded();
  93. AppListControllerImpl* const controller_;
  94. // Whether the view is showing or animating to show. Note that the
  95. // `bubble_widget_` may be null during the zero state search called in
  96. // `Show()`.
  97. bool is_target_visibility_show_ = false;
  98. // Owned by native widget.
  99. views::Widget* bubble_widget_ = nullptr;
  100. // Owned by views.
  101. AppListBubbleView* bubble_view_ = nullptr;
  102. // The page to show after the views are constructed.
  103. AppListBubblePage target_page_ = AppListBubblePage::kApps;
  104. // Closes the widget when the user clicks outside of it.
  105. std::unique_ptr<AppListBubbleEventFilter> bubble_event_filter_;
  106. // Observes display configuration changes.
  107. display::ScopedDisplayObserver display_observer_{this};
  108. base::ScopedObservation<Shelf, ShelfObserver> shelf_observer_{this};
  109. base::WeakPtrFactory<AppListBubblePresenter> weak_factory_{this};
  110. };
  111. } // namespace ash
  112. #endif // ASH_APP_LIST_APP_LIST_BUBBLE_PRESENTER_H_