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 // TODO(mgiuca): When the "classic" app list is removed, also remove this
144 // resource and its icon file.
145 int foreground_image_id
= app_list::switches::IsExperimentalAppListEnabled()
146 ? IDR_ASH_SHELF_ICON_APPLIST
147 : IDR_ASH_SHELF_ICON_APPLIST_CLASSIC
;
148 const gfx::ImageSkia
* forground_image
=
149 rb
.GetImageNamed(foreground_image_id
).ToImageSkia();
151 gfx::Rect contents_bounds
= GetContentsBounds();
152 gfx::Rect background_bounds
, forground_bounds
;
154 ShelfAlignment alignment
= shelf_widget_
->GetAlignment();
155 background_bounds
.set_size(background_image
->size());
156 if (alignment
== SHELF_ALIGNMENT_LEFT
) {
157 background_bounds
.set_x(contents_bounds
.width() -
158 ShelfLayoutManager::kShelfItemInset
- background_image
->width());
159 background_bounds
.set_y(contents_bounds
.y() +
160 (contents_bounds
.height() - background_image
->height()) / 2);
161 } else if(alignment
== SHELF_ALIGNMENT_RIGHT
) {
162 background_bounds
.set_x(ShelfLayoutManager::kShelfItemInset
);
163 background_bounds
.set_y(contents_bounds
.y() +
164 (contents_bounds
.height() - background_image
->height()) / 2);
166 background_bounds
.set_y(ShelfLayoutManager::kShelfItemInset
);
167 background_bounds
.set_x(contents_bounds
.x() +
168 (contents_bounds
.width() - background_image
->width()) / 2);
171 forground_bounds
.set_size(forground_image
->size());
172 forground_bounds
.set_x(background_bounds
.x() +
174 (background_bounds
.width() - forground_bounds
.width()) / 2));
175 forground_bounds
.set_y(background_bounds
.y() +
177 (background_bounds
.height() - forground_bounds
.height()) / 2));
179 canvas
->DrawImageInt(*background_image
,
180 background_bounds
.x(),
181 background_bounds
.y());
182 canvas
->DrawImageInt(*forground_image
,
183 forground_bounds
.x(),
184 forground_bounds
.y());
186 views::Painter::PaintFocusPainter(this, canvas
, focus_painter());
189 void AppListButton::GetAccessibleState(ui::AXViewState
* state
) {
190 state
->role
= ui::AX_ROLE_BUTTON
;
191 state
->name
= host_
->GetAccessibleName(this);
194 void AppListButton::SetDrawBackgroundAsActive(
195 bool draw_background_as_active
) {
196 if (draw_background_as_active_
== draw_background_as_active
)
198 draw_background_as_active_
= draw_background_as_active
;