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/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/ui_base_switches_util.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/layer_animation_element.h"
23 #include "ui/compositor/layer_animation_sequence.h"
24 #include "ui/compositor/scoped_layer_animation_settings.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/image/image_skia_operations.h"
27 #include "ui/views/controls/button/image_button.h"
28 #include "ui/views/painter.h"
32 const int AppListButton::kImageBoundsSize
= 7;
35 AppListButton::AppListButton(views::ButtonListener
* listener
,
36 ShelfButtonHost
* host
,
37 ShelfWidget
* shelf_widget
)
38 : views::ImageButton(listener
),
39 draw_background_as_active_(false),
41 shelf_widget_(shelf_widget
) {
42 SetAccessibleName(l10n_util::GetStringUTF16(IDS_ASH_SHELF_APP_LIST_TITLE
));
43 SetSize(gfx::Size(kShelfSize
, kShelfSize
));
44 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
45 kFocusBorderColor
, gfx::Insets(1, 1, 1, 1)));
48 AppListButton::~AppListButton() {
51 bool AppListButton::OnMousePressed(const ui::MouseEvent
& event
) {
52 ImageButton::OnMousePressed(event
);
53 host_
->PointerPressedOnButton(this, ShelfButtonHost::MOUSE
, event
);
57 void AppListButton::OnMouseReleased(const ui::MouseEvent
& event
) {
58 ImageButton::OnMouseReleased(event
);
59 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, false);
62 void AppListButton::OnMouseCaptureLost() {
63 host_
->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE
, true);
64 ImageButton::OnMouseCaptureLost();
67 bool AppListButton::OnMouseDragged(const ui::MouseEvent
& event
) {
68 ImageButton::OnMouseDragged(event
);
69 host_
->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE
, event
);
73 void AppListButton::OnMouseMoved(const ui::MouseEvent
& event
) {
74 ImageButton::OnMouseMoved(event
);
75 host_
->MouseMovedOverButton(this);
78 void AppListButton::OnMouseEntered(const ui::MouseEvent
& event
) {
79 ImageButton::OnMouseEntered(event
);
80 host_
->MouseEnteredButton(this);
83 void AppListButton::OnMouseExited(const ui::MouseEvent
& event
) {
84 ImageButton::OnMouseExited(event
);
85 host_
->MouseExitedButton(this);
88 void AppListButton::OnGestureEvent(ui::GestureEvent
* event
) {
89 switch (event
->type()) {
90 case ui::ET_GESTURE_SCROLL_BEGIN
:
91 if (switches::IsTouchFeedbackEnabled())
92 SetDrawBackgroundAsActive(false);
93 host_
->PointerPressedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
96 case ui::ET_GESTURE_SCROLL_UPDATE
:
97 host_
->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH
, *event
);
100 case ui::ET_GESTURE_SCROLL_END
:
101 case ui::ET_SCROLL_FLING_START
:
102 host_
->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH
, false);
105 case ui::ET_GESTURE_TAP_DOWN
:
106 if (switches::IsTouchFeedbackEnabled())
107 SetDrawBackgroundAsActive(true);
108 ImageButton::OnGestureEvent(event
);
110 case ui::ET_GESTURE_TAP_CANCEL
:
111 case ui::ET_GESTURE_TAP
:
112 if (switches::IsTouchFeedbackEnabled())
113 SetDrawBackgroundAsActive(false);
114 ImageButton::OnGestureEvent(event
);
117 ImageButton::OnGestureEvent(event
);
122 void AppListButton::OnPaint(gfx::Canvas
* canvas
) {
123 // Call the base class first to paint any background/borders.
124 View::OnPaint(canvas
);
126 int background_image_id
= 0;
127 if (Shell::GetInstance()->GetAppListTargetVisibility() ||
128 draw_background_as_active_
) {
129 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED
;
131 if (shelf_widget_
->GetDimsShelf())
132 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK
;
134 background_image_id
= IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL
;
136 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
137 const gfx::ImageSkia
* background_image
=
138 rb
.GetImageNamed(background_image_id
).ToImageSkia();
139 const gfx::ImageSkia
* forground_image
=
140 rb
.GetImageNamed(IDR_ASH_SHELF_ICON_APPLIST
).ToImageSkia();
142 gfx::Rect contents_bounds
= GetContentsBounds();
143 gfx::Rect background_bounds
, forground_bounds
;
145 ShelfAlignment alignment
= shelf_widget_
->GetAlignment();
146 background_bounds
.set_size(background_image
->size());
147 if (alignment
== SHELF_ALIGNMENT_LEFT
) {
148 background_bounds
.set_x(contents_bounds
.width() -
149 ShelfLayoutManager::kShelfItemInset
- background_image
->width());
150 background_bounds
.set_y(contents_bounds
.y() +
151 (contents_bounds
.height() - background_image
->height()) / 2);
152 } else if(alignment
== SHELF_ALIGNMENT_RIGHT
) {
153 background_bounds
.set_x(ShelfLayoutManager::kShelfItemInset
);
154 background_bounds
.set_y(contents_bounds
.y() +
155 (contents_bounds
.height() - background_image
->height()) / 2);
157 background_bounds
.set_y(ShelfLayoutManager::kShelfItemInset
);
158 background_bounds
.set_x(contents_bounds
.x() +
159 (contents_bounds
.width() - background_image
->width()) / 2);
162 forground_bounds
.set_size(forground_image
->size());
163 forground_bounds
.set_x(background_bounds
.x() +
165 (background_bounds
.width() - forground_bounds
.width()) / 2));
166 forground_bounds
.set_y(background_bounds
.y() +
168 (background_bounds
.height() - forground_bounds
.height()) / 2));
170 canvas
->DrawImageInt(*background_image
,
171 background_bounds
.x(),
172 background_bounds
.y());
173 canvas
->DrawImageInt(*forground_image
,
174 forground_bounds
.x(),
175 forground_bounds
.y());
177 views::Painter::PaintFocusPainter(this, canvas
, focus_painter());
180 void AppListButton::GetAccessibleState(ui::AXViewState
* state
) {
181 state
->role
= ui::AX_ROLE_BUTTON
;
182 state
->name
= host_
->GetAccessibleName(this);
185 void AppListButton::SetDrawBackgroundAsActive(
186 bool draw_background_as_active
) {
187 if (draw_background_as_active_
== draw_background_as_active
)
189 draw_background_as_active_
= draw_background_as_active
;