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.
5 #include "ui/app_list/views/search_result_actions_view.h"
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"
17 SearchResultActionsView::SearchResultActionsView(
18 SearchResultActionsViewDelegate
* delegate
)
19 : delegate_(delegate
),
20 selected_action_(-1) {
22 new views::BoxLayout(views::BoxLayout::kHorizontal
, 0, 10, 0));
25 SearchResultActionsView::~SearchResultActionsView() {}
27 void SearchResultActionsView::SetActions(const SearchResult::Actions
& actions
) {
28 RemoveAllChildViews(true);
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
);
35 CreateBlueButton(action
);
38 PreferredSizeChanged();
39 SetSelectedAction(-1);
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
));
46 if (selected_action_
== action_index
)
49 selected_action_
= action_index
;
52 if (IsValidActionIndex(selected_action_
)) {
53 child_at(selected_action_
)->NotifyAccessibilityEvent(
54 ui::AccessibilityTypes::EVENT_FOCUS
, true);
58 bool SearchResultActionsView::IsValidActionIndex(int action_index
) const {
59 return action_index
>= 0 && action_index
< child_count();
62 void SearchResultActionsView::CreateImageButton(
63 const SearchResult::Action
& action
) {
64 views::ImageButton
* button
= new views::ImageButton(this);
65 button
->SetBorder(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
);
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
->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
82 ui::ResourceBundle::SmallBoldFont
));
83 button
->SetFocusable(false);
87 void SearchResultActionsView::OnPaint(gfx::Canvas
* canvas
) {
88 if (!IsValidActionIndex(selected_action_
))
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
);
96 void SearchResultActionsView::ButtonPressed(views::Button
* sender
,
97 const ui::Event
& event
) {
101 const int index
= GetIndexOf(sender
);
102 DCHECK_NE(-1, index
);
103 delegate_
->OnSearchResultActionActivated(index
, event
.flags());
106 } // namespace app_list