a1b08db257ce30e61786e0aac98de0131f37a89c
[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(
60       text_label_->font_list().DeriveFontListWithSizeDeltaAndStyle(0, style));
61   if (text_default_color_)
62     text_label_->SetEnabledColor(text_default_color_);
63   AddChildView(text_label_);
64
65   SetAccessibleName(text);
66 }
67
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;
74   int left_margin = 0;
75   int right_margin = 0;
76   if (base::i18n::IsRTL())
77     right_margin = margin;
78   else
79     left_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_);
91
92   SetAccessibleName(text);
93   return text_label_;
94 }
95
96 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
97                                                     gfx::Font::FontStyle style,
98                                                     bool checked) {
99   checkable_ = true;
100   checked_ = checked;
101   if (checked) {
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);
113
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_);
122
123     SetAccessibleName(text);
124     return text_label_;
125   }
126   return AddLabel(text, style);
127 }
128
129 void HoverHighlightView::SetExpandable(bool expandable) {
130   if (expandable != expandable_) {
131     expandable_ = expandable;
132     InvalidateLayout();
133   }
134 }
135
136 bool HoverHighlightView::PerformAction(const ui::Event& event) {
137   if (!listener_)
138     return false;
139   listener_->OnViewClicked(this);
140   return true;
141 }
142
143 void HoverHighlightView::GetAccessibleState(ui::AccessibleViewState* state) {
144   ActionableView::GetAccessibleState(state);
145
146   if (checkable_) {
147     state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
148     state->state = checked_ ? ui::AccessibilityTypes::STATE_CHECKED : 0;
149   }
150 }
151
152 gfx::Size HoverHighlightView::GetPreferredSize() {
153   gfx::Size size = ActionableView::GetPreferredSize();
154   if (!expandable_ || size.height() < kTrayPopupItemHeight)
155     size.set_height(kTrayPopupItemHeight);
156   return size;
157 }
158
159 int HoverHighlightView::GetHeightForWidth(int width) {
160   return GetPreferredSize().height();
161 }
162
163 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
164   hover_ = true;
165   if (text_highlight_color_ && text_label_)
166     text_label_->SetEnabledColor(text_highlight_color_);
167   SchedulePaint();
168 }
169
170 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
171   hover_ = false;
172   if (text_default_color_ && text_label_)
173     text_label_->SetEnabledColor(text_default_color_);
174   SchedulePaint();
175 }
176
177 void HoverHighlightView::OnEnabledChanged() {
178   for (int i = 0; i < child_count(); ++i)
179     child_at(i)->SetEnabled(enabled());
180 }
181
182 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
183   canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
184 }
185
186 void HoverHighlightView::OnFocus() {
187   ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
188   ActionableView::OnFocus();
189 }
190
191 }  // namespace internal
192 }  // namespace ash