Don't preload rarely seen large images
[chromium-blink-merge.git] / ash / shelf / app_list_button.cc
blobacb0dd3e723f0769d5e47310c216a923f6b885e2
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"
31 namespace ash {
32 // static
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),
41 host_(host),
42 shelf_widget_(shelf_widget) {
43 SetAccessibleName(
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)));
50 set_notify_action(CustomButton::NOTIFY_ON_PRESS);
53 AppListButton::~AppListButton() {
56 bool AppListButton::OnMousePressed(const ui::MouseEvent& event) {
57 ImageButton::OnMousePressed(event);
58 host_->PointerPressedOnButton(this, ShelfButtonHost::MOUSE, event);
59 return true;
62 void AppListButton::OnMouseReleased(const ui::MouseEvent& event) {
63 ImageButton::OnMouseReleased(event);
64 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, false);
67 void AppListButton::OnMouseCaptureLost() {
68 host_->PointerReleasedOnButton(this, ShelfButtonHost::MOUSE, true);
69 ImageButton::OnMouseCaptureLost();
72 bool AppListButton::OnMouseDragged(const ui::MouseEvent& event) {
73 ImageButton::OnMouseDragged(event);
74 host_->PointerDraggedOnButton(this, ShelfButtonHost::MOUSE, event);
75 return true;
78 void AppListButton::OnMouseMoved(const ui::MouseEvent& event) {
79 ImageButton::OnMouseMoved(event);
80 host_->MouseMovedOverButton(this);
83 void AppListButton::OnMouseEntered(const ui::MouseEvent& event) {
84 ImageButton::OnMouseEntered(event);
85 host_->MouseEnteredButton(this);
88 void AppListButton::OnMouseExited(const ui::MouseEvent& event) {
89 ImageButton::OnMouseExited(event);
90 host_->MouseExitedButton(this);
93 void AppListButton::OnGestureEvent(ui::GestureEvent* event) {
94 switch (event->type()) {
95 case ui::ET_GESTURE_SCROLL_BEGIN:
96 if (switches::IsTouchFeedbackEnabled())
97 SetDrawBackgroundAsActive(false);
98 host_->PointerPressedOnButton(this, ShelfButtonHost::TOUCH, *event);
99 event->SetHandled();
100 return;
101 case ui::ET_GESTURE_SCROLL_UPDATE:
102 host_->PointerDraggedOnButton(this, ShelfButtonHost::TOUCH, *event);
103 event->SetHandled();
104 return;
105 case ui::ET_GESTURE_SCROLL_END:
106 case ui::ET_SCROLL_FLING_START:
107 host_->PointerReleasedOnButton(this, ShelfButtonHost::TOUCH, false);
108 event->SetHandled();
109 return;
110 case ui::ET_GESTURE_TAP_DOWN:
111 if (switches::IsTouchFeedbackEnabled())
112 SetDrawBackgroundAsActive(true);
113 ImageButton::OnGestureEvent(event);
114 break;
115 case ui::ET_GESTURE_TAP_CANCEL:
116 case ui::ET_GESTURE_TAP:
117 if (switches::IsTouchFeedbackEnabled())
118 SetDrawBackgroundAsActive(false);
119 ImageButton::OnGestureEvent(event);
120 break;
121 default:
122 ImageButton::OnGestureEvent(event);
123 return;
127 void AppListButton::OnPaint(gfx::Canvas* canvas) {
128 // Call the base class first to paint any background/borders.
129 View::OnPaint(canvas);
131 int background_image_id = 0;
132 if (Shell::GetInstance()->GetAppListTargetVisibility() ||
133 draw_background_as_active_) {
134 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED;
135 } else {
136 if (shelf_widget_->GetDimsShelf())
137 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK;
138 else
139 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL;
141 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
142 const gfx::ImageSkia* background_image =
143 rb.GetImageNamed(background_image_id).ToImageSkia();
144 // TODO(mgiuca): When the "classic" app list is removed, also remove this
145 // resource and its icon file.
146 int foreground_image_id = app_list::switches::IsExperimentalAppListEnabled()
147 ? IDR_ASH_SHELF_ICON_APPLIST
148 : IDR_ASH_SHELF_ICON_APPLIST_CLASSIC;
149 const gfx::ImageSkia* forground_image =
150 rb.GetImageNamed(foreground_image_id).ToImageSkia();
152 gfx::Rect contents_bounds = GetContentsBounds();
153 gfx::Rect background_bounds, forground_bounds;
155 ShelfAlignment alignment = shelf_widget_->GetAlignment();
156 background_bounds.set_size(background_image->size());
157 if (alignment == SHELF_ALIGNMENT_LEFT) {
158 background_bounds.set_x(contents_bounds.width() -
159 ShelfLayoutManager::kShelfItemInset - background_image->width());
160 background_bounds.set_y(contents_bounds.y() +
161 (contents_bounds.height() - background_image->height()) / 2);
162 } else if(alignment == SHELF_ALIGNMENT_RIGHT) {
163 background_bounds.set_x(ShelfLayoutManager::kShelfItemInset);
164 background_bounds.set_y(contents_bounds.y() +
165 (contents_bounds.height() - background_image->height()) / 2);
166 } else {
167 background_bounds.set_y(ShelfLayoutManager::kShelfItemInset);
168 background_bounds.set_x(contents_bounds.x() +
169 (contents_bounds.width() - background_image->width()) / 2);
172 forground_bounds.set_size(forground_image->size());
173 forground_bounds.set_x(background_bounds.x() +
174 std::max(0,
175 (background_bounds.width() - forground_bounds.width()) / 2));
176 forground_bounds.set_y(background_bounds.y() +
177 std::max(0,
178 (background_bounds.height() - forground_bounds.height()) / 2));
180 canvas->DrawImageInt(*background_image,
181 background_bounds.x(),
182 background_bounds.y());
183 canvas->DrawImageInt(*forground_image,
184 forground_bounds.x(),
185 forground_bounds.y());
187 views::Painter::PaintFocusPainter(this, canvas, focus_painter());
190 void AppListButton::GetAccessibleState(ui::AXViewState* state) {
191 state->role = ui::AX_ROLE_BUTTON;
192 state->name = host_->GetAccessibleName(this);
195 void AppListButton::SetDrawBackgroundAsActive(
196 bool draw_background_as_active) {
197 if (draw_background_as_active_ == draw_background_as_active)
198 return;
199 draw_background_as_active_ = draw_background_as_active;
200 SchedulePaint();
203 } // namespace ash