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"
27 const int kButtonHoverAlpha
= 150;
29 const int kButtonCornerRadius
= 2;
31 const int kButtonHoverSize
= 28;
33 const int kBackgroundOffset
= (48 - kButtonHoverSize
) / 2;
37 OverflowButton::OverflowButton(views::ButtonListener
* listener
)
38 : CustomButton(listener
),
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() {
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));
66 rect
.set_origin(gfx::Point(
67 bounds
.x() + kBackgroundOffset
- 1,
68 bounds
.y() + ((bounds
.height() - kButtonHoverSize
) / 2) - 1));
72 paint
.setAntiAlias(true);
73 paint
.setStyle(SkPaint::kFill_Style
);
74 paint
.setColor(SkColorSetARGB(
75 kButtonHoverAlpha
* hover_animation_
->GetCurrentValue(),
78 const SkScalar radius
= SkIntToScalar(kButtonCornerRadius
);
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
;
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
) {
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
) {
109 bounds
.x() + ShelfLayoutManager::kShelfItemInset
,
110 bounds
.y() + (bounds
.height() - background
->height()) / 2,
111 background
->width(), background
->height());
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
)
123 const gfx::ImageSkia
* image
= NULL
;
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_
;
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_
;
141 image
= bottom_image_
;
145 canvas
->DrawImageInt(*image
,
146 bounds
.x() + ((bounds
.width() - image
->width()) / 2),
147 bounds
.y() + ((bounds
.height() - image
->height()) / 2));