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
3 // found in the LICENSE file.
5 #include "ash/shelf/app_list_button.h"
7 #include "ash/ash_constants.h"
8 #include "ash/shelf/shelf_button.h"
9 #include "ash/shelf/shelf_button_host.h"
10 #include "ash/shelf/shelf_item_types.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shelf/shelf_widget.h"
13 #include "ash/shell.h"
14 #include "base/command_line.h"
15 #include "grit/ash_resources.h"
16 #include "grit/ash_strings.h"
17 #include "ui/accessibility/ax_view_state.h"
18 #include "ui/app_list/app_list_switches.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/base/ui_base_switches_util.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/compositor/layer_animation_element.h"
24 #include "ui/compositor/layer_animation_sequence.h"
25 #include "ui/compositor/scoped_layer_animation_settings.h"
26 #include "ui/gfx/canvas.h"
27 #include "ui/gfx/image/image_skia_operations.h"
28 #include "ui/views/controls/button/image_button.h"
29 #include "ui/views/painter.h"
33 const int AppListButton::kImageBoundsSize
= 7;
36 AppListButton::AppListButton(views::ButtonListener
* listener
,
37 ShelfButtonHost
* host
,
38 ShelfWidget
* shelf_widget
)
39 : views::ImageButton(listener
),
40 draw_background_as_active_(false),
42 shelf_widget_(shelf_widget
) {
44 app_list::switches::IsExperimentalAppListEnabled()
45 ? l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_LAUNCHER_TITLE
)
46 : l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_TITLE
));
47 SetSize(gfx::Size(kShelfSize
, kShelfSize
));
48 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
49 kFocusBorderColor
, gfx::Insets(1, 1, 1, 1)));
52 AppListButton::~AppListButton() {
55 bool AppListButton::OnMousePressed(const ui::MouseEvent
& event
) {
56 ImageButton::OnMousePressed(event
);
57 host_
->PointerPressedOnButton(this, ShelfButtonHost::MOUSE
, event
);
61 void AppListButton::OnMouseReleased(const ui::MouseEvent
& event
) {
62 ImageButton::OnMouseReleased(event
);
63 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, false);
66 void AppListButton::OnMouseCaptureLost() {
67 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, true);
68 ImageButton::OnMouseCaptureLost();
71 bool AppListButton::OnMouseDragged(const ui::MouseEvent
& event
) {
72 ImageButton::OnMouseDragged(event
);
73 host_
->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE
, event
);
77 void AppListButton::OnMouseMoved(const ui::MouseEvent
& event
) {
78 ImageButton::OnMouseMoved(event
);
79 host_
->MouseMovedOverButton(this);
82 void AppListButton::OnMouseEntered(const ui::MouseEvent
& event
) {
83 ImageButton::OnMouseEntered(event
);
84 host_
->MouseEnteredButton(this);
87 void AppListButton::OnMouseExited(const ui::MouseEvent
& event
) {
88 ImageButton::OnMouseExited(event
);
89 host_
->MouseExitedButton(this);
92 void AppListButton::OnGestureEvent(ui::GestureEvent
* event
) {
93 switch (event
->type()) {
94 case ui::ET_GESTURE_SCROLL_BEGIN
:
95 if (switches::IsTouchFeedbackEnabled())
96 SetDrawBackgroundAsActive(false);
97 host_
->PointerPressedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
100 case ui::ET_GESTURE_SCROLL_UPDATE
:
101 host_
->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
104 case ui::ET_GESTURE_SCROLL_END
:
105 case ui::ET_SCROLL_FLING_START
:
106 host_
->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH
, false);
109 case ui::ET_GESTURE_TAP_DOWN
:
110 if (switches::IsTouchFeedbackEnabled())
111 SetDrawBackgroundAsActive(true);
112 ImageButton::OnGestureEvent(event
);
114 case ui::ET_GESTURE_TAP_CANCEL
:
115 case ui::ET_GESTURE_TAP
:
116 if (switches::IsTouchFeedbackEnabled())
117 SetDrawBackgroundAsActive(false);
118 ImageButton::OnGestureEvent(event
);
121 ImageButton::OnGestureEvent(event
);
126 void AppListButton::OnPaint(gfx::Canvas
* canvas
) {
127 // Call the base class first to paint any background/borders.
128 View::OnPaint(canvas
);
130 int background_image_id
= 0;
131 if (Shell::GetInstance()->GetAppListTargetVisibility() ||
132 draw_background_as_active_
) {
133 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED
;
135 if (shelf_widget_
->GetDimsShelf())
136 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK
;
138 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL
;
140 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
141 const gfx::ImageSkia
* background_image
=
142 rb
.GetImageNamed(background_image_id
).ToImageSkia();
143 const gfx::ImageSkia
* forground_image
=
144 rb
.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST
).ToImageSkia();
146 gfx::Rect contents_bounds
= GetContentsBounds();
147 gfx::Rect background_bounds
, forground_bounds
;
149 ShelfAlignment alignment
= shelf_widget_
->GetAlignment();
150 background_bounds
.set_size(background_image
->size());
151 if (alignment
== SHELF_ALIGNMENT_LEFT
) {
152 background_bounds
.set_x(contents_bounds
.width() -
153 ShelfLayoutManager::kShelfItemInset
- background_image
->width());
154 background_bounds
.set_y(contents_bounds
.y() +
155 (contents_bounds
.height() - background_image
->height()) / 2);
156 } else if(alignment
== SHELF_ALIGNMENT_RIGHT
) {
157 background_bounds
.set_x(ShelfLayoutManager::kShelfItemInset
);
158 background_bounds
.set_y(contents_bounds
.y() +
159 (contents_bounds
.height() - background_image
->height()) / 2);
161 background_bounds
.set_y(ShelfLayoutManager::kShelfItemInset
);
162 background_bounds
.set_x(contents_bounds
.x() +
163 (contents_bounds
.width() - background_image
->width()) / 2);
166 forground_bounds
.set_size(forground_image
->size());
167 forground_bounds
.set_x(background_bounds
.x() +
169 (background_bounds
.width() - forground_bounds
.width()) / 2));
170 forground_bounds
.set_y(background_bounds
.y() +
172 (background_bounds
.height() - forground_bounds
.height()) / 2));
174 canvas
->DrawImageInt(*background_image
,
175 background_bounds
.x(),
176 background_bounds
.y());
177 canvas
->DrawImageInt(*forground_image
,
178 forground_bounds
.x(),
179 forground_bounds
.y());
181 views::Painter::PaintFocusPainter(this, canvas
, focus_painter());
184 void AppListButton::GetAccessibleState(ui::AXViewState
* state
) {
185 state
->role
= ui::AX_ROLE_BUTTON
;
186 state
->name
= host_
->GetAccessibleName(this);
189 void AppListButton::SetDrawBackgroundAsActive(
190 bool draw_background_as_active
) {
191 if (draw_background_as_active_
== draw_background_as_active
)
193 draw_background_as_active_
= draw_background_as_active
;