• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/views/border.h"
15 #include "ui/views/controls/image_view.h"
16 #include "ui/views/controls/label.h"
17 #include "ui/views/layout/box_layout.h"
18 #include "ui/views/layout/fill_layout.h"
19 
20 namespace {
21 
22 const int kCheckLabelPadding = 4;
23 
24 }  // namespace
25 
26 namespace ash {
27 namespace internal {
28 
HoverHighlightView(ViewClickListener * listener)29 HoverHighlightView::HoverHighlightView(ViewClickListener* listener)
30     : listener_(listener),
31       text_label_(NULL),
32       highlight_color_(kHoverBackgroundColor),
33       default_color_(0),
34       text_highlight_color_(0),
35       text_default_color_(0),
36       hover_(false),
37       expandable_(false),
38       checkable_(false),
39       checked_(false) {
40   set_notify_enter_exit_on_child(true);
41 }
42 
~HoverHighlightView()43 HoverHighlightView::~HoverHighlightView() {
44 }
45 
AddIconAndLabel(const gfx::ImageSkia & image,const base::string16 & text,gfx::Font::FontStyle style)46 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image,
47                                          const base::string16& text,
48                                          gfx::Font::FontStyle style) {
49   SetLayoutManager(new views::BoxLayout(
50       views::BoxLayout::kHorizontal, 0, 3, kTrayPopupPaddingBetweenItems));
51   views::ImageView* image_view =
52       new FixedSizedImageView(kTrayPopupDetailsIconWidth, 0);
53   image_view->SetImage(image);
54   AddChildView(image_view);
55 
56   text_label_ = new views::Label(text);
57   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
58   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
59   if (text_default_color_)
60     text_label_->SetEnabledColor(text_default_color_);
61   AddChildView(text_label_);
62 
63   SetAccessibleName(text);
64 }
65 
AddLabel(const base::string16 & text,gfx::Font::FontStyle style)66 views::Label* HoverHighlightView::AddLabel(const base::string16& text,
67                                            gfx::Font::FontStyle style) {
68   SetLayoutManager(new views::FillLayout());
69   text_label_ = new views::Label(text);
70   int margin = kTrayPopupPaddingHorizontal +
71       kTrayPopupDetailsLabelExtraLeftMargin;
72   int left_margin = 0;
73   int right_margin = 0;
74   if (base::i18n::IsRTL())
75     right_margin = margin;
76   else
77     left_margin = margin;
78   text_label_->set_border(
79       views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin));
80   text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
81   text_label_->SetFont(text_label_->font().DeriveFont(0, style));
82   // Do not set alpha value in disable color. It will have issue with elide
83   // blending filter in disabled state for rendering label text color.
84   text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127));
85   if (text_default_color_)
86     text_label_->SetEnabledColor(text_default_color_);
87   AddChildView(text_label_);
88 
89   SetAccessibleName(text);
90   return text_label_;
91 }
92 
AddCheckableLabel(const base::string16 & text,gfx::Font::FontStyle style,bool checked)93 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text,
94                                                     gfx::Font::FontStyle style,
95                                                     bool checked) {
96   checkable_ = true;
97   checked_ = checked;
98   if (checked) {
99     ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
100     const gfx::ImageSkia* check =
101         rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia();
102     int margin = kTrayPopupPaddingHorizontal +
103         kTrayPopupDetailsLabelExtraLeftMargin - kCheckLabelPadding;
104     SetLayoutManager(new views::BoxLayout(
105         views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding));
106     views::ImageView* image_view = new FixedSizedImageView(margin, 0);
107     image_view->SetImage(check);
108     image_view->SetHorizontalAlignment(views::ImageView::TRAILING);
109     AddChildView(image_view);
110 
111     text_label_ = new views::Label(text);
112     text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
113     text_label_->SetFont(text_label_->font().DeriveFont(0, style));
114     text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0));
115     if (text_default_color_)
116       text_label_->SetEnabledColor(text_default_color_);
117     AddChildView(text_label_);
118 
119     SetAccessibleName(text);
120     return text_label_;
121   }
122   return AddLabel(text, style);
123 }
124 
SetExpandable(bool expandable)125 void HoverHighlightView::SetExpandable(bool expandable) {
126   if (expandable != expandable_) {
127     expandable_ = expandable;
128     InvalidateLayout();
129   }
130 }
131 
PerformAction(const ui::Event & event)132 bool HoverHighlightView::PerformAction(const ui::Event& event) {
133   if (!listener_)
134     return false;
135   listener_->OnViewClicked(this);
136   return true;
137 }
138 
GetAccessibleState(ui::AccessibleViewState * state)139 void HoverHighlightView::GetAccessibleState(ui::AccessibleViewState* state) {
140   ActionableView::GetAccessibleState(state);
141 
142   if (checkable_) {
143     state->role = ui::AccessibilityTypes::ROLE_CHECKBUTTON;
144     state->state = checked_ ? ui::AccessibilityTypes::STATE_CHECKED : 0;
145   }
146 }
147 
GetPreferredSize()148 gfx::Size HoverHighlightView::GetPreferredSize() {
149   gfx::Size size = ActionableView::GetPreferredSize();
150   if (!expandable_ || size.height() < kTrayPopupItemHeight)
151     size.set_height(kTrayPopupItemHeight);
152   return size;
153 }
154 
GetHeightForWidth(int width)155 int HoverHighlightView::GetHeightForWidth(int width) {
156   return GetPreferredSize().height();
157 }
158 
OnMouseEntered(const ui::MouseEvent & event)159 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) {
160   hover_ = true;
161   if (text_highlight_color_ && text_label_)
162     text_label_->SetEnabledColor(text_highlight_color_);
163   SchedulePaint();
164 }
165 
OnMouseExited(const ui::MouseEvent & event)166 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) {
167   hover_ = false;
168   if (text_default_color_ && text_label_)
169     text_label_->SetEnabledColor(text_default_color_);
170   SchedulePaint();
171 }
172 
OnEnabledChanged()173 void HoverHighlightView::OnEnabledChanged() {
174   for (int i = 0; i < child_count(); ++i)
175     child_at(i)->SetEnabled(enabled());
176 }
177 
OnPaintBackground(gfx::Canvas * canvas)178 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) {
179   canvas->DrawColor(hover_ ? highlight_color_ : default_color_);
180 }
181 
OnFocus()182 void HoverHighlightView::OnFocus() {
183   ScrollRectToVisible(gfx::Rect(gfx::Point(), size()));
184   ActionableView::OnFocus();
185 }
186 
187 }  // namespace internal
188 }  // namespace ash
189