Home | History | Annotate | Download | only in views
      1 // Copyright (c) 2012 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/search_result_list_view.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "base/bind.h"
     10 #include "base/message_loop/message_loop.h"
     11 #include "ui/app_list/views/search_result_list_view_delegate.h"
     12 #include "ui/app_list/views/search_result_view.h"
     13 #include "ui/events/event.h"
     14 #include "ui/views/layout/box_layout.h"
     15 
     16 namespace {
     17 
     18 const int kMaxResults = 6;
     19 
     20 }  // namespace
     21 
     22 namespace app_list {
     23 
     24 SearchResultListView::SearchResultListView(
     25     SearchResultListViewDelegate* delegate)
     26     : delegate_(delegate),
     27       results_(NULL),
     28       last_visible_index_(0),
     29       selected_index_(-1),
     30       update_factory_(this) {
     31   SetLayoutManager(
     32       new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
     33 
     34   for (int i = 0; i < kMaxResults; ++i)
     35     AddChildView(new SearchResultView(this, this));
     36 }
     37 
     38 SearchResultListView::~SearchResultListView() {
     39   if (results_)
     40     results_->RemoveObserver(this);
     41 }
     42 
     43 void SearchResultListView::SetResults(AppListModel::SearchResults* results) {
     44   if (results_)
     45     results_->RemoveObserver(this);
     46 
     47   results_ = results;
     48   if (results_)
     49     results_->AddObserver(this);
     50 
     51   Update();
     52 }
     53 
     54 void SearchResultListView::SetSelectedIndex(int selected_index) {
     55   if (selected_index_ == selected_index)
     56     return;
     57 
     58   if (selected_index_ >= 0) {
     59     SearchResultView* selected_view  = GetResultViewAt(selected_index_);
     60     selected_view->ClearSelectedAction();
     61     selected_view->SchedulePaint();
     62   }
     63 
     64   selected_index_ = selected_index;
     65 
     66   if (selected_index_ >= 0) {
     67     SearchResultView* selected_view  = GetResultViewAt(selected_index_);
     68     selected_view->ClearSelectedAction();
     69     selected_view->SchedulePaint();
     70     selected_view->NotifyAccessibilityEvent(ui::AccessibilityTypes::EVENT_FOCUS,
     71                                             true);
     72   }
     73 }
     74 
     75 bool SearchResultListView::IsResultViewSelected(
     76     const SearchResultView* result_view) const {
     77   if (selected_index_ < 0)
     78     return false;
     79 
     80   return static_cast<const SearchResultView*>(child_at(selected_index_)) ==
     81       result_view;
     82 }
     83 
     84 bool SearchResultListView::OnKeyPressed(const ui::KeyEvent& event) {
     85   if (selected_index_ >= 0 && child_at(selected_index_)->OnKeyPressed(event))
     86     return true;
     87 
     88   switch (event.key_code()) {
     89     case ui::VKEY_TAB:
     90       if (event.IsShiftDown())
     91         SetSelectedIndex(std::max(selected_index_ - 1, 0));
     92       else
     93         SetSelectedIndex(std::min(selected_index_ + 1, last_visible_index_));
     94       return true;
     95     case ui::VKEY_UP:
     96       SetSelectedIndex(std::max(selected_index_ - 1, 0));
     97       return true;
     98     case ui::VKEY_DOWN:
     99       SetSelectedIndex(std::min(selected_index_ + 1, last_visible_index_));
    100       return true;
    101     default:
    102       break;
    103   }
    104   return false;
    105 }
    106 
    107 SearchResultView* SearchResultListView::GetResultViewAt(int index) {
    108   DCHECK(index >= 0 && index < child_count());
    109   return static_cast<SearchResultView*>(child_at(index));
    110 }
    111 
    112 void SearchResultListView::Update() {
    113   last_visible_index_ = 0;
    114   for (size_t i = 0; i < static_cast<size_t>(child_count()); ++i) {
    115     SearchResultView* result_view = GetResultViewAt(i);
    116     if (i < results_->item_count()) {
    117       result_view->SetResult(results_->GetItemAt(i));
    118       result_view->SetVisible(true);
    119       last_visible_index_ = i;
    120     } else {
    121       result_view->SetResult(NULL);
    122       result_view->SetVisible(false);
    123     }
    124   }
    125   if (selected_index_ > last_visible_index_)
    126     SetSelectedIndex(last_visible_index_);
    127 
    128   Layout();
    129   update_factory_.InvalidateWeakPtrs();
    130 }
    131 
    132 void SearchResultListView::ScheduleUpdate() {
    133   // When search results are added one by one, each addition generates an update
    134   // request. Consolidates those update requests into one Update call.
    135   if (!update_factory_.HasWeakPtrs()) {
    136     base::MessageLoop::current()->PostTask(
    137         FROM_HERE,
    138         base::Bind(&SearchResultListView::Update,
    139                    update_factory_.GetWeakPtr()));
    140   }
    141 }
    142 
    143 void SearchResultListView::ListItemsAdded(size_t start, size_t count) {
    144   ScheduleUpdate();
    145 }
    146 
    147 void SearchResultListView::ListItemsRemoved(size_t start, size_t count) {
    148   size_t last = std::min(start + count, static_cast<size_t>(child_count()));
    149   for (size_t i = start; i < last; ++i)
    150     GetResultViewAt(i)->ClearResultNoRepaint();
    151 
    152   ScheduleUpdate();
    153 }
    154 
    155 void SearchResultListView::ListItemMoved(size_t index, size_t target_index) {
    156   NOTREACHED();
    157 }
    158 
    159 void SearchResultListView::ListItemsChanged(size_t start, size_t count) {
    160   ScheduleUpdate();
    161 }
    162 
    163 void SearchResultListView::SearchResultActivated(SearchResultView* view,
    164                                                  int event_flags) {
    165   if (delegate_ && view->result())
    166     delegate_->OpenResult(view->result(), event_flags);
    167 }
    168 
    169 void SearchResultListView::SearchResultActionActivated(SearchResultView* view,
    170                                                        size_t action_index,
    171                                                        int event_flags) {
    172   if (delegate_ && view->result()) {
    173     delegate_->InvokeResultAction(
    174         view->result(), action_index, event_flags);
    175   }
    176 }
    177 
    178 void SearchResultListView::OnSearchResultInstalled(SearchResultView* view) {
    179   if (delegate_ && view->result())
    180     delegate_->OnResultInstalled(view->result());
    181 }
    182 
    183 void SearchResultListView::OnSearchResultUninstalled(SearchResultView* view) {
    184   if (delegate_ && view->result())
    185     delegate_->OnResultUninstalled(view->result());
    186 }
    187 
    188 }  // namespace app_list
    189