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/base/ui_base_switches_util.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/resources/grit/ui_resources.h"
16 #include "ui/views/border.h"
17 #include "ui/views/controls/image_view.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/layout/fill_layout.h"
24 const int kCheckLabelPadding
= 4;
30 HoverHighlightView::HoverHighlightView(ViewClickListener
* listener
)
31 : listener_(listener
),
33 highlight_color_(kHoverBackgroundColor
),
35 text_highlight_color_(0),
36 text_default_color_(0),
41 set_notify_enter_exit_on_child(true);
44 HoverHighlightView::~HoverHighlightView() {
47 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia
& image
,
48 const base::string16
& text
,
49 gfx::Font::FontStyle style
) {
50 SetLayoutManager(new views::BoxLayout(
51 views::BoxLayout::kHorizontal
, 0, 3, kTrayPopupPaddingBetweenItems
));
52 views::ImageView
* image_view
=
53 new FixedSizedImageView(kTrayPopupDetailsIconWidth
, 0);
54 image_view
->SetImage(image
);
55 AddChildView(image_view
);
57 text_label_
= new views::Label(text
);
58 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
59 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
60 if (text_default_color_
)
61 text_label_
->SetEnabledColor(text_default_color_
);
62 AddChildView(text_label_
);
64 SetAccessibleName(text
);
67 views::Label
* HoverHighlightView::AddLabel(const base::string16
& text
,
68 gfx::HorizontalAlignment alignment
,
69 gfx::Font::FontStyle style
) {
70 SetLayoutManager(new views::FillLayout());
71 text_label_
= new views::Label(text
);
72 int left_margin
= kTrayPopupPaddingHorizontal
;
73 int right_margin
= kTrayPopupPaddingHorizontal
;
74 if (alignment
!= gfx::ALIGN_CENTER
) {
75 if (base::i18n::IsRTL())
76 right_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
78 left_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
80 text_label_
->SetBorder(
81 views::Border::CreateEmptyBorder(5, left_margin
, 5, right_margin
));
82 text_label_
->SetHorizontalAlignment(alignment
);
83 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
84 // Do not set alpha value in disable color. It will have issue with elide
85 // blending filter in disabled state for rendering label text color.
86 text_label_
->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
87 if (text_default_color_
)
88 text_label_
->SetEnabledColor(text_default_color_
);
89 AddChildView(text_label_
);
91 SetAccessibleName(text
);
95 views::Label
* HoverHighlightView::AddCheckableLabel(const base::string16
& text
,
96 gfx::Font::FontStyle style
,
101 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
102 const gfx::ImageSkia
* check
=
103 rb
.GetImageNamed(IDR_MENU_CHECK
).ToImageSkia();
104 int margin
= kTrayPopupPaddingHorizontal
+
105 kTrayPopupDetailsLabelExtraLeftMargin
- kCheckLabelPadding
;
106 SetLayoutManager(new views::BoxLayout(
107 views::BoxLayout::kHorizontal
, 0, 3, kCheckLabelPadding
));
108 views::ImageView
* image_view
= new FixedSizedImageView(margin
, 0);
109 image_view
->SetImage(check
);
110 image_view
->SetHorizontalAlignment(views::ImageView::TRAILING
);
111 AddChildView(image_view
);
113 text_label_
= new views::Label(text
);
114 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
115 text_label_
->SetFontList(text_label_
->font_list().DeriveWithStyle(style
));
116 text_label_
->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
117 if (text_default_color_
)
118 text_label_
->SetEnabledColor(text_default_color_
);
119 AddChildView(text_label_
);
121 SetAccessibleName(text
);
124 return AddLabel(text
, gfx::ALIGN_LEFT
, style
);
127 void HoverHighlightView::SetExpandable(bool expandable
) {
128 if (expandable
!= expandable_
) {
129 expandable_
= expandable
;
134 void HoverHighlightView::SetHoverHighlight(bool hover
) {
140 if (hover_
&& text_highlight_color_
)
141 text_label_
->SetEnabledColor(text_highlight_color_
);
142 if (!hover_
&& text_default_color_
)
143 text_label_
->SetEnabledColor(text_default_color_
);
147 bool HoverHighlightView::PerformAction(const ui::Event
& event
) {
150 listener_
->OnViewClicked(this);
154 void HoverHighlightView::GetAccessibleState(ui::AXViewState
* state
) {
155 ActionableView::GetAccessibleState(state
);
158 state
->role
= ui::AX_ROLE_CHECK_BOX
;
160 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
164 gfx::Size
HoverHighlightView::GetPreferredSize() const {
165 gfx::Size size
= ActionableView::GetPreferredSize();
166 if (!expandable_
|| size
.height() < kTrayPopupItemHeight
)
167 size
.set_height(kTrayPopupItemHeight
);
171 int HoverHighlightView::GetHeightForWidth(int width
) const {
172 return GetPreferredSize().height();
175 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent
& event
) {
176 SetHoverHighlight(true);
179 void HoverHighlightView::OnMouseExited(const ui::MouseEvent
& event
) {
180 SetHoverHighlight(false);
183 void HoverHighlightView::OnGestureEvent(ui::GestureEvent
* event
) {
184 if (switches::IsTouchFeedbackEnabled()) {
185 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
186 SetHoverHighlight(true);
187 } else if (event
->type() == ui::ET_GESTURE_TAP_CANCEL
||
188 event
->type() == ui::ET_GESTURE_TAP
) {
189 SetHoverHighlight(false);
192 ActionableView::OnGestureEvent(event
);
195 void HoverHighlightView::OnEnabledChanged() {
196 for (int i
= 0; i
< child_count(); ++i
)
197 child_at(i
)->SetEnabled(enabled());
200 void HoverHighlightView::OnPaintBackground(gfx::Canvas
* canvas
) {
201 canvas
->DrawColor(hover_
? highlight_color_
: default_color_
);
204 void HoverHighlightView::OnFocus() {
205 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
206 ActionableView::OnFocus();