| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/badge_util.h" | 5 #include "chrome/common/badge_util.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "third_party/skia/include/core/SkPaint.h" | 11 #include "third_party/skia/include/core/SkPaint.h" |
| 12 #include "third_party/skia/include/core/SkTypeface.h" | 12 #include "third_party/skia/include/core/SkTypeface.h" |
| 13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/font.h" | 15 #include "ui/gfx/font.h" |
| 16 #include "ui/gfx/geometry/rect.h" | 16 #include "ui/gfx/geometry/rect.h" |
| 17 #include "ui/gfx/geometry/size.h" | 17 #include "ui/gfx/geometry/size.h" |
| 18 #include "ui/resources/grit/ui_resources.h" | 18 #include "ui/resources/grit/ui_resources.h" |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 // Different platforms need slightly different constants to look good. | 22 // Different platforms need slightly different constants to look good. |
| 23 // TODO(devlin): Comb through these and see if they are all still needed/ |
| 24 // appropriate. |
| 23 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
| 24 const float kTextSize = 10; | 26 const float kTextSize = 10; |
| 25 const int kBottomMarginBrowserAction = 0; | |
| 26 const int kBottomMarginPageAction = 2; | |
| 27 const int kPadding = 2; | |
| 28 // The padding between the top of the badge and the top of the text. | 27 // The padding between the top of the badge and the top of the text. |
| 29 const int kTopTextPadding = -1; | 28 const int kTopTextPadding = -1; |
| 30 #elif defined(OS_MACOSX) | 29 #elif defined(OS_MACOSX) |
| 31 const float kTextSize = 9.0; | 30 const float kTextSize = 9.0; |
| 32 const int kBottomMarginBrowserAction = 5; | |
| 33 const int kBottomMarginPageAction = 2; | |
| 34 const int kPadding = 2; | |
| 35 const int kTopTextPadding = 0; | 31 const int kTopTextPadding = 0; |
| 36 #elif defined(OS_CHROMEOS) | 32 #elif defined(OS_CHROMEOS) |
| 37 const float kTextSize = 8.0; | 33 const float kTextSize = 8.0; |
| 38 const int kBottomMarginBrowserAction = 0; | |
| 39 const int kBottomMarginPageAction = 2; | |
| 40 const int kPadding = 2; | |
| 41 const int kTopTextPadding = 1; | 34 const int kTopTextPadding = 1; |
| 42 #elif defined(OS_POSIX) | 35 #elif defined(OS_POSIX) |
| 43 const float kTextSize = 9.0; | 36 const float kTextSize = 9.0; |
| 44 const int kBottomMarginBrowserAction = 0; | |
| 45 const int kBottomMarginPageAction = 2; | |
| 46 const int kPadding = 2; | |
| 47 const int kTopTextPadding = 0; | 37 const int kTopTextPadding = 0; |
| 48 #endif | 38 #endif |
| 49 | 39 |
| 40 const int kPadding = 2; |
| 50 const int kBadgeHeight = 11; | 41 const int kBadgeHeight = 11; |
| 51 const int kMaxTextWidth = 23; | 42 const int kMaxTextWidth = 23; |
| 52 | 43 |
| 53 // The minimum width for center-aligning the badge. | 44 // The minimum width for center-aligning the badge. |
| 54 const int kCenterAlignThreshold = 20; | 45 const int kCenterAlignThreshold = 20; |
| 55 | 46 |
| 56 } // namespace | 47 } // namespace |
| 57 | 48 |
| 58 namespace badge_util { | 49 namespace badge_util { |
| 59 | 50 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 // |text_paint| adds its own ref. Release the ref from CreateFontName. | 83 // |text_paint| adds its own ref. Release the ref from CreateFontName. |
| 93 } | 84 } |
| 94 return text_paint; | 85 return text_paint; |
| 95 } | 86 } |
| 96 | 87 |
| 97 void PaintBadge(gfx::Canvas* canvas, | 88 void PaintBadge(gfx::Canvas* canvas, |
| 98 const gfx::Rect& bounds, | 89 const gfx::Rect& bounds, |
| 99 const std::string& text, | 90 const std::string& text, |
| 100 const SkColor& text_color_in, | 91 const SkColor& text_color_in, |
| 101 const SkColor& background_color_in, | 92 const SkColor& background_color_in, |
| 102 int icon_width, | 93 int icon_width) { |
| 103 extensions::ActionInfo::Type action_type) { | |
| 104 if (text.empty()) | 94 if (text.empty()) |
| 105 return; | 95 return; |
| 106 | 96 |
| 107 SkColor text_color = text_color_in; | 97 SkColor text_color = text_color_in; |
| 108 if (SkColorGetA(text_color_in) == 0x00) | 98 if (SkColorGetA(text_color_in) == 0x00) |
| 109 text_color = SK_ColorWHITE; | 99 text_color = SK_ColorWHITE; |
| 110 | 100 |
| 111 SkColor background_color = background_color_in; | 101 SkColor background_color = background_color_in; |
| 112 if (SkColorGetA(background_color_in) == 0x00) | 102 if (SkColorGetA(background_color_in) == 0x00) |
| 113 background_color = SkColorSetARGB(255, 218, 0, 24); | 103 background_color = SkColorSetARGB(255, 218, 0, 24); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 139 int badge_width = text_width + kPadding * 2; | 129 int badge_width = text_width + kPadding * 2; |
| 140 // Force the pixel width of badge to be either odd (if the icon width is odd) | 130 // Force the pixel width of badge to be either odd (if the icon width is odd) |
| 141 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. | 131 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. |
| 142 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) | 132 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) |
| 143 badge_width += 1; | 133 badge_width += 1; |
| 144 badge_width = std::max(kBadgeHeight, badge_width); | 134 badge_width = std::max(kBadgeHeight, badge_width); |
| 145 | 135 |
| 146 // Paint the badge background color in the right location. It is usually | 136 // Paint the badge background color in the right location. It is usually |
| 147 // right-aligned, but it can also be center-aligned if it is large. | 137 // right-aligned, but it can also be center-aligned if it is large. |
| 148 int rect_height = kBadgeHeight; | 138 int rect_height = kBadgeHeight; |
| 149 int bottom_margin = | 139 int rect_y = bounds.bottom() - kBadgeHeight; |
| 150 action_type == extensions::ActionInfo::TYPE_BROWSER ? | |
| 151 kBottomMarginBrowserAction : kBottomMarginPageAction; | |
| 152 int rect_y = bounds.bottom() - bottom_margin - kBadgeHeight; | |
| 153 int rect_width = badge_width; | 140 int rect_width = badge_width; |
| 154 int rect_x = (badge_width >= kCenterAlignThreshold) ? | 141 int rect_x = (badge_width >= kCenterAlignThreshold) ? |
| 155 bounds.x() + (bounds.width() - badge_width) / 2 : | 142 bounds.x() + (bounds.width() - badge_width) / 2 : |
| 156 bounds.right() - badge_width; | 143 bounds.right() - badge_width; |
| 157 gfx::Rect rect(rect_x, rect_y, rect_width, rect_height); | 144 gfx::Rect rect(rect_x, rect_y, rect_width, rect_height); |
| 158 | 145 |
| 159 SkPaint rect_paint; | 146 SkPaint rect_paint; |
| 160 rect_paint.setStyle(SkPaint::kFill_Style); | 147 rect_paint.setStyle(SkPaint::kFill_Style); |
| 161 rect_paint.setAntiAlias(true); | 148 rect_paint.setAntiAlias(true); |
| 162 rect_paint.setColor(background_color); | 149 rect_paint.setColor(background_color); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 187 canvas->sk_canvas()->drawText( | 174 canvas->sk_canvas()->drawText( |
| 188 text.c_str(), text.size(), | 175 text.c_str(), text.size(), |
| 189 SkFloatToScalar(rect.x() + | 176 SkFloatToScalar(rect.x() + |
| 190 static_cast<float>(rect.width() - text_width) / 2), | 177 static_cast<float>(rect.width() - text_width) / 2), |
| 191 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), | 178 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), |
| 192 *text_paint); | 179 *text_paint); |
| 193 canvas->Restore(); | 180 canvas->Restore(); |
| 194 } | 181 } |
| 195 | 182 |
| 196 } // namespace badge_util | 183 } // namespace badge_util |
| OLD | NEW |