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.
5 #include "ash/system/tray/hover_highlight_view.h"
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"
20 const int kCheckLabelPadding = 4;
27 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
28 : listener_(listener),
30 highlight_color_(kHoverBackgroundColor),
32 text_highlight_color_(0),
33 text_default_color_(0),
36 set_notify_enter_exit_on_child(true);
39 HoverHighlightView::~HoverHighlightView() {
42 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image,
43 const base::string16& text,
44 gfx::Font::FontStyle style) {
45 SetLayoutManager(new views::BoxLayout(
46 views::BoxLayout::kHorizontal, 0, 3, kTrayPopupPaddingBetweenItems));
47 views::ImageView* image_view =
48 new FixedSizedImageView(kTrayPopupDetailsIconWidth, 0);
49 image_view->SetImage(image);
50 AddChildView(image_view);
52 text_label_ = new views::Label(text);
53 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
54 text_label_->SetFont(text_label_->font().DeriveFont(0, style));
55 if (text_default_color_)
56 text_label_->SetEnabledColor(text_default_color_);
57 AddChildView(text_label_);
59 SetAccessibleName(text);
62 views::Label* HoverHighlightView::AddLabel(const base::string16& text,
63 gfx::Font::FontStyle style) {
64 SetLayoutManager(new views::FillLayout());
65 text_label_ = new views::Label(text);
66 int margin = kTrayPopupPaddingHorizontal +
67 kTrayPopupDetailsLabelExtraLeftMargin;
70 if (base::i18n::IsRTL())
71 right_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_);
85 SetAccessibleName(text);
89 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
90 gfx::Font::FontStyle style,
93 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
94 const gfx::ImageSkia* check =
95 rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
96 int margin = kTrayPopupPaddingHorizontal +
97 kTrayPopupDetailsLabelExtraLeftMargin - 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);
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_);
113 SetAccessibleName(text);
116 return AddLabel(text, style);
119 void HoverHighlightView::SetExpandable(bool expandable) {
120 if (expandable != expandable_) {
121 expandable_ = expandable;
126 bool HoverHighlightView::PerformAction(const ui::Event& event) {
129 listener_->OnViewClicked(this);
133 gfx::Size HoverHighlightView::GetPreferredSize() {
134 gfx::Size size = ActionableView::GetPreferredSize();
135 if (!expandable_ || size.height() < kTrayPopupItemHeight)
136 size.set_height(kTrayPopupItemHeight);
140 int HoverHighlightView::GetHeightForWidth(int width) {
141 return GetPreferredSize().height();
144 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
146 if (text_highlight_color_ && text_label_)
147 text_label_->SetEnabledColor(text_highlight_color_);
151 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
153 if (text_default_color_ && text_label_)
154 text_label_->SetEnabledColor(text_default_color_);
158 void HoverHighlightView::OnEnabledChanged() {
159 for (int i = 0; i < child_count(); ++i)
160 child_at(i)->SetEnabled(enabled());
163 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
164 canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
167 void HoverHighlightView::OnFocus() {
168 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
169 ActionableView::OnFocus();
172 } // namespace internal