Don't preload rarely seen large images
[chromium-blink-merge.git] / ash / shelf / overflow_button.cc
blob79b2bcdfa040953c4276430ba38325b0978660a6
1 // Copyright 2013 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/overflow_button.h"
7 #include "ash/ash_switches.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shelf/shelf_widget.h"
10 #include "grit/ash_resources.h"
11 #include "grit/ash_strings.h"
12 #include "third_party/skia/include/core/SkPaint.h"
13 #include "third_party/skia/include/core/SkPath.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/animation/throb_animation.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/image/image_skia_operations.h"
19 #include "ui/gfx/skbitmap_operations.h"
20 #include "ui/gfx/skia_util.h"
21 #include "ui/gfx/transform.h"
22 #include "ui/views/widget/widget.h"
24 namespace ash {
25 namespace {
27 const int kButtonHoverAlpha = 150;
29 const int kButtonCornerRadius = 2;
31 const int kButtonHoverSize = 28;
33 const int kBackgroundOffset = (48 - kButtonHoverSize) / 2;
35 } // namesapce
37 OverflowButton::OverflowButton(views::ButtonListener* listener)
38 : CustomButton(listener),
39 bottom_image_(NULL) {
40 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
41 bottom_image_ = rb->GetImageNamed(IDR_ASH_SHELF_OVERFLOW).ToImageSkia();
43 SetAccessibilityFocusable(true);
44 SetAccessibleName(l10n_util::GetStringUTF16(IDS_ASH_SHELF_OVERFLOW_NAME));
47 OverflowButton::~OverflowButton() {}
49 void OverflowButton::OnShelfAlignmentChanged() {
50 SchedulePaint();
53 void OverflowButton::PaintBackground(gfx::Canvas* canvas, int alpha) {
54 gfx::Rect bounds(GetContentsBounds());
55 gfx::Rect rect(0, 0, kButtonHoverSize, kButtonHoverSize);
56 ShelfLayoutManager* shelf =
57 ShelfLayoutManager::ForShelf(GetWidget()->GetNativeView());
59 // Nudge the background a little to line up right.
60 if (shelf->IsHorizontalAlignment()) {
61 rect.set_origin(gfx::Point(
62 bounds.x() + ((bounds.width() - kButtonHoverSize) / 2) - 1,
63 bounds.y() + kBackgroundOffset - 1));
65 } else {
66 rect.set_origin(gfx::Point(
67 bounds.x() + kBackgroundOffset - 1,
68 bounds.y() + ((bounds.height() - kButtonHoverSize) / 2) - 1));
71 SkPaint paint;
72 paint.setAntiAlias(true);
73 paint.setStyle(SkPaint::kFill_Style);
74 paint.setColor(SkColorSetARGB(
75 kButtonHoverAlpha * hover_animation_->GetCurrentValue(),
76 0, 0, 0));
78 const SkScalar radius = SkIntToScalar(kButtonCornerRadius);
79 SkPath path;
80 path.addRoundRect(gfx::RectToSkRect(rect), radius, radius);
81 canvas->DrawPath(path, paint);
84 void OverflowButton::OnPaint(gfx::Canvas* canvas) {
85 ShelfLayoutManager* layout_manager =
86 ShelfLayoutManager::ForShelf(GetWidget()->GetNativeView());
87 ShelfAlignment alignment = layout_manager->GetAlignment();
89 gfx::Rect bounds(GetContentsBounds());
90 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
91 int background_image_id = 0;
92 if (layout_manager->shelf_widget()->shelf()->IsShowingOverflowBubble())
93 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_PRESSED;
94 else if(layout_manager->shelf_widget()->GetDimsShelf())
95 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_ON_BLACK;
96 else
97 background_image_id = IDR_AURA_NOTIFICATION_BACKGROUND_NORMAL;
99 const gfx::ImageSkia* background =
100 rb.GetImageNamed(background_image_id).ToImageSkia();
101 if (alignment == SHELF_ALIGNMENT_LEFT) {
102 bounds = gfx::Rect(
103 bounds.right() - background->width() -
104 ShelfLayoutManager::kShelfItemInset,
105 bounds.y() + (bounds.height() - background->height()) / 2,
106 background->width(), background->height());
107 } else if (alignment == SHELF_ALIGNMENT_RIGHT) {
108 bounds = gfx::Rect(
109 bounds.x() + ShelfLayoutManager::kShelfItemInset,
110 bounds.y() + (bounds.height() - background->height()) / 2,
111 background->width(), background->height());
112 } else {
113 bounds = gfx::Rect(
114 bounds.x() + (bounds.width() - background->width()) / 2,
115 bounds.y() + ShelfLayoutManager::kShelfItemInset,
116 background->width(), background->height());
118 canvas->DrawImageInt(*background, bounds.x(), bounds.y());
120 if (height() < kButtonHoverSize)
121 return;
123 const gfx::ImageSkia* image = NULL;
125 switch(alignment) {
126 case SHELF_ALIGNMENT_LEFT:
127 if (left_image_.isNull()) {
128 left_image_ = gfx::ImageSkiaOperations::CreateRotatedImage(
129 *bottom_image_, SkBitmapOperations::ROTATION_90_CW);
131 image = &left_image_;
132 break;
133 case SHELF_ALIGNMENT_RIGHT:
134 if (right_image_.isNull()) {
135 right_image_ = gfx::ImageSkiaOperations::CreateRotatedImage(
136 *bottom_image_, SkBitmapOperations::ROTATION_270_CW);
138 image = &right_image_;
139 break;
140 default:
141 image = bottom_image_;
142 break;
145 canvas->DrawImageInt(*image,
146 bounds.x() + ((bounds.width() - image->width()) / 2),
147 bounds.y() + ((bounds.height() - image->height()) / 2));
150 } // namespace ash