Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / ash / shelf / app_list_button.cc
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.
4
5 #include "ash/shelf/app_list_button.h"
6
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"
29
30 namespace ash {
31 // static
32 const int AppListButton::kImageBoundsSize = 7;
33
34
35 AppListButton::AppListButton(views::ButtonListener* listener,
36                              ShelfButtonHost* host,
37                              ShelfWidget* shelf_widget)
38     : views::ImageButton(listener),
39       draw_background_as_active_(false),
40       host_(host),
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)));
46 }
47
48 AppListButton::~AppListButton() {
49 }
50
51 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
52   ImageButton::OnMousePressed(event);
53   host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
54   return true;
55 }
56
57 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
58   ImageButton::OnMouseReleased(event);
59   host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
60 }
61
62 void AppListButton::OnMouseCaptureLost() {
63   host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
64   ImageButton::OnMouseCaptureLost();
65 }
66
67 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
68   ImageButton::OnMouseDragged(event);
69   host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
70   return true;
71 }
72
73 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
74   ImageButton::OnMouseMoved(event);
75   host_->MouseMovedOverButton(this);
76 }
77
78 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
79   ImageButton::OnMouseEntered(event);
80   host_->MouseEnteredButton(this);
81 }
82
83 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
84   ImageButton::OnMouseExited(event);
85   host_->MouseExitedButton(this);
86 }
87
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);
94      event->SetHandled();
95      return;
96    case ui::ET_GESTURE_SCROLL_UPDATE:
97      host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
98      event->SetHandled();
99      return;
100    case ui::ET_GESTURE_SCROLL_END:
101    case ui::ET_SCROLL_FLING_START:
102      host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
103      event->SetHandled();
104      return;
105    case ui::ET_GESTURE_TAP_DOWN:
106      if (switches::IsTouchFeedbackEnabled())
107        SetDrawBackgroundAsActive(true);
108      ImageButton::OnGestureEvent(event);
109      break;
110    case ui::ET_GESTURE_TAP_CANCEL:
111    case ui::ET_GESTURE_TAP:
112      if (switches::IsTouchFeedbackEnabled())
113        SetDrawBackgroundAsActive(false);
114      ImageButton::OnGestureEvent(event);
115      break;
116    default:
117      ImageButton::OnGestureEvent(event);
118      return;
119   }
120 }
121
122 void AppListButton::OnPaint(gfx::Canvas* canvas) {
123   // Call the base class first to paint any background/borders.
124   View::OnPaint(canvas);
125
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;
130   } else {
131     if (shelf_widget_->GetDimsShelf())
132       background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK;
133     else
134       background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL;
135   }
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();
141
142   gfx::Rect contents_bounds = GetContentsBounds();
143   gfx::Rect background_bounds, forground_bounds;
144
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);
156   } else {
157     background_bounds.set_y(ShelfLayoutManager::kShelfItemInset);
158     background_bounds.set_x(contents_bounds.x() +
159         (contents_bounds.width() - background_image->width()) / 2);
160   }
161
162   forground_bounds.set_size(forground_image->size());
163   forground_bounds.set_x(background_bounds.x() +
164       std::max(0,
165           (background_bounds.width() - forground_bounds.width()) / 2));
166   forground_bounds.set_y(background_bounds.y() +
167       std::max(0,
168           (background_bounds.height() - forground_bounds.height()) / 2));
169
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());
176
177   views::Painter::PaintFocusPainter(this, canvas, focus_painter());
178 }
179
180 void AppListButton::GetAccessibleState(ui::AXViewState* state) {
181   state->role = ui::AX_ROLE_BUTTON;
182   state->name = host_->GetAccessibleName(this);
183 }
184
185 void AppListButton::SetDrawBackgroundAsActive(
186     bool draw_background_as_active) {
187   if (draw_background_as_active_ == draw_background_as_active)
188     return;
189   draw_background_as_active_ = draw_background_as_active;
190   SchedulePaint();
191 }
192
193 }  // namespace ash