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.
5 #include "ui/app_list/views/search_result_page_view.h"
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/app_list/app_list_view_delegate.h"
9 #include "ui/app_list/views/app_list_main_view.h"
10 #include "ui/app_list/views/search_result_list_view.h"
11 #include "ui/app_list/views/search_result_tile_item_list_view.h"
12 #include "ui/views/background.h"
13 #include "ui/views/layout/box_layout.h"
14 #include "ui/views/layout/fill_layout.h"
15 #include "ui/views/shadow_border.h"
21 const int kGroupSpacing
= 20;
22 const int kTopPadding
= 5;
24 // A container view that ensures the card background and the shadow are painted
25 // in the correct order.
26 class SearchCardView
: public views::View
{
28 explicit SearchCardView(views::View
* content_view
) {
29 SetBorder(make_scoped_ptr(new views::ShadowBorder(
30 kCardShadowBlur
, kCardShadowColor
, kCardShadowYOffset
, 0)));
31 SetLayoutManager(new views::FillLayout());
32 content_view
->set_background(
33 views::Background::CreateSolidBackground(kCardBackgroundColor
));
34 AddChildView(content_view
);
37 ~SearchCardView() override
{}
39 void ChildPreferredSizeChanged(views::View
* child
) override
{
41 PreferredSizeChanged();
47 SearchResultPageView::SearchResultPageView() : selected_index_(0) {
48 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical
,
49 kExperimentalWindowPadding
, kTopPadding
,
53 SearchResultPageView::~SearchResultPageView() {
56 void SearchResultPageView::AddSearchResultContainerView(
57 AppListModel::SearchResults
* results_model
,
58 SearchResultContainerView
* result_container
) {
59 AddChildView(new SearchCardView(result_container
));
60 result_container_views_
.push_back(result_container
);
61 result_container
->SetResults(results_model
);
64 bool SearchResultPageView::OnKeyPressed(const ui::KeyEvent
& event
) {
65 if (result_container_views_
.at(selected_index_
)->OnKeyPressed(event
))
69 switch (event
.key_code()) {
71 dir
= event
.IsShiftDown() ? -1 : 1;
83 // Find the next result container with results.
84 int new_selected
= selected_index_
;
87 } while (IsValidSelectionIndex(new_selected
) &&
88 result_container_views_
[new_selected
]->num_results() == 0);
90 if (IsValidSelectionIndex(new_selected
)) {
91 SetSelectedIndex(new_selected
);
95 // Capture the Tab key to prevent defocusing of the search box.
96 return event
.key_code() == ui::VKEY_TAB
;
99 void SearchResultPageView::SetSelectedIndex(int index
) {
100 bool from_bottom
= index
< selected_index_
;
102 // Reset the old selected view's selection.
103 result_container_views_
[selected_index_
]->ClearSelectedIndex();
104 selected_index_
= index
;
105 // Set the new selected view's selection to its first result.
106 result_container_views_
[selected_index_
]->OnContainerSelected(from_bottom
);
109 bool SearchResultPageView::IsValidSelectionIndex(int index
) {
110 return index
>= 0 && index
< static_cast<int>(result_container_views_
.size());
113 void SearchResultPageView::ChildPreferredSizeChanged(views::View
* child
) {
114 DCHECK(!result_container_views_
.empty());
119 } // namespace app_list