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;
26 const gfx::FontList
& GetFontList(bool highlight
) {
27 return ui::ResourceBundle::GetSharedInstance().GetFontList(
28 highlight
? ui::ResourceBundle::BoldFont
: ui::ResourceBundle::BaseFont
);
35 HoverHighlightView::HoverHighlightView(ViewClickListener
* listener
)
36 : listener_(listener
),
38 highlight_color_(kHoverBackgroundColor
),
40 text_highlight_color_(0),
41 text_default_color_(0),
46 set_notify_enter_exit_on_child(true);
49 HoverHighlightView::~HoverHighlightView() {
52 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia
& image
,
53 const base::string16
& text
,
55 SetLayoutManager(new views::BoxLayout(
56 views::BoxLayout::kHorizontal
, 0, 3, kTrayPopupPaddingBetweenItems
));
57 views::ImageView
* image_view
=
58 new FixedSizedImageView(kTrayPopupDetailsIconWidth
, 0);
59 image_view
->SetImage(image
);
60 AddChildView(image_view
);
62 text_label_
= new views::Label(text
);
63 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
64 text_label_
->SetFontList(GetFontList(highlight
));
65 if (text_default_color_
)
66 text_label_
->SetEnabledColor(text_default_color_
);
67 AddChildView(text_label_
);
69 SetAccessibleName(text
);
72 views::Label
* HoverHighlightView::AddLabel(const base::string16
& text
,
73 gfx::HorizontalAlignment alignment
,
75 SetLayoutManager(new views::FillLayout());
76 text_label_
= new views::Label(text
);
77 int left_margin
= kTrayPopupPaddingHorizontal
;
78 int right_margin
= kTrayPopupPaddingHorizontal
;
79 if (alignment
!= gfx::ALIGN_CENTER
) {
80 if (base::i18n::IsRTL())
81 right_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
83 left_margin
+= kTrayPopupDetailsLabelExtraLeftMargin
;
85 text_label_
->SetBorder(
86 views::Border::CreateEmptyBorder(5, left_margin
, 5, right_margin
));
87 text_label_
->SetHorizontalAlignment(alignment
);
88 text_label_
->SetFontList(GetFontList(highlight
));
89 // Do not set alpha value in disable color. It will have issue with elide
90 // blending filter in disabled state for rendering label text color.
91 text_label_
->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
92 if (text_default_color_
)
93 text_label_
->SetEnabledColor(text_default_color_
);
94 AddChildView(text_label_
);
96 SetAccessibleName(text
);
100 views::Label
* HoverHighlightView::AddCheckableLabel(const base::string16
& text
,
106 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
107 const gfx::ImageSkia
* check
=
108 rb
.GetImageNamed(IDR_MENU_CHECK
).ToImageSkia();
109 int margin
= kTrayPopupPaddingHorizontal
+
110 kTrayPopupDetailsLabelExtraLeftMargin
- kCheckLabelPadding
;
111 SetLayoutManager(new views::BoxLayout(
112 views::BoxLayout::kHorizontal
, 0, 3, kCheckLabelPadding
));
113 views::ImageView
* image_view
= new FixedSizedImageView(margin
, 0);
114 image_view
->SetImage(check
);
115 image_view
->SetHorizontalAlignment(views::ImageView::TRAILING
);
116 AddChildView(image_view
);
118 text_label_
= new views::Label(text
);
119 text_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
120 text_label_
->SetFontList(GetFontList(highlight
));
121 text_label_
->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
122 if (text_default_color_
)
123 text_label_
->SetEnabledColor(text_default_color_
);
124 AddChildView(text_label_
);
126 SetAccessibleName(text
);
129 return AddLabel(text
, gfx::ALIGN_LEFT
, highlight
);
132 void HoverHighlightView::SetExpandable(bool expandable
) {
133 if (expandable
!= expandable_
) {
134 expandable_
= expandable
;
139 void HoverHighlightView::SetHoverHighlight(bool hover
) {
145 if (hover_
&& text_highlight_color_
)
146 text_label_
->SetEnabledColor(text_highlight_color_
);
147 if (!hover_
&& text_default_color_
)
148 text_label_
->SetEnabledColor(text_default_color_
);
152 bool HoverHighlightView::PerformAction(const ui::Event
& event
) {
155 listener_
->OnViewClicked(this);
159 void HoverHighlightView::GetAccessibleState(ui::AXViewState
* state
) {
160 ActionableView::GetAccessibleState(state
);
163 state
->role
= ui::AX_ROLE_CHECK_BOX
;
165 state
->AddStateFlag(ui::AX_STATE_CHECKED
);
169 gfx::Size
HoverHighlightView::GetPreferredSize() const {
170 gfx::Size size
= ActionableView::GetPreferredSize();
171 if (!expandable_
|| size
.height() < kTrayPopupItemHeight
)
172 size
.set_height(kTrayPopupItemHeight
);
176 int HoverHighlightView::GetHeightForWidth(int width
) const {
177 return GetPreferredSize().height();
180 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent
& event
) {
181 SetHoverHighlight(true);
184 void HoverHighlightView::OnMouseExited(const ui::MouseEvent
& event
) {
185 SetHoverHighlight(false);
188 void HoverHighlightView::OnGestureEvent(ui::GestureEvent
* event
) {
189 if (switches::IsTouchFeedbackEnabled()) {
190 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
191 SetHoverHighlight(true);
192 } else if (event
->type() == ui::ET_GESTURE_TAP_CANCEL
||
193 event
->type() == ui::ET_GESTURE_TAP
) {
194 SetHoverHighlight(false);
197 ActionableView::OnGestureEvent(event
);
200 void HoverHighlightView::OnEnabledChanged() {
201 for (int i
= 0; i
< child_count(); ++i
)
202 child_at(i
)->SetEnabled(enabled());
205 void HoverHighlightView::OnPaintBackground(gfx::Canvas
* canvas
) {
206 canvas
->DrawColor(hover_
? highlight_color_
: default_color_
);
209 void HoverHighlightView::OnFocus() {
210 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
211 ActionableView::OnFocus();