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 "chrome/common/extensions/extension_action.h" | 5 #include "chrome/common/extensions/extension_action.h" |
6 | 6 |
7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
| 8 #include "app/gfx/font.h" |
8 #include "app/resource_bundle.h" | 9 #include "app/resource_bundle.h" |
9 #include "base/gfx/rect.h" | 10 #include "base/gfx/rect.h" |
| 11 #include "base/string_util.h" |
10 #include "chrome/app/chrome_dll_resource.h" | 12 #include "chrome/app/chrome_dll_resource.h" |
11 #include "grit/app_resources.h" | 13 #include "grit/app_resources.h" |
12 #include "third_party/skia/include/core/SkBitmap.h" | 14 #include "third_party/skia/include/core/SkBitmap.h" |
13 #include "third_party/skia/include/core/SkTypeface.h" | 15 #include "third_party/skia/include/core/SkTypeface.h" |
14 #include "third_party/skia/include/effects/SkGradientShader.h" | 16 #include "third_party/skia/include/effects/SkGradientShader.h" |
15 | 17 |
| 18 namespace { |
| 19 |
| 20 // Different platforms need slightly different constants to look good. |
| 21 #if defined(OS_LINUX) |
| 22 const int kTextSize = 9; |
| 23 const int kBottomMargin = 0; |
| 24 const int kPadding = 2; |
| 25 const int kTopTextPadding = 0; |
| 26 const int kBadgeHeight = 11; |
| 27 const int kMaxTextWidth = 23; |
| 28 // The minimum width for center-aligning the badge. |
| 29 const int kCenterAlignThreshold = 20; |
| 30 #else |
| 31 const int kTextSize = 8; |
| 32 const int kBottomMargin = 5; |
| 33 const int kPadding = 2; |
| 34 // The padding between the top of the badge and the top of the text. |
| 35 const int kTopTextPadding = 1; |
| 36 const int kBadgeHeight = 11; |
| 37 const int kMaxTextWidth = 23; |
| 38 // The minimum width for center-aligning the badge. |
| 39 const int kCenterAlignThreshold = 20; |
| 40 #endif |
| 41 |
| 42 #if defined(OS_MACOSX) |
| 43 const char kPreferredTypeface[] = "Helvetica"; |
| 44 #else |
| 45 const char kPreferredTypeface[] = "Arial"; |
| 46 #endif |
| 47 |
| 48 SkPaint* GetTextPaint() { |
| 49 static SkPaint* text_paint = NULL; |
| 50 if (!text_paint) { |
| 51 text_paint = new SkPaint; |
| 52 text_paint->setAntiAlias(true); |
| 53 |
| 54 text_paint->setTextAlign(SkPaint::kLeft_Align); |
| 55 text_paint->setTextSize(SkIntToScalar(kTextSize)); |
| 56 |
| 57 SkTypeface* typeface = SkTypeface::CreateFromName( |
| 58 kPreferredTypeface, SkTypeface::kBold); |
| 59 // Skia doesn't do any font fallback---if the user is missing the font then |
| 60 // typeface will be NULL. If we don't do manual fallback then we'll crash. |
| 61 if (typeface) { |
| 62 text_paint->setFakeBoldText(true); |
| 63 } else { |
| 64 // Fall back to the system font. We don't bold it because we aren't sure |
| 65 // how it will look. |
| 66 // For the most part this code path will only be hit on Linux systems |
| 67 // that don't have Arial. |
| 68 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 69 const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont); |
| 70 typeface = SkTypeface::CreateFromName( |
| 71 WideToUTF8(base_font.FontName()).c_str(), SkTypeface::kNormal); |
| 72 } |
| 73 |
| 74 text_paint->setTypeface(typeface); |
| 75 // |text_paint| adds its own ref. Release the ref from CreateFontName. |
| 76 typeface->unref(); |
| 77 } |
| 78 return text_paint; |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
16 const int ExtensionAction::kDefaultTabId = -1; | 83 const int ExtensionAction::kDefaultTabId = -1; |
17 | 84 |
18 void ExtensionAction::ClearAllValuesForTab(int tab_id) { | 85 void ExtensionAction::ClearAllValuesForTab(int tab_id) { |
19 title_.erase(tab_id); | 86 title_.erase(tab_id); |
20 icon_.erase(tab_id); | 87 icon_.erase(tab_id); |
21 icon_index_.erase(tab_id); | 88 icon_index_.erase(tab_id); |
22 badge_text_.erase(tab_id); | 89 badge_text_.erase(tab_id); |
23 badge_text_color_.erase(tab_id); | 90 badge_text_color_.erase(tab_id); |
24 badge_background_color_.erase(tab_id); | 91 badge_background_color_.erase(tab_id); |
25 visible_.erase(tab_id); | 92 visible_.erase(tab_id); |
26 } | 93 } |
27 | 94 |
28 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, | 95 void ExtensionAction::PaintBadge(gfx::Canvas* canvas, |
29 const gfx::Rect& bounds, | 96 const gfx::Rect& bounds, |
30 int tab_id) { | 97 int tab_id) { |
31 std::string text = GetBadgeText(tab_id); | 98 std::string text = GetBadgeText(tab_id); |
32 if (text.empty()) | 99 if (text.empty()) |
33 return; | 100 return; |
34 | 101 |
35 SkColor text_color = GetBadgeTextColor(tab_id); | 102 SkColor text_color = GetBadgeTextColor(tab_id); |
36 SkColor background_color = GetBadgeBackgroundColor(tab_id); | 103 SkColor background_color = GetBadgeBackgroundColor(tab_id); |
37 | 104 |
38 if (SkColorGetA(text_color) == 0x00) | 105 if (SkColorGetA(text_color) == 0x00) |
39 text_color = SK_ColorWHITE; | 106 text_color = SK_ColorWHITE; |
40 | 107 |
41 if (SkColorGetA(background_color) == 0x00) | 108 if (SkColorGetA(background_color) == 0x00) |
42 background_color = SkColorSetARGB(255, 218, 0, 24); // default badge color | 109 background_color = SkColorSetARGB(255, 218, 0, 24); // default badge color |
43 | 110 |
44 // Different platforms need slightly different constants to look good. | |
45 #if defined(OS_LINUX) | |
46 const int kTextSize = 9; | |
47 const int kBottomMargin = 0; | |
48 const int kPadding = 2; | |
49 const int kTopTextPadding = 0; | |
50 const int kBadgeHeight = 11; | |
51 const int kMaxTextWidth = 23; | |
52 // The minimum width for center-aligning the badge. | |
53 const int kCenterAlignThreshold = 20; | |
54 #else | |
55 const int kTextSize = 8; | |
56 const int kBottomMargin = 5; | |
57 const int kPadding = 2; | |
58 // The padding between the top of the badge and the top of the text. | |
59 const int kTopTextPadding = 1; | |
60 const int kBadgeHeight = 11; | |
61 const int kMaxTextWidth = 23; | |
62 // The minimum width for center-aligning the badge. | |
63 const int kCenterAlignThreshold = 20; | |
64 #endif | |
65 | |
66 #if defined(OS_MACOSX) | |
67 const char kTypeFaceName[] = "Helvetica"; | |
68 #else | |
69 const char kTypeFaceName[] = "Arial"; | |
70 #endif | |
71 | |
72 canvas->save(); | 111 canvas->save(); |
73 | 112 |
74 SkTypeface* typeface = SkTypeface::CreateFromName(kTypeFaceName, | 113 SkPaint* text_paint = GetTextPaint(); |
75 SkTypeface::kBold); | 114 text_paint->setColor(text_color); |
76 SkPaint text_paint; | |
77 text_paint.setAntiAlias(true); | |
78 text_paint.setColor(text_color); | |
79 text_paint.setFakeBoldText(true); | |
80 text_paint.setTextAlign(SkPaint::kLeft_Align); | |
81 text_paint.setTextSize(SkIntToScalar(kTextSize)); | |
82 text_paint.setTypeface(typeface); | |
83 | 115 |
84 // Calculate text width. We clamp it to a max size. | 116 // Calculate text width. We clamp it to a max size. |
85 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); | 117 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); |
86 text_width = SkIntToScalar( | 118 text_width = SkIntToScalar( |
87 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | 119 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
88 | 120 |
89 // Cacluate badge size. It is clamped to a min width just because it looks | 121 // Cacluate badge size. It is clamped to a min width just because it looks |
90 // silly if it is too skinny. | 122 // silly if it is too skinny. |
91 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | 123 int badge_width = SkScalarFloor(text_width) + kPadding * 2; |
92 badge_width = std::max(kBadgeHeight, badge_width); | 124 badge_width = std::max(kBadgeHeight, badge_width); |
93 | 125 |
94 // Paint the badge background color in the right location. It is usually | 126 // Paint the badge background color in the right location. It is usually |
95 // right-aligned, but it can also be center-aligned if it is large. | 127 // right-aligned, but it can also be center-aligned if it is large. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); | 162 rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); |
131 | 163 |
132 // Finally, draw the text centered within the badge. We set a clip in case the | 164 // Finally, draw the text centered within the badge. We set a clip in case the |
133 // text was too large. | 165 // text was too large. |
134 rect.fLeft += kPadding; | 166 rect.fLeft += kPadding; |
135 rect.fRight -= kPadding; | 167 rect.fRight -= kPadding; |
136 canvas->clipRect(rect); | 168 canvas->clipRect(rect); |
137 canvas->drawText(text.c_str(), text.size(), | 169 canvas->drawText(text.c_str(), text.size(), |
138 rect.fLeft + (rect.width() - text_width) / 2, | 170 rect.fLeft + (rect.width() - text_width) / 2, |
139 rect.fTop + kTextSize + kTopTextPadding, | 171 rect.fTop + kTextSize + kTopTextPadding, |
140 text_paint); | 172 *text_paint); |
141 canvas->restore(); | 173 canvas->restore(); |
142 } | 174 } |
OLD | NEW |