From d480d0e4974707af2c9af65f08d1df54278e6609 Mon Sep 17 00:00:00 2001
From: "mgiuca@chromium.org"
 <mgiuca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Thu, 5 Jun 2014 14:49:02 +0000
Subject: [PATCH] Refactor app list ContentsView to use pointer members to find
 views.

Removed access functions GetAppsContainerView and friends (which look up
a specific index in view_model_ and static_cast it); just directly use
the private fields of ContentsView that hold pointers of the correct
type to these special views.

Added such a pointer for the SearchResultListView.

BUG=377381

Review URL: https://codereview.chromium.org/312213005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@275109 0039d316-1c4b-4281-b951-d872f2087c98
---
 ui/app_list/views/contents_view.cc | 46 ++++++++++--------------------
 ui/app_list/views/contents_view.h  |  7 +++--
 2 files changed, 20 insertions(+), 33 deletions(-)

diff --git a/ui/app_list/views/contents_view.cc b/ui/app_list/views/contents_view.cc
index a05516866a85c..0fcea771c37c7 100644
--- a/ui/app_list/views/contents_view.cc
+++ b/ui/app_list/views/contents_view.cc
@@ -37,19 +37,6 @@ const int kMinHorizVelocityToSwitchPage = 800;
 
 const double kFinishTransitionThreshold = 0.33;
 
-AppsContainerView* GetAppsContainerView(views::ViewModel* model) {
-  return static_cast<AppsContainerView*>(model->view_at(kIndexAppsContainer));
-}
-
-SearchResultListView* GetSearchResultListView(views::ViewModel* model) {
-  return static_cast<SearchResultListView*>(
-      model->view_at(kIndexSearchResults));
-}
-
-StartPageView* GetStartPageView(views::ViewModel* model) {
-  return static_cast<StartPageView*>(model->view_at(kIndexStartPage));
-}
-
 }  // namespace
 
 ContentsView::ContentsView(AppListMainView* app_list_main_view,
@@ -66,10 +53,10 @@ ContentsView::ContentsView(AppListMainView* app_list_main_view,
   AddChildView(apps_container_view_);
   view_model_->Add(apps_container_view_, kIndexAppsContainer);
 
-  SearchResultListView* search_results_view = new SearchResultListView(
-      app_list_main_view, view_delegate);
-  AddChildView(search_results_view);
-  view_model_->Add(search_results_view, kIndexSearchResults);
+  search_results_view_ =
+      new SearchResultListView(app_list_main_view, view_delegate);
+  AddChildView(search_results_view_);
+  view_model_->Add(search_results_view_, kIndexSearchResults);
 
   if (app_list::switches::IsExperimentalAppListEnabled()) {
     start_page_view_ = new StartPageView(app_list_main_view, view_delegate);
@@ -77,7 +64,7 @@ ContentsView::ContentsView(AppListMainView* app_list_main_view,
     view_model_->Add(start_page_view_, kIndexStartPage);
   }
 
-  GetSearchResultListView(view_model_.get())->SetResults(model->results());
+  search_results_view_->SetResults(model->results());
 }
 
 ContentsView::~ContentsView() {
@@ -108,18 +95,16 @@ void ContentsView::SetShowState(ShowState show_state) {
 }
 
 void ContentsView::ShowStateChanged() {
-  SearchResultListView* results_view =
-      GetSearchResultListView(view_model_.get());
   // TODO(xiyuan): Highlight default match instead of the first.
-  if (show_state_ == SHOW_SEARCH_RESULTS && results_view->visible())
-    results_view->SetSelectedIndex(0);
-  results_view->UpdateAutoLaunchState();
+  if (show_state_ == SHOW_SEARCH_RESULTS && search_results_view_->visible())
+    search_results_view_->SetSelectedIndex(0);
+  search_results_view_->UpdateAutoLaunchState();
 
   // Notify parent AppListMainView of show state change.
   app_list_main_view_->OnContentsViewShowStateChanged();
 
   if (show_state_ == SHOW_START_PAGE)
-    GetStartPageView(view_model_.get())->Reset();
+    start_page_view_->Reset();
 
   AnimateToIdealBounds();
 }
@@ -207,10 +192,9 @@ void ContentsView::Prerender() {
 }
 
 gfx::Size ContentsView::GetPreferredSize() const {
-  const gfx::Size container_size = GetAppsContainerView(view_model_.get())->
-      apps_grid_view()->GetPreferredSize();
-  const gfx::Size results_size =
-      GetSearchResultListView(view_model_.get())->GetPreferredSize();
+  const gfx::Size container_size =
+      apps_container_view_->apps_grid_view()->GetPreferredSize();
+  const gfx::Size results_size = search_results_view_->GetPreferredSize();
 
   int width = std::max(container_size.width(), results_size.width());
   int height = std::max(container_size.height(), results_size.height());
@@ -225,11 +209,11 @@ void ContentsView::Layout() {
 bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
   switch (show_state_) {
     case SHOW_APPS:
-      return GetAppsContainerView(view_model_.get())->OnKeyPressed(event);
+      return apps_container_view_->OnKeyPressed(event);
     case SHOW_SEARCH_RESULTS:
-      return GetSearchResultListView(view_model_.get())->OnKeyPressed(event);
+      return search_results_view_->OnKeyPressed(event);
     case SHOW_START_PAGE:
-      return GetStartPageView(view_model_.get())->OnKeyPressed(event);
+      return start_page_view_->OnKeyPressed(event);
     default:
       NOTREACHED() << "Unknown show state " << show_state_;
   }
diff --git a/ui/app_list/views/contents_view.h b/ui/app_list/views/contents_view.h
index 670eae169a4a7..e7df7bc0ccbf4 100644
--- a/ui/app_list/views/contents_view.h
+++ b/ui/app_list/views/contents_view.h
@@ -30,6 +30,7 @@ class AppListModel;
 class AppListViewDelegate;
 class AppsContainerView;
 class PaginationModel;
+class SearchResultListView;
 class StartPageView;
 
 // A view to manage sub views under the search box (apps grid view + page
@@ -92,8 +93,10 @@ class APP_LIST_EXPORT ContentsView : public views::View {
 
   ShowState show_state_;
 
-  AppsContainerView* apps_container_view_;  // Owned by the views hierarchy.
-  StartPageView* start_page_view_;          // Owned by the views hierarchy.
+  // Special sub views of the ContentsView. All owned by the views hierarchy.
+  AppsContainerView* apps_container_view_;
+  SearchResultListView* search_results_view_;
+  StartPageView* start_page_view_;
 
   AppListMainView* app_list_main_view_;     // Parent view, owns this.