Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / ash / system / tray / hover_highlight_view.cc
blob2ae1ef1825efffe941b2c83c2e35f9d388a59365
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"
23 namespace {
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);
32 } // namespace
34 namespace ash {
36 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
37 : listener_(listener),
38 text_label_(NULL),
39 highlight_color_(kHoverBackgroundColor),
40 default_color_(0),
41 text_highlight_color_(0),
42 text_default_color_(0),
43 hover_(false),
44 expandable_(false),
45 checkable_(false),
46 checked_(false) {
47 set_notify_enter_exit_on_child(true);
50 HoverHighlightView::~HoverHighlightView() {
53 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image,
54 const base::string16& text,
55 bool highlight) {
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,
63 bool highlight) {
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,
72 bool highlight) {
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,
90 bool highlight) {
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;
98 else
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);
113 return text_label_;
116 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
117 bool highlight,
118 bool checked) {
119 checkable_ = true;
120 checked_ = checked;
121 if (checked) {
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);
143 return text_label_;
145 return AddLabel(text, gfx::ALIGN_LEFT, highlight);
148 void HoverHighlightView::SetExpandable(bool expandable) {
149 if (expandable != expandable_) {
150 expandable_ = expandable;
151 InvalidateLayout();
155 void HoverHighlightView::SetHoverHighlight(bool hover) {
156 if (hover_ == hover)
157 return;
158 hover_ = hover;
159 if (!text_label_)
160 return;
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_);
165 SchedulePaint();
168 bool HoverHighlightView::PerformAction(const ui::Event& event) {
169 if (!listener_)
170 return false;
171 listener_->OnViewClicked(this);
172 return true;
175 void HoverHighlightView::GetAccessibleState(ui::AXViewState* state) {
176 ActionableView::GetAccessibleState(state);
178 if (checkable_) {
179 state->role = ui::AX_ROLE_CHECK_BOX;
180 if (checked_)
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);
189 return size;
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();
234 } // namespace ash