1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/app_list/views/start_page_view.h"
#include <string>
#include "base/i18n/rtl.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/accessibility/ax_view_state.h"
#include "ui/app_list/app_list_constants.h"
#include "ui/app_list/app_list_item.h"
#include "ui/app_list/app_list_model.h"
#include "ui/app_list/app_list_view_delegate.h"
#include "ui/app_list/search_result.h"
#include "ui/app_list/views/all_apps_tile_item_view.h"
#include "ui/app_list/views/app_list_main_view.h"
#include "ui/app_list/views/contents_view.h"
#include "ui/app_list/views/custom_launcher_page_view.h"
#include "ui/app_list/views/search_box_view.h"
#include "ui/app_list/views/search_result_container_view.h"
#include "ui/app_list/views/search_result_tile_item_view.h"
#include "ui/app_list/views/tile_item_view.h"
#include "ui/gfx/canvas.h"
#include "ui/views/background.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/widget/widget.h"
namespace app_list {
namespace {
// Layout constants.
const int kInstantContainerSpacing = 24;
const int kSearchBoxAndTilesSpacing = 35;
const int kStartPageSearchBoxWidth = 480;
// WebView constants.
const int kWebViewWidth = 700;
const int kWebViewHeight = 244;
// Tile container constants.
const size_t kNumStartPageTiles = 4;
const int kTileSpacing = 7;
const int kLauncherPageBackgroundWidth = 400;
// An invisible placeholder view which fills the space for the search box view
// in a box layout. The search box view itself is a child of the AppListView
// (because it is visible on many different pages).
class SearchBoxSpacerView : public views::View {
public:
explicit SearchBoxSpacerView(const gfx::Size& search_box_size)
: size_(kStartPageSearchBoxWidth, search_box_size.height()) {}
~SearchBoxSpacerView() override {}
// Overridden from views::View:
gfx::Size GetPreferredSize() const override { return size_; }
private:
gfx::Size size_;
DISALLOW_COPY_AND_ASSIGN(SearchBoxSpacerView);
};
} // namespace
class CustomLauncherPageBackgroundView : public views::View {
public:
explicit CustomLauncherPageBackgroundView(
const std::string& custom_launcher_page_name)
: selected_(false),
custom_launcher_page_name_(custom_launcher_page_name) {
set_background(views::Background::CreateSolidBackground(kSelectedColor));
}
~CustomLauncherPageBackgroundView() override {}
void SetSelected(bool selected) {
selected_ = selected;
SetVisible(selected);
if (selected)
NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true);
}
bool selected() { return selected_; }
// Overridden from views::View:
void GetAccessibleState(ui::AXViewState* state) override {
state->role = ui::AX_ROLE_BUTTON;
state->name = base::UTF8ToUTF16(custom_launcher_page_name_);
}
private:
bool selected_;
std::string custom_launcher_page_name_;
DISALLOW_COPY_AND_ASSIGN(CustomLauncherPageBackgroundView);
};
// A container that holds the start page recommendation tiles and the all apps
// tile.
class StartPageView::StartPageTilesContainer
: public SearchResultContainerView {
public:
StartPageTilesContainer(ContentsView* contents_view,
AllAppsTileItemView* all_apps_button,
AppListViewDelegate* view_delegate);
~StartPageTilesContainer() override;
TileItemView* GetTileItemView(int index);
const std::vector<SearchResultTileItemView*>& tile_views() const {
return search_result_tile_views_;
}
AllAppsTileItemView* all_apps_button() { return all_apps_button_; }
// Overridden from SearchResultContainerView:
int Update() override;
void UpdateSelectedIndex(int old_selected, int new_selected) override;
void OnContainerSelected(bool from_bottom,
bool directional_movement) override;
void NotifyFirstResultYIndex(int y_index) override;
int GetYSize() override;
private:
ContentsView* contents_view_;
std::vector<SearchResultTileItemView*> search_result_tile_views_;
AllAppsTileItemView* all_apps_button_;
DISALLOW_COPY_AND_ASSIGN(StartPageTilesContainer);
};
StartPageView::StartPageTilesContainer::StartPageTilesContainer(
ContentsView* contents_view,
AllAppsTileItemView* all_apps_button,
AppListViewDelegate* view_delegate)
: contents_view_(contents_view), all_apps_button_(all_apps_button) {
views::BoxLayout* tiles_layout_manager =
new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, kTileSpacing);
tiles_layout_manager->set_main_axis_alignment(
views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
SetLayoutManager(tiles_layout_manager);
set_background(
views::Background::CreateSolidBackground(kLabelBackgroundColor));
// Add SearchResultTileItemViews to the container.
for (size_t i = 0; i < kNumStartPageTiles; ++i) {
SearchResultTileItemView* tile_item =
new SearchResultTileItemView(this, view_delegate);
AddChildView(tile_item);
tile_item->SetParentBackgroundColor(kLabelBackgroundColor);
tile_item->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW);
search_result_tile_views_.push_back(tile_item);
}
// Also add a special "all apps" button to the end of the container.
all_apps_button_->SetHoverStyle(TileItemView::HOVER_STYLE_ANIMATE_SHADOW);
all_apps_button_->UpdateIcon();
all_apps_button_->SetParentBackgroundColor(kLabelBackgroundColor);
AddChildView(all_apps_button_);
}
StartPageView::StartPageTilesContainer::~StartPageTilesContainer() {
}
TileItemView* StartPageView::StartPageTilesContainer::GetTileItemView(
int index) {
DCHECK_GT(num_results(), index);
if (index == num_results() - 1)
return all_apps_button_;
return search_result_tile_views_[index];
}
int StartPageView::StartPageTilesContainer::Update() {
// Ignore updates and disable buttons when transitioning to a different
// state.
if (contents_view_->GetActiveState() != AppListModel::STATE_START) {
for (auto* view : search_result_tile_views_)
view->SetEnabled(false);
return num_results();
}
std::vector<SearchResult*> display_results =
AppListModel::FilterSearchResultsByDisplayType(
results(), SearchResult::DISPLAY_RECOMMENDATION, kNumStartPageTiles);
// Update the tile item results.
for (size_t i = 0; i < search_result_tile_views_.size(); ++i) {
SearchResult* item = NULL;
if (i < display_results.size())
item = display_results[i];
search_result_tile_views_[i]->SetSearchResult(item);
search_result_tile_views_[i]->SetEnabled(true);
}
Layout();
parent()->Layout();
// Add 1 to the results size to account for the all apps button.
return display_results.size() + 1;
}
void StartPageView::StartPageTilesContainer::UpdateSelectedIndex(
int old_selected,
int new_selected) {
if (old_selected >= 0)
GetTileItemView(old_selected)->SetSelected(false);
if (new_selected >= 0)
GetTileItemView(new_selected)->SetSelected(true);
}
void StartPageView::StartPageTilesContainer::OnContainerSelected(
bool /*from_bottom*/,
bool /*directional_movement*/) {
NOTREACHED();
}
void StartPageView::StartPageTilesContainer::NotifyFirstResultYIndex(
int /*y_index*/) {
NOTREACHED();
}
int StartPageView::StartPageTilesContainer::GetYSize() {
NOTREACHED();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
// StartPageView implementation:
StartPageView::StartPageView(AppListMainView* app_list_main_view,
AppListViewDelegate* view_delegate)
: app_list_main_view_(app_list_main_view),
view_delegate_(view_delegate),
search_box_spacer_view_(new SearchBoxSpacerView(
app_list_main_view->search_box_view()->GetPreferredSize())),
instant_container_(new views::View),
custom_launcher_page_background_(new CustomLauncherPageBackgroundView(
view_delegate_->GetModel()->custom_launcher_page_name())),
tiles_container_(new StartPageTilesContainer(
app_list_main_view->contents_view(),
new AllAppsTileItemView(
app_list_main_view_->contents_view(),
view_delegate_->GetModel()->top_level_item_list()),
view_delegate)) {
// The view containing the start page WebContents and SearchBoxSpacerView.
InitInstantContainer();
AddChildView(instant_container_);
// The view containing the start page tiles.
AddChildView(tiles_container_);
AddChildView(custom_launcher_page_background_);
tiles_container_->SetResults(view_delegate_->GetModel()->results());
Reset();
}
StartPageView::~StartPageView() {
}
void StartPageView::InitInstantContainer() {
views::BoxLayout* instant_layout_manager = new views::BoxLayout(
views::BoxLayout::kVertical, 0, 0, kInstantContainerSpacing);
instant_layout_manager->set_inside_border_insets(
gfx::Insets(0, 0, kSearchBoxAndTilesSpacing, 0));
instant_layout_manager->set_main_axis_alignment(
views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
instant_layout_manager->set_cross_axis_alignment(
views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
instant_container_->SetLayoutManager(instant_layout_manager);
views::View* web_view = view_delegate_->CreateStartPageWebView(
gfx::Size(kWebViewWidth, kWebViewHeight));
if (web_view) {
web_view->SetFocusable(false);
instant_container_->AddChildView(web_view);
}
instant_container_->AddChildView(search_box_spacer_view_);
}
void StartPageView::MaybeOpenCustomLauncherPage() {
// Switch to the custom page.
ContentsView* contents_view = app_list_main_view_->contents_view();
if (!app_list_main_view_->ShouldShowCustomLauncherPage())
return;
UMA_HISTOGRAM_ENUMERATION(kPageOpenedHistogram,
AppListModel::STATE_CUSTOM_LAUNCHER_PAGE,
AppListModel::STATE_LAST);
contents_view->SetActiveState(AppListModel::STATE_CUSTOM_LAUNCHER_PAGE);
}
void StartPageView::Reset() {
tiles_container_->Update();
}
void StartPageView::UpdateForTesting() {
tiles_container_->Update();
}
const std::vector<SearchResultTileItemView*>& StartPageView::tile_views()
const {
return tiles_container_->tile_views();
}
TileItemView* StartPageView::all_apps_button() const {
return tiles_container_->all_apps_button();
}
void StartPageView::OnShown() {
// When the start page is shown, show or hide the custom launcher page
// based on whether it is enabled.
CustomLauncherPageView* custom_page_view =
app_list_main_view_->contents_view()->custom_page_view();
if (custom_page_view) {
custom_page_view->SetVisible(
app_list_main_view_->ShouldShowCustomLauncherPage());
}
tiles_container_->Update();
tiles_container_->ClearSelectedIndex();
custom_launcher_page_background_->SetSelected(false);
}
gfx::Rect StartPageView::GetPageBoundsForState(
AppListModel::State state) const {
gfx::Rect onscreen_bounds(GetFullContentsBounds());
if (state == AppListModel::STATE_START)
return onscreen_bounds;
return GetAboveContentsOffscreenBounds(onscreen_bounds.size());
}
gfx::Rect StartPageView::GetSearchBoxBounds() const {
return search_box_spacer_view_->bounds() +
GetPageBoundsForState(AppListModel::STATE_START).OffsetFromOrigin();
}
void StartPageView::Layout() {
gfx::Rect bounds(GetContentsBounds());
bounds.set_height(instant_container_->GetHeightForWidth(bounds.width()));
instant_container_->SetBoundsRect(bounds);
// Tiles begin where the instant container ends.
bounds.set_y(bounds.bottom());
bounds.set_height(tiles_container_->GetHeightForWidth(bounds.width()));
tiles_container_->SetBoundsRect(bounds);
CustomLauncherPageView* custom_launcher_page_view =
app_list_main_view_->contents_view()->custom_page_view();
if (!custom_launcher_page_view)
return;
bounds = app_list_main_view_->contents_view()
->custom_page_view()
->GetCollapsedLauncherPageBounds();
bounds.Intersect(GetContentsBounds());
bounds.ClampToCenteredSize(
gfx::Size(kLauncherPageBackgroundWidth, bounds.height()));
custom_launcher_page_background_->SetBoundsRect(bounds);
}
bool StartPageView::OnKeyPressed(const ui::KeyEvent& event) {
const int forward_dir = base::i18n::IsRTL() ? -1 : 1;
int selected_index = tiles_container_->selected_index();
if (custom_launcher_page_background_->selected()) {
selected_index = tiles_container_->num_results();
switch (event.key_code()) {
case ui::VKEY_RETURN:
MaybeOpenCustomLauncherPage();
return true;
default:
break;
}
} else if (selected_index >= 0 &&
tiles_container_->GetTileItemView(selected_index)
->OnKeyPressed(event)) {
return true;
}
int dir = 0;
switch (event.key_code()) {
case ui::VKEY_LEFT:
dir = -forward_dir;
break;
case ui::VKEY_RIGHT:
// Don't go to the custom launcher page from the All apps tile.
if (selected_index != tiles_container_->num_results() - 1)
dir = forward_dir;
break;
case ui::VKEY_UP:
// Up selects the first tile if the custom launcher is selected.
if (custom_launcher_page_background_->selected()) {
selected_index = -1;
dir = 1;
}
break;
case ui::VKEY_DOWN:
// Down selects the first tile if nothing is selected.
dir = 1;
// If something is selected, select the custom launcher page.
if (tiles_container_->IsValidSelectionIndex(selected_index))
selected_index = tiles_container_->num_results() - 1;
break;
case ui::VKEY_TAB:
dir = event.IsShiftDown() ? -1 : 1;
break;
default:
break;
}
if (dir == 0)
return false;
if (selected_index == -1) {
custom_launcher_page_background_->SetSelected(false);
tiles_container_->SetSelectedIndex(
dir == -1 ? tiles_container_->num_results() - 1 : 0);
return true;
}
int selection_index = selected_index + dir;
if (tiles_container_->IsValidSelectionIndex(selection_index)) {
custom_launcher_page_background_->SetSelected(false);
tiles_container_->SetSelectedIndex(selection_index);
return true;
}
if (selection_index == tiles_container_->num_results() &&
app_list_main_view_->ShouldShowCustomLauncherPage()) {
custom_launcher_page_background_->SetSelected(true);
tiles_container_->ClearSelectedIndex();
return true;
}
if (event.key_code() == ui::VKEY_TAB && selection_index == -1)
tiles_container_->ClearSelectedIndex(); // ContentsView will handle focus.
return false;
}
bool StartPageView::OnMousePressed(const ui::MouseEvent& event) {
ContentsView* contents_view = app_list_main_view_->contents_view();
if (contents_view->custom_page_view() &&
!contents_view->custom_page_view()
->GetCollapsedLauncherPageBounds()
.Contains(event.location())) {
return false;
}
MaybeOpenCustomLauncherPage();
return true;
}
bool StartPageView::OnMouseWheel(const ui::MouseWheelEvent& event) {
// Negative y_offset is a downward scroll.
if (event.y_offset() < 0) {
MaybeOpenCustomLauncherPage();
return true;
}
return false;
}
void StartPageView::OnGestureEvent(ui::GestureEvent* event) {
if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN &&
event->details().scroll_y_hint() < 0) {
MaybeOpenCustomLauncherPage();
}
ContentsView* contents_view = app_list_main_view_->contents_view();
if (event->type() == ui::ET_GESTURE_TAP &&
contents_view->custom_page_view() &&
contents_view->custom_page_view()
->GetCollapsedLauncherPageBounds()
.Contains(event->location())) {
MaybeOpenCustomLauncherPage();
}
}
void StartPageView::OnScrollEvent(ui::ScrollEvent* event) {
// Negative y_offset is a downward scroll (or upward, if Australian Scrolling
// is enabled).
if (event->type() == ui::ET_SCROLL && event->y_offset() < 0)
MaybeOpenCustomLauncherPage();
}
TileItemView* StartPageView::GetTileItemView(size_t index) {
return tiles_container_->GetTileItemView(index);
}
} // namespace app_list
|