1 // Copyright (c) 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.
5 #include "ash/launcher/app_list_button.h"
9 #include "ash/launcher/launcher_button_host.h"
10 #include "ash/launcher/launcher_types.h"
11 #include "grit/ash_resources.h"
12 #include "grit/ash_strings.h"
13 #include "ui/base/accessibility/accessible_view_state.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/compositor/layer.h"
17 #include "ui/compositor/layer_animation_element.h"
18 #include "ui/compositor/layer_animation_sequence.h"
19 #include "ui/compositor/scoped_layer_animation_settings.h"
26 const int kAnimationDurationInMs
= 600;
27 const float kAnimationOpacity
[] = { 1.0f
, 0.4f
, 1.0f
};
28 const int kBorderSize
= 9;
31 AppListButton::AppListButton(views::ButtonListener
* listener
,
32 LauncherButtonHost
* host
)
33 : views::ImageButton(listener
),
35 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
37 views::CustomButton::STATE_NORMAL
,
38 rb
.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST
).ToImageSkia());
40 views::CustomButton::STATE_HOVERED
,
41 rb
.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_HOT
).
44 views::CustomButton::STATE_PRESSED
,
45 rb
.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_PUSHED
).
47 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE
));
48 SetSize(gfx::Size(kLauncherPreferredSize
, kLauncherPreferredSize
));
49 SetImageAlignment(ImageButton::ALIGN_CENTER
, ImageButton::ALIGN_TOP
);
52 AppListButton::~AppListButton() {
55 void AppListButton::StartLoadingAnimation() {
56 layer()->GetAnimator()->StopAnimating();
58 scoped_ptr
<ui::LayerAnimationSequence
> opacity_sequence(
59 new ui::LayerAnimationSequence());
61 opacity_sequence
->set_is_cyclic(true);
63 for (size_t i
= 0; i
< arraysize(kAnimationOpacity
); ++i
) {
64 opacity_sequence
->AddElement(
65 ui::LayerAnimationElement::CreateOpacityElement(
67 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs
)));
70 ui::LayerAnimationElement::AnimatableProperties opacity_properties
;
71 opacity_properties
.insert(ui::LayerAnimationElement::OPACITY
);
72 opacity_sequence
->AddElement(
73 ui::LayerAnimationElement::CreatePauseElement(
75 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs
)));
77 // LayerAnimator takes ownership of the sequences.
78 layer()->GetAnimator()->ScheduleAnimation(opacity_sequence
.release());
81 void AppListButton::StopLoadingAnimation() {
82 layer()->GetAnimator()->StopAnimating();
84 ui::ScopedLayerAnimationSettings
settings(layer()->GetAnimator());
85 settings
.SetTransitionDuration(
86 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs
));
87 layer()->SetOpacity(1.0f
);
88 layer()->SetTransform(gfx::Transform());
91 bool AppListButton::OnMousePressed(const ui::MouseEvent
& event
) {
92 ImageButton::OnMousePressed(event
);
93 host_
->PointerPressedOnButton(this, LauncherButtonHost::MOUSE
, event
);
97 void AppListButton::OnMouseReleased(const ui::MouseEvent
& event
) {
98 ImageButton::OnMouseReleased(event
);
99 host_
->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE
, false);
102 void AppListButton::OnMouseCaptureLost() {
103 host_
->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE
, true);
104 ImageButton::OnMouseCaptureLost();
107 bool AppListButton::OnMouseDragged(const ui::MouseEvent
& event
) {
108 ImageButton::OnMouseDragged(event
);
109 host_
->PointerDraggedOnButton(this, LauncherButtonHost::MOUSE
, event
);
113 void AppListButton::OnMouseMoved(const ui::MouseEvent
& event
) {
114 ImageButton::OnMouseMoved(event
);
115 host_
->MouseMovedOverButton(this);
118 void AppListButton::OnMouseEntered(const ui::MouseEvent
& event
) {
119 ImageButton::OnMouseEntered(event
);
120 host_
->MouseEnteredButton(this);
123 void AppListButton::OnMouseExited(const ui::MouseEvent
& event
) {
124 ImageButton::OnMouseExited(event
);
125 host_
->MouseExitedButton(this);
128 void AppListButton::GetAccessibleState(ui::AccessibleViewState
* state
) {
129 state
->role
= ui::AccessibilityTypes::ROLE_PUSHBUTTON
;
130 state
->name
= host_
->GetAccessibleName(this);
133 } // namespace internal