• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "ui/app_list/views/start_page_view.h"
6 
7 #include "base/strings/utf_string_conversions.h"
8 #include "ui/app_list/app_list_constants.h"
9 #include "ui/app_list/app_list_item.h"
10 #include "ui/app_list/app_list_model.h"
11 #include "ui/app_list/app_list_view_delegate.h"
12 #include "ui/app_list/search_result.h"
13 #include "ui/app_list/views/app_list_main_view.h"
14 #include "ui/app_list/views/search_box_view.h"
15 #include "ui/app_list/views/search_result_list_view.h"
16 #include "ui/app_list/views/tile_item_view.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/views/background.h"
19 #include "ui/views/controls/image_view.h"
20 #include "ui/views/controls/label.h"
21 #include "ui/views/controls/textfield/textfield.h"
22 #include "ui/views/layout/box_layout.h"
23 #include "ui/views/widget/widget.h"
24 
25 namespace app_list {
26 
27 namespace {
28 
29 // Layout constants.
30 const int kTopMargin = 84;
31 const int kInstantContainerSpacing = 11;
32 const int kSearchBoxAndTilesSpacing = 40;
33 
34 // WebView constants.
35 const int kWebViewWidth = 500;
36 const int kWebViewHeight = 105;
37 
38 // DummySearchBoxView constants.
39 const int kDummySearchBoxWidth = 480;
40 
41 // Tile container constants.
42 const size_t kNumStartPageTiles = 5;
43 const int kTileSpacing = 10;
44 
45 // A placeholder search box which is sized to fit within the start page view.
46 class DummySearchBoxView : public SearchBoxView {
47  public:
DummySearchBoxView(SearchBoxViewDelegate * delegate,AppListViewDelegate * view_delegate)48   DummySearchBoxView(SearchBoxViewDelegate* delegate,
49                      AppListViewDelegate* view_delegate)
50       : SearchBoxView(delegate, view_delegate) {
51   }
52 
~DummySearchBoxView()53   virtual ~DummySearchBoxView() {}
54 
55   // Overridden from views::View:
GetPreferredSize() const56   virtual gfx::Size GetPreferredSize() const OVERRIDE {
57     gfx::Size size(SearchBoxView::GetPreferredSize());
58     size.set_width(kDummySearchBoxWidth);
59     return size;
60   }
61 
62  private:
63   DISALLOW_COPY_AND_ASSIGN(DummySearchBoxView);
64 };
65 
66 }  // namespace
67 
StartPageView(AppListMainView * app_list_main_view,AppListViewDelegate * view_delegate)68 StartPageView::StartPageView(AppListMainView* app_list_main_view,
69                              AppListViewDelegate* view_delegate)
70     : app_list_main_view_(app_list_main_view),
71       search_results_model_(NULL),
72       view_delegate_(view_delegate),
73       search_box_view_(new DummySearchBoxView(this, view_delegate_)),
74       results_view_(
75           new SearchResultListView(app_list_main_view, view_delegate)),
76       instant_container_(new views::View),
77       tiles_container_(new views::View),
78       show_state_(SHOW_START_PAGE),
79       update_factory_(this) {
80   // The view containing the start page WebContents and DummySearchBoxView.
81   InitInstantContainer();
82   AddChildView(instant_container_);
83 
84   // The view containing the search results.
85   AddChildView(results_view_);
86 
87   // The view containing the start page tiles.
88   InitTilesContainer();
89   AddChildView(tiles_container_);
90 
91   SetModel(view_delegate_->GetModel());
92 }
93 
~StartPageView()94 StartPageView::~StartPageView() {
95   if (search_results_model_)
96     search_results_model_->RemoveObserver(this);
97 }
98 
InitInstantContainer()99 void StartPageView::InitInstantContainer() {
100   views::BoxLayout* instant_layout_manager = new views::BoxLayout(
101       views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing);
102   instant_layout_manager->set_inside_border_insets(
103       gfx::Insets(kTopMargin, 0, kSearchBoxAndTilesSpacing, 0));
104   instant_layout_manager->set_main_axis_alignment(
105       views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
106   instant_layout_manager->set_cross_axis_alignment(
107       views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
108   instant_container_->SetLayoutManager(instant_layout_manager);
109 
110   views::View* web_view = view_delegate_->CreateStartPageWebView(
111       gfx::Size(kWebViewWidth, kWebViewHeight));
112   if (web_view)
113     instant_container_->AddChildView(web_view);
114 
115   // TODO(calamity): This container is needed to horizontally center the search
116   // box view. Remove this container once BoxLayout supports CrossAxisAlignment.
117   views::View* search_box_container = new views::View();
118   views::BoxLayout* layout_manager =
119       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
120   layout_manager->set_main_axis_alignment(
121       views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
122   search_box_container->SetLayoutManager(layout_manager);
123   search_box_container->AddChildView(search_box_view_);
124 
125   instant_container_->AddChildView(search_box_container);
126 }
127 
InitTilesContainer()128 void StartPageView::InitTilesContainer() {
129   views::BoxLayout* tiles_layout_manager =
130       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing);
131   tiles_layout_manager->set_main_axis_alignment(
132       views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
133   tiles_container_->SetLayoutManager(tiles_layout_manager);
134   for (size_t i = 0; i < kNumStartPageTiles; ++i) {
135     TileItemView* tile_item = new TileItemView();
136     tiles_container_->AddChildView(tile_item);
137     tile_views_.push_back(tile_item);
138   }
139 }
140 
SetModel(AppListModel * model)141 void StartPageView::SetModel(AppListModel* model) {
142   DCHECK(model);
143   if (search_results_model_)
144     search_results_model_->RemoveObserver(this);
145   search_results_model_ = model->results();
146   search_results_model_->AddObserver(this);
147   results_view_->SetResults(search_results_model_);
148   Reset();
149 }
150 
Reset()151 void StartPageView::Reset() {
152   SetShowState(SHOW_START_PAGE);
153   Update();
154 }
155 
ShowSearchResults()156 void StartPageView::ShowSearchResults() {
157   SetShowState(SHOW_SEARCH_RESULTS);
158   Update();
159 }
160 
SetShowState(ShowState show_state)161 void StartPageView::SetShowState(ShowState show_state) {
162   instant_container_->SetVisible(show_state == SHOW_START_PAGE);
163   results_view_->SetVisible(show_state == SHOW_SEARCH_RESULTS);
164 
165   // This can be called when the app list is closing (widget is invisible). In
166   // that case, do not steal focus from other elements.
167   if (show_state == SHOW_START_PAGE && GetWidget() && GetWidget()->IsVisible())
168     search_box_view_->search_box()->RequestFocus();
169 
170   if (show_state_ == show_state)
171     return;
172 
173   show_state_ = show_state;
174 
175   if (show_state_ == SHOW_START_PAGE)
176     search_box_view_->ClearSearch();
177 
178   results_view_->UpdateAutoLaunchState();
179   if (show_state == SHOW_SEARCH_RESULTS)
180     results_view_->SetSelectedIndex(0);
181 }
182 
IsShowingSearchResults() const183 bool StartPageView::IsShowingSearchResults() const {
184   return show_state_ == SHOW_SEARCH_RESULTS;
185 }
186 
UpdateForTesting()187 void StartPageView::UpdateForTesting() {
188   Update();
189 }
190 
OnKeyPressed(const ui::KeyEvent & event)191 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) {
192   if (show_state_ == SHOW_SEARCH_RESULTS)
193     return results_view_->OnKeyPressed(event);
194 
195   return false;
196 }
197 
Layout()198 void StartPageView::Layout() {
199   // Instant and search results take up the height of the instant container.
200   gfx::Rect bounds(GetContentsBounds());
201   bounds.set_height(instant_container_->GetHeightForWidth(bounds.width()));
202   instant_container_->SetBoundsRect(bounds);
203   results_view_->SetBoundsRect(bounds);
204 
205   // Tiles begin where the instant container ends.
206   bounds.set_y(bounds.bottom());
207   bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width()));
208   tiles_container_->SetBoundsRect(bounds);
209 }
210 
Update()211 void StartPageView::Update() {
212   std::vector<SearchResult*> display_results =
213       AppListModel::FilterSearchResultsByDisplayType(search_results_model_,
214                                                      SearchResult::DISPLAY_TILE,
215                                                      kNumStartPageTiles);
216   for (size_t i = 0; i < kNumStartPageTiles; ++i) {
217     SearchResult* item = NULL;
218     if (i < display_results.size())
219       item = display_results[i];
220     tile_views_[i]->SetSearchResult(item);
221   }
222   tiles_container_->Layout();
223   Layout();
224   update_factory_.InvalidateWeakPtrs();
225 }
226 
ScheduleUpdate()227 void StartPageView::ScheduleUpdate() {
228   // When search results are added one by one, each addition generates an update
229   // request. Consolidates those update requests into one Update call.
230   if (!update_factory_.HasWeakPtrs()) {
231     base::MessageLoop::current()->PostTask(
232         FROM_HERE,
233         base::Bind(&StartPageView::Update, update_factory_.GetWeakPtr()));
234   }
235 }
236 
QueryChanged(SearchBoxView * sender)237 void StartPageView::QueryChanged(SearchBoxView* sender) {
238   // Forward the search terms on to the real search box and clear the dummy
239   // search box.
240   app_list_main_view_->OnStartPageSearchTextfieldChanged(
241       sender->search_box()->text());
242   sender->search_box()->SetText(base::string16());
243 }
244 
ListItemsAdded(size_t start,size_t count)245 void StartPageView::ListItemsAdded(size_t start, size_t count) {
246   ScheduleUpdate();
247 }
248 
ListItemsRemoved(size_t start,size_t count)249 void StartPageView::ListItemsRemoved(size_t start, size_t count) {
250   ScheduleUpdate();
251 }
252 
ListItemMoved(size_t index,size_t target_index)253 void StartPageView::ListItemMoved(size_t index, size_t target_index) {
254   ScheduleUpdate();
255 }
256 
ListItemsChanged(size_t start,size_t count)257 void StartPageView::ListItemsChanged(size_t start, size_t count) {
258   ScheduleUpdate();
259 }
260 
261 }  // namespace app_list
262