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"
21 #include "ui/views/resources/grit/views_resources.h"
25 const int kCheckLabelPadding
= 4;
27 const gfx::FontList
& GetFontList(bool highlight
) {
28 return ui::ResourceBundle::GetSharedInstance().GetFontList(
29 highlight
? ui::ResourceBundle::BoldFont
: ui::ResourceBundle::BaseFont
);
36 HoverHighlightView::HoverHighlightView(ViewClickListener
* listener
)
37 : listener_(listener
),
39 highlight_color_(kHoverBackgroundColor
),
41 text_highlight_color_(0),
42 text_default_color_(0),
47 set_notify_enter_exit_on_child(true);
50 HoverHighlightView::~HoverHighlightView() {
53 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia
& image
,
54 const base::string16
& text
,
56 SetLayoutManager(new views::BoxLayout(
57 views::BoxLayout::kHorizontal
, 0, 3, kTrayPopupPaddingBetweenItems
));
58 DoAddIconAndLabel(image
, text
, highlight
);
61 void HoverHighlightView::AddIndentedIconAndLabel(const gfx::ImageSkia
& image
,
62 const base::string16
& text
,
64 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal
,
65 kTrayPopupPaddingHorizontal
, 0,
66 kTrayPopupPaddingBetweenItems
));
67 DoAddIconAndLabel(image
, text
, highlight
);
70 void HoverHighlightView::DoAddIconAndLabel(const gfx::ImageSkia
& image
,
71 const base::string16
& text
,
73 views::ImageView
* image_view
=
74 new FixedSizedImageView(kTrayPopupDetailsIconWidth
, 0);
75 image_view
->SetImage(image
);
76 AddChildView(image_view
);
78 text_label_
= new views::Label(text
);
79 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
80 text_label_
->SetFontList(GetFontList(highlight
));
81 if (text_default_color_
)
82 text_label_
->SetEnabledColor(text_default_color_
);
83 AddChildView(text_label_
);
85 SetAccessibleName(text
);
88 views::Label
* HoverHighlightView::AddLabel(const base::string16
& text
,
89 gfx::HorizontalAlignment alignment
,
91 SetLayoutManager(new views::FillLayout());
92 text_label_
= new views::Label(text
);
93 int left_margin
= kTrayPopupPaddingHorizontal
;
94 int right_margin
= kTrayPopupPaddingHorizontal
;
95 if (alignment
!= gfx::ALIGN_CENTER
) {
96 if (base::i18n::IsRTL())
97 right_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
99 left_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
101 text_label_
->SetBorder(
102 views::Border::CreateEmptyBorder(5, left_margin
, 5, right_margin
));
103 text_label_
->SetHorizontalAlignment(alignment
);
104 text_label_
->SetFontList(GetFontList(highlight
));
105 // Do not set alpha value in disable color. It will have issue with elide
106 // blending filter in disabled state for rendering label text color.
107 text_label_
->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
108 if (text_default_color_
)
109 text_label_
->SetEnabledColor(text_default_color_
);
110 AddChildView(text_label_
);
112 SetAccessibleName(text
);
116 views::Label
* HoverHighlightView::AddCheckableLabel(const base::string16
& text
,
122 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
123 const gfx::ImageSkia
* check
=
124 rb
.GetImageNamed(IDR_MENU_CHECK
).ToImageSkia();
125 int margin
= kTrayPopupPaddingHorizontal
+
126 kTrayPopupDetailsLabelExtraLeftMargin
- kCheckLabelPadding
;
127 SetLayoutManager(new views::BoxLayout(
128 views::BoxLayout::kHorizontal
, 0, 3, kCheckLabelPadding
));
129 views::ImageView
* image_view
= new FixedSizedImageView(margin
, 0);
130 image_view
->SetImage(check
);
131 image_view
->SetHorizontalAlignment(views::ImageView::TRAILING
);
132 AddChildView(image_view
);
134 text_label_
= new views::Label(text
);
135 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
136 text_label_
->SetFontList(GetFontList(highlight
));
137 text_label_
->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
138 if (text_default_color_
)
139 text_label_
->SetEnabledColor(text_default_color_
);
140 AddChildView(text_label_
);
142 SetAccessibleName(text
);
145 return AddLabel(text
, gfx::ALIGN_LEFT
, highlight
);
148 void HoverHighlightView::SetExpandable(bool expandable
) {
149 if (expandable
!= expandable_
) {
150 expandable_
= expandable
;
155 void HoverHighlightView::SetHoverHighlight(bool hover
) {
161 if (hover_
&& text_highlight_color_
)
162 text_label_
->SetEnabledColor(text_highlight_color_
);
163 if (!hover_
&& text_default_color_
)
164 text_label_
->SetEnabledColor(text_default_color_
);
168 bool HoverHighlightView::PerformAction(const ui::Event
& event
) {
171 listener_
->OnViewClicked(this);
175 void HoverHighlightView::GetAccessibleState(ui::AXViewState
* state
) {
176 ActionableView::GetAccessibleState(state
);
179 state
->role
= ui::AX_ROLE_CHECK_BOX
;
181 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
185 gfx::Size
HoverHighlightView::GetPreferredSize() const {
186 gfx::Size size
= ActionableView::GetPreferredSize();
187 if (!expandable_
|| size
.height() < kTrayPopupItemHeight
)
188 size
.set_height(kTrayPopupItemHeight
);
192 int HoverHighlightView::GetHeightForWidth(int width
) const {
193 return GetPreferredSize().height();
196 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent
& event
) {
197 SetHoverHighlight(true);
200 void HoverHighlightView::OnMouseExited(const ui::MouseEvent
& event
) {
201 SetHoverHighlight(false);
204 void HoverHighlightView::OnGestureEvent(ui::GestureEvent
* event
) {
205 if (switches::IsTouchFeedbackEnabled()) {
206 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
207 SetHoverHighlight(true);
208 } else if (event
->type() == ui::ET_GESTURE_TAP_CANCEL
||
209 event
->type() == ui::ET_GESTURE_TAP
) {
210 SetHoverHighlight(false);
213 ActionableView::OnGestureEvent(event
);
216 void HoverHighlightView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
217 SetHoverHighlight(IsMouseHovered());
220 void HoverHighlightView::OnEnabledChanged() {
221 for (int i
= 0; i
< child_count(); ++i
)
222 child_at(i
)->SetEnabled(enabled());
225 void HoverHighlightView::OnPaintBackground(gfx::Canvas
* canvas
) {
226 canvas
->DrawColor(hover_
? highlight_color_
: default_color_
);
229 void HoverHighlightView::OnFocus() {
230 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
231 ActionableView::OnFocus();