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/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"
21 const int kPopupDetailLabelExtraLeftMargin
= 8;
22 const int kCheckLabelPadding
= 4;
29 HoverHighlightView::HoverHighlightView(ViewClickListener
* listener
)
30 : listener_(listener
),
32 highlight_color_(kHoverBackgroundColor
),
34 text_highlight_color_(0),
35 text_default_color_(0),
38 set_notify_enter_exit_on_child(true);
41 HoverHighlightView::~HoverHighlightView() {
44 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia
& image
,
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
);
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_
);
61 SetAccessibleName(text
);
64 views::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
;
71 if (base::i18n::IsRTL())
72 right_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_
);
86 SetAccessibleName(text
);
90 views::Label
* HoverHighlightView::AddCheckableLabel(const string16
& text
,
91 gfx::Font::FontStyle style
,
94 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
95 const gfx::ImageSkia
* check
=
96 rb
.GetImageNamed(IDR_MENU_CHECK
).ToImageSkia();
97 int margin
= kTrayPopupPaddingHorizontal
+ kPopupDetailLabelExtraLeftMargin
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
);
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_
);
114 SetAccessibleName(text
);
117 return AddLabel(text
, style
);
120 void HoverHighlightView::SetExpandable(bool expandable
) {
121 if (expandable
!= expandable_
) {
122 expandable_
= expandable
;
127 bool HoverHighlightView::PerformAction(const ui::Event
& event
) {
130 listener_
->OnViewClicked(this);
134 gfx::Size
HoverHighlightView::GetPreferredSize() {
135 gfx::Size size
= ActionableView::GetPreferredSize();
136 if (!expandable_
|| size
.height() < kTrayPopupItemHeight
)
137 size
.set_height(kTrayPopupItemHeight
);
141 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent
& event
) {
143 if (text_highlight_color_
&& text_label_
)
144 text_label_
->SetEnabledColor(text_highlight_color_
);
148 void HoverHighlightView::OnMouseExited(const ui::MouseEvent
& event
) {
150 if (text_default_color_
&& text_label_
)
151 text_label_
->SetEnabledColor(text_default_color_
);
155 void HoverHighlightView::OnEnabledChanged() {
156 for (int i
= 0; i
< child_count(); ++i
)
157 child_at(i
)->SetEnabled(enabled());
160 void HoverHighlightView::OnPaintBackground(gfx::Canvas
* canvas
) {
161 canvas
->DrawColor(hover_
? highlight_color_
: default_color_
);
164 void HoverHighlightView::OnFocus() {
165 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
166 ActionableView::OnFocus();
169 } // namespace internal