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 "ui/accessibility/ax_view_state.h"
11 #include "ui/base/resource/resource_bundle.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/font_list.h"
14 #include "ui/resources/grit/ui_resources.h"
15 #include "ui/views/border.h"
16 #include "ui/views/controls/image_view.h"
17 #include "ui/views/controls/label.h"
18 #include "ui/views/layout/box_layout.h"
19 #include "ui/views/layout/fill_layout.h"
23 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),
40 set_notify_enter_exit_on_child(true);
43 HoverHighlightView::~HoverHighlightView() {
46 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia
& image
,
47 const base::string16
& text
,
48 gfx::Font::FontStyle style
) {
49 SetLayoutManager(new views::BoxLayout(
50 views::BoxLayout::kHorizontal
, 0, 3, kTrayPopupPaddingBetweenItems
));
51 views::ImageView
* image_view
=
52 new FixedSizedImageView(kTrayPopupDetailsIconWidth
, 0);
53 image_view
->SetImage(image
);
54 AddChildView(image_view
);
56 text_label_
= new views::Label(text
);
57 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
58 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
59 if (text_default_color_
)
60 text_label_
->SetEnabledColor(text_default_color_
);
61 AddChildView(text_label_
);
63 SetAccessibleName(text
);
66 views::Label
* HoverHighlightView::AddLabel(const base::string16
& text
,
67 gfx::HorizontalAlignment alignment
,
68 gfx::Font::FontStyle style
) {
69 SetLayoutManager(new views::FillLayout());
70 text_label_
= new views::Label(text
);
71 int left_margin
= kTrayPopupPaddingHorizontal
;
72 int right_margin
= kTrayPopupPaddingHorizontal
;
73 if (alignment
!= gfx::ALIGN_CENTER
) {
74 if (base::i18n::IsRTL())
75 right_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
77 left_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
79 text_label_
->SetBorder(
80 views::Border::CreateEmptyBorder(5, left_margin
, 5, right_margin
));
81 text_label_
->SetHorizontalAlignment(alignment
);
82 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
83 // Do not set alpha value in disable color. It will have issue with elide
84 // blending filter in disabled state for rendering label text color.
85 text_label_
->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
86 if (text_default_color_
)
87 text_label_
->SetEnabledColor(text_default_color_
);
88 AddChildView(text_label_
);
90 SetAccessibleName(text
);
94 views::Label
* HoverHighlightView::AddCheckableLabel(const base::string16
& text
,
95 gfx::Font::FontStyle style
,
100 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
101 const gfx::ImageSkia
* check
=
102 rb
.GetImageNamed(IDR_MENU_CHECK
).ToImageSkia();
103 int margin
= kTrayPopupPaddingHorizontal
+
104 kTrayPopupDetailsLabelExtraLeftMargin
- kCheckLabelPadding
;
105 SetLayoutManager(new views::BoxLayout(
106 views::BoxLayout::kHorizontal
, 0, 3, kCheckLabelPadding
));
107 views::ImageView
* image_view
= new FixedSizedImageView(margin
, 0);
108 image_view
->SetImage(check
);
109 image_view
->SetHorizontalAlignment(views::ImageView::TRAILING
);
110 AddChildView(image_view
);
112 text_label_
= new views::Label(text
);
113 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
114 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
115 text_label_
->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
116 if (text_default_color_
)
117 text_label_
->SetEnabledColor(text_default_color_
);
118 AddChildView(text_label_
);
120 SetAccessibleName(text
);
123 return AddLabel(text
, gfx::ALIGN_LEFT
, style
);
126 void HoverHighlightView::SetExpandable(bool expandable
) {
127 if (expandable
!= expandable_
) {
128 expandable_
= expandable
;
133 bool HoverHighlightView::PerformAction(const ui::Event
& event
) {
136 listener_
->OnViewClicked(this);
140 void HoverHighlightView::GetAccessibleState(ui::AXViewState
* state
) {
141 ActionableView::GetAccessibleState(state
);
144 state
->role
= ui::AX_ROLE_CHECK_BOX
;
146 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
150 gfx::Size
HoverHighlightView::GetPreferredSize() const {
151 gfx::Size size
= ActionableView::GetPreferredSize();
152 if (!expandable_
|| size
.height() < kTrayPopupItemHeight
)
153 size
.set_height(kTrayPopupItemHeight
);
157 int HoverHighlightView::GetHeightForWidth(int width
) const {
158 return GetPreferredSize().height();
161 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent
& event
) {
163 if (text_highlight_color_
&& text_label_
)
164 text_label_
->SetEnabledColor(text_highlight_color_
);
168 void HoverHighlightView::OnMouseExited(const ui::MouseEvent
& event
) {
170 if (text_default_color_
&& text_label_
)
171 text_label_
->SetEnabledColor(text_default_color_
);
175 void HoverHighlightView::OnEnabledChanged() {
176 for (int i
= 0; i
< child_count(); ++i
)
177 child_at(i
)->SetEnabled(enabled());
180 void HoverHighlightView::OnPaintBackground(gfx::Canvas
* canvas
) {
181 canvas
->DrawColor(hover_
? highlight_color_
: default_color_
);
184 void HoverHighlightView::OnFocus() {
185 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
186 ActionableView::OnFocus();