app_list_toast_container_view.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_VIEWS_APP_LIST_TOAST_CONTAINER_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_APP_LIST_TOAST_CONTAINER_VIEW_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/app_list/views/app_list_nudge_controller.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "ui/views/view.h"
  11. namespace views {
  12. class Button;
  13. class LabelButton;
  14. } // namespace views
  15. namespace ash {
  16. class AppListA11yAnnouncer;
  17. class AppListKeyboardController;
  18. class AppListNudgeController;
  19. class AppListToastView;
  20. class AppsGridContextMenu;
  21. class AppListViewDelegate;
  22. enum class AppListSortOrder;
  23. enum class AppListToastType;
  24. // A container view accommodating a toast view with type `ToastType`. See
  25. // `ToastType` for more detail.
  26. class AppListToastContainerView : public views::View {
  27. public:
  28. // The visibility state of the container.
  29. enum class VisibilityState {
  30. // The toast container is showing.
  31. kShown,
  32. // The toast container is shadowed by other views so it is inactive and
  33. // showing in the background.
  34. kShownInBackground,
  35. // The toast container is hidden.
  36. kHidden
  37. };
  38. class Delegate {
  39. public:
  40. virtual ~Delegate() = default;
  41. // Called when the nudge gets removed by the close or dismiss buttons.
  42. virtual void OnNudgeRemoved() = 0;
  43. };
  44. AppListToastContainerView(AppListNudgeController* nudge_controller,
  45. AppListKeyboardController* keyboard_controller,
  46. AppListA11yAnnouncer* a11y_announcer,
  47. AppListViewDelegate* view_delegate,
  48. Delegate* delegate,
  49. bool tablet_mode);
  50. AppListToastContainerView(const AppListToastContainerView&) = delete;
  51. AppListToastContainerView& operator=(const AppListToastContainerView&) =
  52. delete;
  53. ~AppListToastContainerView() override;
  54. // views::View:
  55. bool OnKeyPressed(const ui::KeyEvent& event) override;
  56. // Handle focus passed from the app on column `column` in AppsGridView or
  57. // RecentAppsView.
  58. bool HandleFocus(int column);
  59. // Disables focus when a folder is open.
  60. void DisableFocusForShowingActiveFolder(bool disabled);
  61. // Updates the toast container to show/hide the reorder nudge if needed.
  62. void MaybeUpdateReorderNudgeView();
  63. // Creates a reorder nudge view in the container.
  64. void CreateReorderNudgeView();
  65. // Removes the reorder nudge view if the nudge view is showing.
  66. void RemoveReorderNudgeView();
  67. // Removes the current toast that is showing.
  68. void RemoveCurrentView();
  69. // Updates `visibility_state_` and the states in `nudge_controller_`.
  70. void UpdateVisibilityState(VisibilityState state);
  71. // Called when the app list temporary sort order changes. If `new_order` is
  72. // null, the temporary sort order is cleared.
  73. void OnTemporarySortOrderChanged(
  74. const absl::optional<AppListSortOrder>& new_order);
  75. // Returns the toast's target visibility for the specified sort order. If
  76. // `order` is null, the temporary sort order is cleared.
  77. bool GetVisibilityForSortOrder(
  78. const absl::optional<AppListSortOrder>& order) const;
  79. // Fires an accessibility alert with the text of the sort order toast.
  80. void AnnounceSortOrder(AppListSortOrder new_order);
  81. // Fires an accessibility alert to notify the users that the sort is reverted.
  82. void AnnounceUndoSort();
  83. // This function expects that `toast_view_` exists.
  84. views::LabelButton* GetToastButton();
  85. // Gets the close button if one exists.
  86. views::Button* GetCloseButton();
  87. AppListToastView* toast_view() { return toast_view_; }
  88. AppListToastType current_toast() const { return current_toast_; }
  89. // Whether toast view exists and is not being hidden.
  90. bool IsToastVisible() const;
  91. AppListA11yAnnouncer* a11y_announcer_for_test() { return a11y_announcer_; }
  92. private:
  93. // Called when the `toast_view_`'s reorder undo button is clicked.
  94. void OnReorderUndoButtonClicked();
  95. // Called when the `toast_view_`'s close button is clicked.
  96. void OnReorderCloseButtonClicked();
  97. // Calculates the toast text based on the temporary sorting order.
  98. [[nodiscard]] std::u16string CalculateToastTextFromOrder(
  99. AppListSortOrder order) const;
  100. std::u16string GetA11yTextOnUndoButtonFromOrder(AppListSortOrder order) const;
  101. // Animates the opacity of `toast_view_` to fade out, then calls
  102. // OnFadeOutToastViewComplete().
  103. void FadeOutToastView();
  104. // Called when the fade out animation for the `toast_view_` is finished.
  105. void OnFadeOutToastViewComplete();
  106. AppListA11yAnnouncer* const a11y_announcer_;
  107. // The app list toast container is visually part of the apps grid and should
  108. // provide context menu options generally available in the apps grid.
  109. std::unique_ptr<AppsGridContextMenu> context_menu_;
  110. // Whether the toast container is part of the tablet mode app list UI.
  111. const bool tablet_mode_;
  112. AppListToastView* toast_view_ = nullptr;
  113. AppListViewDelegate* const view_delegate_;
  114. Delegate* const delegate_;
  115. AppListNudgeController* const nudge_controller_;
  116. AppListKeyboardController* const keyboard_controller_;
  117. // Caches the current toast type.
  118. AppListToastType current_toast_;
  119. // Caches the current visibility state which is used to help tracking the
  120. // status of reorder nudge..
  121. VisibilityState visibility_state_ = VisibilityState::kHidden;
  122. // Caches the column of previously focused app. Used when passing focus
  123. // between apps grid view and recent apps.
  124. int focused_app_column_ = 0;
  125. // True if committing the sort order via the close button is in progress.
  126. bool committing_sort_order_ = false;
  127. base::WeakPtrFactory<AppListToastContainerView> weak_factory_{this};
  128. };
  129. } // namespace ash
  130. #endif // ASH_APP_LIST_VIEWS_APP_LIST_TOAST_CONTAINER_VIEW_H_