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/views/app_list_main_view.h"
13 #include "ui/app_list/views/search_box_view.h"
14 #include "ui/app_list/views/search_result_list_view.h"
15 #include "ui/app_list/views/tile_item_view.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/views/background.h"
18 #include "ui/views/controls/image_view.h"
19 #include "ui/views/controls/label.h"
20 #include "ui/views/controls/textfield/textfield.h"
21 #include "ui/views/layout/box_layout.h"
22
23 namespace app_list {
24
25 namespace {
26
27 // Layout constants.
28 const int kTopMargin = 30;
29 const int kInstantContainerSpacing = 20;
30
31 // WebView constants.
32 const int kWebViewWidth = 200;
33 const int kWebViewHeight = 105;
34
35 // DummySearchBoxView constants.
36 const int kDummySearchBoxWidth = 490;
37 const int kDummySearchBoxHeight = 40;
38 const int kDummySearchBoxBorderWidth = 1;
39 const int kDummySearchBoxBorderBottomWidth = 2;
40 const int kDummySearchBoxBorderCornerRadius = 2;
41
42 // Tile container constants.
43 const size_t kNumStartPageTiles = 5;
44 const int kTileSpacing = 10;
45
46 // A background that paints a solid white rounded rect with a thin grey border.
47 class DummySearchBoxBackground : public views::Background {
48 public:
DummySearchBoxBackground()49 DummySearchBoxBackground() {}
~DummySearchBoxBackground()50 virtual ~DummySearchBoxBackground() {}
51
52 private:
53 // views::Background overrides:
Paint(gfx::Canvas * canvas,views::View * view) const54 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE {
55 gfx::Rect bounds = view->GetContentsBounds();
56
57 SkPaint paint;
58 paint.setFlags(SkPaint::kAntiAlias_Flag);
59 paint.setColor(kStartPageBorderColor);
60 canvas->DrawRoundRect(bounds, kDummySearchBoxBorderCornerRadius, paint);
61 bounds.Inset(kDummySearchBoxBorderWidth,
62 kDummySearchBoxBorderWidth,
63 kDummySearchBoxBorderWidth,
64 kDummySearchBoxBorderBottomWidth);
65 paint.setColor(SK_ColorWHITE);
66 canvas->DrawRoundRect(bounds, kDummySearchBoxBorderCornerRadius, paint);
67 }
68
69 DISALLOW_COPY_AND_ASSIGN(DummySearchBoxBackground);
70 };
71
72 // A placeholder search box which is sized to fit within the start page view.
73 class DummySearchBoxView : public SearchBoxView {
74 public:
DummySearchBoxView(SearchBoxViewDelegate * delegate,AppListViewDelegate * view_delegate)75 DummySearchBoxView(SearchBoxViewDelegate* delegate,
76 AppListViewDelegate* view_delegate)
77 : SearchBoxView(delegate, view_delegate) {
78 set_background(new DummySearchBoxBackground());
79 }
80
~DummySearchBoxView()81 virtual ~DummySearchBoxView() {}
82
83 // Overridden from views::View:
GetPreferredSize() const84 virtual gfx::Size GetPreferredSize() const OVERRIDE {
85 return gfx::Size(kDummySearchBoxWidth, kDummySearchBoxHeight);
86 }
87
88 private:
89 DISALLOW_COPY_AND_ASSIGN(DummySearchBoxView);
90 };
91
92 } // namespace
93
StartPageView(AppListMainView * app_list_main_view,AppListViewDelegate * view_delegate)94 StartPageView::StartPageView(AppListMainView* app_list_main_view,
95 AppListViewDelegate* view_delegate)
96 : app_list_main_view_(app_list_main_view),
97 model_(NULL),
98 view_delegate_(view_delegate),
99 search_box_view_(new DummySearchBoxView(this, view_delegate_)),
100 results_view_(
101 new SearchResultListView(app_list_main_view, view_delegate)),
102 instant_container_(new views::View),
103 tiles_container_(new views::View),
104 show_state_(SHOW_START_PAGE) {
105 // The view containing the start page WebContents and DummySearchBoxView.
106 InitInstantContainer();
107 AddChildView(instant_container_);
108
109 // The view containing the search results.
110 AddChildView(results_view_);
111
112 // The view containing the start page tiles.
113 InitTilesContainer();
114 AddChildView(tiles_container_);
115
116 SetModel(view_delegate_->GetModel());
117 view_delegate_->AddObserver(this);
118 }
119
~StartPageView()120 StartPageView::~StartPageView() {
121 view_delegate_->RemoveObserver(this);
122 if (model_)
123 model_->RemoveObserver(this);
124 }
125
InitInstantContainer()126 void StartPageView::InitInstantContainer() {
127 views::BoxLayout* instant_layout_manager = new views::BoxLayout(
128 views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing);
129 instant_layout_manager->set_inside_border_insets(
130 gfx::Insets(kTopMargin, 0, kInstantContainerSpacing, 0));
131 instant_layout_manager->set_main_axis_alignment(
132 views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
133 instant_container_->SetLayoutManager(instant_layout_manager);
134
135 views::View* web_view = view_delegate_->CreateStartPageWebView(
136 gfx::Size(kWebViewWidth, kWebViewHeight));
137 if (web_view)
138 instant_container_->AddChildView(web_view);
139
140 // TODO(calamity): This container is needed to horizontally center the search
141 // box view. Remove this container once BoxLayout supports CrossAxisAlignment.
142 views::View* search_box_container = new views::View();
143 views::BoxLayout* layout_manager =
144 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
145 layout_manager->set_main_axis_alignment(
146 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
147 search_box_container->SetLayoutManager(layout_manager);
148 search_box_container->AddChildView(search_box_view_);
149
150 instant_container_->AddChildView(search_box_container);
151 }
152
InitTilesContainer()153 void StartPageView::InitTilesContainer() {
154 views::BoxLayout* tiles_layout_manager =
155 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing);
156 tiles_layout_manager->set_main_axis_alignment(
157 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
158 tiles_container_->SetLayoutManager(tiles_layout_manager);
159 for (size_t i = 0; i < kNumStartPageTiles; ++i) {
160 TileItemView* tile_item = new TileItemView();
161 tiles_container_->AddChildView(tile_item);
162 tile_views_.push_back(tile_item);
163 }
164 }
165
SetModel(AppListModel * model)166 void StartPageView::SetModel(AppListModel* model) {
167 DCHECK(model);
168 if (model_)
169 model_->RemoveObserver(this);
170 model_ = model;
171 model_->AddObserver(this);
172 results_view_->SetResults(model_->results());
173 Reset();
174 }
175
Reset()176 void StartPageView::Reset() {
177 SetShowState(SHOW_START_PAGE);
178 if (!model_ || !model_->top_level_item_list())
179 return;
180
181 for (size_t i = 0; i < kNumStartPageTiles; ++i) {
182 AppListItem* item = NULL;
183 if (i < model_->top_level_item_list()->item_count())
184 item = model_->top_level_item_list()->item_at(i);
185 tile_views_[i]->SetAppListItem(item);
186 }
187 }
188
ShowSearchResults()189 void StartPageView::ShowSearchResults() {
190 SetShowState(SHOW_SEARCH_RESULTS);
191 }
192
SetShowState(ShowState show_state)193 void StartPageView::SetShowState(ShowState show_state) {
194 instant_container_->SetVisible(show_state == SHOW_START_PAGE);
195 results_view_->SetVisible(show_state == SHOW_SEARCH_RESULTS);
196
197 if (show_state == SHOW_START_PAGE)
198 search_box_view_->search_box()->RequestFocus();
199
200 if (show_state_ == show_state)
201 return;
202
203 show_state_ = show_state;
204
205 results_view_->UpdateAutoLaunchState();
206 if (show_state == SHOW_SEARCH_RESULTS)
207 results_view_->SetSelectedIndex(0);
208 }
209
IsShowingSearchResults() const210 bool StartPageView::IsShowingSearchResults() const {
211 return show_state_ == SHOW_SEARCH_RESULTS;
212 }
213
OnKeyPressed(const ui::KeyEvent & event)214 bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) {
215 if (show_state_ == SHOW_SEARCH_RESULTS)
216 return results_view_->OnKeyPressed(event);
217
218 return false;
219 }
220
Layout()221 void StartPageView::Layout() {
222 // Instant and search results take up the height of the instant container.
223 gfx::Rect bounds(GetContentsBounds());
224 bounds.set_height(instant_container_->GetHeightForWidth(bounds.width()));
225 instant_container_->SetBoundsRect(bounds);
226 results_view_->SetBoundsRect(bounds);
227
228 // Tiles begin where the instant container ends.
229 bounds.set_y(bounds.bottom());
230 bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width()));
231 tiles_container_->SetBoundsRect(bounds);
232 }
233
QueryChanged(SearchBoxView * sender)234 void StartPageView::QueryChanged(SearchBoxView* sender) {
235 // Forward the search terms on to the real search box and clear the dummy
236 // search box.
237 app_list_main_view_->OnStartPageSearchTextfieldChanged(
238 sender->search_box()->text());
239 sender->search_box()->SetText(base::string16());
240 }
241
OnProfilesChanged()242 void StartPageView::OnProfilesChanged() {
243 SetModel(view_delegate_->GetModel());
244 }
245
OnAppListModelStatusChanged()246 void StartPageView::OnAppListModelStatusChanged() {
247 Reset();
248 }
249
OnAppListItemAdded(AppListItem * item)250 void StartPageView::OnAppListItemAdded(AppListItem* item) {
251 Reset();
252 }
253
OnAppListItemDeleted()254 void StartPageView::OnAppListItemDeleted() {
255 Reset();
256 }
257
OnAppListItemUpdated(AppListItem * item)258 void StartPageView::OnAppListItemUpdated(AppListItem* item) {
259 Reset();
260 }
261
262 } // namespace app_list
263