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