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