1 // Copyright 2013 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_actions_view.h" 6 7 #include "ui/app_list/views/search_result_actions_view_delegate.h" 8 #include "ui/base/resource/resource_bundle.h" 9 #include "ui/gfx/canvas.h" 10 #include "ui/views/border.h" 11 #include "ui/views/controls/button/blue_button.h" 12 #include "ui/views/controls/button/image_button.h" 13 #include "ui/views/layout/box_layout.h" 14 15 namespace app_list { 16 17 SearchResultActionsView::SearchResultActionsView( 18 SearchResultActionsViewDelegate* delegate) 19 : delegate_(delegate), 20 selected_action_(-1) { 21 SetLayoutManager( 22 new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 10, 0)); 23 } 24 25 SearchResultActionsView::~SearchResultActionsView() {} 26 27 void SearchResultActionsView::SetActions(const SearchResult::Actions& actions) { 28 RemoveAllChildViews(true); 29 30 for (size_t i = 0; i < actions.size(); ++i) { 31 const SearchResult::Action& action = actions.at(i); 32 if (action.label_text.empty()) 33 CreateImageButton(action); 34 else 35 CreateBlueButton(action); 36 } 37 38 PreferredSizeChanged(); 39 SetSelectedAction(-1); 40 } 41 42 void SearchResultActionsView::SetSelectedAction(int action_index) { 43 // Clamp |action_index| in [-1, child_count()]. 44 action_index = std::min(child_count(), std::max(-1, action_index)); 45 46 if (selected_action_ == action_index) 47 return; 48 49 selected_action_ = action_index; 50 SchedulePaint(); 51 52 if (IsValidActionIndex(selected_action_)) { 53 child_at(selected_action_)->NotifyAccessibilityEvent( 54 ui::AccessibilityTypes::EVENT_FOCUS, true); 55 } 56 } 57 58 bool SearchResultActionsView::IsValidActionIndex(int action_index) const { 59 return action_index >= 0 && action_index < child_count(); 60 } 61 62 void SearchResultActionsView::CreateImageButton( 63 const SearchResult::Action& action) { 64 views::ImageButton* button = new views::ImageButton(this); 65 button->set_border(views::Border::CreateEmptyBorder(0, 9, 0, 9)); 66 button->SetAccessibleName(action.tooltip_text); 67 button->SetImageAlignment(views::ImageButton::ALIGN_CENTER, 68 views::ImageButton::ALIGN_MIDDLE); 69 button->SetImage(views::CustomButton::STATE_NORMAL, &action.base_image); 70 button->SetImage(views::CustomButton::STATE_HOVERED, &action.hover_image); 71 button->SetImage(views::CustomButton::STATE_PRESSED, &action.pressed_image); 72 button->SetTooltipText(action.tooltip_text); 73 AddChildView(button); 74 } 75 76 void SearchResultActionsView::CreateBlueButton( 77 const SearchResult::Action& action) { 78 views::BlueButton* button = new views::BlueButton(this, action.label_text); 79 button->SetAccessibleName(action.label_text); 80 button->SetTooltipText(action.tooltip_text); 81 button->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( 82 ui::ResourceBundle::SmallBoldFont)); 83 button->SetFocusable(false); 84 AddChildView(button); 85 } 86 87 void SearchResultActionsView::OnPaint(gfx::Canvas* canvas) { 88 if (!IsValidActionIndex(selected_action_)) 89 return; 90 91 const gfx::Rect active_action_bounds(child_at(selected_action_)->bounds()); 92 const SkColor kActiveActionBackground = SkColorSetRGB(0xA0, 0xA0, 0xA0); 93 canvas->FillRect(active_action_bounds, kActiveActionBackground); 94 } 95 96 void SearchResultActionsView::ButtonPressed(views::Button* sender, 97 const ui::Event& event) { 98 if (!delegate_) 99 return; 100 101 const int index = GetIndexOf(sender); 102 DCHECK_NE(-1, index); 103 delegate_->OnSearchResultActionActivated(index, event.flags()); 104 } 105 106 } // namespace app_list 107