Skip to content

Commit

Permalink
Extract a SearchResultContainerView base class from SearchResultListV…
Browse files Browse the repository at this point in the history
…iew.

This CL extracts a SearchResultContainerView which is responsible for
dealing with the batching of updates from AppListModel::SearchResults.

This dedupes code from StartPageView and sets up work for adding
a SearchResultTileItemListView which will use this as a base.

BUG=425444

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

Cr-Commit-Position: refs/heads/master@{#302979}
  • Loading branch information
nik3daz authored and Commit bot committed Nov 6, 2014
1 parent 46d39c8 commit 1fcd212
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 143 deletions.
2 changes: 2 additions & 0 deletions ui/app_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ component("app_list") {
"views/search_box_view.cc",
"views/search_box_view.h",
"views/search_box_view_delegate.h",
"views/search_result_container_view.cc",
"views/search_result_container_view.h",
"views/search_result_actions_view.cc",
"views/search_result_actions_view.h",
"views/search_result_list_view.cc",
Expand Down
2 changes: 2 additions & 0 deletions ui/app_list/app_list.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
'views/search_box_view_delegate.h',
'views/search_result_actions_view.cc',
'views/search_result_actions_view.h',
'views/search_result_container_view.cc',
'views/search_result_container_view.h',
'views/search_result_list_view.cc',
'views/search_result_list_view.h',
'views/search_result_list_view_delegate.h',
Expand Down
66 changes: 66 additions & 0 deletions ui/app_list/views/search_result_container_view.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright (c) 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/search_result_container_view.h"

#include "base/bind.h"
#include "base/message_loop/message_loop.h"

namespace app_list {

SearchResultContainerView::SearchResultContainerView()
: results_(NULL), update_factory_(this) {
}

SearchResultContainerView::~SearchResultContainerView() {
if (results_)
results_->RemoveObserver(this);
}

void SearchResultContainerView::SetResults(
AppListModel::SearchResults* results) {
if (results_)
results_->RemoveObserver(this);

results_ = results;
if (results_)
results_->AddObserver(this);

Update();
}

void SearchResultContainerView::ScheduleUpdate() {
// When search results are added one by one, each addition generates an update
// request. Consolidates those update requests into one Update call.
if (!update_factory_.HasWeakPtrs()) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SearchResultContainerView::DoUpdate,
update_factory_.GetWeakPtr()));
}
}

void SearchResultContainerView::ListItemsAdded(size_t start, size_t count) {
ScheduleUpdate();
}

void SearchResultContainerView::ListItemsRemoved(size_t start, size_t count) {
ScheduleUpdate();
}

void SearchResultContainerView::ListItemMoved(size_t index,
size_t target_index) {
ScheduleUpdate();
}

void SearchResultContainerView::ListItemsChanged(size_t start, size_t count) {
ScheduleUpdate();
}

void SearchResultContainerView::DoUpdate() {
Update();
update_factory_.InvalidateWeakPtrs();
}

} // namespace app_list
52 changes: 52 additions & 0 deletions ui/app_list/views/search_result_container_view.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 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.

#ifndef UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_
#define UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_

#include "base/memory/weak_ptr.h"
#include "ui/app_list/app_list_model.h"
#include "ui/views/view.h"

namespace app_list {

// SearchResultContainerView is a base class that batches updates from a
// ListModelObserver.
class APP_LIST_EXPORT SearchResultContainerView : public views::View,
public ui::ListModelObserver {
public:
SearchResultContainerView();
~SearchResultContainerView() override;

void SetResults(AppListModel::SearchResults* results);

AppListModel::SearchResults* results() { return results_; }

// Schedules an Update call using |update_factory_|. Do nothing if there is a
// pending call.
void ScheduleUpdate();

// Overridden from ui::ListModelObserver:
void ListItemsAdded(size_t start, size_t count) override;
void ListItemsRemoved(size_t start, size_t count) override;
void ListItemMoved(size_t index, size_t target_index) override;
void ListItemsChanged(size_t start, size_t count) override;

// Updates UI with model.
virtual void Update() = 0;

private:
void DoUpdate();

AppListModel::SearchResults* results_; // Owned by AppListModel.

// The factory that consolidates multiple Update calls into one.
base::WeakPtrFactory<SearchResultContainerView> update_factory_;

DISALLOW_COPY_AND_ASSIGN(SearchResultContainerView);
};

} // namespace app_list

#endif // UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_
64 changes: 12 additions & 52 deletions ui/app_list/views/search_result_list_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ SearchResultListView::SearchResultListView(
AppListViewDelegate* view_delegate)
: delegate_(delegate),
view_delegate_(view_delegate),
results_(NULL),
results_container_(new views::View),
auto_launch_indicator_(new views::View),
last_visible_index_(0),
selected_index_(-1),
update_factory_(this) {
selected_index_(-1) {
results_container_->SetLayoutManager(
new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));

Expand All @@ -62,19 +60,6 @@ SearchResultListView::SearchResultListView(
}

SearchResultListView::~SearchResultListView() {
if (results_)
results_->RemoveObserver(this);
}

void SearchResultListView::SetResults(AppListModel::SearchResults* results) {
if (results_)
results_->RemoveObserver(this);

results_ = results;
if (results_)
results_->AddObserver(this);

Update();
}

void SearchResultListView::SetSelectedIndex(int selected_index) {
Expand Down Expand Up @@ -163,10 +148,19 @@ SearchResultView* SearchResultListView::GetResultViewAt(int index) {
return static_cast<SearchResultView*>(results_container_->child_at(index));
}

void SearchResultListView::ListItemsRemoved(size_t start, size_t count) {
size_t last = std::min(
start + count, static_cast<size_t>(results_container_->child_count()));
for (size_t i = start; i < last; ++i)
GetResultViewAt(i)->ClearResultNoRepaint();

SearchResultContainerView::ListItemsRemoved(start, count);
}

void SearchResultListView::Update() {
std::vector<SearchResult*> display_results =
AppListModel::FilterSearchResultsByDisplayType(
results_,
results(),
SearchResult::DISPLAY_LIST,
results_container_->child_count());
last_visible_index_ = display_results.size() - 1;
Expand All @@ -186,21 +180,9 @@ void SearchResultListView::Update() {
SetSelectedIndex(last_visible_index_);

Layout();
update_factory_.InvalidateWeakPtrs();
UpdateAutoLaunchState();
}

void SearchResultListView::ScheduleUpdate() {
// When search results are added one by one, each addition generates an update
// request. Consolidates those update requests into one Update call.
if (!update_factory_.HasWeakPtrs()) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SearchResultListView::Update,
update_factory_.GetWeakPtr()));
}
}

