| OLD | NEW |
| (Empty) |
| 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/common/system/tray/fixed_sized_image_view.h" | |
| 8 #include "ash/common/system/tray/tray_constants.h" | |
| 9 #include "ash/system/tray/view_click_listener.h" | |
| 10 #include "ui/accessibility/ax_view_state.h" | |
| 11 #include "ui/base/resource/resource_bundle.h" | |
| 12 #include "ui/base/ui_base_switches_util.h" | |
| 13 #include "ui/gfx/canvas.h" | |
| 14 #include "ui/gfx/font_list.h" | |
| 15 #include "ui/resources/grit/ui_resources.h" | |
| 16 #include "ui/views/border.h" | |
| 17 #include "ui/views/controls/image_view.h" | |
| 18 #include "ui/views/controls/label.h" | |
| 19 #include "ui/views/layout/box_layout.h" | |
| 20 #include "ui/views/layout/fill_layout.h" | |
| 21 #include "ui/views/resources/grit/views_resources.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 const int kCheckLabelPadding = 4; | |
| 26 | |
| 27 const gfx::FontList& GetFontList(bool highlight) { | |
| 28 return ui::ResourceBundle::GetSharedInstance().GetFontList( | |
| 29 highlight ? ui::ResourceBundle::BoldFont : ui::ResourceBundle::BaseFont); | |
| 30 } | |
| 31 | |
| 32 } // namespace | |
| 33 | |
| 34 namespace ash { | |
| 35 | |
| 36 HoverHighlightView::HoverHighlightView(ViewClickListener* listener) | |
| 37 : listener_(listener), | |
| 38 text_label_(NULL), | |
| 39 highlight_color_(kHoverBackgroundColor), | |
| 40 default_color_(0), | |
| 41 text_highlight_color_(0), | |
| 42 text_default_color_(0), | |
| 43 hover_(false), | |
| 44 expandable_(false), | |
| 45 checkable_(false), | |
| 46 checked_(false) { | |
| 47 set_notify_enter_exit_on_child(true); | |
| 48 } | |
| 49 | |
| 50 HoverHighlightView::~HoverHighlightView() { | |
| 51 } | |
| 52 | |
| 53 bool HoverHighlightView::GetTooltipText(const gfx::Point& p, | |
| 54 base::string16* tooltip) const { | |
| 55 if (tooltip_.empty()) | |
| 56 return false; | |
| 57 *tooltip = tooltip_; | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image, | |
| 62 const base::string16& text, | |
| 63 bool highlight) { | |
| 64 SetLayoutManager(new views::BoxLayout( | |
| 65 views::BoxLayout::kHorizontal, 0, 3, kTrayPopupPaddingBetweenItems)); | |
| 66 DoAddIconAndLabel(image, text, highlight); | |
| 67 } | |
| 68 | |
| 69 void HoverHighlightView::AddIndentedIconAndLabel(const gfx::ImageSkia& image, | |
| 70 const base::string16& text, | |
| 71 bool highlight) { | |
| 72 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, | |
| 73 kTrayPopupPaddingHorizontal, 0, | |
| 74 kTrayPopupPaddingBetweenItems)); | |
| 75 DoAddIconAndLabel(image, text, highlight); | |
| 76 } | |
| 77 | |
| 78 void HoverHighlightView::DoAddIconAndLabel(const gfx::ImageSkia& image, | |
| 79 const base::string16& text, | |
| 80 bool highlight) { | |
| 81 views::ImageView* image_view = | |
| 82 new FixedSizedImageView(kTrayPopupDetailsIconWidth, 0); | |
| 83 image_view->SetImage(image); | |
| 84 image_view->SetEnabled(enabled()); | |
| 85 AddChildView(image_view); | |
| 86 | |
| 87 text_label_ = new views::Label(text); | |
| 88 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 89 text_label_->SetFontList(GetFontList(highlight)); | |
| 90 if (text_default_color_) | |
| 91 text_label_->SetEnabledColor(text_default_color_); | |
| 92 text_label_->SetEnabled(enabled()); | |
| 93 AddChildView(text_label_); | |
| 94 | |
| 95 SetAccessibleName(text); | |
| 96 } | |
| 97 | |
| 98 views::Label* HoverHighlightView::AddLabel(const base::string16& text, | |
| 99 gfx::HorizontalAlignment alignment, | |
| 100 bool highlight) { | |
| 101 SetLayoutManager(new views::FillLayout()); | |
| 102 text_label_ = new views::Label(text); | |
| 103 int left_margin = kTrayPopupPaddingHorizontal; | |
| 104 int right_margin = kTrayPopupPaddingHorizontal; | |
| 105 if (alignment != gfx::ALIGN_CENTER) { | |
| 106 if (base::i18n::IsRTL()) | |
| 107 right_margin += kTrayPopupDetailsLabelExtraLeftMargin; | |
| 108 else | |
| 109 left_margin += kTrayPopupDetailsLabelExtraLeftMargin; | |
| 110 } | |
| 111 text_label_->SetBorder( | |
| 112 views::Border::CreateEmptyBorder(5, left_margin, 5, right_margin)); | |
| 113 text_label_->SetHorizontalAlignment(alignment); | |
| 114 text_label_->SetFontList(GetFontList(highlight)); | |
| 115 // Do not set alpha value in disable color. It will have issue with elide | |
| 116 // blending filter in disabled state for rendering label text color. | |
| 117 text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127)); | |
| 118 if (text_default_color_) | |
| 119 text_label_->SetEnabledColor(text_default_color_); | |
| 120 text_label_->SetEnabled(enabled()); | |
| 121 AddChildView(text_label_); | |
| 122 | |
| 123 SetAccessibleName(text); | |
| 124 return text_label_; | |
| 125 } | |
| 126 | |
| 127 views::Label* HoverHighlightView::AddCheckableLabel(const base::string16& text, | |
| 128 bool highlight, | |
| 129 bool checked) { | |
| 130 checkable_ = true; | |
| 131 checked_ = checked; | |
| 132 if (checked) { | |
| 133 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 134 const gfx::ImageSkia* check = | |
| 135 rb.GetImageNamed(IDR_MENU_CHECK).ToImageSkia(); | |
| 136 int margin = kTrayPopupPaddingHorizontal + | |
| 137 kTrayPopupDetailsLabelExtraLeftMargin - kCheckLabelPadding; | |
| 138 SetLayoutManager(new views::BoxLayout( | |
| 139 views::BoxLayout::kHorizontal, 0, 3, kCheckLabelPadding)); | |
| 140 views::ImageView* image_view = new FixedSizedImageView(margin, 0); | |
| 141 image_view->SetImage(check); | |
| 142 image_view->SetHorizontalAlignment(views::ImageView::TRAILING); | |
| 143 image_view->SetEnabled(enabled()); | |
| 144 AddChildView(image_view); | |
| 145 | |
| 146 text_label_ = new views::Label(text); | |
| 147 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 148 text_label_->SetFontList(GetFontList(highlight)); | |
| 149 text_label_->SetDisabledColor(SkColorSetARGB(127, 0, 0, 0)); | |
| 150 if (text_default_color_) | |
| 151 text_label_->SetEnabledColor(text_default_color_); | |
| 152 text_label_->SetEnabled(enabled()); | |
| 153 AddChildView(text_label_); | |
| 154 | |
| 155 SetAccessibleName(text); | |
| 156 return text_label_; | |
| 157 } | |
| 158 return AddLabel(text, gfx::ALIGN_LEFT, highlight); | |
| 159 } | |
| 160 | |
| 161 void HoverHighlightView::SetExpandable(bool expandable) { | |
| 162 if (expandable != expandable_) { | |
| 163 expandable_ = expandable; | |
| 164 InvalidateLayout(); | |
| 165 } | |
| 166 } | |
| 167 | |
| 168 void HoverHighlightView::SetHoverHighlight(bool hover) { | |
| 169 if (!enabled() && hover) | |
| 170 return; | |
| 171 if (hover_ == hover) | |
| 172 return; | |
| 173 hover_ = hover; | |
| 174 if (!text_label_) | |
| 175 return; | |
| 176 if (hover_ && text_highlight_color_) | |
| 177 text_label_->SetEnabledColor(text_highlight_color_); | |
| 178 if (!hover_ && text_default_color_) | |
| 179 text_label_->SetEnabledColor(text_default_color_); | |
| 180 SchedulePaint(); | |
| 181 } | |
| 182 | |
| 183 bool HoverHighlightView::PerformAction(const ui::Event& event) { | |
| 184 if (!listener_) | |
| 185 return false; | |
| 186 listener_->OnViewClicked(this); | |
| 187 return true; | |
| 188 } | |
| 189 | |
| 190 void HoverHighlightView::GetAccessibleState(ui::AXViewState* state) { | |
| 191 ActionableView::GetAccessibleState(state); | |
| 192 | |
| 193 if (checkable_) { | |
| 194 state->role = ui::AX_ROLE_CHECK_BOX; | |
| 195 if (checked_) | |
| 196 state->AddStateFlag(ui::AX_STATE_CHECKED); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 gfx::Size HoverHighlightView::GetPreferredSize() const { | |
| 201 gfx::Size size = ActionableView::GetPreferredSize(); | |
| 202 if (!expandable_ || size.height() < kTrayPopupItemHeight) | |
| 203 size.set_height(kTrayPopupItemHeight); | |
| 204 return size; | |
| 205 } | |
| 206 | |
| 207 int HoverHighlightView::GetHeightForWidth(int width) const { | |
| 208 return GetPreferredSize().height(); | |
| 209 } | |
| 210 | |
| 211 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) { | |
| 212 SetHoverHighlight(true); | |
| 213 } | |
| 214 | |
| 215 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) { | |
| 216 SetHoverHighlight(false); | |
| 217 } | |
| 218 | |
| 219 void HoverHighlightView::OnGestureEvent(ui::GestureEvent* event) { | |
| 220 if (switches::IsTouchFeedbackEnabled()) { | |
| 221 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { | |
| 222 SetHoverHighlight(true); | |
| 223 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL || | |
| 224 event->type() == ui::ET_GESTURE_TAP) { | |
| 225 SetHoverHighlight(false); | |
| 226 } | |
| 227 } | |
| 228 ActionableView::OnGestureEvent(event); | |
| 229 } | |
| 230 | |
| 231 void HoverHighlightView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
| 232 SetHoverHighlight(IsMouseHovered()); | |
| 233 } | |
| 234 | |
| 235 void HoverHighlightView::OnEnabledChanged() { | |
| 236 if (!enabled()) | |
| 237 SetHoverHighlight(false); | |
| 238 for (int i = 0; i < child_count(); ++i) | |
| 239 child_at(i)->SetEnabled(enabled()); | |
| 240 } | |
| 241 | |
| 242 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) { | |
| 243 canvas->DrawColor(hover_ ? highlight_color_ : default_color_); | |
| 244 } | |
| 245 | |
| 246 void HoverHighlightView::OnFocus() { | |
| 247 ScrollRectToVisible(gfx::Rect(gfx::Point(), size())); | |
| 248 ActionableView::OnFocus(); | |
| 249 } | |
| 250 | |
| 251 } // namespace ash | |
| OLD | NEW |