Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: ui/app_list/views/search_result_answer_card_view.cc

Issue 2905523004: Making answer card to behave like other results. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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_answer_card_view.h"
6
7 #include "ui/app_list/app_list_constants.h"
8 #include "ui/app_list/app_list_features.h"
9 #include "ui/app_list/views/search_result_page_view.h"
10 #include "ui/views/background.h"
11 #include "ui/views/controls/button/custom_button.h"
12 #include "ui/views/layout/box_layout.h"
13 #include "ui/views/layout/fill_layout.h"
14
15 namespace app_list {
16
17 namespace {
18
19 // Answer card relevance is high to always have it first.
20 constexpr double kSearchAnswerCardRelevance = 100;
21
22 // Container of the search answer view.
23 class SearchAnswerContainerView : public views::CustomButton {
24 public:
25 explicit SearchAnswerContainerView(views::View* search_results_page_view)
26 : CustomButton(nullptr),
27 search_results_page_view_(search_results_page_view) {
28 // Center the card horizontally in the container.
29 views::BoxLayout* answer_container_layout =
30 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
31 answer_container_layout->set_main_axis_alignment(
32 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER);
33 SetLayoutManager(answer_container_layout);
34 }
35
36 void SetSelected(bool selected) {
37 if (selected == selected_)
38 return;
39 selected_ = selected;
40 UpdateBackgroundColor();
41 }
42
43 // views::CustomButton overrides:
44 void ChildPreferredSizeChanged(View* child) override {
45 // Card size changed.
46 if (visible())
47 search_results_page_view_->Layout();
48 }
49
50 const char* GetClassName() const override {
51 return "SearchAnswerContainerView";
52 }
53
54 void StateChanged(ButtonState old_state) override { UpdateBackgroundColor(); }
55
56 private:
57 void UpdateBackgroundColor() {
58 views::Background* background = nullptr;
59
60 if (selected_) {
61 background = views::Background::CreateSolidBackground(kSelectedColor);
62 } else if (state() == STATE_HOVERED || state() == STATE_PRESSED) {
63 background = views::Background::CreateSolidBackground(kHighlightedColor);
64 }
65
66 set_background(background);
67 SchedulePaint();
68 }
69
70 views::View* const search_results_page_view_;
71 bool selected_ = false;
72
73 DISALLOW_COPY_AND_ASSIGN(SearchAnswerContainerView);
74 };
75
76 } // namespace
77
78 SearchResultAnswerCardView::SearchResultAnswerCardView(
79 AppListModel* model,
80 SearchResultPageView* search_results_page_view,
81 views::View* search_answer_view)
82 : model_(model),
83 search_answer_container_view_(
84 new SearchAnswerContainerView(search_results_page_view)) {
85 search_answer_container_view_->SetVisible(false);
86 search_answer_container_view_->AddChildView(search_answer_view);
87 AddChildView(search_answer_container_view_);
88 model->AddObserver(this);
xiyuan 2017/05/25 16:42:53 We don't need to explicitly start/stop observing t
vadimt 2017/05/25 20:15:16 We do this for OnSearchAnswerAvailableChanged() ca
xiyuan 2017/05/25 20:52:04 You are right. |model_| is not a SearchResults. Th
89 }
90
91 SearchResultAnswerCardView::~SearchResultAnswerCardView() {
92 model_->RemoveObserver(this);
93 }
94
95 void SearchResultAnswerCardView::OnContainerSelected(
96 bool from_bottom,
97 bool directional_movement) {
98 if (num_results() == 0)
99 return;
100
101 SetSelectedIndex(0);
102 }
103
104 int SearchResultAnswerCardView::GetYSize() {
105 return num_results();
106 }
107
108 int SearchResultAnswerCardView::DoUpdate() {
109 const bool have_result = search_answer_container_view_->visible() &&
110 !features::IsAnswerCardDarkRunEnabled();
111 set_container_score(have_result ? kSearchAnswerCardRelevance : 0);
112 SetLayoutManager(have_result ? new views::FillLayout : nullptr);
xiyuan 2017/05/25 16:42:53 Why do we need to do this? Can we set FillLayout i
vadimt 2017/05/25 20:15:16 You are right; not needed anymore.
vadimt 2017/05/25 22:59:04 Actually, there is a bug/feature of FillLayout: it
xiyuan 2017/05/25 23:01:21 In this case, since we have only one child, I woul
vadimt 2017/05/26 20:25:03 Solved.
113 return have_result ? 1 : 0;
114 }
115
116 void SearchResultAnswerCardView::UpdateSelectedIndex(int old_selected,
117 int new_selected) {
118 if (new_selected == old_selected)
119 return;
120
121 const bool is_selected = new_selected == 0;
122 search_answer_container_view_->SetSelected(is_selected);
123 if (is_selected)
124 NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true);
125 }
126
127 const char* SearchResultAnswerCardView::GetClassName() const {
128 return "SearchResultAnswerCardView";
129 }
130
131 void SearchResultAnswerCardView::OnSearchAnswerAvailableChanged(
132 bool has_answer) {
133 if (has_answer == search_answer_container_view_->visible())
134 return;
135
136 search_answer_container_view_->SetVisible(has_answer);
137 ScheduleUpdate();
138 }
139
140 } // namespace app_list
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698