OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/shelf/shelf_button.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "ash/common/ash_constants.h" | |
10 #include "ash/common/shelf/ink_drop_button_listener.h" | |
11 #include "ash/common/shelf/shelf_constants.h" | |
12 #include "ash/common/shelf/shelf_view.h" | |
13 #include "ash/common/shelf/wm_shelf.h" | |
14 #include "base/memory/ptr_util.h" | |
15 #include "base/time/time.h" | |
16 #include "skia/ext/image_operations.h" | |
17 #include "ui/accessibility/ax_node_data.h" | |
18 #include "ui/compositor/layer.h" | |
19 #include "ui/gfx/animation/animation_delegate.h" | |
20 #include "ui/gfx/animation/throb_animation.h" | |
21 #include "ui/gfx/canvas.h" | |
22 #include "ui/gfx/geometry/vector2d.h" | |
23 #include "ui/gfx/image/image_skia_operations.h" | |
24 #include "ui/gfx/scoped_canvas.h" | |
25 #include "ui/gfx/skbitmap_operations.h" | |
26 #include "ui/views/animation/ink_drop_impl.h" | |
27 #include "ui/views/animation/square_ink_drop_ripple.h" | |
28 #include "ui/views/controls/image_view.h" | |
29 | |
30 namespace { | |
31 | |
32 const int kIconSize = 32; | |
33 const int kAttentionThrobDurationMS = 800; | |
34 const int kMaxAnimationSeconds = 10; | |
35 const int kIndicatorOffsetFromBottom = 3; | |
36 const int kIndicatorRadiusDip = 2; | |
37 const SkColor kIndicatorColor = SK_ColorWHITE; | |
38 | |
39 // Shelf item ripple constants. | |
40 const int kInkDropSmallSize = 48; | |
41 const int kInkDropLargeSize = 60; | |
42 | |
43 // Padding from the edge of the shelf to the application icon when the shelf | |
44 // is horizontally and vertically aligned, respectively. | |
45 const int kIconPaddingHorizontal = 7; | |
46 const int kIconPaddingVertical = 8; | |
47 | |
48 // Simple AnimationDelegate that owns a single ThrobAnimation instance to | |
49 // keep all Draw Attention animations in sync. | |
50 class ShelfButtonAnimation : public gfx::AnimationDelegate { | |
51 public: | |
52 class Observer { | |
53 public: | |
54 virtual void AnimationProgressed() = 0; | |
55 | |
56 protected: | |
57 virtual ~Observer() {} | |
58 }; | |
59 | |
60 static ShelfButtonAnimation* GetInstance() { | |
61 static ShelfButtonAnimation* s_instance = new ShelfButtonAnimation(); | |
62 return s_instance; | |
63 } | |
64 | |
65 void AddObserver(Observer* observer) { observers_.AddObserver(observer); } | |
66 | |
67 void RemoveObserver(Observer* observer) { | |
68 observers_.RemoveObserver(observer); | |
69 if (!observers_.might_have_observers()) | |
70 animation_.Stop(); | |
71 } | |
72 | |
73 bool HasObserver(Observer* observer) const { | |
74 return observers_.HasObserver(observer); | |
75 } | |
76 | |
77 SkAlpha GetAlpha() { | |
78 return GetThrobAnimation().CurrentValueBetween(SK_AlphaTRANSPARENT, | |
79 SK_AlphaOPAQUE); | |
80 } | |
81 | |
82 double GetAnimation() { return GetThrobAnimation().GetCurrentValue(); } | |
83 | |
84 private: | |
85 ShelfButtonAnimation() : animation_(this) { | |
86 animation_.SetThrobDuration(kAttentionThrobDurationMS); | |
87 animation_.SetTweenType(gfx::Tween::SMOOTH_IN_OUT); | |
88 } | |
89 | |
90 ~ShelfButtonAnimation() override {} | |
91 | |
92 gfx::ThrobAnimation& GetThrobAnimation() { | |
93 if (!animation_.is_animating()) { | |
94 animation_.Reset(); | |
95 animation_.StartThrobbing(-1 /*throb indefinitely*/); | |
96 } | |
97 return animation_; | |
98 } | |
99 | |
100 // gfx::AnimationDelegate | |
101 void AnimationProgressed(const gfx::Animation* animation) override { | |
102 if (animation != &animation_) | |
103 return; | |
104 if (!animation_.is_animating()) | |
105 return; | |
106 for (auto& observer : observers_) | |
107 observer.AnimationProgressed(); | |
108 } | |
109 | |
110 gfx::ThrobAnimation animation_; | |
111 base::ObserverList<Observer> observers_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(ShelfButtonAnimation); | |
114 }; | |
115 | |
116 } // namespace | |
117 | |
118 namespace ash { | |
119 | |
120 //////////////////////////////////////////////////////////////////////////////// | |
121 // ShelfButton::AppStatusIndicatorView | |
122 | |
123 class ShelfButton::AppStatusIndicatorView | |
124 : public views::View, | |
125 public ShelfButtonAnimation::Observer { | |
126 public: | |
127 AppStatusIndicatorView() | |
128 : show_attention_(false), animation_end_time_(base::TimeTicks()) { | |
129 // Make sure the events reach the parent view for handling. | |
130 set_can_process_events_within_subtree(false); | |
131 } | |
132 | |
133 ~AppStatusIndicatorView() override { | |
134 ShelfButtonAnimation::GetInstance()->RemoveObserver(this); | |
135 } | |
136 | |
137 // views::View: | |
138 void OnPaint(gfx::Canvas* canvas) override { | |
139 gfx::ScopedCanvas scoped(canvas); | |
140 if (show_attention_) { | |
141 SkAlpha alpha = ShelfButtonAnimation::GetInstance()->HasObserver(this) | |
142 ? ShelfButtonAnimation::GetInstance()->GetAlpha() | |
143 : SK_AlphaOPAQUE; | |
144 canvas->SaveLayerAlpha(alpha); | |
145 } | |
146 | |
147 DCHECK_EQ(width(), height()); | |
148 DCHECK_EQ(kIndicatorRadiusDip, width() / 2); | |
149 const float dsf = canvas->UndoDeviceScaleFactor(); | |
150 const int kStrokeWidthPx = 1; | |
151 gfx::PointF center = gfx::RectF(GetLocalBounds()).CenterPoint(); | |
152 center.Scale(dsf); | |
153 | |
154 // Fill the center. | |
155 cc::PaintFlags flags; | |
156 flags.setColor(kIndicatorColor); | |
157 flags.setFlags(cc::PaintFlags::kAntiAlias_Flag); | |
158 canvas->DrawCircle(center, dsf * kIndicatorRadiusDip - kStrokeWidthPx, | |
159 flags); | |
160 | |
161 // Stroke the border. | |
162 flags.setColor(SkColorSetA(SK_ColorBLACK, 0x4D)); | |
163 flags.setStyle(SkPaint::kStroke_Style); | |
164 canvas->DrawCircle( | |
165 center, dsf * kIndicatorRadiusDip - kStrokeWidthPx / 2.0f, flags); | |
166 } | |
167 | |
168 // ShelfButtonAnimation::Observer | |
169 void AnimationProgressed() override { | |
170 UpdateAnimating(); | |
171 SchedulePaint(); | |
172 } | |
173 | |
174 void ShowAttention(bool show) { | |
175 if (show_attention_ == show) | |
176 return; | |
177 | |
178 show_attention_ = show; | |
179 if (show_attention_) { | |
180 animation_end_time_ = base::TimeTicks::Now() + | |
181 base::TimeDelta::FromSeconds(kMaxAnimationSeconds); | |
182 ShelfButtonAnimation::GetInstance()->AddObserver(this); | |
183 } else { | |
184 ShelfButtonAnimation::GetInstance()->RemoveObserver(this); | |
185 } | |
186 } | |
187 | |
188 private: | |
189 void UpdateAnimating() { | |
190 if (base::TimeTicks::Now() > animation_end_time_) | |
191 ShelfButtonAnimation::GetInstance()->RemoveObserver(this); | |
192 } | |
193 | |
194 bool show_attention_; | |
195 base::TimeTicks animation_end_time_; // For attention throbbing underline. | |
196 | |
197 DISALLOW_COPY_AND_ASSIGN(AppStatusIndicatorView); | |
198 }; | |
199 | |
200 //////////////////////////////////////////////////////////////////////////////// | |
201 // ShelfButton | |
202 | |
203 // static | |
204 const char ShelfButton::kViewClassName[] = "ash/ShelfButton"; | |
205 | |
206 ShelfButton::ShelfButton(InkDropButtonListener* listener, ShelfView* shelf_view) | |
207 : CustomButton(nullptr), | |
208 listener_(listener), | |
209 shelf_view_(shelf_view), | |
210 icon_view_(new views::ImageView()), | |
211 indicator_(new AppStatusIndicatorView()), | |
212 state_(STATE_NORMAL), | |
213 destroyed_flag_(nullptr) { | |
214 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | |
215 SetInkDropMode(InkDropMode::ON); | |
216 set_ink_drop_base_color(kShelfInkDropBaseColor); | |
217 set_ink_drop_visible_opacity(kShelfInkDropVisibleOpacity); | |
218 | |
219 const gfx::ShadowValue kShadows[] = { | |
220 gfx::ShadowValue(gfx::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)), | |
221 gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)), | |
222 gfx::ShadowValue(gfx::Vector2d(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)), | |
223 }; | |
224 icon_shadows_.assign(kShadows, kShadows + arraysize(kShadows)); | |
225 | |
226 // TODO: refactor the layers so each button doesn't require 2. | |
227 icon_view_->SetPaintToLayer(); | |
228 icon_view_->layer()->SetFillsBoundsOpaquely(false); | |
229 icon_view_->SetHorizontalAlignment(views::ImageView::CENTER); | |
230 icon_view_->SetVerticalAlignment(views::ImageView::LEADING); | |
231 // Do not make this interactive, so that events are sent to ShelfView. | |
232 icon_view_->set_can_process_events_within_subtree(false); | |
233 | |
234 AddChildView(indicator_); | |
235 AddChildView(icon_view_); | |
236 } | |
237 | |
238 ShelfButton::~ShelfButton() { | |
239 if (destroyed_flag_) | |
240 *destroyed_flag_ = true; | |
241 } | |
242 | |
243 void ShelfButton::SetShadowedImage(const gfx::ImageSkia& image) { | |
244 icon_view_->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow( | |
245 image, icon_shadows_)); | |
246 } | |
247 | |
248 void ShelfButton::SetImage(const gfx::ImageSkia& image) { | |
249 if (image.isNull()) { | |
250 // TODO: need an empty image. | |
251 icon_view_->SetImage(image); | |
252 return; | |
253 } | |
254 | |
255 // Resize the image maintaining our aspect ratio. | |
256 float aspect_ratio = | |
257 static_cast<float>(image.width()) / static_cast<float>(image.height()); | |
258 int height = kIconSize; | |
259 int width = static_cast<int>(aspect_ratio * height); | |
260 if (width > kIconSize) { | |
261 width = kIconSize; | |
262 height = static_cast<int>(width / aspect_ratio); | |
263 } | |
264 | |
265 if (width == image.width() && height == image.height()) { | |
266 SetShadowedImage(image); | |
267 return; | |
268 } | |
269 | |
270 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage( | |
271 image, skia::ImageOperations::RESIZE_BEST, gfx::Size(width, height))); | |
272 } | |
273 | |
274 const gfx::ImageSkia& ShelfButton::GetImage() const { | |
275 return icon_view_->GetImage(); | |
276 } | |
277 | |
278 void ShelfButton::AddState(State state) { | |
279 if (!(state_ & state)) { | |
280 state_ |= state; | |
281 Layout(); | |
282 if (state & STATE_ATTENTION) | |
283 indicator_->ShowAttention(true); | |
284 } | |
285 } | |
286 | |
287 void ShelfButton::ClearState(State state) { | |
288 if (state_ & state) { | |
289 state_ &= ~state; | |
290 Layout(); | |
291 if (state & STATE_ATTENTION) | |
292 indicator_->ShowAttention(false); | |
293 } | |
294 } | |
295 | |
296 gfx::Rect ShelfButton::GetIconBounds() const { | |
297 return icon_view_->bounds(); | |
298 } | |
299 | |
300 void ShelfButton::OnDragStarted(const ui::LocatedEvent* event) { | |
301 AnimateInkDrop(views::InkDropState::HIDDEN, event); | |
302 } | |
303 | |
304 void ShelfButton::ShowContextMenu(const gfx::Point& p, | |
305 ui::MenuSourceType source_type) { | |
306 if (!context_menu_controller()) | |
307 return; | |
308 | |
309 bool destroyed = false; | |
310 destroyed_flag_ = &destroyed; | |
311 | |
312 CustomButton::ShowContextMenu(p, source_type); | |
313 | |
314 if (!destroyed) { | |
315 destroyed_flag_ = nullptr; | |
316 // The menu will not propagate mouse events while its shown. To address, | |
317 // the hover state gets cleared once the menu was shown (and this was not | |
318 // destroyed). | |
319 ClearState(STATE_HOVERED); | |
320 } | |
321 } | |
322 | |
323 const char* ShelfButton::GetClassName() const { | |
324 return kViewClassName; | |
325 } | |
326 | |
327 bool ShelfButton::OnMousePressed(const ui::MouseEvent& event) { | |
328 CustomButton::OnMousePressed(event); | |
329 shelf_view_->PointerPressedOnButton(this, ShelfView::MOUSE, event); | |
330 return true; | |
331 } | |
332 | |
333 void ShelfButton::OnMouseReleased(const ui::MouseEvent& event) { | |
334 CustomButton::OnMouseReleased(event); | |
335 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, false); | |
336 } | |
337 | |
338 void ShelfButton::OnMouseCaptureLost() { | |
339 ClearState(STATE_HOVERED); | |
340 shelf_view_->PointerReleasedOnButton(this, ShelfView::MOUSE, true); | |
341 CustomButton::OnMouseCaptureLost(); | |
342 } | |
343 | |
344 bool ShelfButton::OnMouseDragged(const ui::MouseEvent& event) { | |
345 CustomButton::OnMouseDragged(event); | |
346 shelf_view_->PointerDraggedOnButton(this, ShelfView::MOUSE, event); | |
347 return true; | |
348 } | |
349 | |
350 void ShelfButton::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
351 node_data->role = ui::AX_ROLE_BUTTON; | |
352 node_data->SetName(shelf_view_->GetTitleForView(this)); | |
353 } | |
354 | |
355 void ShelfButton::Layout() { | |
356 const gfx::Rect button_bounds(GetContentsBounds()); | |
357 WmShelf* wm_shelf = shelf_view_->wm_shelf(); | |
358 const bool is_horizontal_shelf = wm_shelf->IsHorizontalAlignment(); | |
359 const int icon_pad = | |
360 is_horizontal_shelf ? kIconPaddingHorizontal : kIconPaddingVertical; | |
361 int x_offset = is_horizontal_shelf ? 0 : icon_pad; | |
362 int y_offset = is_horizontal_shelf ? icon_pad : 0; | |
363 | |
364 int icon_width = std::min(kIconSize, button_bounds.width() - x_offset); | |
365 int icon_height = std::min(kIconSize, button_bounds.height() - y_offset); | |
366 | |
367 // If on the left or top 'invert' the inset so the constant gap is on | |
368 // the interior (towards the center of display) edge of the shelf. | |
369 if (SHELF_ALIGNMENT_LEFT == wm_shelf->GetAlignment()) | |
370 x_offset = button_bounds.width() - (kIconSize + icon_pad); | |
371 | |
372 // Center icon with respect to the secondary axis. | |
373 if (is_horizontal_shelf) | |
374 x_offset = std::max(0, button_bounds.width() - icon_width) / 2; | |
375 else | |
376 y_offset = std::max(0, button_bounds.height() - icon_height) / 2; | |
377 | |
378 // Expand bounds to include shadows. | |
379 gfx::Insets insets_shadows = gfx::ShadowValue::GetMargin(icon_shadows_); | |
380 // Adjust offsets to center icon, not icon + shadow. | |
381 x_offset += (insets_shadows.left() - insets_shadows.right()) / 2; | |
382 y_offset += (insets_shadows.top() - insets_shadows.bottom()) / 2; | |
383 gfx::Rect icon_view_bounds = | |
384 gfx::Rect(button_bounds.x() + x_offset, button_bounds.y() + y_offset, | |
385 icon_width, icon_height); | |
386 // The indicator should be aligned with the icon, not the icon + shadow. | |
387 gfx::Point indicator_midpoint = icon_view_bounds.CenterPoint(); | |
388 icon_view_bounds.Inset(insets_shadows); | |
389 icon_view_bounds.AdjustToFit(gfx::Rect(size())); | |
390 icon_view_->SetBoundsRect(icon_view_bounds); | |
391 | |
392 // Icon size has been incorrect when running | |
393 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see | |
394 // http://crbug.com/234854. | |
395 DCHECK_LE(icon_width, kIconSize); | |
396 DCHECK_LE(icon_height, kIconSize); | |
397 | |
398 switch (wm_shelf->GetAlignment()) { | |
399 case SHELF_ALIGNMENT_BOTTOM: | |
400 case SHELF_ALIGNMENT_BOTTOM_LOCKED: | |
401 indicator_midpoint.set_y(button_bounds.bottom() - kIndicatorRadiusDip - | |
402 kIndicatorOffsetFromBottom); | |
403 break; | |
404 case SHELF_ALIGNMENT_LEFT: | |
405 indicator_midpoint.set_x(button_bounds.x() + kIndicatorRadiusDip + | |
406 kIndicatorOffsetFromBottom); | |
407 break; | |
408 case SHELF_ALIGNMENT_RIGHT: | |
409 indicator_midpoint.set_x(button_bounds.right() - kIndicatorRadiusDip - | |
410 kIndicatorOffsetFromBottom); | |
411 break; | |
412 } | |
413 | |
414 gfx::Rect indicator_bounds(indicator_midpoint, gfx::Size()); | |
415 indicator_bounds.Inset(gfx::Insets(-kIndicatorRadiusDip)); | |
416 indicator_->SetBoundsRect(indicator_bounds); | |
417 | |
418 UpdateState(); | |
419 } | |
420 | |
421 void ShelfButton::ChildPreferredSizeChanged(views::View* child) { | |
422 Layout(); | |
423 } | |
424 | |
425 void ShelfButton::OnFocus() { | |
426 AddState(STATE_FOCUSED); | |
427 CustomButton::OnFocus(); | |
428 } | |
429 | |
430 void ShelfButton::OnBlur() { | |
431 ClearState(STATE_FOCUSED); | |
432 CustomButton::OnBlur(); | |
433 } | |
434 | |
435 void ShelfButton::OnPaint(gfx::Canvas* canvas) { | |
436 CustomButton::OnPaint(canvas); | |
437 if (HasFocus()) { | |
438 canvas->DrawSolidFocusRect(gfx::RectF(GetLocalBounds()), kFocusBorderColor, | |
439 kFocusBorderThickness); | |
440 } | |
441 } | |
442 | |
443 void ShelfButton::OnGestureEvent(ui::GestureEvent* event) { | |
444 switch (event->type()) { | |
445 case ui::ET_GESTURE_TAP_DOWN: | |
446 AddState(STATE_HOVERED); | |
447 return CustomButton::OnGestureEvent(event); | |
448 case ui::ET_GESTURE_END: | |
449 ClearState(STATE_HOVERED); | |
450 return CustomButton::OnGestureEvent(event); | |
451 case ui::ET_GESTURE_SCROLL_BEGIN: | |
452 shelf_view_->PointerPressedOnButton(this, ShelfView::TOUCH, *event); | |
453 event->SetHandled(); | |
454 return; | |
455 case ui::ET_GESTURE_SCROLL_UPDATE: | |
456 shelf_view_->PointerDraggedOnButton(this, ShelfView::TOUCH, *event); | |
457 event->SetHandled(); | |
458 return; | |
459 case ui::ET_GESTURE_SCROLL_END: | |
460 case ui::ET_SCROLL_FLING_START: | |
461 shelf_view_->PointerReleasedOnButton(this, ShelfView::TOUCH, false); | |
462 event->SetHandled(); | |
463 return; | |
464 default: | |
465 return CustomButton::OnGestureEvent(event); | |
466 } | |
467 } | |
468 | |
469 std::unique_ptr<views::InkDropRipple> ShelfButton::CreateInkDropRipple() const { | |
470 return base::MakeUnique<views::SquareInkDropRipple>( | |
471 gfx::Size(kInkDropLargeSize, kInkDropLargeSize), | |
472 kInkDropLargeCornerRadius, | |
473 gfx::Size(kInkDropSmallSize, kInkDropSmallSize), | |
474 kInkDropSmallCornerRadius, GetLocalBounds().CenterPoint(), | |
475 GetInkDropBaseColor(), ink_drop_visible_opacity()); | |
476 } | |
477 | |
478 bool ShelfButton::ShouldEnterPushedState(const ui::Event& event) { | |
479 if (!shelf_view_->ShouldEventActivateButton(this, event)) | |
480 return false; | |
481 | |
482 return CustomButton::ShouldEnterPushedState(event); | |
483 } | |
484 | |
485 std::unique_ptr<views::InkDrop> ShelfButton::CreateInkDrop() { | |
486 std::unique_ptr<views::InkDropImpl> ink_drop = | |
487 CustomButton::CreateDefaultInkDropImpl(); | |
488 ink_drop->SetShowHighlightOnHover(false); | |
489 return std::move(ink_drop); | |
490 } | |
491 | |
492 void ShelfButton::NotifyClick(const ui::Event& event) { | |
493 CustomButton::NotifyClick(event); | |
494 if (listener_) | |
495 listener_->ButtonPressed(this, event, GetInkDrop()); | |
496 } | |
497 | |
498 void ShelfButton::UpdateState() { | |
499 indicator_->SetVisible(!(state_ & STATE_HIDDEN) && | |
500 (state_ & STATE_ACTIVE || state_ & STATE_ATTENTION || | |
501 state_ & STATE_RUNNING)); | |
502 | |
503 const bool is_horizontal_shelf = | |
504 shelf_view_->wm_shelf()->IsHorizontalAlignment(); | |
505 icon_view_->SetHorizontalAlignment(is_horizontal_shelf | |
506 ? views::ImageView::CENTER | |
507 : views::ImageView::LEADING); | |
508 icon_view_->SetVerticalAlignment(is_horizontal_shelf | |
509 ? views::ImageView::LEADING | |
510 : views::ImageView::CENTER); | |
511 SchedulePaint(); | |
512 } | |
513 | |
514 } // namespace ash | |
OLD | NEW |