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.
5 #include "ash/shelf/shelf_button.h"
9 #include "ash/ash_constants.h"
10 #include "ash/ash_switches.h"
11 #include "ash/shelf/shelf_button_host.h"
12 #include "ash/shelf/shelf_layout_manager.h"
13 #include "grit/ash_resources.h"
14 #include "skia/ext/image_operations.h"
15 #include "ui/accessibility/ax_view_state.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/scoped_layer_animation_settings.h"
19 #include "ui/events/event_constants.h"
20 #include "ui/gfx/animation/animation_delegate.h"
21 #include "ui/gfx/animation/throb_animation.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/geometry/vector2d.h"
24 #include "ui/gfx/image/image.h"
25 #include "ui/gfx/image/image_skia_operations.h"
26 #include "ui/gfx/skbitmap_operations.h"
27 #include "ui/views/controls/image_view.h"
31 // Size of the bar. This is along the opposite axis of the shelf. For example,
32 // if the shelf is aligned horizontally then this is the height of the bar.
33 const int kBarSize
= 3;
34 const int kIconSize
= 32;
35 const int kIconPad
= 5;
36 const int kIconPadVertical
= 6;
37 const int kAttentionThrobDurationMS
= 800;
39 // Simple AnimationDelegate that owns a single ThrobAnimation instance to
40 // keep all Draw Attention animations in sync.
41 class ShelfButtonAnimation
: public gfx::AnimationDelegate
{
45 virtual void AnimationProgressed() = 0;
48 virtual ~Observer() {}
51 static ShelfButtonAnimation
* GetInstance() {
52 static ShelfButtonAnimation
* s_instance
= new ShelfButtonAnimation();
56 void AddObserver(Observer
* observer
) {
57 observers_
.AddObserver(observer
);
60 void RemoveObserver(Observer
* observer
) {
61 observers_
.RemoveObserver(observer
);
62 if (!observers_
.might_have_observers())
67 return GetThrobAnimation().CurrentValueBetween(0, 255);
70 double GetAnimation() {
71 return GetThrobAnimation().GetCurrentValue();
75 ShelfButtonAnimation()
77 animation_
.SetThrobDuration(kAttentionThrobDurationMS
);
78 animation_
.SetTweenType(gfx::Tween::SMOOTH_IN_OUT
);
81 ~ShelfButtonAnimation() override
{}
83 gfx::ThrobAnimation
& GetThrobAnimation() {
84 if (!animation_
.is_animating()) {
86 animation_
.StartThrobbing(-1 /*throb indefinitely*/);
91 // gfx::AnimationDelegate
92 void AnimationProgressed(const gfx::Animation
* animation
) override
{
93 if (animation
!= &animation_
)
95 if (!animation_
.is_animating())
97 FOR_EACH_OBSERVER(Observer
, observers_
, AnimationProgressed());
100 gfx::ThrobAnimation animation_
;
101 base::ObserverList
<Observer
> observers_
;
103 DISALLOW_COPY_AND_ASSIGN(ShelfButtonAnimation
);
110 ////////////////////////////////////////////////////////////////////////////////
111 // ShelfButton::BarView
113 class ShelfButton::BarView
: public views::ImageView
,
114 public ShelfButtonAnimation::Observer
{
116 BarView(ShelfButton
* host
)
118 show_attention_(false) {
119 // Make sure the events reach the parent view for handling.
120 set_interactive(false);
123 ~BarView() override
{
125 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
129 void OnPaint(gfx::Canvas
* canvas
) override
{
130 if (show_attention_
) {
131 int alpha
= ShelfButtonAnimation::GetInstance()->GetAlpha();
132 canvas
->SaveLayerAlpha(alpha
);
133 views::ImageView::OnPaint(canvas
);
136 views::ImageView::OnPaint(canvas
);
140 // ShelfButtonAnimation::Observer
141 void AnimationProgressed() override
{
146 void SetBarBoundsRect(const gfx::Rect
& bounds
) {
147 base_bounds_
= bounds
;
151 void ShowAttention(bool show
) {
152 if (show_attention_
!= show
) {
153 show_attention_
= show
;
155 ShelfButtonAnimation::GetInstance()->AddObserver(this);
157 ShelfButtonAnimation::GetInstance()->RemoveObserver(this);
163 void UpdateBounds() {
164 gfx::Rect bounds
= base_bounds_
;
165 if (show_attention_
) {
166 // Scale from .35 to 1.0 of the total width (which is wider than the
167 // visible width of the image), so the animation "rests" briefly at full
168 // visible width. Cap bounds length at kIconSize to prevent visual
169 // flutter while centering bar within further expanding bounds.
170 double animation
= ShelfButtonAnimation::GetInstance()->GetAnimation();
171 double scale
= .35 + .65 * animation
;
172 if (host_
->shelf_layout_manager()->GetAlignment() ==
173 SHELF_ALIGNMENT_BOTTOM
) {
174 int width
= base_bounds_
.width() * scale
;
175 bounds
.set_width(std::min(width
, kIconSize
));
176 int x_offset
= (base_bounds_
.width() - bounds
.width()) / 2;
177 bounds
.set_x(base_bounds_
.x() + x_offset
);
179 int height
= base_bounds_
.height() * scale
;
180 bounds
.set_height(std::min(height
, kIconSize
));
181 int y_offset
= (base_bounds_
.height() - bounds
.height()) / 2;
182 bounds
.set_y(base_bounds_
.y() + y_offset
);
185 SetBoundsRect(bounds
);
189 bool show_attention_
;
190 gfx::Rect base_bounds_
;
192 DISALLOW_COPY_AND_ASSIGN(BarView
);
195 ////////////////////////////////////////////////////////////////////////////////
196 // ShelfButton::IconView
198 ShelfButton::IconView::IconView() : icon_size_(kIconSize
) {
199 // Do not make this interactive, so that events are sent to ShelfView for
201 set_interactive(false);
204 ShelfButton::IconView::~IconView() {
207 ////////////////////////////////////////////////////////////////////////////////
211 const char ShelfButton::kViewClassName
[] = "ash/ShelfButton";
213 ShelfButton
* ShelfButton::Create(views::ButtonListener
* listener
,
214 ShelfButtonHost
* host
,
215 ShelfLayoutManager
* shelf_layout_manager
) {
216 ShelfButton
* button
= new ShelfButton(listener
, host
, shelf_layout_manager
);
221 ShelfButton::ShelfButton(views::ButtonListener
* listener
,
222 ShelfButtonHost
* host
,
223 ShelfLayoutManager
* shelf_layout_manager
)
224 : CustomButton(listener
),
227 bar_(new BarView(this)),
228 state_(STATE_NORMAL
),
229 shelf_layout_manager_(shelf_layout_manager
),
230 destroyed_flag_(NULL
) {
231 SetAccessibilityFocusable(true);
233 const gfx::ShadowValue kShadows
[] = {
234 gfx::ShadowValue(gfx::Vector2d(0, 2), 0, SkColorSetARGB(0x1A, 0, 0, 0)),
235 gfx::ShadowValue(gfx::Vector2d(0, 3), 1, SkColorSetARGB(0x1A, 0, 0, 0)),
236 gfx::ShadowValue(gfx::Vector2d(0, 0), 1, SkColorSetARGB(0x54, 0, 0, 0)),
238 icon_shadows_
.assign(kShadows
, kShadows
+ arraysize(kShadows
));
243 ShelfButton::~ShelfButton() {
245 *destroyed_flag_
= true;
248 void ShelfButton::SetShadowedImage(const gfx::ImageSkia
& image
) {
249 icon_view_
->SetImage(gfx::ImageSkiaOperations::CreateImageWithDropShadow(
250 image
, icon_shadows_
));
253 void ShelfButton::SetImage(const gfx::ImageSkia
& image
) {
254 if (image
.isNull()) {
255 // TODO: need an empty image.
256 icon_view_
->SetImage(image
);
260 if (icon_view_
->icon_size() == 0) {
261 SetShadowedImage(image
);
265 // Resize the image maintaining our aspect ratio.
266 int pref
= icon_view_
->icon_size();
268 static_cast<float>(image
.width()) / static_cast<float>(image
.height());
270 int width
= static_cast<int>(aspect_ratio
* height
);
273 height
= static_cast<int>(width
/ aspect_ratio
);
276 if (width
== image
.width() && height
== image
.height()) {
277 SetShadowedImage(image
);
281 SetShadowedImage(gfx::ImageSkiaOperations::CreateResizedImage(image
,
282 skia::ImageOperations::RESIZE_BEST
, gfx::Size(width
, height
)));
285 const gfx::ImageSkia
& ShelfButton::GetImage() const {
286 return icon_view_
->GetImage();
289 void ShelfButton::AddState(State state
) {
290 if (!(state_
& state
)) {
293 if (state
& STATE_ATTENTION
)
294 bar_
->ShowAttention(true);
298 void ShelfButton::ClearState(State state
) {
299 if (state_
& state
) {
302 if (state
& STATE_ATTENTION
)
303 bar_
->ShowAttention(false);
307 gfx::Rect
ShelfButton::GetIconBounds() const {
308 return icon_view_
->bounds();
311 void ShelfButton::ShowContextMenu(const gfx::Point
& p
,
312 ui::MenuSourceType source_type
) {
313 if (!context_menu_controller())
316 bool destroyed
= false;
317 destroyed_flag_
= &destroyed
;
319 CustomButton::ShowContextMenu(p
, source_type
);
322 destroyed_flag_
= NULL
;
323 // The menu will not propagate mouse events while its shown. To address,
324 // the hover state gets cleared once the menu was shown (and this was not
326 ClearState(STATE_HOVERED
);
330 const char* ShelfButton::GetClassName() const {
331 return kViewClassName
;
334 bool ShelfButton::OnMousePressed(const ui::MouseEvent
& event
) {
335 CustomButton::OnMousePressed(event
);
336 host_
->PointerPressedOnButton(this, ShelfButtonHost::MOUSE
, event
);
340 void ShelfButton::OnMouseReleased(const ui::MouseEvent
& event
) {
341 CustomButton::OnMouseReleased(event
);
342 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, false);
345 void ShelfButton::OnMouseCaptureLost() {
346 ClearState(STATE_HOVERED
);
347 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, true);
348 CustomButton::OnMouseCaptureLost();
351 bool ShelfButton::OnMouseDragged(const ui::MouseEvent
& event
) {
352 CustomButton::OnMouseDragged(event
);
353 host_
->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE
, event
);
357 void ShelfButton::OnMouseMoved(const ui::MouseEvent
& event
) {
358 CustomButton::OnMouseMoved(event
);
359 host_
->MouseMovedOverButton(this);
362 void ShelfButton::OnMouseEntered(const ui::MouseEvent
& event
) {
363 AddState(STATE_HOVERED
);
364 CustomButton::OnMouseEntered(event
);
365 host_
->MouseEnteredButton(this);
368 void ShelfButton::OnMouseExited(const ui::MouseEvent
& event
) {
369 ClearState(STATE_HOVERED
);
370 CustomButton::OnMouseExited(event
);
371 host_
->MouseExitedButton(this);
374 void ShelfButton::GetAccessibleState(ui::AXViewState
* state
) {
375 state
->role
= ui::AX_ROLE_BUTTON
;
376 state
->name
= host_
->GetAccessibleName(this);
379 void ShelfButton::Layout() {
380 const gfx::Rect
button_bounds(GetContentsBounds());
382 shelf_layout_manager_
->GetAlignment() != SHELF_ALIGNMENT_BOTTOM
?
383 kIconPadVertical
: kIconPad
;
384 int x_offset
= shelf_layout_manager_
->PrimaryAxisValue(0, icon_pad
);
385 int y_offset
= shelf_layout_manager_
->PrimaryAxisValue(icon_pad
, 0);
387 int icon_width
= std::min(kIconSize
,
388 button_bounds
.width() - x_offset
);
389 int icon_height
= std::min(kIconSize
,
390 button_bounds
.height() - y_offset
);
392 // If on the left or top 'invert' the inset so the constant gap is on
393 // the interior (towards the center of display) edge of the shelf.
394 if (SHELF_ALIGNMENT_LEFT
== shelf_layout_manager_
->GetAlignment())
395 x_offset
= button_bounds
.width() - (kIconSize
+ icon_pad
);
397 if (SHELF_ALIGNMENT_TOP
== shelf_layout_manager_
->GetAlignment())
398 y_offset
= button_bounds
.height() - (kIconSize
+ icon_pad
);
400 // Center icon with respect to the secondary axis, and ensure
401 // that the icon doesn't occlude the bar highlight.
402 if (shelf_layout_manager_
->IsHorizontalAlignment()) {
403 x_offset
= std::max(0, button_bounds
.width() - icon_width
) / 2;
404 if (y_offset
+ icon_height
+ kBarSize
> button_bounds
.height())
405 icon_height
= button_bounds
.height() - (y_offset
+ kBarSize
);
407 y_offset
= std::max(0, button_bounds
.height() - icon_height
) / 2;
408 if (x_offset
+ icon_width
+ kBarSize
> button_bounds
.width())
409 icon_width
= button_bounds
.width() - (x_offset
+ kBarSize
);
412 // Expand bounds to include shadows.
413 gfx::Insets insets_shadows
= gfx::ShadowValue::GetMargin(icon_shadows_
);
414 // Adjust offsets to center icon, not icon + shadow.
415 x_offset
+= (insets_shadows
.left() - insets_shadows
.right()) / 2;
416 y_offset
+= (insets_shadows
.top() - insets_shadows
.bottom()) / 2;
417 gfx::Rect icon_view_bounds
=
418 gfx::Rect(button_bounds
.x() + x_offset
, button_bounds
.y() + y_offset
,
419 icon_width
, icon_height
);
420 icon_view_bounds
.Inset(insets_shadows
);
421 icon_view_
->SetBoundsRect(icon_view_bounds
);
423 // Icon size has been incorrect when running
424 // PanelLayoutManagerTest.PanelAlignmentSecondDisplay on valgrind bot, see
425 // http://crbug.com/234854.
426 DCHECK_LE(icon_width
, kIconSize
);
427 DCHECK_LE(icon_height
, kIconSize
);
429 bar_
->SetBarBoundsRect(button_bounds
);
434 void ShelfButton::ChildPreferredSizeChanged(views::View
* child
) {
438 void ShelfButton::OnFocus() {
439 AddState(STATE_FOCUSED
);
440 CustomButton::OnFocus();
443 void ShelfButton::OnBlur() {
444 ClearState(STATE_FOCUSED
);
445 CustomButton::OnBlur();
448 void ShelfButton::OnPaint(gfx::Canvas
* canvas
) {
449 CustomButton::OnPaint(canvas
);
451 gfx::Rect
paint_bounds(GetLocalBounds());
452 paint_bounds
.Inset(1, 1, 1, 1);
453 canvas
->DrawSolidFocusRect(paint_bounds
, kFocusBorderColor
);
457 void ShelfButton::OnGestureEvent(ui::GestureEvent
* event
) {
458 switch (event
->type()) {
459 case ui::ET_GESTURE_TAP_DOWN
:
460 AddState(STATE_HOVERED
);
461 return CustomButton::OnGestureEvent(event
);
462 case ui::ET_GESTURE_END
:
463 ClearState(STATE_HOVERED
);
464 return CustomButton::OnGestureEvent(event
);
465 case ui::ET_GESTURE_SCROLL_BEGIN
:
466 host_
->PointerPressedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
469 case ui::ET_GESTURE_SCROLL_UPDATE
:
470 host_
->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
473 case ui::ET_GESTURE_SCROLL_END
:
474 case ui::ET_SCROLL_FLING_START
:
475 host_
->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH
, false);
479 return CustomButton::OnGestureEvent(event
);
483 void ShelfButton::Init() {
484 icon_view_
= CreateIconView();
486 // TODO: refactor the layers so each button doesn't require 2.
487 icon_view_
->SetPaintToLayer(true);
488 icon_view_
->SetFillsBoundsOpaquely(false);
489 icon_view_
->SetHorizontalAlignment(views::ImageView::CENTER
);
490 icon_view_
->SetVerticalAlignment(views::ImageView::LEADING
);
492 AddChildView(icon_view_
);
495 ShelfButton::IconView
* ShelfButton::CreateIconView() {
499 bool ShelfButton::IsShelfHorizontal() const {
500 return shelf_layout_manager_
->IsHorizontalAlignment();
503 void ShelfButton::UpdateState() {
506 icon_view_
->SetHorizontalAlignment(
507 shelf_layout_manager_
->PrimaryAxisValue(views::ImageView::CENTER
,
508 views::ImageView::LEADING
));
509 icon_view_
->SetVerticalAlignment(
510 shelf_layout_manager_
->PrimaryAxisValue(views::ImageView::LEADING
,
511 views::ImageView::CENTER
));
515 void ShelfButton::UpdateBar() {
516 if (state_
& STATE_HIDDEN
) {
517 bar_
->SetVisible(false);
522 if (state_
& (STATE_ACTIVE
| STATE_ATTENTION
))
523 bar_id
= IDR_ASH_SHELF_UNDERLINE_ACTIVE
;
524 else if (state_
& STATE_RUNNING
)
525 bar_id
= IDR_ASH_SHELF_UNDERLINE_RUNNING
;
528 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
529 const gfx::ImageSkia
* image
= rb
.GetImageNamed(bar_id
).ToImageSkia();
530 if (shelf_layout_manager_
->GetAlignment() == SHELF_ALIGNMENT_BOTTOM
) {
531 bar_
->SetImage(*image
);
533 bar_
->SetImage(gfx::ImageSkiaOperations::CreateRotatedImage(*image
,
534 shelf_layout_manager_
->SelectValueForShelfAlignment(
535 SkBitmapOperations::ROTATION_90_CW
,
536 SkBitmapOperations::ROTATION_90_CW
,
537 SkBitmapOperations::ROTATION_270_CW
,
538 SkBitmapOperations::ROTATION_180_CW
)));
540 bar_
->SetHorizontalAlignment(
541 shelf_layout_manager_
->SelectValueForShelfAlignment(
542 views::ImageView::CENTER
,
543 views::ImageView::LEADING
,
544 views::ImageView::TRAILING
,
545 views::ImageView::CENTER
));
546 bar_
->SetVerticalAlignment(
547 shelf_layout_manager_
->SelectValueForShelfAlignment(
548 views::ImageView::TRAILING
,
549 views::ImageView::CENTER
,
550 views::ImageView::CENTER
,
551 views::ImageView::LEADING
));
552 bar_
->SchedulePaint();
555 bar_
->SetVisible(bar_id
!= 0 && state_
!= STATE_NORMAL
);