Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / views / start_page_view.cc
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 = 30;
31 const int kInstantContainerSpacing = 20;
32
33 // WebView constants.
34 const int kWebViewWidth = 500;
35 const int kWebViewHeight = 105;
36
37 // DummySearchBoxView constants.
38 const int kDummySearchBoxWidth = 490;
39 const int kDummySearchBoxHeight = 40;
40 const int kDummySearchBoxBorderWidth = 1;
41 const int kDummySearchBoxBorderBottomWidth = 2;
42 const int kDummySearchBoxBorderCornerRadius = 2;
43
44 // Tile container constants.
45 const size_t kNumStartPageTiles = 5;
46 const int kTileSpacing = 10;
47
48 // A background that paints a solid white rounded rect with a thin grey border.
49 class DummySearchBoxBackground : public views::Background {
50  public:
51   DummySearchBoxBackground() {}
52   virtual ~DummySearchBoxBackground() {}
53
54  private:
55   // views::Background overrides:
56   virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
57     gfx::Rect bounds = view->GetContentsBounds();
58
59     SkPaint paint;
60     paint.setFlags(SkPaint::kAntiAlias_Flag);
61     paint.setColor(kStartPageBorderColor);
62     canvas->DrawRoundRect(bounds, kDummySearchBoxBorderCornerRadius, paint);
63     bounds.Inset(kDummySearchBoxBorderWidth,
64                  kDummySearchBoxBorderWidth,
65                  kDummySearchBoxBorderWidth,
66                  kDummySearchBoxBorderBottomWidth);
67     paint.setColor(SK_ColorWHITE);
68     canvas->DrawRoundRect(bounds, kDummySearchBoxBorderCornerRadius, paint);
69   }
70
71   DISALLOW_COPY_AND_ASSIGN(DummySearchBoxBackground);
72 };
73
74 // A placeholder search box which is sized to fit within the start page view.
75 class DummySearchBoxView : public SearchBoxView {
76  public:
77   DummySearchBoxView(SearchBoxViewDelegate* delegate,
78                      AppListViewDelegate* view_delegate)
79       : SearchBoxView(delegate, view_delegate) {
80     set_background(new DummySearchBoxBackground());
81   }
82
83   virtual ~DummySearchBoxView() {}
84
85   // Overridden from views::View:
86   virtual gfx::Size GetPreferredSize() const OVERRIDE {
87     return gfx::Size(kDummySearchBoxWidth, kDummySearchBoxHeight);
88   }
89
90  private:
91   DISALLOW_COPY_AND_ASSIGN(DummySearchBoxView);
92 };
93
94 }  // namespace
95
96 StartPageView::StartPageView(AppListMainView* app_list_main_view,
97                              AppListViewDelegate* view_delegate)
98     : app_list_main_view_(app_list_main_view),
99       search_results_model_(NULL),
100       view_delegate_(view_delegate),
101       search_box_view_(new DummySearchBoxView(this, view_delegate_)),
102       results_view_(
103           new SearchResultListView(app_list_main_view, view_delegate)),
104       instant_container_(new views::View),
105       tiles_container_(new views::View),
106       show_state_(SHOW_START_PAGE),
107       update_factory_(this) {
108   // The view containing the start page WebContents and DummySearchBoxView.
109   InitInstantContainer();
110   AddChildView(instant_container_);
111
112   // The view containing the search results.
113   AddChildView(results_view_);
114
115   // The view containing the start page tiles.
116   InitTilesContainer();
117   AddChildView(tiles_container_);
118
119   SetModel(view_delegate_->GetModel());
120   view_delegate_->AddObserver(this);
121 }
122
123 StartPageView::~StartPageView() {
124   view_delegate_->RemoveObserver(this);
125   if (search_results_model_)
126     search_results_model_->RemoveObserver(this);
127 }
128
129 void StartPageView::InitInstantContainer() {
130   views::BoxLayout* instant_layout_manager = new views::BoxLayout(
131       views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing);
132   instant_layout_manager->set_inside_border_insets(
133       gfx::Insets(kTopMargin, 0, kInstantContainerSpacing, 0));
134   instant_layout_manager->set_main_axis_alignment(
135       views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
136   instant_layout_manager->set_cross_axis_alignment(
137       views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
138   instant_container_->SetLayoutManager(instant_layout_manager);
139
140   views::View* web_view = view_delegate_->CreateStartPageWebView(
141       gfx::Size(kWebViewWidth, kWebViewHeight));
142   if (web_view)
143     instant_container_->AddChildView(web_view);
144
145   // TODO(calamity): This container is needed to horizontally center the search
146   // box view. Remove this container once BoxLayout supports CrossAxisAlignment.
147   views::View* search_box_container = new views::View();
148   views::BoxLayout* layout_manager =
149       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
150   layout_manager->set_main_axis_alignment(
151       views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
152   search_box_container->SetLayoutManager(layout_manager);
153   search_box_container->AddChildView(search_box_view_);
154
155   instant_container_->AddChildView(search_box_container);
156 }
157
158 void StartPageView::InitTilesContainer() {
159   views::BoxLayout* tiles_layout_manager =
160       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing);
161   tiles_layout_manager->set_main_axis_alignment(
162       views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
163   tiles_container_->SetLayoutManager(tiles_layout_manager);
164   for (size_t i = 0; i < kNumStartPageTiles; ++i) {
165     TileItemView* tile_item = new TileItemView();
166     tiles_container_->AddChildView(tile_item);
167     tile_views_.push_back(tile_item);
168   }
169 }
170
171 void StartPageView::SetModel(AppListModel* model) {
172   DCHECK(model);
173   if (search_results_model_)
174     search_results_model_->RemoveObserver(this);
175   search_results_model_ = model->results();
176   search_results_model_->AddObserver(this);
177   results_view_->SetResults(search_results_model_);
178   Reset();
179 }
180
181 void StartPageView::Reset() {
182   SetShowState(SHOW_START_PAGE);
183   Update();
184 }
185
186 void StartPageView::ShowSearchResults() {
187   SetShowState(SHOW_SEARCH_RESULTS);
188   Update();
189 }
190
191 void StartPageView::SetShowState(ShowState show_state) {
192   instant_container_->SetVisible(show_state == SHOW_START_PAGE);
193   results_view_->SetVisible(show_state == SHOW_SEARCH_RESULTS);
194
195   // This can be called when the app list is closing (widget is invisible). In
196   // that case, do not steal focus from other elements.
197   if (show_state == SHOW_START_PAGE && GetWidget() && GetWidget()->IsVisible())
198     search_box_view_->search_box()->RequestFocus();
199
200   if (show_state_ == show_state)
201     return;
202
203   show_state_ = show_state;
204
205   if (show_state_ == SHOW_START_PAGE)
206     search_box_view_->ClearSearch();
207
208   results_view_->UpdateAutoLaunchState();
209   if (show_state == SHOW_SEARCH_RESULTS)
210     results_view_->SetSelectedIndex(0);
211 }
212
213 bool StartPageView::IsShowingSearchResults() const {
214   return show_state_ == SHOW_SEARCH_RESULTS;
215 }
216
217 void StartPageView::UpdateForTesting() {
218   Update();
219 }
220
221 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) {
222   if (show_state_ == SHOW_SEARCH_RESULTS)
223     return results_view_->OnKeyPressed(event);
224
225   return false;
226 }
227
228 void StartPageView::Layout() {
229   // Instant and search results take up the height of the instant container.
230   gfx::Rect bounds(GetContentsBounds());
231   bounds.set_height(instant_container_->GetHeightForWidth(bounds.width()));
232   instant_container_->SetBoundsRect(bounds);
233   results_view_->SetBoundsRect(bounds);
234
235   // Tiles begin where the instant container ends.
236   bounds.set_y(bounds.bottom());
237   bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width()));
238   tiles_container_->SetBoundsRect(bounds);
239 }
240
241 void StartPageView::Update() {
242   std::vector<SearchResult*> display_results =
243       AppListModel::FilterSearchResultsByDisplayType(search_results_model_,
244                                                      SearchResult::DISPLAY_TILE,
245                                                      kNumStartPageTiles);
246   for (size_t i = 0; i < kNumStartPageTiles; ++i) {
247     SearchResult* item = NULL;
248     if (i < display_results.size())
249       item = display_results[i];
250     tile_views_[i]->SetSearchResult(item);
251   }
252   tiles_container_->Layout();
253   Layout();
254   update_factory_.InvalidateWeakPtrs();
255 }
256
257 void StartPageView::ScheduleUpdate() {
258   // When search results are added one by one, each addition generates an update
259   // request. Consolidates those update requests into one Update call.
260   if (!update_factory_.HasWeakPtrs()) {
261     base::MessageLoop::current()->PostTask(
262         FROM_HERE,
263         base::Bind(&StartPageView::Update, update_factory_.GetWeakPtr()));
264   }
265 }
266
267 void StartPageView::QueryChanged(SearchBoxView* sender) {
268   // Forward the search terms on to the real search box and clear the dummy
269   // search box.
270   app_list_main_view_->OnStartPageSearchTextfieldChanged(
271       sender->search_box()->text());
272   sender->search_box()->SetText(base::string16());
273 }
274
275 void StartPageView::OnProfilesChanged() {
276   SetModel(view_delegate_->GetModel());
277 }
278
279 void StartPageView::ListItemsAdded(size_t start, size_t count) {
280   ScheduleUpdate();
281 }
282
283 void StartPageView::ListItemsRemoved(size_t start, size_t count) {
284   ScheduleUpdate();
285 }
286
287 void StartPageView::ListItemMoved(size_t index, size_t target_index) {
288   ScheduleUpdate();
289 }
290
291 void StartPageView::ListItemsChanged(size_t start, size_t count) {
292   ScheduleUpdate();
293 }
294
295 }  // namespace app_list