Skip to content

Commit

Permalink
Add a selection index to SearchResultPageView.
Browse files Browse the repository at this point in the history
This CL adds a selection index to SearchResultPageView which selects
between its child SearchResultContainerViews.

BUG=416756

Review URL: https://codereview.chromium.org/722113005

Cr-Commit-Position: refs/heads/master@{#304749}
  • Loading branch information
nik3daz authored and Commit bot committed Nov 19, 2014
1 parent b84edfb commit d3abd58
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 9 deletions.
4 changes: 4 additions & 0 deletions ui/app_list/test/test_search_result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ TestSearchResult::TestSearchResult() {
TestSearchResult::~TestSearchResult() {
}

void TestSearchResult::SetDisplayType(SearchResult::DisplayType type) {
set_display_type(type);
}

scoped_ptr<SearchResult> TestSearchResult::Duplicate() {
NOTREACHED();
return scoped_ptr<SearchResult>();
Expand Down
2 changes: 2 additions & 0 deletions ui/app_list/test/test_search_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TestSearchResult : public SearchResult {
// SearchResult:
scoped_ptr<SearchResult> Duplicate() override;

void SetDisplayType(SearchResult::DisplayType type);

private:
DISALLOW_COPY_AND_ASSIGN(TestSearchResult);
};
Expand Down
3 changes: 1 addition & 2 deletions ui/app_list/views/search_result_list_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ void SearchResultListView::OnContainerSelected(bool from_bottom) {
if (num_results() == 0)
return;

// TODO(calamity): select result based on |from_bottom|.
SetSelectedIndex(0);
SetSelectedIndex(from_bottom ? num_results() - 1 : 0);
}

int SearchResultListView::Update() {
Expand Down
7 changes: 5 additions & 2 deletions ui/app_list/views/search_result_list_view_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ class SearchResultListViewTest : public views::ViewsTestBase,

void SetUpSearchResults() {
AppListModel::SearchResults* results = GetResults();
for (int i = 0; i < kDefaultSearchItems; ++i)
results->Add(new TestSearchResult());
for (int i = 0; i < kDefaultSearchItems; ++i) {
TestSearchResult* result = new TestSearchResult();
result->SetDisplayType(SearchResult::DISPLAY_LIST);
results->Add(result);
}

// Adding results will schedule Update().
RunPendingMessages();
Expand Down
53 changes: 48 additions & 5 deletions ui/app_list/views/search_result_page_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class SearchCardView : public views::View {
} // namespace

SearchResultPageView::SearchResultPageView(AppListMainView* app_list_main_view,
AppListViewDelegate* view_delegate) {
AppListViewDelegate* view_delegate)
: selected_index_(0) {
SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical,
kExperimentalWindowPadding, kTopPadding,
kGroupSpacing));
Expand All @@ -60,16 +61,58 @@ SearchResultPageView::~SearchResultPageView() {
}

bool SearchResultPageView::OnKeyPressed(const ui::KeyEvent& event) {
DCHECK(!result_container_views_.empty());
if (result_container_views_.at(selected_index_)->OnKeyPressed(event))
return true;

int dir = 0;
switch (event.key_code()) {
case ui::VKEY_TAB:
dir = event.IsShiftDown() ? -1 : 1;
break;
case ui::VKEY_UP:
dir = -1;
break;
case ui::VKEY_DOWN:
dir = 1;
break;
default:
return false;
}

// Find the next result container with results.
int new_selected = selected_index_;
do {
new_selected += dir;
} while (IsValidSelectionIndex(new_selected) &&
result_container_views_[new_selected]->num_results() == 0);

if (IsValidSelectionIndex(new_selected)) {
SetSelectedIndex(new_selected);
return true;
}

// Capture the Tab key to prevent defocusing of the search box.
return result_container_views_[0]->OnKeyPressed(event) ||
event.key_code() == ui::VKEY_TAB;
return event.key_code() == ui::VKEY_TAB;
}

void SearchResultPageView::SetSelectedIndex(int index) {
bool from_bottom = index < selected_index_;

// Reset the old selected view's selection.
result_container_views_[selected_index_]->ClearSelectedIndex();
selected_index_ = index;
// Set the new selected view's selection to its first result.
result_container_views_[selected_index_]->OnContainerSelected(from_bottom);
}

bool SearchResultPageView::IsValidSelectionIndex(int index) {
return index >= 0 && index < static_cast<int>(result_container_views_.size());
}

void SearchResultPageView::ChildPreferredSizeChanged(views::View* child) {
DCHECK(!result_container_views_.empty());
Layout();
result_container_views_[0]->OnContainerSelected(false);
SetSelectedIndex(0);
}

void SearchResultPageView::AddSearchResultContainerView(
Expand Down
5 changes: 5 additions & 0 deletions ui/app_list/views/search_result_page_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class APP_LIST_EXPORT SearchResultPageView : public views::View {
void ChildPreferredSizeChanged(views::View* child) override;

private:
void SetSelectedIndex(int index);
bool IsValidSelectionIndex(int index);

void AddSearchResultContainerView(
AppListModel::SearchResults* result_model,
SearchResultContainerView* result_container);
Expand All @@ -37,6 +40,8 @@ class APP_LIST_EXPORT SearchResultPageView : public views::View {
// the views hierarchy.
std::vector<SearchResultContainerView*> result_container_views_;

int selected_index_;

DISALLOW_COPY_AND_ASSIGN(SearchResultPageView);
};

Expand Down

0 comments on commit d3abd58

Please sign in to comment.