Blink roll 174125:174137
[chromium-blink-merge.git] / ash / shelf / app_list_button.cc
blobd1246c873b1b6caba06735ba48e63ca918632b10
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/ash_switches.h"
9 #include "ash/shelf/shelf_button.h"
10 #include "ash/shelf/shelf_button_host.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"
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/compositor/layer.h"
21 #include "ui/compositor/layer_animation_element.h"
22 #include "ui/compositor/layer_animation_sequence.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"
27 #include "ui/views/painter.h"
29 namespace ash {
30 // static
31 const int AppListButton::kImageBoundsSize = 7;
34 AppListButton::AppListButton(views::ButtonListener* listener,
35 ShelfButtonHost* host,
36 ShelfWidget* shelf_widget)
37 : views::ImageButton(listener),
38 host_(host),
39 shelf_widget_(shelf_widget) {
40 SetAccessibleName(l10n_util::GetStringUTF16(IDS_AURA_APP_LIST_TITLE));
41 SetSize(gfx::Size(kShelfSize, kShelfSize));
42 SetFocusPainter(views::Painter::CreateSolidFocusPainter(
43 kFocusBorderColor, gfx::Insets(1, 1, 1, 1)));
46 AppListButton::~AppListButton() {
49 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
50 ImageButton::OnMousePressed(event);
51 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
52 return true;
55 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
56 ImageButton::OnMouseReleased(event);
57 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
60 void AppListButton::OnMouseCaptureLost() {
61 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
62 ImageButton::OnMouseCaptureLost();
65 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
66 ImageButton::OnMouseDragged(event);
67 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
68 return true;
71 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
72 ImageButton::OnMouseMoved(event);
73 host_->MouseMovedOverButton(this);
76 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
77 ImageButton::OnMouseEntered(event);
78 host_->MouseEnteredButton(this);
81 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
82 ImageButton::OnMouseExited(event);
83 host_->MouseExitedButton(this);
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;
107 void AppListButton::OnPaint(gfx::Canvas* canvas) {
108 // Call the base class first to paint any background/borders.
109 View::OnPaint(canvas);
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;
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();
126 gfx::Rect contents_bounds = GetContentsBounds();
127 gfx::Rect background_bounds, forground_bounds;
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);
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));
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());
161 views::Painter::PaintFocusPainter(this, canvas, focus_painter());
164 void AppListButton::GetAccessibleState(ui::AXViewState* state) {
165 state->role = ui::AX_ROLE_BUTTON;
166 state->name = host_->GetAccessibleName(this);
169 } // namespace ash