OLD | NEW |
(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 } // namespace |
| 23 |
| 24 // Container of the search answer view. |
| 25 class SearchResultAnswerCardView::SearchAnswerContainerView |
| 26 : public views::CustomButton { |
| 27 public: |
| 28 explicit SearchAnswerContainerView(views::View* search_results_page_view) |
| 29 : CustomButton(nullptr), |
| 30 search_results_page_view_(search_results_page_view) { |
| 31 // Center the card horizontally in the container. |
| 32 views::BoxLayout* answer_container_layout = |
| 33 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0); |
| 34 answer_container_layout->set_main_axis_alignment( |
| 35 views::BoxLayout::MAIN_AXIS_ALIGNMENT_CENTER); |
| 36 SetLayoutManager(answer_container_layout); |
| 37 } |
| 38 |
| 39 void SetSelected(bool selected) { |
| 40 if (selected == selected_) |
| 41 return; |
| 42 selected_ = selected; |
| 43 UpdateBackgroundColor(); |
| 44 } |
| 45 |
| 46 // views::CustomButton overrides: |
| 47 void ChildPreferredSizeChanged(View* child) override { |
| 48 // Card size changed. |
| 49 if (visible()) |
| 50 search_results_page_view_->Layout(); |
| 51 } |
| 52 |
| 53 int GetHeightForWidth(int w) const override { |
| 54 return visible() ? CustomButton::GetHeightForWidth(w) : 0; |
| 55 } |
| 56 |
| 57 const char* GetClassName() const override { |
| 58 return "SearchAnswerContainerView"; |
| 59 } |
| 60 |
| 61 void StateChanged(ButtonState old_state) override { UpdateBackgroundColor(); } |
| 62 |
| 63 private: |
| 64 void UpdateBackgroundColor() { |
| 65 views::Background* background = nullptr; |
| 66 |
| 67 if (selected_) { |
| 68 background = views::Background::CreateSolidBackground(kSelectedColor); |
| 69 } else if (state() == STATE_HOVERED || state() == STATE_PRESSED) { |
| 70 background = views::Background::CreateSolidBackground(kHighlightedColor); |
| 71 } |
| 72 |
| 73 set_background(background); |
| 74 SchedulePaint(); |
| 75 } |
| 76 |
| 77 views::View* const search_results_page_view_; |
| 78 bool selected_ = false; |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(SearchAnswerContainerView); |
| 81 }; |
| 82 |
| 83 SearchResultAnswerCardView::SearchResultAnswerCardView( |
| 84 AppListModel* model, |
| 85 SearchResultPageView* search_results_page_view, |
| 86 views::View* search_answer_view) |
| 87 : model_(model), |
| 88 search_answer_container_view_( |
| 89 new SearchAnswerContainerView(search_results_page_view)) { |
| 90 search_answer_container_view_->SetVisible(false); |
| 91 search_answer_container_view_->AddChildView(search_answer_view); |
| 92 AddChildView(search_answer_container_view_); |
| 93 model->AddObserver(this); |
| 94 SetLayoutManager(new views::FillLayout); |
| 95 } |
| 96 |
| 97 SearchResultAnswerCardView::~SearchResultAnswerCardView() { |
| 98 model_->RemoveObserver(this); |
| 99 } |
| 100 |
| 101 const char* SearchResultAnswerCardView::GetClassName() const { |
| 102 return "SearchResultAnswerCardView"; |
| 103 } |
| 104 |
| 105 void SearchResultAnswerCardView::OnContainerSelected( |
| 106 bool from_bottom, |
| 107 bool directional_movement) { |
| 108 if (num_results() == 0) |
| 109 return; |
| 110 |
| 111 SetSelectedIndex(0); |
| 112 } |
| 113 |
| 114 int SearchResultAnswerCardView::GetYSize() { |
| 115 return num_results(); |
| 116 } |
| 117 |
| 118 int SearchResultAnswerCardView::DoUpdate() { |
| 119 const bool have_result = search_answer_container_view_->visible(); |
| 120 set_container_score(have_result ? kSearchAnswerCardRelevance : 0); |
| 121 return have_result ? 1 : 0; |
| 122 } |
| 123 |
| 124 void SearchResultAnswerCardView::UpdateSelectedIndex(int old_selected, |
| 125 int new_selected) { |
| 126 if (new_selected == old_selected) |
| 127 return; |
| 128 |
| 129 const bool is_selected = new_selected == 0; |
| 130 search_answer_container_view_->SetSelected(is_selected); |
| 131 if (is_selected) |
| 132 NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true); |
| 133 } |
| 134 |
| 135 void SearchResultAnswerCardView::OnSearchAnswerAvailableChanged( |
| 136 bool has_answer) { |
| 137 const bool visible = has_answer && !features::IsAnswerCardDarkRunEnabled(); |
| 138 if (visible == search_answer_container_view_->visible()) |
| 139 return; |
| 140 |
| 141 search_answer_container_view_->SetVisible(visible); |
| 142 ScheduleUpdate(); |
| 143 } |
| 144 |
| 145 } // namespace app_list |
OLD | NEW |