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 const int kInstantContainerSpacing = 24; |
41 const int kSearchBoxAndTilesSpacing = 35; | 43 const int kSearchBoxAndTilesSpacing = 35; |
42 const int kStartPageSearchBoxWidth = 480; | 44 const int kStartPageSearchBoxWidth = 480; |
43 | 45 |
44 // WebView constants. | 46 // WebView constants. |
45 const int kWebViewWidth = 700; | 47 const int kWebViewWidth = 700; |
46 const int kWebViewHeight = 244; | 48 const int kWebViewHeight = 244; |
47 | 49 |
48 // Tile container constants. | 50 // Tile container constants. |
49 const size_t kNumStartPageTiles = 4; | |
50 const int kTileSpacing = 7; | 51 const int kTileSpacing = 7; |
52 const int kNumStartPageTilesCols = 5; | |
53 const int kTilesHorizontalMarginLeft = 145; | |
xiyuan
2016/08/09 17:49:09
nit: const int -> constexpr int here and all other
xdai1
2016/08/09 20:14:17
Done.
| |
51 | 54 |
52 const int kLauncherPageBackgroundWidth = 400; | 55 const 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()) {} |
(...skipping 63 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 | |
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_->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); | 157 all_apps_button_->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); |
167 all_apps_button_->UpdateIcon(); | |
168 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); | 158 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); |
169 AddChildView(all_apps_button_); | 159 CreateAppsGrid(kNumStartPageTiles); |
170 } | 160 } |
171 | 161 |
172 StartPageView::StartPageTilesContainer::~StartPageTilesContainer() { | 162 StartPageView::StartPageTilesContainer::~StartPageTilesContainer() { |
173 } | 163 } |
174 | 164 |
175 TileItemView* StartPageView::StartPageTilesContainer::GetTileItemView( | 165 TileItemView* StartPageView::StartPageTilesContainer::GetTileItemView( |
176 int index) { | 166 int index) { |
177 DCHECK_GT(num_results(), index); | 167 DCHECK_GT(num_results(), index); |
178 if (index == num_results() - 1) | 168 if (index == num_results() - 1) |
179 return all_apps_button_; | 169 return all_apps_button_; |
180 | 170 |
181 return search_result_tile_views_[index]; | 171 return search_result_tile_views_[index]; |
182 } | 172 } |
183 | 173 |
184 int StartPageView::StartPageTilesContainer::Update() { | 174 int StartPageView::StartPageTilesContainer::Update() { |
185 // Ignore updates and disable buttons when transitioning to a different | 175 // Ignore updates and disable buttons when transitioning to a different |
186 // state. | 176 // state. |
187 if (contents_view_->GetActiveState() != AppListModel::STATE_START) { | 177 if (contents_view_->GetActiveState() != AppListModel::STATE_START) { |
188 for (auto* view : search_result_tile_views_) | 178 for (auto* view : search_result_tile_views_) |
189 view->SetEnabled(false); | 179 view->SetEnabled(false); |
190 | 180 |
191 return num_results(); | 181 return num_results(); |
192 } | 182 } |
193 | 183 |
194 std::vector<SearchResult*> display_results = | 184 std::vector<SearchResult*> display_results = |
195 AppListModel::FilterSearchResultsByDisplayType( | 185 AppListModel::FilterSearchResultsByDisplayType( |
196 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 RemoveChildView(search_result_tile_views_[i]); | |
xiyuan
2016/08/09 17:49:09
Not necessary. Deleting a view would remove it fro
xdai1
2016/08/09 20:14:18
Done.
| |
191 delete search_result_tile_views_[i]; | |
192 } | |
193 search_result_tile_views_.clear(); | |
194 RemoveChildView(all_apps_button_); | |
195 CreateAppsGrid(std::min(kNumStartPageTiles, display_results.size())); | |
196 } | |
197 | 197 |
198 // Update the tile item results. | 198 // Update the tile item results. |
199 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { | 199 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { |
200 SearchResult* item = NULL; | 200 SearchResult* item = nullptr; |
201 if (i < display_results.size()) | 201 if (i < display_results.size()) |
202 item = display_results[i]; | 202 item = display_results[i]; |
203 search_result_tile_views_[i]->SetSearchResult(item); | 203 search_result_tile_views_[i]->SetSearchResult(item); |
204 search_result_tile_views_[i]->SetEnabled(true); | 204 search_result_tile_views_[i]->SetEnabled(true); |
205 } | 205 } |
206 | 206 |
207 Layout(); | 207 Layout(); |
208 parent()->Layout(); | 208 parent()->Layout(); |
209 // Add 1 to the results size to account for the all apps button. | 209 // Add 1 to the results size to account for the all apps button. |
210 return display_results.size() + 1; | 210 return display_results.size() + 1; |
(...skipping 18 matching lines...) Expand all Loading... | |
229 void StartPageView::StartPageTilesContainer::NotifyFirstResultYIndex( | 229 void StartPageView::StartPageTilesContainer::NotifyFirstResultYIndex( |
230 int /*y_index*/) { | 230 int /*y_index*/) { |
231 NOTREACHED(); | 231 NOTREACHED(); |
232 } | 232 } |
233 | 233 |
234 int StartPageView::StartPageTilesContainer::GetYSize() { | 234 int StartPageView::StartPageTilesContainer::GetYSize() { |
235 NOTREACHED(); | 235 NOTREACHED(); |
236 return 0; | 236 return 0; |
237 } | 237 } |
238 | 238 |
239 void StartPageView::StartPageTilesContainer::CreateAppsGrid(int apps_num) { | |
240 DCHECK(search_result_tile_views_.empty()); | |
241 views::GridLayout* tiles_layout_manager = new views::GridLayout(this); | |
242 SetLayoutManager(tiles_layout_manager); | |
243 | |
244 views::ColumnSet* column_set = tiles_layout_manager->AddColumnSet(0); | |
245 column_set->AddPaddingColumn(0, kTilesHorizontalMarginLeft); | |
246 for (int col = 0; col < kNumStartPageTilesCols; col++) { | |
xiyuan
2016/08/09 17:49:09
nit: col++ -> ++col
xdai1
2016/08/09 20:14:17
I think they're same?
xiyuan
2016/08/09 20:22:12
Haha... This used to be an interview question aske
xdai1
2016/08/09 20:33:06
I see. Thanks!
| |
247 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0, | |
248 views::GridLayout::USE_PREF, 0, 0); | |
249 column_set->AddPaddingColumn(0, kTileSpacing); | |
250 } | |
251 | |
252 // Add SearchResultTileItemViews to the container. | |
253 int i = 0; | |
254 for (; i < apps_num; i++) { | |
xiyuan
2016/08/09 17:49:09
nit: i++ -> ++i
xdai1
2016/08/09 20:14:17
same?
| |
255 SearchResultTileItemView* tile_item = | |
256 new SearchResultTileItemView(this, view_delegate_); | |
257 if (i % kNumStartPageTilesCols == 0) | |
258 tiles_layout_manager->StartRow(0, 0); | |
259 tiles_layout_manager->AddView(tile_item); | |
260 AddChildView(tile_item); | |
261 tile_item->SetParentBackgroundColor(kLabelBackgroundColor); | |
262 tile_item->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW); | |
263 search_result_tile_views_.push_back(tile_item); | |
264 } | |
265 | |
266 // Also add a special "all apps" button to the end of the container. | |
267 all_apps_button_->UpdateIcon(); | |
268 if (i % kNumStartPageTilesCols == 0) | |
269 tiles_layout_manager->StartRow(0, 0); | |
270 tiles_layout_manager->AddView(all_apps_button_); | |
271 AddChildView(all_apps_button_); | |
272 } | |
273 | |
239 //////////////////////////////////////////////////////////////////////////////// | 274 //////////////////////////////////////////////////////////////////////////////// |
240 // StartPageView implementation: | 275 // StartPageView implementation: |
241 StartPageView::StartPageView(AppListMainView* app_list_main_view, | 276 StartPageView::StartPageView(AppListMainView* app_list_main_view, |
242 AppListViewDelegate* view_delegate) | 277 AppListViewDelegate* view_delegate) |
243 : app_list_main_view_(app_list_main_view), | 278 : app_list_main_view_(app_list_main_view), |
244 view_delegate_(view_delegate), | 279 view_delegate_(view_delegate), |
245 search_box_spacer_view_(new SearchBoxSpacerView( | 280 search_box_spacer_view_(new SearchBoxSpacerView( |
246 app_list_main_view->search_box_view()->GetPreferredSize())), | 281 app_list_main_view->search_box_view()->GetPreferredSize())), |
247 instant_container_(new views::View), | 282 instant_container_(new views::View), |
248 custom_launcher_page_background_(new CustomLauncherPageBackgroundView( | 283 custom_launcher_page_background_(new CustomLauncherPageBackgroundView( |
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
496 // is enabled). | 531 // is enabled). |
497 if (event->type() == ui::ET_SCROLL && event->y_offset() < 0) | 532 if (event->type() == ui::ET_SCROLL && event->y_offset() < 0) |
498 MaybeOpenCustomLauncherPage(); | 533 MaybeOpenCustomLauncherPage(); |
499 } | 534 } |
500 | 535 |
501 TileItemView* StartPageView::GetTileItemView(size_t index) { | 536 TileItemView* StartPageView::GetTileItemView(size_t index) { |
502 return tiles_container_->GetTileItemView(index); | 537 return tiles_container_->GetTileItemView(index); |
503 } | 538 } |
504 | 539 |
505 } // namespace app_list | 540 } // namespace app_list |
OLD | NEW |