1 // Copyright (c) 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 #ifndef UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_
6 #define UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_
8 #include "base/memory/weak_ptr.h"
9 #include "ui/app_list/app_list_model.h"
10 #include "ui/views/view.h"
14 // SearchResultContainerView is a base class for views that contain multiple
15 // search results. SearchPageView holds these in a list and manages which one is
16 // selected. There can be one result within one SearchResultContainerView
17 // selected at a time; moving off the end of one container view selects the
18 // first element of the next container view, and vice versa
19 class APP_LIST_EXPORT SearchResultContainerView
: public views::View
,
20 public ui::ListModelObserver
{
22 SearchResultContainerView();
23 ~SearchResultContainerView() override
;
25 // Sets the search results to listen to.
26 void SetResults(AppListModel::SearchResults
* results
);
27 AppListModel::SearchResults
* results() { return results_
; }
29 // Sets the index of the selected search result within this container. This
30 // must be a valid index.
31 void SetSelectedIndex(int selected_index
);
33 void ClearSelectedIndex();
35 // The currently selected index. Returns -1 on no selection.
36 int selected_index() const { return selected_index_
; }
38 // Returns whether |index| is a valid index for selection.
39 bool IsValidSelectionIndex(int index
) const;
41 int num_results() const { return num_results_
; }
43 void set_container_score(double score
) { container_score_
= score
; }
44 double container_score() const { return container_score_
; }
46 // Schedules an Update call using |update_factory_|. Do nothing if there is a
48 void ScheduleUpdate();
50 // Overridden from ui::ListModelObserver:
51 void ListItemsAdded(size_t start
, size_t count
) override
;
52 void ListItemsRemoved(size_t start
, size_t count
) override
;
53 void ListItemMoved(size_t index
, size_t target_index
) override
;
54 void ListItemsChanged(size_t start
, size_t count
) override
;
56 // Updates the container for being selected. |from_bottom| is true if the view
57 // was entered into from a selected view below it; false if entered into from
58 // above. |directional_movement| is true if the navigation was caused by
59 // directional controls (eg, arrow keys), as opposed to linear controls (eg,
61 virtual void OnContainerSelected(bool from_bottom
,
62 bool directional_movement
) = 0;
65 // Updates UI with model. Returns the number of visible results.
66 virtual int Update() = 0;
68 // Updates UI for a change in the selected index.
69 virtual void UpdateSelectedIndex(int old_selected
, int new_selected
) = 0;
71 // Batching method that actually performs the update and updates layout.
77 double container_score_
;
79 AppListModel::SearchResults
* results_
; // Owned by AppListModel.
81 // The factory that consolidates multiple Update calls into one.
82 base::WeakPtrFactory
<SearchResultContainerView
> update_factory_
;
84 DISALLOW_COPY_AND_ASSIGN(SearchResultContainerView
);
87 } // namespace app_list
89 #endif // UI_APP_LIST_VIEWS_SEARCH_RESULT_CONTAINER_VIEW_H_