void SearchResultListView::ForceAutoLaunchForTest() {
if (auto_launch_animation_)
AnimationEnded(auto_launch_animation_.get());
Expand Down Expand Up @@ -228,7 +210,7 @@ void SearchResultListView::VisibilityChanged(views::View* starting_from,

void SearchResultListView::AnimationEnded(const gfx::Animation* animation) {
DCHECK_EQ(auto_launch_animation_.get(), animation);
view_delegate_->OpenSearchResult(results_->GetItemAt(0), true, ui::EF_NONE);
view_delegate_->OpenSearchResult(results()->GetItemAt(0), true, ui::EF_NONE);

// The auto-launch has to be canceled explicitly. Think that one of searcher
// is extremely slow. Sometimes the events would happen in the following
Expand All @@ -248,28 +230,6 @@ void SearchResultListView::AnimationProgressed(
0, 0, indicator_width, kTimeoutIndicatorHeight);
}

void SearchResultListView::ListItemsAdded(size_t start, size_t count) {
ScheduleUpdate();
}

void SearchResultListView::ListItemsRemoved(size_t start, size_t count) {
size_t last = std::min(
start + count,
static_cast<size_t>(results_container_->child_count()));
for (size_t i = start; i < last; ++i)
GetResultViewAt(i)->ClearResultNoRepaint();

ScheduleUpdate();
}

void SearchResultListView::ListItemMoved(size_t index, size_t target_index) {
NOTREACHED();
}

void SearchResultListView::ListItemsChanged(size_t start, size_t count) {
NOTREACHED();
}

void SearchResultListView::SearchResultActivated(SearchResultView* view,
int event_flags) {
if (view_delegate_ && view->result())
Expand Down
33 changes: 9 additions & 24 deletions ui/app_list/views/search_result_list_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

#include "base/basictypes.h"
#include "base/memory/weak_ptr.h"
#include "ui/app_list/app_list_model.h"
#include "ui/base/models/list_model_observer.h"
#include "ui/app_list/views/search_result_container_view.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/views/view.h"

Expand All @@ -27,16 +26,13 @@ class SearchResultView;

// SearchResultListView displays SearchResultList with a list of
// SearchResultView.
class APP_LIST_EXPORT SearchResultListView : public views::View,
public gfx::AnimationDelegate,
public ui::ListModelObserver {
class APP_LIST_EXPORT SearchResultListView : public gfx::AnimationDelegate,
public SearchResultContainerView {
public:
SearchResultListView(SearchResultListViewDelegate* delegate,
AppListViewDelegate* view_delegate);
~SearchResultListView() override;

void SetResults(AppListModel::SearchResults* results);

void SetSelectedIndex(int selected_index);

void UpdateAutoLaunchState();
Expand All @@ -57,6 +53,12 @@ class APP_LIST_EXPORT SearchResultListView : public views::View,
bool OnKeyPressed(const ui::KeyEvent& event) override;
gfx::Size GetPreferredSize() const override;

// Overridden from ui::ListModelObserver:
void ListItemsRemoved(size_t start, size_t count) override;

// Overridden from SearchResultContainerView:
void Update() override;

private:
friend class test::SearchResultListViewTest;

Expand All @@ -67,13 +69,6 @@ class APP_LIST_EXPORT SearchResultListView : public views::View,
// Helper function to get SearchResultView at given |index|.
SearchResultView* GetResultViewAt(int index);

// Updates UI with model.
void Update();

// Schedules an Update call using |update_factory_|. Do nothing if there is a
// pending call.
void ScheduleUpdate();

// Forcibly auto-launch for test if it is in auto-launching state.
void ForceAutoLaunchForTest();

Expand All @@ -86,15 +81,8 @@ class APP_LIST_EXPORT SearchResultListView : public views::View,
void AnimationEnded(const gfx::Animation* animation) override;
void AnimationProgressed(const gfx::Animation* animation) override;

// Overridden from ui::ListModelObserver:
void ListItemsAdded(size_t start, size_t count) override;
void ListItemsRemoved(size_t start, size_t count) override;
void ListItemMoved(size_t index, size_t target_index) override;
void ListItemsChanged(size_t start, size_t count) override;

SearchResultListViewDelegate* delegate_; // Not owned.
AppListViewDelegate* view_delegate_; // Not owned.
AppListModel::SearchResults* results_; // Owned by AppListModel.

views::View* results_container_;
views::View* auto_launch_indicator_;
Expand All @@ -103,9 +91,6 @@ class APP_LIST_EXPORT SearchResultListView : public views::View,
int last_visible_index_;
int selected_index_;

// The factory that consolidates multiple Update calls into one.
base::WeakPtrFactory<SearchResultListView> update_factory_;

DISALLOW_COPY_AND_ASSIGN(SearchResultListView);
};

Expand Down
Loading

0 comments on commit 1fcd212

Please sign in to comment.