OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/launcher/overflow_button.h" |
| 6 |
| 7 #include "grit/ash_strings.h" |
| 8 #include "grit/ui_resources.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "third_party/skia/include/core/SkPath.h" |
| 11 #include "ui/base/animation/throb_animation.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/skia_util.h" |
| 16 #include "ui/gfx/transform.h" |
| 17 |
| 18 namespace ash { |
| 19 namespace internal { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const int kButtonHoverAlpha = 150; |
| 24 |
| 25 const int kButtonCornerRadius = 2; |
| 26 |
| 27 const int kButtonHoverSize = 28; |
| 28 |
| 29 void RotateCounterclockwise(ui::Transform* transform) { |
| 30 transform->matrix().set3x3(0, -1, 0, |
| 31 1, 0, 0, |
| 32 0, 0, 1); |
| 33 } |
| 34 |
| 35 void RotateClockwise(ui::Transform* transform) { |
| 36 transform->matrix().set3x3( 0, 1, 0, |
| 37 -1, 0, 0, |
| 38 0, 0, 1); |
| 39 } |
| 40 |
| 41 } // namesapce |
| 42 |
| 43 OverflowButton::OverflowButton(views::ButtonListener* listener) |
| 44 : CustomButton(listener), |
| 45 alignment_(SHELF_ALIGNMENT_BOTTOM), |
| 46 image_(NULL) { |
| 47 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 48 image_ = rb.GetImageNamed(IDR_AURA_LAUNCHER_OVERFLOW).ToImageSkia(); |
| 49 |
| 50 set_accessibility_focusable(true); |
| 51 SetAccessibleName( |
| 52 l10n_util::GetStringUTF16(IDS_AURA_LAUNCHER_OVERFLOW_NAME)); |
| 53 } |
| 54 |
| 55 |
| 56 OverflowButton::~OverflowButton() { |
| 57 } |
| 58 |
| 59 void OverflowButton::SetShelfAlignment(ShelfAlignment alignment) { |
| 60 if (alignment_ == alignment) |
| 61 return; |
| 62 |
| 63 alignment_ = alignment; |
| 64 SchedulePaint(); |
| 65 } |
| 66 |
| 67 void OverflowButton::PaintBackground(gfx::Canvas* canvas, int alpha) { |
| 68 gfx::Rect rect(GetContentsBounds()); |
| 69 rect = rect.Center(gfx::Size(kButtonHoverSize, kButtonHoverSize)); |
| 70 |
| 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)); |
| 77 |
| 78 const SkScalar radius = SkIntToScalar(kButtonCornerRadius); |
| 79 SkPath path; |
| 80 path.addRoundRect(gfx::RectToSkRect(rect), radius, radius); |
| 81 canvas->DrawPath(path, paint); |
| 82 } |
| 83 |
| 84 void OverflowButton::OnPaint(gfx::Canvas* canvas) { |
| 85 if (hover_animation_->is_animating()) { |
| 86 PaintBackground( |
| 87 canvas, |
| 88 kButtonHoverAlpha * hover_animation_->GetCurrentValue()); |
| 89 } else if (state() == BS_HOT || state() == BS_PUSHED) { |
| 90 PaintBackground(canvas, kButtonHoverAlpha); |
| 91 } |
| 92 |
| 93 ui::Transform transform; |
| 94 |
| 95 switch (alignment_) { |
| 96 case SHELF_ALIGNMENT_BOTTOM: |
| 97 // Shift 1 pixel left to align with overflow bubble tip. |
| 98 transform.ConcatTranslate(-1, 0); |
| 99 break; |
| 100 case SHELF_ALIGNMENT_LEFT: |
| 101 RotateClockwise(&transform); |
| 102 transform.ConcatTranslate(width(), -1); |
| 103 break; |
| 104 case SHELF_ALIGNMENT_RIGHT: |
| 105 RotateCounterclockwise(&transform); |
| 106 transform.ConcatTranslate(0, height()); |
| 107 break; |
| 108 } |
| 109 |
| 110 canvas->Save(); |
| 111 canvas->Transform(transform); |
| 112 |
| 113 gfx::Rect rect(GetContentsBounds()); |
| 114 canvas->DrawImageInt(*image_, |
| 115 rect.x() + (rect.width() - image_->width()) / 2, |
| 116 rect.y() + (rect.height() - image_->height()) / 2); |
| 117 |
| 118 canvas->Restore(); |
| 119 } |
| 120 |
| 121 } // namespace internal |
| 122 } // namespace ash |
OLD | NEW |