OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_item_types.h" |
| 12 #include "ash/shelf/shelf_layout_manager.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 { |
| 30 // static |
| 31 const int AppListButton::kImageBoundsSize = 7; |
24 | 32 |
25 const int kAnimationDurationInMs = 600; | |
26 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f }; | |
27 | 33 |
28 AppListButton::AppListButton(views::ButtonListener* listener, | 34 AppListButton::AppListButton(views::ButtonListener* listener, |
29 ShelfButtonHost* host) | 35 ShelfButtonHost* host, |
| 36 ShelfWidget* shelf_widget) |
30 : views::ImageButton(listener), | 37 : views::ImageButton(listener), |
31 host_(host) { | 38 host_(host), |
32 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 39 shelf_widget_(shelf_widget) { |
33 SetImage(views::CustomButton::STATE_NORMAL, | |
34 rb.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST).ToImageSkia()); | |
35 SetImage(views::CustomButton::STATE_HOVERED, | |
36 rb.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST_HOT).ToImageSkia()); | |
37 SetImage(views::CustomButton::STATE_PRESSED, | |
38 rb.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST_PUSHED).ToImageSkia()); | |
39 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE)); | 40 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE)); |
40 SetSize(gfx::Size(kShelfPreferredSize, kShelfPreferredSize)); | 41 SetSize(gfx::Size(kShelfSize, kShelfSize)); |
41 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP); | |
42 SetFocusPainter(views::Painter::CreateSolidFocusPainter( | 42 SetFocusPainter(views::Painter::CreateSolidFocusPainter( |
43 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); | 43 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); |
44 } | 44 } |
45 | 45 |
46 AppListButton::~AppListButton() { | 46 AppListButton::~AppListButton() { |
47 } | 47 } |
48 | 48 |
49 void AppListButton::StartLoadingAnimation() { | |
50 layer()->GetAnimator()->StopAnimating(); | |
51 | |
52 scoped_ptr<ui::LayerAnimationSequence> opacity_sequence( | |
53 new ui::LayerAnimationSequence()); | |
54 | |
55 opacity_sequence->set_is_cyclic(true); | |
56 | |
57 for (size_t i = 0; i < arraysize(kAnimationOpacity); ++i) { | |
58 opacity_sequence->AddElement( | |
59 ui::LayerAnimationElement::CreateOpacityElement( | |
60 kAnimationOpacity[i], | |
61 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
62 } | |
63 | |
64 opacity_sequence->AddElement( | |
65 ui::LayerAnimationElement::CreatePauseElement( | |
66 ui::LayerAnimationElement::OPACITY, | |
67 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs))); | |
68 | |
69 // LayerAnimator takes ownership of the sequences. | |
70 layer()->GetAnimator()->ScheduleAnimation(opacity_sequence.release()); | |
71 } | |
72 | |
73 void AppListButton::StopLoadingAnimation() { | |
74 layer()->GetAnimator()->StopAnimating(); | |
75 | |
76 ui::ScopedLayerAnimationSettings settings(layer()->GetAnimator()); | |
77 settings.SetTransitionDuration( | |
78 base::TimeDelta::FromMilliseconds(kAnimationDurationInMs)); | |
79 layer()->SetOpacity(1.0f); | |
80 layer()->SetTransform(gfx::Transform()); | |
81 } | |
82 | |
83 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { | 49 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { |
84 ImageButton::OnMousePressed(event); | 50 ImageButton::OnMousePressed(event); |
85 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); | 51 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); |
86 return true; | 52 return true; |
87 } | 53 } |
88 | 54 |
89 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { | 55 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { |
90 ImageButton::OnMouseReleased(event); | 56 ImageButton::OnMouseReleased(event); |
91 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); | 57 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); |
92 } | 58 } |
(...skipping 17 matching lines...) Expand all Loading... |
110 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { | 76 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { |
111 ImageButton::OnMouseEntered(event); | 77 ImageButton::OnMouseEntered(event); |
112 host_->MouseEnteredButton(this); | 78 host_->MouseEnteredButton(this); |
113 } | 79 } |
114 | 80 |
115 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { | 81 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { |
116 ImageButton::OnMouseExited(event); | 82 ImageButton::OnMouseExited(event); |
117 host_->MouseExitedButton(this); | 83 host_->MouseExitedButton(this); |
118 } | 84 } |
119 | 85 |
| 86 void AppListButton::OnGestureEvent(ui::GestureEvent* event) { |
| 87 switch (event->type()) { |
| 88 case ui::ET_GESTURE_SCROLL_BEGIN: |
| 89 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event); |
| 90 event->SetHandled(); |
| 91 return; |
| 92 case ui::ET_GESTURE_SCROLL_UPDATE: |
| 93 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event); |
| 94 event->SetHandled(); |
| 95 return; |
| 96 case ui::ET_GESTURE_SCROLL_END: |
| 97 case ui::ET_SCROLL_FLING_START: |
| 98 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false); |
| 99 event->SetHandled(); |
| 100 return; |
| 101 default: |
| 102 ImageButton::OnGestureEvent(event); |
| 103 return; |
| 104 } |
| 105 } |
| 106 |
| 107 void AppListButton::OnPaint(gfx::Canvas* canvas) { |
| 108 // Call the base class first to paint any background/borders. |
| 109 View::OnPaint(canvas); |
| 110 |
| 111 int background_image_id = 0; |
| 112 if (Shell::GetInstance()->GetAppListTargetVisibility()) { |
| 113 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED; |
| 114 } else { |
| 115 if (shelf_widget_->GetDimsShelf()) |
| 116 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK; |
| 117 else |
| 118 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL; |
| 119 } |
| 120 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 121 const gfx::ImageSkia* background_image = |
| 122 rb.GetImageNamed(background_image_id).ToImageSkia(); |
| 123 const gfx::ImageSkia* forground_image = |
| 124 rb.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST).ToImageSkia(); |
| 125 |
| 126 gfx::Rect contents_bounds = GetContentsBounds(); |
| 127 gfx::Rect background_bounds, forground_bounds; |
| 128 |
| 129 ShelfAlignment alignment = shelf_widget_->GetAlignment(); |
| 130 background_bounds.set_size(background_image->size()); |
| 131 if (alignment == SHELF_ALIGNMENT_LEFT) { |
| 132 background_bounds.set_x(contents_bounds.width() - |
| 133 ShelfLayoutManager::kShelfItemInset - background_image->width()); |
| 134 background_bounds.set_y(contents_bounds.y() + |
| 135 (contents_bounds.height() - background_image->height()) / 2); |
| 136 } else if(alignment == SHELF_ALIGNMENT_RIGHT) { |
| 137 background_bounds.set_x(ShelfLayoutManager::kShelfItemInset); |
| 138 background_bounds.set_y(contents_bounds.y() + |
| 139 (contents_bounds.height() - background_image->height()) / 2); |
| 140 } else { |
| 141 background_bounds.set_y(ShelfLayoutManager::kShelfItemInset); |
| 142 background_bounds.set_x(contents_bounds.x() + |
| 143 (contents_bounds.width() - background_image->width()) / 2); |
| 144 } |
| 145 |
| 146 forground_bounds.set_size(forground_image->size()); |
| 147 forground_bounds.set_x(background_bounds.x() + |
| 148 std::max(0, |
| 149 (background_bounds.width() - forground_bounds.width()) / 2)); |
| 150 forground_bounds.set_y(background_bounds.y() + |
| 151 std::max(0, |
| 152 (background_bounds.height() - forground_bounds.height()) / 2)); |
| 153 |
| 154 canvas->DrawImageInt(*background_image, |
| 155 background_bounds.x(), |
| 156 background_bounds.y()); |
| 157 canvas->DrawImageInt(*forground_image, |
| 158 forground_bounds.x(), |
| 159 forground_bounds.y()); |
| 160 |
| 161 views::Painter::PaintFocusPainter(this, canvas, focus_painter()); |
| 162 } |
| 163 |
120 void AppListButton::GetAccessibleState(ui::AXViewState* state) { | 164 void AppListButton::GetAccessibleState(ui::AXViewState* state) { |
121 state->role = ui::AX_ROLE_BUTTON; | 165 state->role = ui::AX_ROLE_BUTTON; |
122 state->name = host_->GetAccessibleName(this); | 166 state->name = host_->GetAccessibleName(this); |
123 } | 167 } |
124 | 168 |
125 } // namespace ash | 169 } // namespace ash |
OLD | NEW |