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/common/system/tray/hover_highlight_view.h" | |
6 | |
7 #include "ash/common/material_design/material_design_controller.h" | |
8 #include "ash/common/system/tray/fixed_sized_image_view.h" | |
9 #include "ash/common/system/tray/tray_constants.h" | |
10 #include "ash/common/system/tray/tray_popup_utils.h" | |
11 #include "ash/common/system/tray/tri_view.h" | |
12 #include "ash/common/system/tray/view_click_listener.h" | |
13 #include "ash/resources/vector_icons/vector_icons.h" | |
14 #include "ui/accessibility/ax_node_data.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/font_list.h" | |
18 #include "ui/gfx/paint_vector_icon.h" | |
19 #include "ui/resources/grit/ui_resources.h" | |
20 #include "ui/views/border.h" | |
21 #include "ui/views/controls/image_view.h" | |
22 #include "ui/views/controls/label.h" | |
23 #include "ui/views/layout/box_layout.h" | |
24 #include "ui/views/layout/fill_layout.h" | |
25 #include "ui/views/resources/grit/views_resources.h" | |
26 | |
27 namespace { | |
28 | |
29 const gfx::FontList& GetFontList(bool highlight) { | |
30 return ui::ResourceBundle::GetSharedInstance().GetFontList( | |
31 highlight ? ui::ResourceBundle::BoldFont : ui::ResourceBundle::BaseFont); | |
32 } | |
33 | |
34 } // namespace | |
35 | |
36 namespace ash { | |
37 | |
38 HoverHighlightView::HoverHighlightView(ViewClickListener* listener) | |
39 : ActionableView(nullptr, TrayPopupInkDropStyle::FILL_BOUNDS), | |
40 listener_(listener), | |
41 highlight_color_(kHoverBackgroundColor) { | |
42 set_notify_enter_exit_on_child(true); | |
43 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | |
44 SetInkDropMode(InkDropHostView::InkDropMode::ON); | |
45 } | |
46 | |
47 HoverHighlightView::~HoverHighlightView() {} | |
48 | |
49 bool HoverHighlightView::GetTooltipText(const gfx::Point& p, | |
50 base::string16* tooltip) const { | |
51 if (tooltip_.empty()) | |
52 return false; | |
53 *tooltip = tooltip_; | |
54 return true; | |
55 } | |
56 | |
57 void HoverHighlightView::AddRightIcon(const gfx::ImageSkia& image, | |
58 int icon_size) { | |
59 DCHECK(!right_view_); | |
60 | |
61 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
62 views::ImageView* right_icon = TrayPopupUtils::CreateMainImageView(); | |
63 right_icon->SetImage(image); | |
64 AddRightView(right_icon); | |
65 return; | |
66 } | |
67 | |
68 views::ImageView* right_icon = new FixedSizedImageView(icon_size, icon_size); | |
69 right_icon->SetImage(image); | |
70 AddRightView(right_icon); | |
71 } | |
72 | |
73 void HoverHighlightView::AddRightView(views::View* view) { | |
74 DCHECK(!right_view_); | |
75 | |
76 right_view_ = view; | |
77 right_view_->SetEnabled(enabled()); | |
78 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
79 DCHECK(tri_view_); | |
80 tri_view_->AddView(TriView::Container::END, right_view_); | |
81 tri_view_->SetContainerVisible(TriView::Container::END, true); | |
82 return; | |
83 } | |
84 DCHECK(box_layout_); | |
85 AddChildView(right_view_); | |
86 } | |
87 | |
88 // TODO(tdanderson): Ensure all checkable detailed view rows use this | |
89 // mechanism, and share the code that sets the accessible state for | |
90 // a checkbox. See crbug.com/652674. | |
91 void HoverHighlightView::SetRightViewVisible(bool visible) { | |
92 if (!right_view_) | |
93 return; | |
94 | |
95 right_view_->SetVisible(visible); | |
96 Layout(); | |
97 } | |
98 | |
99 void HoverHighlightView::AddIconAndLabel(const gfx::ImageSkia& image, | |
100 const base::string16& text, | |
101 bool highlight) { | |
102 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
103 DoAddIconAndLabelMd(image, text, | |
104 TrayPopupItemStyle::FontStyle::DETAILED_VIEW_LABEL); | |
105 return; | |
106 } | |
107 | |
108 box_layout_ = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 3, | |
109 kTrayPopupPaddingBetweenItems); | |
110 SetLayoutManager(box_layout_); | |
111 DoAddIconAndLabel(image, kTrayPopupDetailsIconWidth, text, highlight); | |
112 } | |
113 | |
114 void HoverHighlightView::AddIconAndLabels(const gfx::ImageSkia& image, | |
115 const base::string16& text, | |
116 const base::string16& sub_text) { | |
117 DCHECK(MaterialDesignController::IsSystemTrayMenuMaterial()); | |
118 DoAddIconAndLabelsMd(image, text, | |
119 TrayPopupItemStyle::FontStyle::DETAILED_VIEW_LABEL, | |
120 sub_text); | |
121 } | |
122 | |
123 void HoverHighlightView::AddIconAndLabelCustomSize(const gfx::ImageSkia& image, | |
124 const base::string16& text, | |
125 bool highlight, | |
126 int icon_size, | |
127 int indent, | |
128 int space_between_items) { | |
129 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
130 DoAddIconAndLabelMd(image, text, | |
131 TrayPopupItemStyle::FontStyle::DETAILED_VIEW_LABEL); | |
132 return; | |
133 } | |
134 | |
135 box_layout_ = new views::BoxLayout(views::BoxLayout::kHorizontal, indent, 0, | |
136 space_between_items); | |
137 SetLayoutManager(box_layout_); | |
138 DoAddIconAndLabel(image, icon_size, text, highlight); | |
139 } | |
140 | |
141 void HoverHighlightView::AddIconAndLabelForDefaultView( | |
142 const gfx::ImageSkia& image, | |
143 const base::string16& text, | |
144 bool highlight) { | |
145 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
146 DoAddIconAndLabelMd(image, text, | |
147 TrayPopupItemStyle::FontStyle::DEFAULT_VIEW_LABEL); | |
148 return; | |
149 } | |
150 | |
151 // For non-MD, call AddIconAndLabel() so that |box_layout_| is instantiated | |
152 // and installed as the layout manager. | |
153 AddIconAndLabel(image, text, highlight); | |
154 } | |
155 | |
156 void HoverHighlightView::DoAddIconAndLabel(const gfx::ImageSkia& image, | |
157 int icon_size, | |
158 const base::string16& text, | |
159 bool highlight) { | |
160 DCHECK(!MaterialDesignController::IsSystemTrayMenuMaterial()); | |
161 DCHECK(box_layout_); | |
162 | |
163 views::ImageView* image_view = new FixedSizedImageView(icon_size, 0); | |
164 image_view->SetImage(image); | |
165 image_view->SetEnabled(enabled()); | |
166 AddChildView(image_view); | |
167 | |
168 text_label_ = new views::Label(text); | |
169 text_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
170 text_label_->SetFontList(GetFontList(highlight)); | |
171 if (text_default_color_) | |
172 text_label_->SetEnabledColor(text_default_color_); | |
173 text_label_->SetEnabled(enabled()); | |
174 AddChildView(text_label_); | |
175 box_layout_->SetFlexForView(text_label_, 1); | |
176 | |
177 SetAccessibleName(text); | |
178 } | |
179 | |
180 void HoverHighlightView::DoAddIconAndLabelMd( | |
181 const gfx::ImageSkia& image, | |
182 const base::string16& text, | |
183 TrayPopupItemStyle::FontStyle font_style) { | |
184 DoAddIconAndLabelsMd(image, text, font_style, base::string16()); | |
185 } | |
186 | |
187 void HoverHighlightView::DoAddIconAndLabelsMd( | |
188 const gfx::ImageSkia& image, | |
189 const base::string16& text, | |
190 TrayPopupItemStyle::FontStyle font_style, | |
191 const base::string16& sub_text) { | |
192 DCHECK(MaterialDesignController::IsSystemTrayMenuMaterial()); | |
193 | |
194 SetLayoutManager(new views::FillLayout); | |
195 tri_view_ = TrayPopupUtils::CreateDefaultRowView(); | |
196 AddChildView(tri_view_); | |
197 | |
198 left_icon_ = TrayPopupUtils::CreateMainImageView(); | |
199 left_icon_->SetImage(image); | |
200 left_icon_->SetEnabled(enabled()); | |
201 tri_view_->AddView(TriView::Container::START, left_icon_); | |
202 | |
203 text_label_ = TrayPopupUtils::CreateDefaultLabel(); | |
204 text_label_->SetText(text); | |
205 text_label_->SetEnabled(enabled()); | |
206 TrayPopupItemStyle style(font_style); | |
207 style.SetupLabel(text_label_); | |
208 | |
209 tri_view_->AddView(TriView::Container::CENTER, text_label_); | |
210 if (!sub_text.empty()) { | |
211 sub_text_label_ = TrayPopupUtils::CreateDefaultLabel(); | |
212 sub_text_label_->SetText(sub_text); | |
213 TrayPopupItemStyle sub_style(TrayPopupItemStyle::FontStyle::CAPTION); | |
214 sub_style.set_color_style(TrayPopupItemStyle::ColorStyle::INACTIVE); | |
215 sub_style.SetupLabel(sub_text_label_); | |
216 tri_view_->AddView(TriView::Container::CENTER, sub_text_label_); | |
217 } | |
218 | |
219 tri_view_->SetContainerVisible(TriView::Container::END, false); | |
220 | |
221 SetAccessibleName(text); | |
222 } | |
223 | |
224 views::Label* HoverHighlightView::AddLabel(const base::string16& text, | |
225 gfx::HorizontalAlignment alignment, | |
226 bool highlight) { | |
227 box_layout_ = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0); | |
228 SetLayoutManager(box_layout_); | |
229 text_label_ = new views::Label(text); | |
230 int left_margin = kTrayPopupPaddingHorizontal; | |
231 int right_margin = kTrayPopupPaddingHorizontal; | |
232 if (alignment != gfx::ALIGN_CENTER) { | |
233 if (base::i18n::IsRTL()) | |
234 right_margin += kTrayPopupDetailsLabelExtraLeftMargin; | |
235 else | |
236 left_margin += kTrayPopupDetailsLabelExtraLeftMargin; | |
237 } | |
238 text_label_->SetBorder( | |
239 views::CreateEmptyBorder(5, left_margin, 5, right_margin)); | |
240 text_label_->SetHorizontalAlignment(alignment); | |
241 text_label_->SetFontList(GetFontList(highlight)); | |
242 // Do not set alpha value in disable color. It will have issue with elide | |
243 // blending filter in disabled state for rendering label text color. | |
244 text_label_->SetDisabledColor(SkColorSetARGB(255, 127, 127, 127)); | |
245 if (text_default_color_) | |
246 text_label_->SetEnabledColor(text_default_color_); | |
247 text_label_->SetEnabled(enabled()); | |
248 AddChildView(text_label_); | |
249 box_layout_->SetFlexForView(text_label_, 1); | |
250 | |
251 SetAccessibleName(text); | |
252 return text_label_; | |
253 } | |
254 | |
255 void HoverHighlightView::AddLabelRowMd(const base::string16& text) { | |
256 DCHECK(MaterialDesignController::IsSystemTrayMenuMaterial()); | |
257 | |
258 SetLayoutManager(new views::FillLayout); | |
259 tri_view_ = TrayPopupUtils::CreateDefaultRowView(); | |
260 AddChildView(tri_view_); | |
261 | |
262 text_label_ = TrayPopupUtils::CreateDefaultLabel(); | |
263 text_label_->SetText(text); | |
264 | |
265 TrayPopupItemStyle style(TrayPopupItemStyle::FontStyle::DETAILED_VIEW_LABEL); | |
266 style.SetupLabel(text_label_); | |
267 tri_view_->AddView(TriView::Container::CENTER, text_label_); | |
268 | |
269 SetAccessibleName(text); | |
270 } | |
271 | |
272 void HoverHighlightView::SetExpandable(bool expandable) { | |
273 if (expandable != expandable_) { | |
274 expandable_ = expandable; | |
275 InvalidateLayout(); | |
276 } | |
277 } | |
278 | |
279 void HoverHighlightView::SetHighlight(bool highlight) { | |
280 // Do not change the font styling for a highlighted row in material design. | |
281 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | |
282 return; | |
283 | |
284 DCHECK(text_label_); | |
285 text_label_->SetFontList(GetFontList(highlight)); | |
286 text_label_->InvalidateLayout(); | |
287 } | |
288 | |
289 void HoverHighlightView::SetAccessiblityState( | |
290 AccessibilityState accessibility_state) { | |
291 accessibility_state_ = accessibility_state; | |
292 if (accessibility_state_ != AccessibilityState::DEFAULT) | |
293 NotifyAccessibilityEvent(ui::AX_EVENT_CHECKED_STATE_CHANGED, true); | |
294 } | |
295 | |
296 void HoverHighlightView::SetHoverHighlight(bool hover) { | |
297 // We do not show any hover effects for material design. | |
298 if (MaterialDesignController::IsSystemTrayMenuMaterial()) | |
299 return; | |
300 | |
301 if (!enabled() && hover) | |
302 return; | |
303 if (hover_ == hover) | |
304 return; | |
305 hover_ = hover; | |
306 if (!text_label_) | |
307 return; | |
308 if (hover_ && text_highlight_color_) | |
309 text_label_->SetEnabledColor(text_highlight_color_); | |
310 if (!hover_ && text_default_color_) | |
311 text_label_->SetEnabledColor(text_default_color_); | |
312 SchedulePaint(); | |
313 } | |
314 | |
315 bool HoverHighlightView::PerformAction(const ui::Event& event) { | |
316 if (!listener_) | |
317 return false; | |
318 listener_->OnViewClicked(this); | |
319 return true; | |
320 } | |
321 | |
322 void HoverHighlightView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
323 ActionableView::GetAccessibleNodeData(node_data); | |
324 | |
325 if (accessibility_state_ == AccessibilityState::CHECKED_CHECKBOX || | |
326 accessibility_state_ == AccessibilityState::UNCHECKED_CHECKBOX) { | |
327 node_data->role = ui::AX_ROLE_CHECK_BOX; | |
328 } | |
329 | |
330 if (accessibility_state_ == AccessibilityState::CHECKED_CHECKBOX) | |
331 node_data->AddStateFlag(ui::AX_STATE_CHECKED); | |
332 } | |
333 | |
334 gfx::Size HoverHighlightView::GetPreferredSize() const { | |
335 gfx::Size size = ActionableView::GetPreferredSize(); | |
336 | |
337 if (custom_height_) | |
338 size.set_height(custom_height_); | |
339 else if (!expandable_ || size.height() < kTrayPopupItemMinHeight) | |
340 size.set_height(kTrayPopupItemMinHeight); | |
341 | |
342 return size; | |
343 } | |
344 | |
345 int HoverHighlightView::GetHeightForWidth(int width) const { | |
346 return GetPreferredSize().height(); | |
347 } | |
348 | |
349 void HoverHighlightView::OnMouseEntered(const ui::MouseEvent& event) { | |
350 SetHoverHighlight(true); | |
351 } | |
352 | |
353 void HoverHighlightView::OnMouseExited(const ui::MouseEvent& event) { | |
354 SetHoverHighlight(false); | |
355 } | |
356 | |
357 void HoverHighlightView::OnGestureEvent(ui::GestureEvent* event) { | |
358 if (event->type() == ui::ET_GESTURE_TAP_DOWN) { | |
359 SetHoverHighlight(true); | |
360 } else if (event->type() == ui::ET_GESTURE_TAP_CANCEL || | |
361 event->type() == ui::ET_GESTURE_TAP) { | |
362 SetHoverHighlight(false); | |
363 } | |
364 ActionableView::OnGestureEvent(event); | |
365 } | |
366 | |
367 void HoverHighlightView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
368 SetHoverHighlight(IsMouseHovered()); | |
369 } | |
370 | |
371 void HoverHighlightView::OnEnabledChanged() { | |
372 if (MaterialDesignController::IsSystemTrayMenuMaterial()) { | |
373 if (left_icon_) | |
374 left_icon_->SetEnabled(enabled()); | |
375 if (text_label_) | |
376 text_label_->SetEnabled(enabled()); | |
377 if (right_view_) | |
378 right_view_->SetEnabled(enabled()); | |
379 } else { | |
380 if (!enabled()) | |
381 SetHoverHighlight(false); | |
382 for (int i = 0; i < child_count(); ++i) | |
383 child_at(i)->SetEnabled(enabled()); | |
384 } | |
385 } | |
386 | |
387 void HoverHighlightView::OnPaintBackground(gfx::Canvas* canvas) { | |
388 canvas->DrawColor(hover_ ? highlight_color_ : default_color_); | |
389 } | |
390 | |
391 void HoverHighlightView::OnFocus() { | |
392 ScrollRectToVisible(gfx::Rect(gfx::Point(), size())); | |
393 ActionableView::OnFocus(); | |
394 } | |
395 | |
396 } // namespace ash | |
OLD | NEW |