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/shelf/app_list_button.h"
6
7 #include <vector>
8
9 #include "ash/ash_constants.h"
10 #include "ash/launcher/launcher_types.h"
11 #include "ash/shelf/shelf_button_host.h"
12 #include "grit/ash_resources.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/accessibility/accessible_view_state.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/compositor/layer_animation_element.h"
19 #include "ui/compositor/layer_animation_sequence.h"
20 #include "ui/compositor/scoped_layer_animation_settings.h"
21 #include "ui/views/painter.h"
22
23 namespace ash {
24 namespace internal {
25
26 const int kAnimationDurationInMs = 600;
27 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f };
28
AppListButton(views::ButtonListener * listener,ShelfButtonHost * host)29 AppListButton::AppListButton(views::ButtonListener* listener,
30 ShelfButtonHost* host)
31 : views::ImageButton(listener),
32 host_(host) {
33 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
34 SetImage(
35 views::CustomButton::STATE_NORMAL,
36 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST).ToImageSkia());
37 SetImage(
38 views::CustomButton::STATE_HOVERED,
39 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_HOT).
40 ToImageSkia());
41 SetImage(
42 views::CustomButton::STATE_PRESSED,
43 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_PUSHED).
44 ToImageSkia());
45 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE));
46 SetSize(gfx::Size(kLauncherPreferredSize, kLauncherPreferredSize));
47 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP);
48 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
49 kFocusBorderColor, gfx::Insets(1, 1, 1, 1)));
50 }
51
~AppListButton()52 AppListButton::~AppListButton() {
53 }
54
StartLoadingAnimation()55 void AppListButton::StartLoadingAnimation() {
56 layer()->GetAnimator()->StopAnimating();
57
58 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
59 new ui::LayerAnimationSequence());
60
61 opacity_sequence->set_is_cyclic(true);
62
63 for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) {
64 opacity_sequence->AddElement(
65 ui::LayerAnimationElement::CreateOpacityElement(
66 kAnimationOpacity[i],
67 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
68 }
69
70 ui::LayerAnimationElement::AnimatableProperties opacity_properties;
71 opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
72 opacity_sequence->AddElement(
73 ui::LayerAnimationElement::CreatePauseElement(
74 opacity_properties,
75 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
76
77 // LayerAnimator takes ownership of the sequences.
78 layer()->GetAnimator()->ScheduleAnimation(opacity_sequence.release());
79 }
80
StopLoadingAnimation()81 void AppListButton::StopLoadingAnimation() {
82 layer()->GetAnimator()->StopAnimating();
83
84 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
85 settings.SetTransitionDuration(
86 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs));
87 layer()->SetOpacity(1.0f);
88 layer()->SetTransform(gfx::Transform());
89 }
90
OnMousePressed(const ui::MouseEvent & event)91 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
92 ImageButton::OnMousePressed(event);
93 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
94 return true;
95 }
96
OnMouseReleased(const ui::MouseEvent & event)97 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
98 ImageButton::OnMouseReleased(event);
99 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
100 }
101
OnMouseCaptureLost()102 void AppListButton::OnMouseCaptureLost() {
103 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
104 ImageButton::OnMouseCaptureLost();
105 }
106
OnMouseDragged(const ui::MouseEvent & event)107 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
108 ImageButton::OnMouseDragged(event);
109 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
110 return true;
111 }
112
OnMouseMoved(const ui::MouseEvent & event)113 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
114 ImageButton::OnMouseMoved(event);
115 host_->MouseMovedOverButton(this);
116 }
117
OnMouseEntered(const ui::MouseEvent & event)118 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
119 ImageButton::OnMouseEntered(event);
120 host_->MouseEnteredButton(this);
121 }
122
OnMouseExited(const ui::MouseEvent & event)123 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
124 ImageButton::OnMouseExited(event);
125 host_->MouseExitedButton(this);
126 }
127
GetAccessibleState(ui::AccessibleViewState * state)128 void AppListButton::GetAccessibleState(ui::AccessibleViewState* state) {
129 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
130 state->name = host_->GetAccessibleName(this);
131 }
132
133 } // namespace internal
134 } // namespace ash
135