OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/app_list/views/start_page_view.h" | 5 #include "ui/app_list/views/start_page_view.h" |
6 | 6 |
| 7 #include <algorithm> |
7 #include <string> | 8 #include <string> |
8 | 9 |
9 #include "base/i18n/rtl.h" | 10 #include "base/i18n/rtl.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
13 #include "ui/accessibility/ax_view_state.h" | 14 #include "ui/accessibility/ax_view_state.h" |
14 #include "ui/app_list/app_list_constants.h" | 15 #include "ui/app_list/app_list_constants.h" |
15 #include "ui/app_list/app_list_item.h" | 16 #include "ui/app_list/app_list_item.h" |
16 #include "ui/app_list/app_list_model.h" | 17 #include "ui/app_list/app_list_model.h" |
17 #include "ui/app_list/app_list_view_delegate.h" | 18 #include "ui/app_list/app_list_view_delegate.h" |
18 #include "ui/app_list/search_result.h" | 19 #include "ui/app_list/search_result.h" |
19 #include "ui/app_list/views/all_apps_tile_item_view.h" | 20 #include "ui/app_list/views/all_apps_tile_item_view.h" |
20 #include "ui/app_list/views/app_list_main_view.h" | 21 #include "ui/app_list/views/app_list_main_view.h" |
21 #include "ui/app_list/views/contents_view.h" | 22 #include "ui/app_list/views/contents_view.h" |
22 #include "ui/app_list/views/custom_launcher_page_view.h" | 23 #include "ui/app_list/views/custom_launcher_page_view.h" |
23 #include "ui/app_list/views/search_box_view.h" | 24 #include "ui/app_list/views/search_box_view.h" |
24 #include "ui/app_list/views/search_result_container_view.h" | 25 #include "ui/app_list/views/search_result_container_view.h" |
25 #include "ui/app_list/views/search_result_tile_item_view.h" | 26 #include "ui/app_list/views/search_result_tile_item_view.h" |
26 #include "ui/app_list/views/tile_item_view.h" | 27 #include "ui/app_list/views/tile_item_view.h" |
27 #include "ui/gfx/canvas.h" | 28 #include "ui/gfx/canvas.h" |
28 #include "ui/views/background.h" | 29 #include "ui/views/background.h" |
29 #include "ui/views/controls/image_view.h" | 30 #include "ui/views/controls/image_view.h" |
30 #include "ui/views/controls/label.h" | 31 #include "ui/views/controls/label.h" |
31 #include "ui/views/controls/textfield/textfield.h" | 32 #include "ui/views/controls/textfield/textfield.h" |
32 #include "ui/views/layout/box_layout.h" | 33 #include "ui/views/layout/box_layout.h" |
| 34 #include "ui/views/layout/grid_layout.h" |
33 #include "ui/views/widget/widget.h" | 35 #include "ui/views/widget/widget.h" |
34 | 36 |
35 namespace app_list { | 37 namespace app_list { |
36 | 38 |
37 namespace { | 39 namespace { |
38 | 40 |
39 // Layout constants. | 41 // Layout constants. |
40 const int kInstantContainerSpacing = 24; | 42 constexpr int kInstantContainerSpacing = 24; |
41 const int kSearchBoxAndTilesSpacing = 35; | 43 constexpr int kSearchBoxAndTilesSpacing = 35; |
42 const int kStartPageSearchBoxWidth = 480; | 44 constexpr int kStartPageSearchBoxWidth = 480; |
43 | 45 |
44 // WebView constants. | 46 // WebView constants. |
45 const int kWebViewWidth = 700; | 47 constexpr int kWebViewWidth = 700; |
46 const int kWebViewHeight = 244; | 48 constexpr int kWebViewHeight = 224; |
47 | 49 |
48 // Tile container constants. | 50 // Tile container constants. |
49 const size_t kNumStartPageTiles = 4; | 51 constexpr int kTileSpacing = 7; |
50 const int kTileSpacing = 7; | 52 constexpr int kNumStartPageTilesCols = 5; |
| 53 constexpr int kTilesHorizontalMarginLeft = 145; |
51 | 54 |
52 const int kLauncherPageBackgroundWidth = 400; | 55 constexpr int kLauncherPageBackgroundWidth = 400; |
53 | 56 |
54 // An invisible placeholder view which fills the space for the search box view | 57 // An invisible placeholder view which fills the space for the search box view |
55 // in a box layout. The search box view itself is a child of the AppListView | 58 // in a box layout. The search box view itself is a child of the AppListView |
56 // (because it is visible on many different pages). | 59 // (because it is visible on many different pages). |
57 class SearchBoxSpacerView : public views::View { | 60 class SearchBoxSpacerView : public views::View { |
58 public: | 61 public: |
59 explicit SearchBoxSpacerView(const gfx::Size& search_box_size) | 62 explicit SearchBoxSpacerView(const gfx::Size& search_box_size) |
60 : size_(kStartPageSearchBoxWidth, search_box_size.height()) {} | 63 : size_(kStartPageSearchBoxWidth, search_box_size.height()) {} |
61 | 64 |
62 ~SearchBoxSpacerView() override {} | 65 ~SearchBoxSpacerView() override {} |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 | 127 |
125 // Overridden from SearchResultContainerView: | 128 // Overridden from SearchResultContainerView: |
126 int Update() override; | 129 int Update() override; |
127 void UpdateSelectedIndex(int old_selected, int new_selected) override; | 130 void UpdateSelectedIndex(int old_selected, int new_selected) override; |
128 void OnContainerSelected(bool from_bottom, | 131 void OnContainerSelected(bool from_bottom, |
129 bool directional_movement) override; | 132 bool directional_movement) override; |
130 void NotifyFirstResultYIndex(int y_index) override; | 133 void NotifyFirstResultYIndex(int y_index) override; |
131 int GetYSize() override; | 134 int GetYSize() override; |
132 | 135 |
133 private: | 136 private: |
| 137 void CreateAppsGrid(int apps_num); |
| 138 |
134 ContentsView* contents_view_; | 139 ContentsView* contents_view_; |
| 140 AppListViewDelegate* view_delegate_; |
135 | 141 |
136 std::vector<SearchResultTileItemView*> search_result_tile_views_; | 142 std::vector<SearchResultTileItemView*> search_result_tile_views_; |
137 AllAppsTileItemView* all_apps_button_; | 143 AllAppsTileItemView* all_apps_button_; |
138 | 144 |
139 DISALLOW_COPY_AND_ASSIGN(StartPageTilesContainer); | 145 DISALLOW_COPY_AND_ASSIGN(StartPageTilesContainer); |
140 }; | 146 }; |
141 | 147 |
142 StartPageView::StartPageTilesContainer::StartPageTilesContainer( | 148 StartPageView::StartPageTilesContainer::StartPageTilesContainer( |
143 ContentsView* contents_view, | 149 ContentsView* contents_view, |
144 AllAppsTileItemView* all_apps_button, | 150 AllAppsTileItemView* all_apps_button, |
145 AppListViewDelegate* view_delegate) | 151 AppListViewDelegate* view_delegate) |
146 : contents_view_(contents_view), all_apps_button_(all_apps_button) { | 152 : contents_view_(contents_view), |
147 views::BoxLayout* tiles_layout_manager = | 153 view_delegate_(view_delegate), |
148 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing); | 154 all_apps_button_(all_apps_button) { |
149 tiles_layout_manager->set_main_axis_alignment( | |
150 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
151 SetLayoutManager(tiles_layout_manager); | |
152 set_background( | 155 set_background( |
153 views::Background::CreateSolidBackground(kLabelBackgroundColor)); | 156 views::Background::CreateSolidBackground(kLabelBackgroundColor)); |
154 | 157 all_apps_button_->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); |
155 // Add SearchResultTileItemViews to the container. | |
156 for (size_t i = 0; i < kNumStartPageTiles; ++i) { | |
157 SearchResultTileItemView* tile_item = | |
158 new SearchResultTileItemView(this, view_delegate); | |
159 AddChildView(tile_item); | |
160 tile_item->SetParentBackgroundColor(kLabelBackgroundColor); | |
161 tile_item->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); | |
162 search_result_tile_views_.push_back(tile_item); | |
163 } | |
164 | |
165 // Also add a special "all apps" button to the end of the container. | |
166 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); | 158 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); |
167 AddChildView(all_apps_button_); | 159 CreateAppsGrid(kNumStartPageTiles); |
168 } | 160 } |
169 | 161 |
170 StartPageView::StartPageTilesContainer::~StartPageTilesContainer() { | 162 StartPageView::StartPageTilesContainer::~StartPageTilesContainer() { |
171 } | 163 } |
172 | 164 |
173 TileItemView* StartPageView::StartPageTilesContainer::GetTileItemView( | 165 TileItemView* StartPageView::StartPageTilesContainer::GetTileItemView( |
174 int index) { | 166 int index) { |
175 DCHECK_GT(num_results(), index); | 167 DCHECK_GT(num_results(), index); |
176 if (index == num_results() - 1) | 168 if (index == num_results() - 1) |
177 return all_apps_button_; | 169 return all_apps_button_; |
178 | 170 |
179 return search_result_tile_views_[index]; | 171 return search_result_tile_views_[index]; |
180 } | 172 } |
181 | 173 |
182 int StartPageView::StartPageTilesContainer::Update() { | 174 int StartPageView::StartPageTilesContainer::Update() { |
183 // Ignore updates and disable buttons when transitioning to a different | 175 // Ignore updates and disable buttons when transitioning to a different |
184 // state. | 176 // state. |
185 if (contents_view_->GetActiveState() != AppListModel::STATE_START) { | 177 if (contents_view_->GetActiveState() != AppListModel::STATE_START) { |
186 for (auto* view : search_result_tile_views_) | 178 for (auto* view : search_result_tile_views_) |
187 view->SetEnabled(false); | 179 view->SetEnabled(false); |
188 | 180 |
189 return num_results(); | 181 return num_results(); |
190 } | 182 } |
191 | 183 |
192 std::vector<SearchResult*> display_results = | 184 std::vector<SearchResult*> display_results = |
193 AppListModel::FilterSearchResultsByDisplayType( | 185 AppListModel::FilterSearchResultsByDisplayType( |
194 results(), SearchResult::DISPLAY_RECOMMENDATION, kNumStartPageTiles); | 186 results(), SearchResult::DISPLAY_RECOMMENDATION, kNumStartPageTiles); |
| 187 if (display_results.size() != search_result_tile_views_.size()) { |
| 188 // We should recreate the grid layout in this case. |
| 189 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) |
| 190 delete search_result_tile_views_[i]; |
| 191 search_result_tile_views_.clear(); |
| 192 RemoveChildView(all_apps_button_); |
| 193 CreateAppsGrid(std::min(kNumStartPageTiles, display_results.size())); |
| 194 } |
195 | 195 |
196 // Update the tile item results. | 196 // Update the tile item results. |
197 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { | 197 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { |
198 SearchResult* item = NULL; | 198 SearchResult* item = nullptr; |
199 if (i < display_results.size()) | 199 if (i < display_results.size()) |
200 item = display_results[i]; | 200 item = display_results[i]; |
201 search_result_tile_views_[i]->SetSearchResult(item); | 201 search_result_tile_views_[i]->SetSearchResult(item); |
202 search_result_tile_views_[i]->SetEnabled(true); | 202 search_result_tile_views_[i]->SetEnabled(true); |
203 } | 203 } |
204 | 204 |
205 Layout(); | 205 Layout(); |
206 parent()->Layout(); | 206 parent()->Layout(); |
207 // Add 1 to the results size to account for the all apps button. | 207 // Add 1 to the results size to account for the all apps button. |
208 return display_results.size() + 1; | 208 return display_results.size() + 1; |
(...skipping 18 matching lines...) Expand all Loading... |
227 void StartPageView::StartPageTilesContainer::NotifyFirstResultYIndex( | 227 void StartPageView::StartPageTilesContainer::NotifyFirstResultYIndex( |
228 int /*y_index*/) { | 228 int /*y_index*/) { |
229 NOTREACHED(); | 229 NOTREACHED(); |
230 } | 230 } |
231 | 231 |
232 int StartPageView::StartPageTilesContainer::GetYSize() { | 232 int StartPageView::StartPageTilesContainer::GetYSize() { |
233 NOTREACHED(); | 233 NOTREACHED(); |
234 return 0; | 234 return 0; |
235 } | 235 } |
236 | 236 |
| 237 void StartPageView::StartPageTilesContainer::CreateAppsGrid(int apps_num) { |
| 238 DCHECK(search_result_tile_views_.empty()); |
| 239 views::GridLayout* tiles_layout_manager = new views::GridLayout(this); |
| 240 SetLayoutManager(tiles_layout_manager); |
| 241 |
| 242 views::ColumnSet* column_set = tiles_layout_manager->AddColumnSet(0); |
| 243 column_set->AddPaddingColumn(0, kTilesHorizontalMarginLeft); |
| 244 for (int col = 0; col < kNumStartPageTilesCols; ++col) { |
| 245 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, |
| 246 views::GridLayout::USE_PREF, 0, 0); |
| 247 column_set->AddPaddingColumn(0, kTileSpacing); |
| 248 } |
| 249 |
| 250 // Add SearchResultTileItemViews to the container. |
| 251 int i = 0; |
| 252 for (; i < apps_num; ++i) { |
| 253 SearchResultTileItemView* tile_item = |
| 254 new SearchResultTileItemView(this, view_delegate_); |
| 255 if (i % kNumStartPageTilesCols == 0) |
| 256 tiles_layout_manager->StartRow(0, 0); |
| 257 tiles_layout_manager->AddView(tile_item); |
| 258 AddChildView(tile_item); |
| 259 tile_item->SetParentBackgroundColor(kLabelBackgroundColor); |
| 260 tile_item->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); |
| 261 search_result_tile_views_.push_back(tile_item); |
| 262 } |
| 263 |
| 264 // Also add a special "all apps" button to the end of the container. |
| 265 all_apps_button_->UpdateIcon(); |
| 266 if (i % kNumStartPageTilesCols == 0) |
| 267 tiles_layout_manager->StartRow(0, 0); |
| 268 tiles_layout_manager->AddView(all_apps_button_); |
| 269 AddChildView(all_apps_button_); |
| 270 } |
| 271 |
237 //////////////////////////////////////////////////////////////////////////////// | 272 //////////////////////////////////////////////////////////////////////////////// |
238 // StartPageView implementation: | 273 // StartPageView implementation: |
239 StartPageView::StartPageView(AppListMainView* app_list_main_view, | 274 StartPageView::StartPageView(AppListMainView* app_list_main_view, |
240 AppListViewDelegate* view_delegate) | 275 AppListViewDelegate* view_delegate) |
241 : app_list_main_view_(app_list_main_view), | 276 : app_list_main_view_(app_list_main_view), |
242 view_delegate_(view_delegate), | 277 view_delegate_(view_delegate), |
243 search_box_spacer_view_(new SearchBoxSpacerView( | 278 search_box_spacer_view_(new SearchBoxSpacerView( |
244 app_list_main_view->search_box_view()->GetPreferredSize())), | 279 app_list_main_view->search_box_view()->GetPreferredSize())), |
245 instant_container_(new views::View), | 280 instant_container_(new views::View), |
246 custom_launcher_page_background_(new CustomLauncherPageBackgroundView( | 281 custom_launcher_page_background_(new CustomLauncherPageBackgroundView( |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 // is enabled). | 527 // is enabled). |
493 if (event->type() == ui::ET_SCROLL && event->y_offset() < 0) | 528 if (event->type() == ui::ET_SCROLL && event->y_offset() < 0) |
494 MaybeOpenCustomLauncherPage(); | 529 MaybeOpenCustomLauncherPage(); |
495 } | 530 } |
496 | 531 |
497 TileItemView* StartPageView::GetTileItemView(size_t index) { | 532 TileItemView* StartPageView::GetTileItemView(size_t index) { |
498 return tiles_container_->GetTileItemView(index); | 533 return tiles_container_->GetTileItemView(index); |
499 } | 534 } |
500 | 535 |
501 } // namespace app_list | 536 } // namespace app_list |
OLD | NEW |