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(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::Font::FontStyle style) {
69 SetLayoutManager(new views::FillLayout());
70 text_label_ = new views::Label(text);
71 int margin = kTrayPopupPaddingHorizontal +
72 kTrayPopupDetailsLabelExtraLeftMargin;
75 if (base::i18n::IsRTL())
76 right_margin = margin;
79 text_label_->SetBorder(
80 views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin));
81 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
82 text_label_->SetFontList(text_label_->font_list().DeriveWithStyle(style));
83 // Do not set alpha value in disable color. It will have issue with elide
84 // blending filter in disabled state for rendering label text color.
85 text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
86 if (text_default_color_)
87 text_label_->SetEnabledColor(text_default_color_);
88 AddChildView(text_label_);
90 SetAccessibleName(text);
94 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
95 gfx::Font::FontStyle style,
100 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
101 const gfx::ImageSkia* check =
102 rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
103 int margin = kTrayPopupPaddingHorizontal +
104 kTrayPopupDetailsLabelExtraLeftMargin - kCheckLabelPadding;
105 SetLayoutManager(new views::BoxLayout(
106 views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding));
107 views::ImageView* image_view = new FixedSizedImageView(margin, 0);
108 image_view->SetImage(check);
109 image_view->SetHorizontalAlignment(views::ImageView::TRAILING);
110 AddChildView(image_view);
112 text_label_ = new views::Label(text);
113 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
114 text_label_->SetFontList(text_label_->font_list().DeriveWithStyle(style));
115 text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
116 if (text_default_color_)
117 text_label_->SetEnabledColor(text_default_color_);
118 AddChildView(text_label_);
120 SetAccessibleName(text);
123 return AddLabel(text, style);
126 void HoverHighlightView::SetExpandable(bool expandable) {
127 if (expandable != expandable_) {
128 expandable_ = expandable;
133 bool HoverHighlightView::PerformAction(const ui::Event& event) {
136 listener_->OnViewClicked(this);
140 void HoverHighlightView::GetAccessibleState(ui::AccessibleViewState* state) {
141 ActionableView::GetAccessibleState(state);
144 state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
145 state->state = checked_ ? ui::AccessibilityTypes::STATE_CHECKED : 0;
149 gfx::Size HoverHighlightView::GetPreferredSize() {
150 gfx::Size size = ActionableView::GetPreferredSize();
151 if (!expandable_ || size.height() < kTrayPopupItemHeight)
152 size.set_height(kTrayPopupItemHeight);
156 int HoverHighlightView::GetHeightForWidth(int width) {
157 return GetPreferredSize().height();
160 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
162 if (text_highlight_color_ && text_label_)
163 text_label_->SetEnabledColor(text_highlight_color_);
167 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
169 if (text_default_color_ && text_label_)
170 text_label_->SetEnabledColor(text_default_color_);
174 void HoverHighlightView::OnEnabledChanged() {
175 for (int i = 0; i < child_count(); ++i)
176 child_at(i)->SetEnabled(enabled());
179 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
180 canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
183 void HoverHighlightView::OnFocus() {
184 ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
185 ActionableView::OnFocus();
188 } // namespace internal