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