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 "grit/ui_resources.h"
11 #include "ui/base/accessibility/accessible_view_state.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font_list.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;
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(
60 text_label_->font_list().DeriveFontListWithSizeDeltaAndStyle(0, style));
61 if (text_default_color_)
62 text_label_->SetEnabledColor(text_default_color_);
63 AddChildView(text_label_);
65 SetAccessibleName(text);
68 views::Label* HoverHighlightView::AddLabel(const base::string16& text,
69 gfx::Font::FontStyle style) {
70 SetLayoutManager(new views::FillLayout());
71 text_label_ = new views::Label(text);
72 int margin = kTrayPopupPaddingHorizontal +
73 kTrayPopupDetailsLabelExtraLeftMargin;
76 if (base::i18n::IsRTL())
77 right_margin = margin;
80 text_label_->SetBorder(
81 views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin));
82 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
83 text_label_->SetFontList(
84 text_label_->font_list().DeriveFontListWithSizeDeltaAndStyle(0, style));
85 // Do not set alpha value in disable color. It will have issue with elide
86 // blending filter in disabled state for rendering label text color.
87 text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
88 if (text_default_color_)
89 text_label_->SetEnabledColor(text_default_color_);
90 AddChildView(text_label_);
92 SetAccessibleName(text);
96 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
97 gfx::Font::FontStyle style,
102 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
103 const gfx::ImageSkia* check =
104 rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
105 int margin = kTrayPopupPaddingHorizontal +
106 kTrayPopupDetailsLabelExtraLeftMargin - kCheckLabelPadding;
107 SetLayoutManager(new views::BoxLayout(
108 views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding));
109 views::ImageView* image_view = new FixedSizedImageView(margin, 0);
110 image_view->SetImage(check);
111 image_view->SetHorizontalAlignment(views::ImageView::TRAILING);
112 AddChildView(image_view);
114 text_label_ = new views::Label(text);
115 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
116 text_label_->SetFontList(
117 text_label_->font_list().DeriveFontListWithSizeDeltaAndStyle(0, style));
118 text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
119 if (text_default_color_)
120 text_label_->SetEnabledColor(text_default_color_);
121 AddChildView(text_label_);
123 SetAccessibleName(text);
126 return AddLabel(text, style);
129 void HoverHighlightView::SetExpandable(bool expandable) {
130 if (expandable != expandable_) {
131 expandable_ = expandable;
136 bool HoverHighlightView::PerformAction(const ui::Event& event) {
139 listener_->OnViewClicked(this);
143 void HoverHighlightView::GetAccessibleState(ui::AccessibleViewState* state) {
144 ActionableView::GetAccessibleState(state);
147 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
148 state->state = checked_ ? ui::AccessibilityTypes::STATE_CHECKED : 0;
152 gfx::Size HoverHighlightView::GetPreferredSize() {
153 gfx::Size size = ActionableView::GetPreferredSize();
154 if (!expandable_ || size.height() < kTrayPopupItemHeight)
155 size.set_height(kTrayPopupItemHeight);
159 int HoverHighlightView::GetHeightForWidth(int width) {
160 return GetPreferredSize().height();
163 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
165 if (text_highlight_color_ && text_label_)
166 text_label_->SetEnabledColor(text_highlight_color_);
170 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
172 if (text_default_color_ && text_label_)
173 text_label_->SetEnabledColor(text_default_color_);
177 void HoverHighlightView::OnEnabledChanged() {
178 for (int i = 0; i < child_count(); ++i)
179 child_at(i)->SetEnabled(enabled());
182 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
183 canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
186 void HoverHighlightView::OnFocus() {
187 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
188 ActionableView::OnFocus();
191 } // namespace internal