contents_view.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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
  3. // found in the LICENSE file.
  4. #ifndef ASH_APP_LIST_VIEWS_CONTENTS_VIEW_H_
  5. #define ASH_APP_LIST_VIEWS_CONTENTS_VIEW_H_
  6. #include <map>
  7. #include <memory>
  8. #include <utility>
  9. #include <vector>
  10. #include "ash/app_list/model/app_list_model.h"
  11. #include "ash/app_list/model/search/search_model.h"
  12. #include "ash/ash_export.h"
  13. #include "ash/public/cpp/pagination/pagination_model.h"
  14. #include "ash/public/cpp/pagination/pagination_model_observer.h"
  15. #include "base/compiler_specific.h"
  16. #include "base/observer_list_types.h"
  17. #include "ui/views/view.h"
  18. #include "ui/views/view_model.h"
  19. namespace gfx {
  20. class Rect;
  21. }
  22. namespace ui {
  23. class Layer;
  24. class ScopedLayerAnimationSettings;
  25. } // namespace ui
  26. namespace ash {
  27. class AppListPage;
  28. class AppListView;
  29. class ApplicationDragAndDropHost;
  30. class AppListMainView;
  31. class AppsContainerView;
  32. class AssistantPageView;
  33. class ExpandArrowView;
  34. class SearchBoxView;
  35. class SearchResultPageView;
  36. // A view to manage launcher pages within the Launcher (eg. start page, apps
  37. // grid view, search results). There can be any number of launcher pages, only
  38. // one of which can be active at a given time. ContentsView provides the user
  39. // interface for switching between launcher pages, and animates the transition
  40. // between them.
  41. class ASH_EXPORT ContentsView : public views::View,
  42. public PaginationModelObserver {
  43. public:
  44. // Used to SetActiveState without animations.
  45. class ScopedSetActiveStateAnimationDisabler {
  46. public:
  47. explicit ScopedSetActiveStateAnimationDisabler(ContentsView* contents_view)
  48. : contents_view_(contents_view) {
  49. contents_view_->set_active_state_without_animation_ = true;
  50. }
  51. ScopedSetActiveStateAnimationDisabler(
  52. const ScopedSetActiveStateAnimationDisabler&) = delete;
  53. ScopedSetActiveStateAnimationDisabler& operator=(
  54. const ScopedSetActiveStateAnimationDisabler&) = delete;
  55. ~ScopedSetActiveStateAnimationDisabler() {
  56. contents_view_->set_active_state_without_animation_ = false;
  57. }
  58. private:
  59. ContentsView* const contents_view_;
  60. };
  61. explicit ContentsView(AppListView* app_list_view);
  62. ContentsView(const ContentsView&) = delete;
  63. ContentsView& operator=(const ContentsView&) = delete;
  64. ~ContentsView() override;
  65. // Returns the search box top margin when app list view is in peeking/half
  66. // state, and showing the provided `page`.
  67. static int GetPeekingSearchBoxTopMarginOnPage(AppListState page);
  68. // Initialize the pages of the launcher. Should be called after
  69. // set_contents_switcher_view().
  70. void Init();
  71. // Resets the state of the view so it is ready to be shown.
  72. void ResetForShow();
  73. // The app list gets closed and drag and drop operations need to be cancelled.
  74. void CancelDrag();
  75. // If |drag_and_drop| is not nullptr it will be called upon drag and drop
  76. // operations outside the application list.
  77. void SetDragAndDropHostOfCurrentAppList(
  78. ApplicationDragAndDropHost* drag_and_drop_host);
  79. // Called when the target state of AppListView changes.
  80. void OnAppListViewTargetStateChanged(AppListViewState target_state);
  81. // Called from AppListView when the tablet mode state changes.
  82. void OnTabletModeChanged(bool started);
  83. // Shows/hides the search results. Hiding the search results will cause the
  84. // app list to return to the page that was displayed before
  85. // ShowSearchResults(true) was invoked.
  86. void ShowSearchResults(bool show);
  87. bool IsShowingSearchResults() const;
  88. // Shows/hides the Assistant page. Hiding the Assistant page will
  89. // cause the app list to return to the page that was displayed before
  90. // ShowSearchResults(true) was invoked.
  91. void ShowEmbeddedAssistantUI(bool show);
  92. bool IsShowingEmbeddedAssistantUI() const;
  93. // Sets the active launcher page and animates the pages into place.
  94. void SetActiveState(AppListState state);
  95. void SetActiveState(AppListState state, bool animate);
  96. // The index of the currently active launcher page.
  97. int GetActivePageIndex() const;
  98. // The currently active state.
  99. AppListState GetActiveState() const;
  100. // True if |state| is the current active laucher page.
  101. bool IsStateActive(AppListState state) const;
  102. // Gets the index of a launcher page in |view_model_|, by State. Returns
  103. // -1 if there is no view for |state|.
  104. int GetPageIndexForState(AppListState state) const;
  105. // Gets the state of a launcher page in |view_model_|, by index. Returns
  106. // INVALID_STATE if there is no state for |index|.
  107. AppListState GetStateForPageIndex(int index) const;
  108. int NumLauncherPages() const;
  109. SearchResultPageView* search_result_page_view() const {
  110. return search_result_page_view_;
  111. }
  112. AppsContainerView* apps_container_view() const {
  113. return apps_container_view_;
  114. }
  115. AppListPage* GetPageView(int index) const;
  116. SearchBoxView* GetSearchBoxView() const;
  117. AppListMainView* GetAppListMainView() const;
  118. AppListView* app_list_view() const { return app_list_view_; }
  119. ExpandArrowView* expand_arrow_view() const { return expand_arrow_view_; }
  120. AppListViewState target_view_state() const { return target_view_state_; }
  121. // Returns the pagination model for the ContentsView.
  122. const PaginationModel& pagination_model() { return pagination_model_; }
  123. // Returns the search box bounds to use for a given app list (pagination)
  124. // state (in the current app list view state).
  125. gfx::Rect GetSearchBoxBounds(AppListState state) const;
  126. // Returns the search box bounds size to use for a given app list (pagination)
  127. // state (in the current app list view state).
  128. gfx::Size GetSearchBoxSize(AppListState state) const;
  129. // Returns the search box bounds size to use for a given app list (pagination)
  130. // state and app list view state.
  131. gfx::Rect GetSearchBoxBoundsForViewState(AppListState state,
  132. AppListViewState view_state) const;
  133. // Returns the expected search box bounds based on the app list transition
  134. // progress.
  135. gfx::Rect GetSearchBoxExpectedBoundsForProgress(AppListState state,
  136. float progress) const;
  137. // Performs the 'back' action for the active page. Returns whether the action
  138. // was handled.
  139. bool Back();
  140. // Overridden from views::View:
  141. void Layout() override;
  142. const char* GetClassName() const override;
  143. // Overridden from PaginationModelObserver:
  144. void TotalPagesChanged(int previous_page_count, int new_page_count) override;
  145. void SelectedPageChanged(int old_selected, int new_selected) override;
  146. void TransitionStarted() override;
  147. void TransitionChanged() override;
  148. // Updates y position and opacity of the items in this view during dragging.
  149. void UpdateYPositionAndOpacity();
  150. // Starts animated transition to |target_view_state|.
  151. // Manages the child view opacity, and vertically translates search box and
  152. // app list pages to the bounds required for the new view state.
  153. void AnimateToViewState(AppListViewState target_view_state,
  154. const base::TimeDelta& animation_duration);
  155. std::unique_ptr<ui::ScopedLayerAnimationSettings>
  156. CreateTransitionAnimationSettings(ui::Layer* layer) const;
  157. // Adjusts search box view size so it fits within the contents view margins
  158. // (when centered).
  159. gfx::Size AdjustSearchBoxSizeToFitMargins(
  160. const gfx::Size& preferred_size) const;
  161. private:
  162. // Sets the active launcher page.
  163. void SetActiveStateInternal(int page_index, bool animate);
  164. // Invoked when active view is changed.
  165. void ActivePageChanged();
  166. void InitializeSearchBoxAnimation(AppListState current_state,
  167. AppListState target_state);
  168. void UpdateSearchBoxAnimation(double progress,
  169. AppListState current_state,
  170. AppListState target_state);
  171. // Updates the expand arrow's behavior based on AppListViewState.
  172. void UpdateExpandArrowBehavior(AppListViewState target_state);
  173. // Updates the expand arrow visibility depending on the selected app list page
  174. // and the app list view state.
  175. // `target_state` - the target selected app list page.
  176. // `target_app_list_view_state` - the target app list view state.
  177. // `transition_duration` - the opacity transition duration. Should be set to
  178. // zero if the opacity transition should not be animated.
  179. void UpdateExpandArrowOpacity(AppListState target_state,
  180. AppListViewState target_app_list_state,
  181. base::TimeDelta transition_duration);
  182. // Updates search box visibility based on the current state.
  183. void UpdateSearchBoxVisibility(AppListState current_state);
  184. // Adds |view| as a new page to the end of the list of launcher pages. The
  185. // view is inserted as a child of the ContentsView. The page is associated
  186. // with the name |state|. Returns a pointer to the instance of the new page.
  187. template <typename T>
  188. T* AddLauncherPage(std::unique_ptr<T> view, AppListState state) {
  189. auto* result = view.get();
  190. AddLauncherPageInternal(std::move(view), state);
  191. return result;
  192. }
  193. // Internal version of the above that does the actual work.
  194. void AddLauncherPageInternal(std::unique_ptr<AppListPage> view,
  195. AppListState state);
  196. // Returns true if the |page| requires layout when transitioning from
  197. // |current_state| to |target_state|.
  198. bool ShouldLayoutPage(AppListPage* page,
  199. AppListState current_state,
  200. AppListState target_state) const;
  201. // Converts rect to widget without applying transform.
  202. gfx::Rect ConvertRectToWidgetWithoutTransform(const gfx::Rect& rect);
  203. // Returns the search box origin y coordinate to use for a given app list
  204. // (pagination) state and app list view state.
  205. // NOTE: The search box will be horizontally centered in the current content
  206. // bounds.
  207. int GetSearchBoxTopForViewState(AppListState state,
  208. AppListViewState view_state) const;
  209. // Sub-views of the ContentsView. All owned by the views hierarchy.
  210. AssistantPageView* assistant_page_view_ = nullptr;
  211. AppsContainerView* apps_container_view_ = nullptr;
  212. SearchResultPageView* search_result_page_view_ = nullptr;
  213. // The child page views. Owned by the views hierarchy.
  214. std::vector<AppListPage*> app_list_pages_;
  215. // Owned by the views hierarchy.
  216. AppListView* const app_list_view_;
  217. AppListViewState target_view_state_ = AppListViewState::kPeeking;
  218. // Owned by the views hierarchy.
  219. ExpandArrowView* expand_arrow_view_ = nullptr;
  220. // Maps State onto |view_model_| indices.
  221. std::map<AppListState, int> state_to_view_;
  222. // Maps |view_model_| indices onto State.
  223. std::map<int, AppListState> view_to_state_;
  224. // The page that was showing before ShowSearchResults(true) was invoked.
  225. int page_before_search_ = 0;
  226. // Manages the pagination for the launcher pages.
  227. PaginationModel pagination_model_{this};
  228. // If true, SetActiveState immediately.
  229. bool set_active_state_without_animation_ = false;
  230. // If set, the app list page that was used to determine the search box
  231. // placement when the contents view layout was last updated for app list view
  232. // state (either using UpdateYPositionAndOpacity() or AnimateToViewState()).
  233. // Used primarily to determine the initial search box position when animating
  234. // to a new app list view state.
  235. absl::optional<AppListState> target_page_for_last_view_state_update_;
  236. absl::optional<AppListViewState> last_target_view_state_;
  237. };
  238. } // namespace ash
  239. #endif // ASH_APP_LIST_VIEWS_CONTENTS_VIEW_H_