Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / system / tray / hover_highlight_view.cc
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.
4
5 #include "ash/system/tray/hover_highlight_view.h"
6
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"
20
21 namespace {
22
23 const int kCheckLabelPadding = 4;
24
25 }  // namespace
26
27 namespace ash {
28 namespace internal {
29
30 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
31     : listener_(listener),
32       text_label_(NULL),
33       highlight_color_(kHoverBackgroundColor),
34       default_color_(0),
35       text_highlight_color_(0),
36       text_default_color_(0),
37       hover_(false),
38       expandable_(false),
39       checkable_(false),
40       checked_(false) {
41   set_notify_enter_exit_on_child(true);
42 }
43
44 HoverHighlightView::~HoverHighlightView() {
45 }
46
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);
56
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_);
63
64   SetAccessibleName(text);
65 }
66
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;
73   int left_margin = 0;
74   int right_margin = 0;
75   if (base::i18n::IsRTL())
76     right_margin = margin;
77   else
78     left_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_);
89
90   SetAccessibleName(text);
91   return text_label_;
92 }
93
94 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
95                                                     gfx::Font::FontStyle style,
96                                                     bool checked) {
97   checkable_ = true;
98   checked_ = checked;
99   if (checked) {
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);
111
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_);
119
120     SetAccessibleName(text);
121     return text_label_;
122   }
123   return AddLabel(text, style);
124 }
125
126 void HoverHighlightView::SetExpandable(bool expandable) {
127   if (expandable != expandable_) {
128     expandable_ = expandable;
129     InvalidateLayout();
130   }
131 }
132
133 bool HoverHighlightView::PerformAction(const ui::Event& event) {
134   if (!listener_)
135     return false;
136   listener_->OnViewClicked(this);
137   return true;
138 }
139
140 void HoverHighlightView::GetAccessibleState(ui::AccessibleViewState* state) {
141   ActionableView::GetAccessibleState(state);
142
143   if (checkable_) {
144     state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
145     state->state = checked_ ? ui::AccessibilityTypes::STATE_CHECKED : 0;
146   }
147 }
148
149 gfx::Size HoverHighlightView::GetPreferredSize() {
150   gfx::Size size = ActionableView::GetPreferredSize();
151   if (!expandable_ || size.height() < kTrayPopupItemHeight)
152     size.set_height(kTrayPopupItemHeight);
153   return size;
154 }
155
156 int HoverHighlightView::GetHeightForWidth(int width) {
157   return GetPreferredSize().height();
158 }
159
160 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
161   hover_ = true;
162   if (text_highlight_color_ && text_label_)
163     text_label_->SetEnabledColor(text_highlight_color_);
164   SchedulePaint();
165 }
166
167 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
168   hover_ = false;
169   if (text_default_color_ && text_label_)
170     text_label_->SetEnabledColor(text_default_color_);
171   SchedulePaint();
172 }
173
174 void HoverHighlightView::OnEnabledChanged() {
175   for (int i = 0; i < child_count(); ++i)
176     child_at(i)->SetEnabled(enabled());
177 }
178
179 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
180   canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
181 }
182
183 void HoverHighlightView::OnFocus() {
184   ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
185   ActionableView::OnFocus();
186 }
187
188 }  // namespace internal
189 }  // namespace ash