app_list_presenter_impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // Copyright 2016 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_PRESENTER_IMPL_H_
  5. #define ASH_APP_LIST_APP_LIST_PRESENTER_IMPL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "ash/app_list/app_list_metrics.h"
  9. #include "ash/app_list/views/app_list_view.h"
  10. #include "ash/ash_export.h"
  11. #include "ash/public/cpp/pagination/pagination_model_observer.h"
  12. #include "ash/public/cpp/shelf_types.h"
  13. #include "ash/public/cpp/shell_window_ids.h"
  14. #include "ash/shelf/shelf.h"
  15. #include "ash/shelf/shelf_observer.h"
  16. #include "base/callback.h"
  17. #include "base/compiler_specific.h"
  18. #include "base/scoped_observation.h"
  19. #include "base/time/time.h"
  20. #include "ui/aura/client/focus_change_observer.h"
  21. #include "ui/aura/window_observer.h"
  22. #include "ui/compositor/layer_animation_observer.h"
  23. #include "ui/display/display.h"
  24. #include "ui/display/display_observer.h"
  25. #include "ui/display/screen.h"
  26. #include "ui/gfx/geometry/rect.h"
  27. #include "ui/views/widget/widget_observer.h"
  28. namespace ash {
  29. class AppListControllerImpl;
  30. class AppListPresenterEventFilter;
  31. class AppListView;
  32. enum class AppListViewState;
  33. // Manages app list UI. Creates AppListView and schedules showing/hiding
  34. // animation. While the UI is visible, it monitors things such as app list
  35. // activation state and mouse/touch events to dismiss the UI. Updates the shelf
  36. // launcher icon state.
  37. class ASH_EXPORT AppListPresenterImpl
  38. : public PaginationModelObserver,
  39. public aura::client::FocusChangeObserver,
  40. public ui::ImplicitAnimationObserver,
  41. public views::WidgetObserver,
  42. public display::DisplayObserver,
  43. public ShelfObserver {
  44. public:
  45. static constexpr std::array<int, 7> kIdsOfContainersThatWontHideAppList = {
  46. kShellWindowId_AppListContainer,
  47. kShellWindowId_HomeScreenContainer,
  48. kShellWindowId_MenuContainer,
  49. kShellWindowId_PowerMenuContainer,
  50. kShellWindowId_SettingBubbleContainer,
  51. kShellWindowId_ShelfBubbleContainer,
  52. kShellWindowId_ShelfContainer};
  53. // Callback which fills out the passed settings object. Used by
  54. // UpdateYPositionAndOpacityForHomeLauncher so different callers can do
  55. // similar animations with different settings.
  56. using UpdateHomeLauncherAnimationSettingsCallback =
  57. base::RepeatingCallback<void(ui::ScopedLayerAnimationSettings* settings)>;
  58. // |controller| must outlive |this|.
  59. explicit AppListPresenterImpl(AppListControllerImpl* controller);
  60. AppListPresenterImpl(const AppListPresenterImpl&) = delete;
  61. AppListPresenterImpl& operator=(const AppListPresenterImpl&) = delete;
  62. ~AppListPresenterImpl() override;
  63. // Returns app list window or nullptr if it is not visible.
  64. aura::Window* GetWindow() const;
  65. // Returns app list view if one exists, or nullptr otherwise.
  66. AppListView* GetView() { return view_; }
  67. const AppListView* GetView() const { return view_; }
  68. // Show the app list window on the display with the given id. If
  69. // |event_time_stamp| is not 0, it means |Show()| was triggered by one of the
  70. // AppListShowSources: kSearchKey, kShelfButton, or kSwipeFromShelf.
  71. void Show(AppListViewState preferred_state,
  72. int64_t display_id,
  73. base::TimeTicks event_time_stamp,
  74. absl::optional<AppListShowSource> show_source);
  75. // Hide the open app list window. This may leave the view open but hidden.
  76. // If |event_time_stamp| is not 0, it means |Dismiss()| was triggered by
  77. // one AppListShowSource or focusing out side of the launcher.
  78. void Dismiss(base::TimeTicks event_time_stamp);
  79. // Sets the app list view visibility (without updating the app list window
  80. // visibility). No-op if the app list view does not exist.
  81. void SetViewVisibility(bool visible);
  82. // If app list has an opened folder, close it. Returns whether an opened
  83. // folder was closed.
  84. bool HandleCloseOpenFolder();
  85. // Show the app list if it is visible, hide it if it is hidden. If
  86. // |event_time_stamp| is not 0, it means |ToggleAppList()| was triggered by
  87. // one of the AppListShowSources: kSearchKey or kShelfButton.
  88. ShelfAction ToggleAppList(int64_t display_id,
  89. AppListShowSource show_source,
  90. base::TimeTicks event_time_stamp);
  91. // Handles `AppListController::UpdateAppListWithNewSortingOrder()` for the
  92. // app list presenter.
  93. void UpdateForNewSortingOrder(
  94. const absl::optional<AppListSortOrder>& new_order,
  95. bool animate,
  96. base::OnceClosure update_position_closure);
  97. // Updates the continue section visibility based on user preference.
  98. void UpdateContinueSectionVisibility();
  99. // Returns current visibility of the app list. Deprecated, use
  100. // |IsAtLeastPartiallyVisible| instead.
  101. bool IsVisibleDeprecated() const;
  102. // Returns whether the app list is visible. This will only return false if
  103. // the app list is entirely occluded.
  104. bool IsAtLeastPartiallyVisible() const;
  105. // Returns target visibility. This may differ from IsVisible() if a visibility
  106. // transition is in progress.
  107. bool GetTargetVisibility() const;
  108. // Updates y position and opacity of app list.
  109. void UpdateYPositionAndOpacity(float y_position_in_screen,
  110. float background_opacity);
  111. // Ends the drag of app list from shelf.
  112. void EndDragFromShelf(AppListViewState app_list_state);
  113. // Passes data from a Scroll event from the shelf to the
  114. // AppListView.
  115. void ProcessScrollOffset(const gfx::Point& location,
  116. const gfx::Vector2d& scroll_offset_vector);
  117. // Passes data from a MouseWheelEvent event from the shelf to the
  118. // AppListView.
  119. void ProcessMouseWheelOffset(const gfx::Point& location,
  120. const gfx::Vector2d& scroll_offset_vector);
  121. // Scales the home launcher view maintaining the view center point, and
  122. // updates its opacity. If |callback| is non-null, the update should be
  123. // animated, and the |callback| should be called with the animation settings.
  124. // |transition| - The tablet mode animation type. Used to report animation
  125. // metrics if the home launcher change is animated. Should be set only if
  126. // |callback| is non-null. If not set, the animation smoothness metrics will
  127. // not be reported.
  128. void UpdateScaleAndOpacityForHomeLauncher(
  129. float scale,
  130. float opacity,
  131. absl::optional<TabletModeAnimationTransition> transition,
  132. UpdateHomeLauncherAnimationSettingsCallback callback);
  133. // Shows or hides the Assistant page.
  134. // |show| is true to show and false to hide.
  135. void ShowEmbeddedAssistantUI(bool show);
  136. // Returns current visibility of the Assistant page.
  137. bool IsShowingEmbeddedAssistantUI() const;
  138. // Called when tablet mode starts and ends.
  139. void OnTabletModeChanged(bool started);
  140. private:
  141. // Sets the app list view and attempts to show it.
  142. void SetView(AppListView* view);
  143. // Forgets the view.
  144. void ResetView();
  145. // Returns the id of the display containing the app list, if visible. If not
  146. // visible returns kInvalidDisplayId.
  147. int64_t GetDisplayId() const;
  148. void OnVisibilityChanged(bool visible, int64_t display_id);
  149. void OnVisibilityWillChange(bool visible, int64_t display_id);
  150. // Called when the widget is hidden or destroyed.
  151. void OnClosed();
  152. // aura::client::FocusChangeObserver overrides:
  153. void OnWindowFocused(aura::Window* gained_focus,
  154. aura::Window* lost_focus) override;
  155. // ui::ImplicitAnimationObserver overrides:
  156. void OnImplicitAnimationsCompleted() override;
  157. // views::WidgetObserver overrides:
  158. void OnWidgetDestroying(views::Widget* widget) override;
  159. void OnWidgetDestroyed(views::Widget* widget) override;
  160. void OnWidgetVisibilityChanged(views::Widget* widget, bool visible) override;
  161. // PaginationModelObserver overrides:
  162. void TotalPagesChanged(int previous_page_count, int new_page_count) override;
  163. void SelectedPageChanged(int old_selected, int new_selected) override;
  164. // DisplayObserver overrides:
  165. void OnDisplayMetricsChanged(const display::Display& display,
  166. uint32_t changed_metrics) override;
  167. // ShelfObserver overrides:
  168. void OnShelfShuttingDown() override;
  169. void OnBackgroundTypeChanged(ShelfBackgroundType background_type,
  170. AnimationChangeType change_type) override;
  171. // Registers a callback that is run when the next frame successfully makes it
  172. // to the screen.
  173. void RequestPresentationTime(int64_t display_id,
  174. base::TimeTicks event_time_stamp);
  175. // Snaps the app list window bounds to fit the screen size. (See
  176. // https://crbug.com/884889).
  177. void SnapAppListBoundsToDisplayEdge();
  178. // Called when the reorder animation completes.
  179. void OnAppListReorderAnimationDone();
  180. // Owns |this|.
  181. AppListControllerImpl* const controller_;
  182. // Closes the app list when the user clicks outside its bounds.
  183. std::unique_ptr<AppListPresenterEventFilter> event_filter_;
  184. // An observer that notifies AppListView when the display has changed.
  185. display::ScopedDisplayObserver display_observer_{this};
  186. // An observer that notifies AppListView when the shelf state has changed.
  187. base::ScopedObservation<Shelf, ShelfObserver> shelf_observer_{this};
  188. // The target visibility of the AppListView, true if the target visibility is
  189. // shown.
  190. bool is_target_visibility_show_ = false;
  191. // The AppListView this class manages, owned by its widget.
  192. AppListView* view_ = nullptr;
  193. // The current page of the AppsGridView of |view_|. This is stored outside of
  194. // the view's PaginationModel, so that it persists when the view is destroyed.
  195. int current_apps_page_ = -1;
  196. // Cached bounds of |view_| for snapping back animation after over-scroll.
  197. gfx::Rect view_bounds_;
  198. // Data we need to store for metrics.
  199. absl::optional<base::Time> last_open_time_;
  200. absl::optional<AppListShowSource> last_open_source_;
  201. // Whether the presenter is currently changing app list view state to shown.
  202. // TODO(https://crbug.com/1307871): Remove this when the linked crash gets
  203. // diagnosed.
  204. bool showing_app_list_ = false;
  205. base::WeakPtrFactory<AppListPresenterImpl> weak_ptr_factory_{this};
  206. };
  207. } // namespace ash
  208. #endif // ASH_APP_LIST_APP_LIST_PRESENTER_IMPL_H_