Home | History | Annotate | Download | only in tray
      1 // Copyright 2012 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 "ash/system/tray/hover_highlight_view.h"
      6 
      7 #include "ash/system/tray/fixed_sized_image_view.h"
      8 #include "ash/system/tray/tray_constants.h"
      9 #include "ash/system/tray/view_click_listener.h"
     10 #include "grit/ui_resources.h"
     11 #include "ui/base/resource/resource_bundle.h"
     12 #include "ui/gfx/canvas.h"
     13 #include "ui/views/controls/image_view.h"
     14 #include "ui/views/controls/label.h"
     15 #include "ui/views/layout/box_layout.h"
     16 #include "ui/views/layout/fill_layout.h"
     17 
     18 namespace {
     19 
     20 const int kPopupDetailLabelExtraLeftMargin = 8;
     21 const int kCheckLabelPadding = 4;
     22 
     23 }  // namespace
     24 
     25 namespace ash {
     26 namespace internal {
     27 
     28 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
     29     : listener_(listener),
     30       text_label_(NULL),
     31       highlight_color_(kHoverBackgroundColor),
     32       default_color_(0),
     33       text_highlight_color_(0),
     34       text_default_color_(0),
     35       hover_(false),
     36       expandable_(false) {
     37   set_notify_enter_exit_on_child(true);
     38 }
     39 
     40 HoverHighlightView::~HoverHighlightView() {
     41 }
     42 
     43 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image,
     44                                          const base::string16& text,
     45                                          gfx::Font::FontStyle style) {
     46   SetLayoutManager(new views::BoxLayout(
     47       views::BoxLayout::kHorizontal, 0, 3, kTrayPopupPaddingBetweenItems));
     48   views::ImageView* image_view =
     49       new FixedSizedImageView(kTrayPopupDetailsIconWidth, 0);
     50   image_view->SetImage(image);
     51   AddChildView(image_view);
     52 
     53   text_label_ = new views::Label(text);
     54   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     55   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
     56   if (text_default_color_)
     57     text_label_->SetEnabledColor(text_default_color_);
     58   AddChildView(text_label_);
     59 
     60   SetAccessibleName(text);
     61 }
     62 
     63 views::Label* HoverHighlightView::AddLabel(const base::string16& text,
     64                                            gfx::Font::FontStyle style) {
     65   SetLayoutManager(new views::FillLayout());
     66   text_label_ = new views::Label(text);
     67   int margin = kTrayPopupPaddingHorizontal + kPopupDetailLabelExtraLeftMargin;
     68   int left_margin = 0;
     69   int right_margin = 0;
     70   if (base::i18n::IsRTL())
     71     right_margin = margin;
     72   else
     73     left_margin = margin;
     74   text_label_->set_border(
     75       views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin));
     76   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
     77   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
     78   // Do not set alpha value in disable color. It will have issue with elide
     79   // blending filter in disabled state for rendering label text color.
     80   text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
     81   if (text_default_color_)
     82     text_label_->SetEnabledColor(text_default_color_);
     83   AddChildView(text_label_);
     84 
     85   SetAccessibleName(text);
     86   return text_label_;
     87 }
     88 
     89 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
     90                                                     gfx::Font::FontStyle style,
     91                                                     bool checked) {
     92   if (checked) {
     93     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
     94     const gfx::ImageSkia* check =
     95         rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
     96     int margin = kTrayPopupPaddingHorizontal + kPopupDetailLabelExtraLeftMargin
     97         - kCheckLabelPadding;
     98     SetLayoutManager(new views::BoxLayout(
     99         views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding));
    100     views::ImageView* image_view = new FixedSizedImageView(margin, 0);
    101     image_view->SetImage(check);
    102     image_view->SetHorizontalAlignment(views::ImageView::TRAILING);
    103     AddChildView(image_view);
    104 
    105     text_label_ = new views::Label(text);
    106     text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
    107     text_label_->SetFont(text_label_->font().DeriveFont(0, style));
    108     text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
    109     if (text_default_color_)
    110       text_label_->SetEnabledColor(text_default_color_);
    111     AddChildView(text_label_);
    112 
    113     SetAccessibleName(text);
    114     return text_label_;
    115   }
    116   return AddLabel(text, style);
    117 }
    118 
    119 void HoverHighlightView::SetExpandable(bool expandable) {
    120   if (expandable != expandable_) {
    121     expandable_ = expandable;
    122     InvalidateLayout();
    123   }
    124 }
    125 
    126 bool HoverHighlightView::PerformAction(const ui::Event& event) {
    127   if (!listener_)
    128     return false;
    129   listener_->OnViewClicked(this);
    130   return true;
    131 }
    132 
    133 gfx::Size HoverHighlightView::GetPreferredSize() {
    134   gfx::Size size = ActionableView::GetPreferredSize();
    135   if (!expandable_ || size.height() < kTrayPopupItemHeight)
    136     size.set_height(kTrayPopupItemHeight);
    137   return size;
    138 }
    139 
    140 int HoverHighlightView::GetHeightForWidth(int width) {
    141   return GetPreferredSize().height();
    142 }
    143 
    144 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
    145   hover_ = true;
    146   if (text_highlight_color_ && text_label_)
    147     text_label_->SetEnabledColor(text_highlight_color_);
    148   SchedulePaint();
    149 }
    150 
    151 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
    152   hover_ = false;
    153   if (text_default_color_ && text_label_)
    154     text_label_->SetEnabledColor(text_default_color_);
    155   SchedulePaint();
    156 }
    157 
    158 void HoverHighlightView::OnEnabledChanged() {
    159   for (int i = 0; i < child_count(); ++i)
    160     child_at(i)->SetEnabled(enabled());
    161 }
    162 
    163 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
    164   canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
    165 }
    166 
    167 void HoverHighlightView::OnFocus() {
    168   ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
    169   ActionableView::OnFocus();
    170 }
    171 
    172 }  // namespace internal
    173 }  // namespace ash
    174