| 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 |
| 7 #include "ash/ash_constants.h" | 9 #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_layout_manager.h" | 11 #include "ash/shelf/shelf_constants.h" |
| 12 #include "ash/shelf/shelf_types.h" | |
| 13 #include "ash/shelf/shelf_widget.h" | |
| 14 #include "ash/shell.h" | |
| 15 #include "grit/ash_resources.h" | 12 #include "grit/ash_resources.h" |
| 16 #include "grit/ash_strings.h" | 13 #include "grit/ash_strings.h" |
| 17 #include "ui/accessibility/ax_view_state.h" | 14 #include "ui/accessibility/ax_view_state.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/compositor/layer.h" | 17 #include "ui/compositor/layer.h" |
| 21 #include "ui/compositor/layer_animation_element.h" | 18 #include "ui/compositor/layer_animation_element.h" |
| 22 #include "ui/compositor/layer_animation_sequence.h" | 19 #include "ui/compositor/layer_animation_sequence.h" |
| 23 #include "ui/compositor/scoped_layer_animation_settings.h" | 20 #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" | |
| 27 #include "ui/views/painter.h" | 21 #include "ui/views/painter.h" |
| 28 | 22 |
| 29 namespace ash { | 23 namespace ash { |
| 30 namespace internal { | 24 namespace internal { |
| 31 | 25 |
| 32 // static | 26 const int kAnimationDurationInMs = 600; |
| 33 const int AppListButton::kImageBoundsSize = 7; | 27 const float kAnimationOpacity[] = { 1.0f, 0.4f, 1.0f }; |
| 34 | |
| 35 | 28 |
| 36 AppListButton::AppListButton(views::ButtonListener* listener, | 29 AppListButton::AppListButton(views::ButtonListener* listener, |
| 37 ShelfButtonHost* host, | 30 ShelfButtonHost* host) |
| 38 ShelfWidget* shelf_widget) | |
| 39 : views::ImageButton(listener), | 31 : views::ImageButton(listener), |
| 40 host_(host), | 32 host_(host) { |
| 41 shelf_widget_(shelf_widget) { | 33 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 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()); |
| 42 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE)); | 45 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE)); |
| 43 SetSize(gfx::Size(kShelfSize, kShelfSize)); | 46 SetSize(gfx::Size(kShelfPreferredSize, kShelfPreferredSize)); |
| 47 SetImageAlignment(ImageButton::ALIGN_CENTER, ImageButton::ALIGN_TOP); |
| 44 SetFocusPainter(views::Painter::CreateSolidFocusPainter( | 48 SetFocusPainter(views::Painter::CreateSolidFocusPainter( |
| 45 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); | 49 kFocusBorderColor, gfx::Insets(1, 1, 1, 1))); |
| 46 } | 50 } |
| 47 | 51 |
| 48 AppListButton::~AppListButton() { | 52 AppListButton::~AppListButton() { |
| 49 } | 53 } |
| 50 | 54 |
| 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 |
| 51 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { | 89 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) { |
| 52 ImageButton::OnMousePressed(event); | 90 ImageButton::OnMousePressed(event); |
| 53 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); | 91 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event); |
| 54 return true; | 92 return true; |
| 55 } | 93 } |
| 56 | 94 |
| 57 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { | 95 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) { |
| 58 ImageButton::OnMouseReleased(event); | 96 ImageButton::OnMouseReleased(event); |
| 59 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); | 97 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false); |
| 60 } | 98 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 78 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { | 116 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) { |
| 79 ImageButton::OnMouseEntered(event); | 117 ImageButton::OnMouseEntered(event); |
| 80 host_->MouseEnteredButton(this); | 118 host_->MouseEnteredButton(this); |
| 81 } | 119 } |
| 82 | 120 |
| 83 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { | 121 void AppListButton::OnMouseExited(const ui::MouseEvent& event) { |
| 84 ImageButton::OnMouseExited(event); | 122 ImageButton::OnMouseExited(event); |
| 85 host_->MouseExitedButton(this); | 123 host_->MouseExitedButton(this); |
| 86 } | 124 } |
| 87 | 125 |
| 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 | |
| 166 void AppListButton::GetAccessibleState(ui::AXViewState* state) { | 126 void AppListButton::GetAccessibleState(ui::AXViewState* state) { |
| 167 state->role = ui::AX_ROLE_BUTTON; | 127 state->role = ui::AX_ROLE_BUTTON; |
| 168 state->name = host_->GetAccessibleName(this); | 128 state->name = host_->GetAccessibleName(this); |
| 169 } | 129 } |
| 170 | 130 |
| 171 } // namespace internal | 131 } // namespace internal |
| 172 } // namespace ash | 132 } // namespace ash |
| OLD | NEW |