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 kPopupDetailLabelExtraLeftMargin
= 8;
21 const int kCheckLabelPadding
= 4;
28 HoverHighlightView::HoverHighlightView(ViewClickListener
* listener
)
29 : listener_(listener
),
31 highlight_color_(kHoverBackgroundColor
),
33 text_highlight_color_(0),
34 text_default_color_(0),
37 set_notify_enter_exit_on_child(true);
40 HoverHighlightView::~HoverHighlightView() {
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
);
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_
);
60 SetAccessibleName(text
);
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
;
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
+ kPopupDetailLabelExtraLeftMargin
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