| 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/gfx/font.h" |
| 9 #include "app/resource_bundle.h" | 9 #include "app/resource_bundle.h" |
| 10 #include "base/gfx/rect.h" | 10 #include "base/gfx/rect.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 if (text.empty()) | 99 if (text.empty()) |
| 100 return; | 100 return; |
| 101 | 101 |
| 102 SkColor text_color = GetBadgeTextColor(tab_id); | 102 SkColor text_color = GetBadgeTextColor(tab_id); |
| 103 SkColor background_color = GetBadgeBackgroundColor(tab_id); | 103 SkColor background_color = GetBadgeBackgroundColor(tab_id); |
| 104 | 104 |
| 105 if (SkColorGetA(text_color) == 0x00) | 105 if (SkColorGetA(text_color) == 0x00) |
| 106 text_color = SK_ColorWHITE; | 106 text_color = SK_ColorWHITE; |
| 107 | 107 |
| 108 if (SkColorGetA(background_color) == 0x00) | 108 if (SkColorGetA(background_color) == 0x00) |
| 109 background_color = SkColorSetARGB(255, 218, 0, 24); // default badge color | 109 background_color = SkColorSetARGB(255, 218, 0, 24); // Default badge color. |
| 110 | 110 |
| 111 canvas->save(); | 111 canvas->save(); |
| 112 | 112 |
| 113 SkPaint* text_paint = GetTextPaint(); | 113 SkPaint* text_paint = GetTextPaint(); |
| 114 text_paint->setColor(text_color); | 114 text_paint->setColor(text_color); |
| 115 | 115 |
| 116 // Calculate text width. We clamp it to a max size. | 116 // Calculate text width. We clamp it to a max size. |
| 117 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); | 117 SkScalar text_width = text_paint->measureText(text.c_str(), text.size()); |
| 118 text_width = SkIntToScalar( | 118 text_width = SkIntToScalar( |
| 119 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | 119 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
| 120 | 120 |
| 121 // Cacluate badge size. It is clamped to a min width just because it looks | 121 // Calculate badge size. It is clamped to a min width just because it looks |
| 122 // silly if it is too skinny. | 122 // silly if it is too skinny. |
| 123 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | 123 int badge_width = SkScalarFloor(text_width) + kPadding * 2; |
| 124 int icon_width = GetIcon(tab_id).width(); |
| 125 // Force the pixel width of badge to be either odd (if the icon width is odd) |
| 126 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. |
| 127 if (icon_width != 0 && (badge_width % 2 != GetIcon(tab_id).width() % 2)) |
| 128 badge_width += 1; |
| 124 badge_width = std::max(kBadgeHeight, badge_width); | 129 badge_width = std::max(kBadgeHeight, badge_width); |
| 125 | 130 |
| 126 // Paint the badge background color in the right location. It is usually | 131 // Paint the badge background color in the right location. It is usually |
| 127 // right-aligned, but it can also be center-aligned if it is large. | 132 // right-aligned, but it can also be center-aligned if it is large. |
| 128 SkRect rect; | 133 SkRect rect; |
| 129 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); | 134 rect.fBottom = SkIntToScalar(bounds.bottom() - kBottomMargin); |
| 130 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); | 135 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); |
| 131 if (badge_width >= kCenterAlignThreshold) { | 136 if (badge_width >= kCenterAlignThreshold) { |
| 132 rect.fLeft = SkIntToScalar(bounds.CenterPoint().x() - badge_width / 2); | 137 rect.fLeft = SkIntToScalar( |
| 138 SkScalarFloor(SkIntToScalar(bounds.width() / 2.0) - |
| 139 SkIntToScalar(badge_width / 2.0))); |
| 133 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); | 140 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); |
| 134 } else { | 141 } else { |
| 135 rect.fRight = SkIntToScalar(bounds.right()); | 142 rect.fRight = SkIntToScalar(bounds.right()); |
| 136 rect.fLeft = rect.fRight - badge_width; | 143 rect.fLeft = rect.fRight - badge_width; |
| 137 } | 144 } |
| 138 | 145 |
| 139 SkPaint rect_paint; | 146 SkPaint rect_paint; |
| 140 rect_paint.setStyle(SkPaint::kFill_Style); | 147 rect_paint.setStyle(SkPaint::kFill_Style); |
| 141 rect_paint.setAntiAlias(true); | 148 rect_paint.setAntiAlias(true); |
| 142 rect_paint.setColor(background_color); | 149 rect_paint.setColor(background_color); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 165 // text was too large. | 172 // text was too large. |
| 166 rect.fLeft += kPadding; | 173 rect.fLeft += kPadding; |
| 167 rect.fRight -= kPadding; | 174 rect.fRight -= kPadding; |
| 168 canvas->clipRect(rect); | 175 canvas->clipRect(rect); |
| 169 canvas->drawText(text.c_str(), text.size(), | 176 canvas->drawText(text.c_str(), text.size(), |
| 170 rect.fLeft + (rect.width() - text_width) / 2, | 177 rect.fLeft + (rect.width() - text_width) / 2, |
| 171 rect.fTop + kTextSize + kTopTextPadding, | 178 rect.fTop + kTextSize + kTopTextPadding, |
| 172 *text_paint); | 179 *text_paint); |
| 173 canvas->restore(); | 180 canvas->restore(); |
| 174 } | 181 } |
| OLD | NEW |