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/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"
24 const int kAnimationDurationInMs = 600;
25 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f };
27 AppListButton::AppListButton(views::ButtonListener* listener,
28 LauncherButtonHost* host)
29 : views::ImageButton(listener),
31 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
33 views::CustomButton::STATE_NORMAL,
34 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST).ToImageSkia());
36 views::CustomButton::STATE_HOVERED,
37 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_HOT).
40 views::CustomButton::STATE_PRESSED,
41 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_PUSHED).
43 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE));
44 SetSize(gfx::Size(kLauncherPreferredSize, kLauncherPreferredSize));
45 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP);
48 AppListButton::~AppListButton() {
51 void AppListButton::StartLoadingAnimation() {
52 layer()->GetAnimator()->StopAnimating();
54 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence(
55 new ui::LayerAnimationSequence());
57 opacity_sequence->set_is_cyclic(true);
59 for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) {
60 opacity_sequence->AddElement(
61 ui::LayerAnimationElement::CreateOpacityElement(
63 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
66 ui::LayerAnimationElement::AnimatableProperties opacity_properties;
67 opacity_properties.insert(ui::LayerAnimationElement::OPACITY);
68 opacity_sequence->AddElement(
69 ui::LayerAnimationElement::CreatePauseElement(
71 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)));
73 // LayerAnimator takes ownership of the sequences.
74 layer()->GetAnimator()->ScheduleAnimation(opacity_sequence.release());
77 void AppListButton::StopLoadingAnimation() {
78 layer()->GetAnimator()->StopAnimating();
80 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator());
81 settings.SetTransitionDuration(
82 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs));
83 layer()->SetOpacity(1.0f);
84 layer()->SetTransform(gfx::Transform());
87 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
88 ImageButton::OnMousePressed(event);
89 host_->PointerPressedOnButton(this, LauncherButtonHost::MOUSE, event);
93 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
94 ImageButton::OnMouseReleased(event);
95 host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, false);
98 void AppListButton::OnMouseCaptureLost() {
99 host_->PointerReleasedOnButton(this, LauncherButtonHost::MOUSE, true);
100 ImageButton::OnMouseCaptureLost();
103 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
104 ImageButton::OnMouseDragged(event);
105 host_->PointerDraggedOnButton(this, LauncherButtonHost::MOUSE, event);
109 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
110 ImageButton::OnMouseMoved(event);
111 host_->MouseMovedOverButton(this);
114 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
115 ImageButton::OnMouseEntered(event);
116 host_->MouseEnteredButton(this);
119 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
120 ImageButton::OnMouseExited(event);
121 host_->MouseExitedButton(this);
124 void AppListButton::GetAccessibleState(ui::AccessibleViewState* state) {
125 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
126 state->name = host_->GetAccessibleName(this);
129 } // namespace internal