OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/extensions/extension_action2.h" | |
6 | |
7 #include "app/gfx/canvas.h" | |
8 #include "app/resource_bundle.h" | |
9 #include "base/gfx/rect.h" | |
10 #include "chrome/app/chrome_dll_resource.h" | |
11 #include "grit/app_resources.h" | |
12 #include "third_party/skia/include/core/SkBitmap.h" | |
13 #include "third_party/skia/include/core/SkTypeface.h" | |
14 #include "third_party/skia/include/effects/SkGradientShader.h" | |
15 | |
16 const int ExtensionAction2::kDefaultTabId = -1; | |
17 | |
18 void ExtensionAction2::ClearAllValuesForTab(int tab_id) { | |
19 title_.erase(tab_id); | |
20 icon_.erase(tab_id); | |
21 icon_index_.erase(tab_id); | |
22 badge_text_.erase(tab_id); | |
23 badge_text_color_.erase(tab_id); | |
24 badge_background_color_.erase(tab_id); | |
25 visible_.erase(tab_id); | |
26 } | |
27 | |
28 void ExtensionAction2::PaintBadge(gfx::Canvas* canvas, | |
29 const gfx::Rect& bounds, | |
30 int tab_id) { | |
31 std::string text = GetBadgeText(tab_id); | |
32 if (text.empty()) | |
33 return; | |
34 | |
35 SkColor text_color = GetBadgeTextColor(tab_id); | |
36 SkColor background_color = GetBadgeBackgroundColor(tab_id); | |
37 | |
38 if (SkColorGetA(text_color) == 0x00) | |
39 text_color = SK_ColorWHITE; | |
40 | |
41 if (SkColorGetA(background_color) == 0x00) | |
42 background_color = SkColorSetARGB(255, 218, 0, 24); // default badge color | |
43 | |
44 // Different platforms need slightly different constants to look good. | |
45 #if defined(OS_LINUX) | |
46 const int kTextSize = 9; | |
47 const int kBottomMargin = 4; | |
48 const int kPadding = 2; | |
49 const int kBadgeHeight = 12; | |
50 const int kMaxTextWidth = 23; | |
51 // The minimum width for center-aligning the badge. | |
52 const int kCenterAlignThreshold = 20; | |
53 #else | |
54 const int kTextSize = 8; | |
55 const int kBottomMargin = 5; | |
56 const int kPadding = 2; | |
57 const int kBadgeHeight = 11; | |
58 const int kMaxTextWidth = 23; | |
59 // The minimum width for center-aligning the badge. | |
60 const int kCenterAlignThreshold = 20; | |
61 #endif | |
62 | |
63 canvas->save(); | |
64 | |
65 SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); | |
66 SkPaint text_paint; | |
67 text_paint.setAntiAlias(true); | |
68 text_paint.setColor(text_color); | |
69 text_paint.setFakeBoldText(true); | |
70 text_paint.setTextAlign(SkPaint::kLeft_Align); | |
71 text_paint.setTextSize(SkIntToScalar(kTextSize)); | |
72 text_paint.setTypeface(typeface); | |
73 | |
74 // Calculate text width. We clamp it to a max size. | |
75 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); | |
76 text_width = SkIntToScalar( | |
77 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | |
78 | |
79 // Cacluate badge size. It is clamped to a min width just because it looks | |
80 // silly if it is too skinny. | |
81 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | |
82 badge_width = std::max(kBadgeHeight, badge_width); | |
83 | |
84 // Paint the badge background color in the right location. It is usually | |
85 // right-aligned, but it can also be center-aligned if it is large. | |
86 SkRect rect; | |
87 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); | |
88 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); | |
89 if (badge_width >= kCenterAlignThreshold) { | |
90 rect.fLeft = SkIntToScalar((bounds.right() - badge_width) / 2); | |
91 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); | |
92 } else { | |
93 rect.fRight = SkIntToScalar(bounds.right()); | |
94 rect.fLeft = rect.fRight - badge_width; | |
95 } | |
96 | |
97 SkPaint rect_paint; | |
98 rect_paint.setStyle(SkPaint::kFill_Style); | |
99 rect_paint.setAntiAlias(true); | |
100 rect_paint.setColor(background_color); | |
101 canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); | |
102 | |
103 // Overlay the gradient. It is stretchy, so we do this in three parts. | |
104 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); | |
105 SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( | |
106 IDR_BROWSER_ACTION_BADGE_LEFT); | |
107 SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( | |
108 IDR_BROWSER_ACTION_BADGE_RIGHT); | |
109 SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( | |
110 IDR_BROWSER_ACTION_BADGE_CENTER); | |
111 | |
112 canvas->drawBitmap(*gradient_left, rect.fLeft, rect.fTop); | |
113 canvas->TileImageInt(*gradient_center, | |
114 SkScalarFloor(rect.fLeft) + gradient_left->width(), | |
115 SkScalarFloor(rect.fTop), | |
116 SkScalarFloor(rect.width()) - gradient_left->width() - | |
117 gradient_right->width(), | |
118 SkScalarFloor(rect.height())); | |
119 canvas->drawBitmap(*gradient_right, | |
120 rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); | |
121 | |
122 // Finally, draw the text centered within the badge. We set a clip in case the | |
123 // text was too large. | |
124 rect.fLeft += kPadding; | |
125 rect.fRight -= kPadding; | |
126 canvas->clipRect(rect); | |
127 canvas->drawText(text.c_str(), text.size(), | |
128 rect.fLeft + (rect.width() - text_width) / 2, | |
129 rect.fTop + kTextSize + 1, | |
130 text_paint); | |
131 canvas->restore(); | |
132 } | |
OLD | NEW |