OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/shelf/app_list_button.h" | 5 #include "ash/shelf/app_list_button.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "ash/ash_constants.h" | 7 #include "ash/ash_constants.h" |
| 8 #include "ash/ash_switches.h" |
| 9 #include "ash/shelf/shelf_button.h" |
10 #include "ash/shelf/shelf_button_host.h" | 10 #include "ash/shelf/shelf_button_host.h" |
11 #include "ash/shelf/shelf_constants.h" | 11 #include "ash/shelf/shelf_layout_manager.h" |
| 12 #include "ash/shelf/shelf_types.h" |
| 13 #include "ash/shelf/shelf_widget.h" |
| 14 #include "ash/shell.h" |
12 #include "grit/ash_resources.h" | 15 #include "grit/ash_resources.h" |
13 #include "grit/ash_strings.h" | 16 #include "grit/ash_strings.h" |
14 #include "ui/accessibility/ax_view_state.h" | 17 #include "ui/accessibility/ax_view_state.h" |
15 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
16 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
17 #include "ui/compositor/layer.h" | 20 #include "ui/compositor/layer.h" |
18 #include "ui/compositor/layer_animation_element.h" | 21 #include "ui/compositor/layer_animation_element.h" |
19 #include "ui/compositor/layer_animation_sequence.h" | 22 #include "ui/compositor/layer_animation_sequence.h" |
20 #include "ui/compositor/scoped_layer_animation_settings.h" | 23 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 24 #include "ui/gfx/canvas.h" |
| 25 #include "ui/gfx/image/image_skia_operations.h" |
| 26 #include "ui/views/controls/button/image_button.h" |
21 #include "ui/views/painter.h" | 27 #include "ui/views/painter.h" |
22 | 28 |
23 namespace ash { | 29 namespace ash { |
24 namespace internal { | 30 namespace internal { |
25 | 31 |
26 const int kAnimationDurationInMs = 600; | 32 // static |
27 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f }; | 33 const int AppListButton::kImageBoundsSize = 7; |
| 34 |
28 | 35 |
29 AppListButton::AppListButton(views::ButtonListener* listener, | 36 AppListButton::AppListButton(views::ButtonListener* listener, |
30 ShelfButtonHost* host) | 37 ShelfButtonHost* host, |
| 38 ShelfWidget* shelf_widget) |
31 : views::ImageButton(listener), | 39 : views::ImageButton(listener), |
32 host_(host) { | 40 host_(host), |
33 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 41 shelf_widget_(shelf_widget) { |
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)); | 42 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE)); |
46 SetSize(gfx::Size(kShelfPreferredSize, kShelfPreferredSize)); | 43 SetSize(gfx::Size(kShelfSize, kShelfSize)); |
47 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP); | |
48 SetFocusPainter(views::Painter::CreateSolidFocusPainter( | 44 SetFocusPainter(views::Painter::CreateSolidFocusPainter( |
49 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); | 45 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); |
50 } | 46 } |
51 | 47 |
52 AppListButton::~AppListButton() { | 48 AppListButton::~AppListButton() { |
53 } | 49 } |
54 | 50 |
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 opacity_sequence->AddElement( | |
71 ui::LayerAnimationElement::CreatePauseElement( | |
72 ui::LayerAnimationElement::OPACITY, | |
73 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
74 | |
75 // LayerAnimator takes ownership of the sequences. | |
76 layer()->GetAnimator()->ScheduleAnimation(opacity_sequence.release()); | |
77 } | |
78 | |
79 void AppListButton::StopLoadingAnimation() { | |
80 layer()->GetAnimator()->StopAnimating(); | |
81 | |
82 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | |
83 settings.SetTransitionDuration( | |
84 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)); | |
85 layer()->SetOpacity(1.0f); | |
86 layer()->SetTransform(gfx::Transform()); | |
87 } | |
88 | |
89 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { | 51 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { |
90 ImageButton::OnMousePressed(event); | 52 ImageButton::OnMousePressed(event); |
91 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); | 53 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); |
92 return true; | 54 return true; |
93 } | 55 } |
94 | 56 |
95 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { | 57 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { |
96 ImageButton::OnMouseReleased(event); | 58 ImageButton::OnMouseReleased(event); |
97 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); | 59 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); |
98 } | 60 } |
(...skipping 17 matching lines...) Expand all Loading... |
116 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { | 78 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { |
117 ImageButton::OnMouseEntered(event); | 79 ImageButton::OnMouseEntered(event); |
118 host_->MouseEnteredButton(this); | 80 host_->MouseEnteredButton(this); |
119 } | 81 } |
120 | 82 |
121 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { | 83 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { |
122 ImageButton::OnMouseExited(event); | 84 ImageButton::OnMouseExited(event); |
123 host_->MouseExitedButton(this); | 85 host_->MouseExitedButton(this); |
124 } | 86 } |
125 | 87 |
| 88 void AppListButton::OnGestureEvent(ui::GestureEvent* event) { |
| 89 switch (event->type()) { |
| 90 case ui::ET_GESTURE_SCROLL_BEGIN: |
| 91 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event); |
| 92 event->SetHandled(); |
| 93 return; |
| 94 case ui::ET_GESTURE_SCROLL_UPDATE: |
| 95 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event); |
| 96 event->SetHandled(); |
| 97 return; |
| 98 case ui::ET_GESTURE_SCROLL_END: |
| 99 case ui::ET_SCROLL_FLING_START: |
| 100 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false); |
| 101 event->SetHandled(); |
| 102 return; |
| 103 default: |
| 104 ImageButton::OnGestureEvent(event); |
| 105 return; |
| 106 } |
| 107 } |
| 108 |
| 109 void AppListButton::OnPaint(gfx::Canvas* canvas) { |
| 110 // Call the base class first to paint any background/borders. |
| 111 View::OnPaint(canvas); |
| 112 |
| 113 int background_image_id = 0; |
| 114 if (Shell::GetInstance()->GetAppListTargetVisibility()) { |
| 115 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED; |
| 116 } else { |
| 117 if (shelf_widget_->GetDimsShelf()) |
| 118 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK; |
| 119 else |
| 120 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL; |
| 121 } |
| 122 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 123 const gfx::ImageSkia* background_image = |
| 124 rb.GetImageNamed(background_image_id).ToImageSkia(); |
| 125 const gfx::ImageSkia* forground_image = |
| 126 rb.GetImageNamed(IDR_AURA_LAUNCHER_ICON_APPLIST_ALTERNATE).ToImageSkia(); |
| 127 |
| 128 gfx::Rect contents_bounds = GetContentsBounds(); |
| 129 gfx::Rect background_bounds, forground_bounds; |
| 130 |
| 131 ShelfAlignment alignment = shelf_widget_->GetAlignment(); |
| 132 background_bounds.set_size(background_image->size()); |
| 133 if (alignment == SHELF_ALIGNMENT_LEFT) { |
| 134 background_bounds.set_x(contents_bounds.width() - |
| 135 ShelfLayoutManager::kShelfItemInset - background_image->width()); |
| 136 background_bounds.set_y(contents_bounds.y() + |
| 137 (contents_bounds.height() - background_image->height()) / 2); |
| 138 } else if(alignment == SHELF_ALIGNMENT_RIGHT) { |
| 139 background_bounds.set_x(ShelfLayoutManager::kShelfItemInset); |
| 140 background_bounds.set_y(contents_bounds.y() + |
| 141 (contents_bounds.height() - background_image->height()) / 2); |
| 142 } else { |
| 143 background_bounds.set_y(ShelfLayoutManager::kShelfItemInset); |
| 144 background_bounds.set_x(contents_bounds.x() + |
| 145 (contents_bounds.width() - background_image->width()) / 2); |
| 146 } |
| 147 |
| 148 forground_bounds.set_size(forground_image->size()); |
| 149 forground_bounds.set_x(background_bounds.x() + |
| 150 std::max(0, |
| 151 (background_bounds.width() - forground_bounds.width()) / 2)); |
| 152 forground_bounds.set_y(background_bounds.y() + |
| 153 std::max(0, |
| 154 (background_bounds.height() - forground_bounds.height()) / 2)); |
| 155 |
| 156 canvas->DrawImageInt(*background_image, |
| 157 background_bounds.x(), |
| 158 background_bounds.y()); |
| 159 canvas->DrawImageInt(*forground_image, |
| 160 forground_bounds.x(), |
| 161 forground_bounds.y()); |
| 162 |
| 163 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); |
| 164 } |
| 165 |
126 void AppListButton::GetAccessibleState(ui::AXViewState* state) { | 166 void AppListButton::GetAccessibleState(ui::AXViewState* state) { |
127 state->role = ui::AX_ROLE_BUTTON; | 167 state->role = ui::AX_ROLE_BUTTON; |
128 state->name = host_->GetAccessibleName(this); | 168 state->name = host_->GetAccessibleName(this); |
129 } | 169 } |
130 | 170 |
131 } // namespace internal | 171 } // namespace internal |
132 } // namespace ash | 172 } // namespace ash |
OLD | NEW |