Chromium Code Reviews| 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 "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "ui/app_list/app_list_constants.h" | 8 #include "ui/app_list/app_list_constants.h" |
| 9 #include "ui/app_list/app_list_item.h" | 9 #include "ui/app_list/app_list_item.h" |
| 10 #include "ui/app_list/app_list_model.h" | 10 #include "ui/app_list/app_list_model.h" |
| 11 #include "ui/app_list/app_list_view_delegate.h" | 11 #include "ui/app_list/app_list_view_delegate.h" |
| 12 #include "ui/app_list/search_result.h" | 12 #include "ui/app_list/search_result.h" |
| 13 #include "ui/app_list/views/all_apps_tile_item_view.h" | 13 #include "ui/app_list/views/all_apps_tile_item_view.h" |
| 14 #include "ui/app_list/views/app_list_main_view.h" | 14 #include "ui/app_list/views/app_list_main_view.h" |
| 15 #include "ui/app_list/views/contents_view.h" | 15 #include "ui/app_list/views/contents_view.h" |
| 16 #include "ui/app_list/views/search_box_view.h" | 16 #include "ui/app_list/views/search_box_view.h" |
| 17 #include "ui/app_list/views/search_result_list_view.h" | 17 #include "ui/app_list/views/search_result_container_view.h" |
| 18 #include "ui/app_list/views/search_result_tile_item_view.h" | 18 #include "ui/app_list/views/search_result_tile_item_view.h" |
| 19 #include "ui/app_list/views/tile_item_view.h" | 19 #include "ui/app_list/views/tile_item_view.h" |
| 20 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 21 #include "ui/views/background.h" | 21 #include "ui/views/background.h" |
| 22 #include "ui/views/controls/image_view.h" | 22 #include "ui/views/controls/image_view.h" |
| 23 #include "ui/views/controls/label.h" | 23 #include "ui/views/controls/label.h" |
| 24 #include "ui/views/controls/textfield/textfield.h" | 24 #include "ui/views/controls/textfield/textfield.h" |
| 25 #include "ui/views/layout/box_layout.h" | 25 #include "ui/views/layout/box_layout.h" |
| 26 #include "ui/views/widget/widget.h" | 26 #include "ui/views/widget/widget.h" |
| 27 | 27 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 56 gfx::Size GetPreferredSize() const override { return size_; } | 56 gfx::Size GetPreferredSize() const override { return size_; } |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 gfx::Size size_; | 59 gfx::Size size_; |
| 60 | 60 |
| 61 DISALLOW_COPY_AND_ASSIGN(SearchBoxSpacerView); | 61 DISALLOW_COPY_AND_ASSIGN(SearchBoxSpacerView); |
| 62 }; | 62 }; |
| 63 | 63 |
| 64 } // namespace | 64 } // namespace |
| 65 | 65 |
| 66 class StartPageTilesContainer : public SearchResultContainerView { | |
|
Matt Giuca
2015/02/04 06:07:45
I think this can go inside the namespace.
calamity
2015/02/04 06:46:55
Nope. Moved it into SPV's private instead.
| |
| 67 public: | |
| 68 explicit StartPageTilesContainer(AllAppsTileItemView* all_apps_button) | |
| 69 : all_apps_button_(all_apps_button) { | |
|
Matt Giuca
2015/02/04 06:07:45
I'm not a fan of inline methods (inside the class
calamity
2015/02/04 06:46:54
Done.
| |
| 70 views::BoxLayout* tiles_layout_manager = | |
| 71 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing); | |
| 72 tiles_layout_manager->set_main_axis_alignment( | |
| 73 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
| 74 SetLayoutManager(tiles_layout_manager); | |
| 75 set_background( | |
| 76 views::Background::CreateSolidBackground(kLabelBackgroundColor)); | |
| 77 | |
| 78 // Add SearchResultTileItemViews to the container. | |
| 79 for (size_t i = 0; i < kNumStartPageTiles; ++i) { | |
| 80 SearchResultTileItemView* tile_item = new SearchResultTileItemView(); | |
| 81 AddChildView(tile_item); | |
| 82 tile_item->SetParentBackgroundColor(kLabelBackgroundColor); | |
| 83 search_result_tile_views_.push_back(tile_item); | |
| 84 } | |
| 85 | |
| 86 // Also add a special "all apps" button to the end of the container. | |
| 87 all_apps_button_->UpdateIcon(); | |
| 88 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); | |
| 89 AddChildView(all_apps_button_); | |
| 90 } | |
| 91 ~StartPageTilesContainer() override {} | |
| 92 | |
| 93 TileItemView* GetTileItemView(size_t index) { | |
| 94 DCHECK_GT(kNumStartPageTiles + 1, index); | |
| 95 if (index == kNumStartPageTiles) | |
| 96 return all_apps_button_; | |
| 97 | |
| 98 return search_result_tile_views_[index]; | |
| 99 } | |
| 100 | |
| 101 const std::vector<SearchResultTileItemView*>& tile_views() const { | |
| 102 return search_result_tile_views_; | |
| 103 } | |
| 104 | |
| 105 AllAppsTileItemView* all_apps_button() { return all_apps_button_; } | |
| 106 | |
| 107 // Overridden from SearchResultContainerView: | |
| 108 int Update() override { | |
| 109 std::vector<SearchResult*> display_results = | |
| 110 AppListModel::FilterSearchResultsByDisplayType( | |
| 111 results(), SearchResult::DISPLAY_RECOMMENDATION, | |
| 112 kNumStartPageTiles); | |
| 113 | |
| 114 // Update the tile item results. | |
| 115 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { | |
| 116 SearchResult* item = NULL; | |
|
Matt Giuca
2015/02/04 06:07:45
nullptr
calamity
2015/02/04 06:46:55
Done.
| |
| 117 if (i < display_results.size()) | |
| 118 item = display_results[i]; | |
| 119 search_result_tile_views_[i]->SetSearchResult(item); | |
| 120 } | |
| 121 | |
| 122 Layout(); | |
| 123 parent()->Layout(); | |
| 124 // Add 1 to the results size to account for the all apps button. | |
| 125 return display_results.size() + 1; | |
| 126 } | |
| 127 | |
| 128 void UpdateSelectedIndex(int old_selected, int new_selected) override { | |
| 129 if (old_selected >= 0) | |
| 130 GetTileItemView(old_selected)->SetSelected(false); | |
| 131 | |
| 132 if (new_selected >= 0) | |
| 133 GetTileItemView(new_selected)->SetSelected(true); | |
| 134 } | |
| 135 | |
| 136 void OnContainerSelected(bool from_bottom) override {} | |
| 137 | |
| 138 private: | |
| 139 std::vector<SearchResultTileItemView*> search_result_tile_views_; | |
| 140 AllAppsTileItemView* all_apps_button_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(StartPageTilesContainer); | |
| 143 }; | |
| 144 | |
| 66 StartPageView::StartPageView(AppListMainView* app_list_main_view, | 145 StartPageView::StartPageView(AppListMainView* app_list_main_view, |
| 67 AppListViewDelegate* view_delegate) | 146 AppListViewDelegate* view_delegate) |
| 68 : app_list_main_view_(app_list_main_view), | 147 : app_list_main_view_(app_list_main_view), |
| 69 view_delegate_(view_delegate), | 148 view_delegate_(view_delegate), |
| 70 search_box_spacer_view_(new SearchBoxSpacerView( | 149 search_box_spacer_view_(new SearchBoxSpacerView( |
| 71 app_list_main_view->search_box_view()->GetPreferredSize())), | 150 app_list_main_view->search_box_view()->GetPreferredSize())), |
| 72 instant_container_(new views::View), | 151 instant_container_(new views::View), |
| 73 tiles_container_(new views::View) { | 152 tiles_container_(nullptr) { |
|
Matt Giuca
2015/02/04 06:07:45
Any reason you can't do new StartPageTilesContaine
calamity
2015/02/04 06:46:54
It's BIG. Done.
| |
| 74 // The view containing the start page WebContents and SearchBoxSpacerView. | 153 // The view containing the start page WebContents and SearchBoxSpacerView. |
| 75 InitInstantContainer(); | 154 InitInstantContainer(); |
| 76 AddChildView(instant_container_); | 155 AddChildView(instant_container_); |
| 77 | 156 |
| 78 // The view containing the start page tiles. | 157 // The view containing the start page tiles. |
| 79 InitTilesContainer(); | 158 tiles_container_ = new StartPageTilesContainer(new AllAppsTileItemView( |
| 159 app_list_main_view_->contents_view(), | |
| 160 view_delegate_->GetModel()->top_level_item_list())); | |
| 80 AddChildView(tiles_container_); | 161 AddChildView(tiles_container_); |
| 81 | 162 |
| 82 SetResults(view_delegate_->GetModel()->results()); | 163 tiles_container_->SetResults(view_delegate_->GetModel()->results()); |
| 83 Reset(); | 164 Reset(); |
| 84 } | 165 } |
| 85 | 166 |
| 86 StartPageView::~StartPageView() { | 167 StartPageView::~StartPageView() { |
| 87 } | 168 } |
| 88 | 169 |
| 89 void StartPageView::InitInstantContainer() { | 170 void StartPageView::InitInstantContainer() { |
| 90 views::BoxLayout* instant_layout_manager = new views::BoxLayout( | 171 views::BoxLayout* instant_layout_manager = new views::BoxLayout( |
| 91 views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing); | 172 views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing); |
| 92 instant_layout_manager->set_inside_border_insets( | 173 instant_layout_manager->set_inside_border_insets( |
| 93 gfx::Insets(0, 0, kSearchBoxAndTilesSpacing, 0)); | 174 gfx::Insets(0, 0, kSearchBoxAndTilesSpacing, 0)); |
| 94 instant_layout_manager->set_main_axis_alignment( | 175 instant_layout_manager->set_main_axis_alignment( |
| 95 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); | 176 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END); |
| 96 instant_layout_manager->set_cross_axis_alignment( | 177 instant_layout_manager->set_cross_axis_alignment( |
| 97 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); | 178 views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER); |
| 98 instant_container_->SetLayoutManager(instant_layout_manager); | 179 instant_container_->SetLayoutManager(instant_layout_manager); |
| 99 | 180 |
| 100 views::View* web_view = view_delegate_->CreateStartPageWebView( | 181 views::View* web_view = view_delegate_->CreateStartPageWebView( |
| 101 gfx::Size(kWebViewWidth, kWebViewHeight)); | 182 gfx::Size(kWebViewWidth, kWebViewHeight)); |
| 102 if (web_view) { | 183 if (web_view) { |
| 103 web_view->SetFocusable(false); | 184 web_view->SetFocusable(false); |
| 104 instant_container_->AddChildView(web_view); | 185 instant_container_->AddChildView(web_view); |
| 105 } | 186 } |
| 106 | 187 |
| 107 instant_container_->AddChildView(search_box_spacer_view_); | 188 instant_container_->AddChildView(search_box_spacer_view_); |
| 108 } | 189 } |
| 109 | 190 |
| 110 void StartPageView::InitTilesContainer() { | |
| 111 views::BoxLayout* tiles_layout_manager = | |
| 112 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing); | |
| 113 tiles_layout_manager->set_main_axis_alignment( | |
| 114 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); | |
| 115 tiles_container_->SetLayoutManager(tiles_layout_manager); | |
| 116 tiles_container_->set_background( | |
| 117 views::Background::CreateSolidBackground(kLabelBackgroundColor)); | |
| 118 | |
| 119 // Add SearchResultTileItemViews to the container. | |
| 120 for (size_t i = 0; i < kNumStartPageTiles; ++i) { | |
| 121 SearchResultTileItemView* tile_item = new SearchResultTileItemView(); | |
| 122 tiles_container_->AddChildView(tile_item); | |
| 123 tile_item->SetParentBackgroundColor(kLabelBackgroundColor); | |
| 124 search_result_tile_views_.push_back(tile_item); | |
| 125 } | |
| 126 | |
| 127 // Also add a special "all apps" button to the end of the container. | |
| 128 all_apps_button_ = new AllAppsTileItemView( | |
| 129 app_list_main_view_->contents_view(), | |
| 130 view_delegate_->GetModel()->top_level_item_list()); | |
| 131 all_apps_button_->UpdateIcon(); | |
| 132 all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor); | |
| 133 tiles_container_->AddChildView(all_apps_button_); | |
| 134 } | |
| 135 | |
| 136 void StartPageView::Reset() { | 191 void StartPageView::Reset() { |
| 137 Update(); | 192 tiles_container_->Update(); |
| 138 } | 193 } |
| 139 | 194 |
| 140 void StartPageView::UpdateForTesting() { | 195 void StartPageView::UpdateForTesting() { |
| 141 Update(); | 196 tiles_container_->Update(); |
| 197 } | |
| 198 | |
| 199 const std::vector<SearchResultTileItemView*>& StartPageView::tile_views() | |
| 200 const { | |
| 201 return tiles_container_->tile_views(); | |
| 142 } | 202 } |
| 143 | 203 |
| 144 TileItemView* StartPageView::all_apps_button() const { | 204 TileItemView* StartPageView::all_apps_button() const { |
| 145 return all_apps_button_; | 205 return tiles_container_->all_apps_button(); |
| 146 } | 206 } |
| 147 | 207 |
| 148 void StartPageView::OnShow() { | 208 void StartPageView::OnShow() { |
| 149 DCHECK(app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()); | 209 DCHECK(app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()); |
| 150 UpdateCustomPageClickzoneVisibility(); | 210 UpdateCustomPageClickzoneVisibility(); |
| 151 ClearSelectedIndex(); | 211 tiles_container_->ClearSelectedIndex(); |
| 152 } | 212 } |
| 153 | 213 |
| 154 void StartPageView::OnHide() { | 214 void StartPageView::OnHide() { |
| 155 DCHECK( | 215 DCHECK( |
| 156 !app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()); | 216 !app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()); |
| 157 UpdateCustomPageClickzoneVisibility(); | 217 UpdateCustomPageClickzoneVisibility(); |
| 158 } | 218 } |
| 159 | 219 |
| 160 void StartPageView::Layout() { | 220 void StartPageView::Layout() { |
| 161 gfx::Rect bounds(GetContentsBounds()); | 221 gfx::Rect bounds(GetContentsBounds()); |
| 162 bounds.set_height(instant_container_->GetHeightForWidth(bounds.width())); | 222 bounds.set_height(instant_container_->GetHeightForWidth(bounds.width())); |
| 163 instant_container_->SetBoundsRect(bounds); | 223 instant_container_->SetBoundsRect(bounds); |
| 164 | 224 |
| 165 // Tiles begin where the instant container ends. | 225 // Tiles begin where the instant container ends. |
| 166 bounds.set_y(bounds.bottom()); | 226 bounds.set_y(bounds.bottom()); |
| 167 bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width())); | 227 bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width())); |
| 168 tiles_container_->SetBoundsRect(bounds); | 228 tiles_container_->SetBoundsRect(bounds); |
| 169 } | 229 } |
| 170 | 230 |
| 171 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) { | 231 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) { |
| 172 if (selected_index() >= 0 && | 232 int selected_index = tiles_container_->selected_index(); |
| 173 tiles_container_->child_at(selected_index())->OnKeyPressed(event)) | 233 if (selected_index >= 0 && |
| 234 tiles_container_->child_at(selected_index)->OnKeyPressed(event)) | |
| 174 return true; | 235 return true; |
| 175 | 236 |
| 176 int dir = 0; | 237 int dir = 0; |
| 177 switch (event.key_code()) { | 238 switch (event.key_code()) { |
| 178 case ui::VKEY_LEFT: | 239 case ui::VKEY_LEFT: |
| 179 dir = -1; | 240 dir = -1; |
| 180 break; | 241 break; |
| 181 case ui::VKEY_RIGHT: | 242 case ui::VKEY_RIGHT: |
| 182 dir = 1; | 243 dir = 1; |
| 183 break; | 244 break; |
| 184 case ui::VKEY_DOWN: | 245 case ui::VKEY_DOWN: |
| 185 // Down selects the first tile if nothing is selected. | 246 // Down selects the first tile if nothing is selected. |
| 186 if (!IsValidSelectionIndex(selected_index())) | 247 if (!tiles_container_->IsValidSelectionIndex(selected_index)) |
| 187 dir = 1; | 248 dir = 1; |
| 188 break; | 249 break; |
| 189 case ui::VKEY_TAB: | 250 case ui::VKEY_TAB: |
| 190 dir = event.IsShiftDown() ? -1 : 1; | 251 dir = event.IsShiftDown() ? -1 : 1; |
| 191 break; | 252 break; |
| 192 default: | 253 default: |
| 193 break; | 254 break; |
| 194 } | 255 } |
| 195 | 256 |
| 196 if (dir == 0) | 257 if (dir == 0) |
| 197 return false; | 258 return false; |
| 198 | 259 |
| 199 if (!IsValidSelectionIndex(selected_index())) { | 260 if (!tiles_container_->IsValidSelectionIndex(selected_index)) { |
| 200 SetSelectedIndex(dir == -1 ? num_results() - 1 : 0); | 261 tiles_container_->SetSelectedIndex( |
| 262 dir == -1 ? tiles_container_->num_results() - 1 : 0); | |
| 201 return true; | 263 return true; |
| 202 } | 264 } |
| 203 | 265 |
| 204 int selection_index = selected_index() + dir; | 266 int selection_index = selected_index + dir; |
| 205 if (IsValidSelectionIndex(selection_index)) { | 267 if (tiles_container_->IsValidSelectionIndex(selection_index)) { |
| 206 SetSelectedIndex(selection_index); | 268 tiles_container_->SetSelectedIndex(selection_index); |
| 207 return true; | 269 return true; |
| 208 } | 270 } |
| 209 | 271 |
| 210 return false; | 272 return false; |
| 211 } | 273 } |
| 212 | 274 |
| 213 void StartPageView::OnContainerSelected(bool from_bottom) { | |
| 214 } | |
| 215 | |
| 216 gfx::Rect StartPageView::GetSearchBoxBounds() const { | 275 gfx::Rect StartPageView::GetSearchBoxBounds() const { |
| 217 return search_box_spacer_view_->bounds(); | 276 return search_box_spacer_view_->bounds(); |
| 218 } | 277 } |
| 219 | 278 |
| 220 void StartPageView::UpdateCustomPageClickzoneVisibility() { | 279 void StartPageView::UpdateCustomPageClickzoneVisibility() { |
| 221 // This can get called before InitWidgets(), so we cannot guarantee that | 280 // This can get called before InitWidgets(), so we cannot guarantee that |
| 222 // custom_page_clickzone_ will not be null. | 281 // custom_page_clickzone_ will not be null. |
| 223 views::Widget* custom_page_clickzone = | 282 views::Widget* custom_page_clickzone = |
| 224 app_list_main_view_->GetCustomPageClickzone(); | 283 app_list_main_view_->GetCustomPageClickzone(); |
| 225 if (!custom_page_clickzone) | 284 if (!custom_page_clickzone) |
| 226 return; | 285 return; |
| 227 | 286 |
| 228 if (app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()) { | 287 if (app_list_main_view_->contents_view()->ShouldShowCustomPageClickzone()) { |
| 229 custom_page_clickzone->ShowInactive(); | 288 custom_page_clickzone->ShowInactive(); |
| 230 return; | 289 return; |
| 231 } | 290 } |
| 232 | 291 |
| 233 custom_page_clickzone->Hide(); | 292 custom_page_clickzone->Hide(); |
| 234 } | 293 } |
| 235 | 294 |
| 236 int StartPageView::Update() { | |
| 237 std::vector<SearchResult*> display_results = | |
| 238 AppListModel::FilterSearchResultsByDisplayType( | |
| 239 results(), SearchResult::DISPLAY_RECOMMENDATION, kNumStartPageTiles); | |
| 240 | |
| 241 // Update the tile item results. | |
| 242 for (size_t i = 0; i < search_result_tile_views_.size(); ++i) { | |
| 243 SearchResult* item = NULL; | |
| 244 if (i < display_results.size()) | |
| 245 item = display_results[i]; | |
| 246 search_result_tile_views_[i]->SetSearchResult(item); | |
| 247 } | |
| 248 | |
| 249 tiles_container_->Layout(); | |
| 250 Layout(); | |
| 251 // Add 1 to the results size to account for the all apps button. | |
| 252 return display_results.size() + 1; | |
| 253 } | |
| 254 | |
| 255 void StartPageView::UpdateSelectedIndex(int old_selected, int new_selected) { | |
| 256 if (old_selected >= 0) | |
| 257 GetTileItemView(old_selected)->SetSelected(false); | |
| 258 | |
| 259 if (new_selected >= 0) | |
| 260 GetTileItemView(new_selected)->SetSelected(true); | |
| 261 } | |
| 262 | |
| 263 TileItemView* StartPageView::GetTileItemView(size_t index) { | 295 TileItemView* StartPageView::GetTileItemView(size_t index) { |
| 264 DCHECK_GT(kNumStartPageTiles + 1, index); | 296 return tiles_container_->GetTileItemView(index); |
| 265 if (index == kNumStartPageTiles) | |
| 266 return all_apps_button_; | |
| 267 | |
| 268 return search_result_tile_views_[index]; | |
| 269 } | 297 } |
| 270 | 298 |
| 271 } // namespace app_list | 299 } // namespace app_list |
| OLD | NEW |