Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / ui / app_list / views / search_result_actions_view.cc
blob97f99349b401b54d49e542ed22850b2c5d99807a
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 <algorithm>
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"
17 namespace app_list {
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));
27 SearchResultActionsView::~SearchResultActionsView() {}
29 void SearchResultActionsView::SetActions(const SearchResult::Actions& actions) {
30 RemoveAllChildViews(true);
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);
40 PreferredSizeChanged();
41 SetSelectedAction(-1);
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));
48 if (selected_action_ == action_index)
49 return;
51 selected_action_ = action_index;
52 SchedulePaint();
54 if (IsValidActionIndex(selected_action_)) {
55 child_at(selected_action_)->NotifyAccessibilityEvent(
56 ui::AX_EVENT_FOCUS, true);
60 bool SearchResultActionsView::IsValidActionIndex(int action_index) const {
61 return action_index >= 0 && action_index < child_count();
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);
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);
89 void SearchResultActionsView::OnPaint(gfx::Canvas* canvas) {
90 if (!IsValidActionIndex(selected_action_))
91 return;
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);
98 void SearchResultActionsView::ButtonPressed(views::Button* sender,
99 const ui::Event& event) {
100 if (!delegate_)
101 return;
103 const int index = GetIndexOf(sender);
104 DCHECK_NE(-1, index);
105 delegate_->OnSearchResultActionActivated(index, event.flags());
108 } // namespace app_list