OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "app/gfx/canvas.h" | 5 #include "app/gfx/canvas.h" |
6 #include "app/resource_bundle.h" | 6 #include "app/resource_bundle.h" |
7 #include "base/gfx/rect.h" | 7 #include "base/gfx/rect.h" |
8 #include "chrome/app/chrome_dll_resource.h" | 8 #include "chrome/app/chrome_dll_resource.h" |
9 #include "chrome/common/extensions/extension_action.h" | 9 #include "chrome/common/extensions/extension_action.h" |
10 #include "grit/app_resources.h" | 10 #include "grit/app_resources.h" |
11 #include "third_party/skia/include/core/SkBitmap.h" | 11 #include "third_party/skia/include/core/SkBitmap.h" |
12 #include "third_party/skia/include/core/SkTypeface.h" | 12 #include "third_party/skia/include/core/SkTypeface.h" |
13 #include "third_party/skia/include/effects/SkGradientShader.h" | 13 #include "third_party/skia/include/effects/SkGradientShader.h" |
14 | 14 |
15 ExtensionAction::ExtensionAction() | 15 ExtensionAction::ExtensionAction() |
16 : type_(PAGE_ACTION) { | 16 : type_(PAGE_ACTION) { |
17 } | 17 } |
18 | 18 |
19 ExtensionAction::~ExtensionAction() { | 19 ExtensionAction::~ExtensionAction() { |
20 } | 20 } |
21 | 21 |
22 void ExtensionActionState::PaintBadge(gfx::Canvas* canvas, | 22 void ExtensionActionState::PaintBadge(gfx::Canvas* canvas, |
23 const gfx::Rect& bounds) const { | 23 const gfx::Rect& bounds, |
24 const std::string& text = badge_text(); | 24 const std::string& text, |
| 25 SkColor text_color, |
| 26 SkColor background_color) { |
25 if (text.empty()) | 27 if (text.empty()) |
26 return; | 28 return; |
27 | 29 |
| 30 if (SkColorGetA(text_color) == 0x00) |
| 31 text_color = SK_ColorWHITE; |
| 32 |
| 33 if (SkColorGetA(background_color) == 0x00) |
| 34 background_color = SkColorSetARGB(255, 218, 0, 24); // default badge color |
| 35 |
28 // Different platforms need slightly different constants to look good. | 36 // Different platforms need slightly different constants to look good. |
29 #if defined(OS_LINUX) | 37 #if defined(OS_LINUX) |
30 const int kTextSize = 9; | 38 const int kTextSize = 9; |
31 const int kBottomMargin = 4; | 39 const int kBottomMargin = 4; |
32 const int kPadding = 2; | 40 const int kPadding = 2; |
33 const int kBadgeHeight = 12; | 41 const int kBadgeHeight = 12; |
34 const int kMaxTextWidth = 23; | 42 const int kMaxTextWidth = 23; |
35 // The minimum width for center-aligning the badge. | 43 // The minimum width for center-aligning the badge. |
36 const int kCenterAlignThreshold = 20; | 44 const int kCenterAlignThreshold = 20; |
37 #else | 45 #else |
38 const int kTextSize = 8; | 46 const int kTextSize = 8; |
39 const int kBottomMargin = 5; | 47 const int kBottomMargin = 5; |
40 const int kPadding = 2; | 48 const int kPadding = 2; |
41 const int kBadgeHeight = 11; | 49 const int kBadgeHeight = 11; |
42 const int kMaxTextWidth = 23; | 50 const int kMaxTextWidth = 23; |
43 // The minimum width for center-aligning the badge. | 51 // The minimum width for center-aligning the badge. |
44 const int kCenterAlignThreshold = 20; | 52 const int kCenterAlignThreshold = 20; |
45 #endif | 53 #endif |
46 | 54 |
47 canvas->save(); | 55 canvas->save(); |
48 | 56 |
49 SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); | 57 SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); |
50 SkPaint text_paint; | 58 SkPaint text_paint; |
51 text_paint.setAntiAlias(true); | 59 text_paint.setAntiAlias(true); |
52 text_paint.setColor(badge_text_color()); | 60 text_paint.setColor(text_color); |
53 text_paint.setFakeBoldText(true); | 61 text_paint.setFakeBoldText(true); |
54 text_paint.setTextAlign(SkPaint::kLeft_Align); | 62 text_paint.setTextAlign(SkPaint::kLeft_Align); |
55 text_paint.setTextSize(SkIntToScalar(kTextSize)); | 63 text_paint.setTextSize(SkIntToScalar(kTextSize)); |
56 text_paint.setTypeface(typeface); | 64 text_paint.setTypeface(typeface); |
57 | 65 |
58 // Calculate text width. We clamp it to a max size. | 66 // Calculate text width. We clamp it to a max size. |
59 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); | 67 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); |
60 text_width = SkIntToScalar( | 68 text_width = SkIntToScalar( |
61 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | 69 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
62 | 70 |
(...skipping 11 matching lines...) Expand all Loading... |
74 rect.fLeft = SkIntToScalar((bounds.right() - badge_width) / 2); | 82 rect.fLeft = SkIntToScalar((bounds.right() - badge_width) / 2); |
75 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); | 83 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); |
76 } else { | 84 } else { |
77 rect.fRight = SkIntToScalar(bounds.right()); | 85 rect.fRight = SkIntToScalar(bounds.right()); |
78 rect.fLeft = rect.fRight - badge_width; | 86 rect.fLeft = rect.fRight - badge_width; |
79 } | 87 } |
80 | 88 |
81 SkPaint rect_paint; | 89 SkPaint rect_paint; |
82 rect_paint.setStyle(SkPaint::kFill_Style); | 90 rect_paint.setStyle(SkPaint::kFill_Style); |
83 rect_paint.setAntiAlias(true); | 91 rect_paint.setAntiAlias(true); |
84 rect_paint.setColor(badge_background_color()); | 92 rect_paint.setColor(background_color); |
85 canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); | 93 canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); |
86 | 94 |
87 // Overlay the gradient. It is stretchy, so we do this in three parts. | 95 // Overlay the gradient. It is stretchy, so we do this in three parts. |
88 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); | 96 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
89 SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( | 97 SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( |
90 IDR_BROWSER_ACTION_BADGE_LEFT); | 98 IDR_BROWSER_ACTION_BADGE_LEFT); |
91 SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( | 99 SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( |
92 IDR_BROWSER_ACTION_BADGE_RIGHT); | 100 IDR_BROWSER_ACTION_BADGE_RIGHT); |
93 SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( | 101 SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( |
94 IDR_BROWSER_ACTION_BADGE_CENTER); | 102 IDR_BROWSER_ACTION_BADGE_CENTER); |
(...skipping 12 matching lines...) Expand all Loading... |
107 // text was too large. | 115 // text was too large. |
108 rect.fLeft += kPadding; | 116 rect.fLeft += kPadding; |
109 rect.fRight -= kPadding; | 117 rect.fRight -= kPadding; |
110 canvas->clipRect(rect); | 118 canvas->clipRect(rect); |
111 canvas->drawText(text.c_str(), text.size(), | 119 canvas->drawText(text.c_str(), text.size(), |
112 rect.fLeft + (rect.width() - text_width) / 2, | 120 rect.fLeft + (rect.width() - text_width) / 2, |
113 rect.fTop + kTextSize + 1, | 121 rect.fTop + kTextSize + 1, |
114 text_paint); | 122 text_paint); |
115 canvas->restore(); | 123 canvas->restore(); |
116 } | 124 } |
OLD | NEW |