Home | History | Annotate | Download | only in views
      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/views/border.h"
     10 #include "ui/views/controls/button/blue_button.h"
     11 #include "ui/views/controls/button/image_button.h"
     12 #include "ui/views/layout/box_layout.h"
     13 
     14 namespace app_list {
     15 
     16 SearchResultActionsView::SearchResultActionsView(
     17     SearchResultActionsViewDelegate* delegate)
     18     : delegate_(delegate) {
     19   SetLayoutManager(
     20       new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 10, 0));
     21 }
     22 
     23 SearchResultActionsView::~SearchResultActionsView() {}
     24 
     25 void SearchResultActionsView::SetActions(const SearchResult::Actions& actions) {
     26   RemoveAllChildViews(true);
     27 
     28   for (size_t i = 0; i < actions.size(); ++i) {
     29     const SearchResult::Action& action = actions.at(i);
     30     if (action.label_text.empty())
     31       CreateImageButton(action);
     32     else
     33       CreateBlueButton(action);
     34   }
     35 
     36   PreferredSizeChanged();
     37 }
     38 
     39 void SearchResultActionsView::CreateImageButton(
     40     const SearchResult::Action& action) {
     41   views::ImageButton* button = new views::ImageButton(this);
     42   button->set_border(views::Border::CreateEmptyBorder(0, 4, 0, 4));
     43   button->SetImageAlignment(views::ImageButton::ALIGN_CENTER,
     44                             views::ImageButton::ALIGN_MIDDLE);
     45   button->SetImage(views::CustomButton::STATE_NORMAL, &action.base_image);
     46   button->SetImage(views::CustomButton::STATE_HOVERED, &action.hover_image);
     47   button->SetImage(views::CustomButton::STATE_PRESSED, &action.pressed_image);
     48   button->SetTooltipText(action.tooltip_text);
     49   AddChildView(button);
     50 }
     51 
     52 void SearchResultActionsView::CreateBlueButton(
     53     const SearchResult::Action& action) {
     54   views::BlueButton* button = new views::BlueButton(this, action.label_text);
     55   button->SetTooltipText(action.tooltip_text);
     56   button->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont(
     57       ui::ResourceBundle::SmallBoldFont));
     58   AddChildView(button);
     59 }
     60 
     61 void SearchResultActionsView::ButtonPressed(views::Button* sender,
     62                                             const ui::Event& event) {
     63   if (!delegate_)
     64     return;
     65 
     66   const int index = GetIndexOf(sender);
     67   DCHECK_NE(-1, index);
     68   delegate_->OnSearchResultActionActivated(index, event.flags());
     69 }
     70 
     71 }  // namespace app_list
     72