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/browser/views/browser_actions_container.h" | 5 #include "chrome/browser/views/browser_actions_container.h" |
6 | 6 |
7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 button_ = new BrowserActionButton(browser_action, extension, panel); | 196 button_ = new BrowserActionButton(browser_action, extension, panel); |
197 AddChildView(button_); | 197 AddChildView(button_); |
198 } | 198 } |
199 | 199 |
200 void BrowserActionView::Layout() { | 200 void BrowserActionView::Layout() { |
201 button_->SetBounds(0, kControlVertOffset, width(), kButtonSize); | 201 button_->SetBounds(0, kControlVertOffset, width(), kButtonSize); |
202 } | 202 } |
203 | 203 |
204 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 204 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
205 View::PaintChildren(canvas); | 205 View::PaintChildren(canvas); |
206 button_->browser_action_state()->PaintBadge(canvas, | 206 |
207 gfx::Rect(width(), height())); | 207 const std::string& text = button_->browser_action_state()->badge_text(); |
| 208 if (text.empty()) |
| 209 return; |
| 210 |
| 211 const int kTextSize = 8; |
| 212 const int kBottomMargin = 5; |
| 213 const int kPadding = 2; |
| 214 const int kBadgeHeight = 11; |
| 215 const int kMaxTextWidth = 23; |
| 216 const int kCenterAlignThreshold = 20; // at than width, we center align |
| 217 |
| 218 canvas->save(); |
| 219 |
| 220 SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); |
| 221 SkPaint text_paint; |
| 222 text_paint.setAntiAlias(true); |
| 223 text_paint.setColor(SK_ColorWHITE); |
| 224 text_paint.setFakeBoldText(true); |
| 225 text_paint.setTextAlign(SkPaint::kLeft_Align); |
| 226 text_paint.setTextSize(SkIntToScalar(kTextSize)); |
| 227 text_paint.setTypeface(typeface); |
| 228 |
| 229 // Calculate text width. We clamp it to a max size. |
| 230 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); |
| 231 text_width = SkIntToScalar( |
| 232 std::min(kMaxTextWidth, SkScalarFloor(text_width))); |
| 233 |
| 234 // Cacluate badge size. It is clamped to a min width just because it looks |
| 235 // silly if it is too skinny. |
| 236 int badge_width = SkScalarFloor(text_width) + kPadding * 2; |
| 237 badge_width = std::max(kBadgeHeight, badge_width); |
| 238 |
| 239 // Paint the badge background color in the right location. It is usually |
| 240 // right-aligned, but it can also be center-aligned if it is large. |
| 241 SkRect rect; |
| 242 rect.fBottom = SkIntToScalar(height() - kBottomMargin); |
| 243 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); |
| 244 if (badge_width >= kCenterAlignThreshold) { |
| 245 rect.fLeft = SkIntToScalar((width() - badge_width) / 2); |
| 246 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); |
| 247 } else { |
| 248 rect.fRight = SkIntToScalar(width()); |
| 249 rect.fLeft = rect.fRight - badge_width; |
| 250 } |
| 251 |
| 252 SkPaint rect_paint; |
| 253 rect_paint.setStyle(SkPaint::kFill_Style); |
| 254 rect_paint.setAntiAlias(true); |
| 255 rect_paint.setColor( |
| 256 button_->browser_action_state()->badge_background_color()); |
| 257 canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); |
| 258 |
| 259 // Overlay the gradient. It is stretchy, so we do this in three parts. |
| 260 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); |
| 261 SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( |
| 262 IDR_BROWSER_ACTION_BADGE_LEFT); |
| 263 SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( |
| 264 IDR_BROWSER_ACTION_BADGE_RIGHT); |
| 265 SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( |
| 266 IDR_BROWSER_ACTION_BADGE_CENTER); |
| 267 |
| 268 canvas->drawBitmap(*gradient_left, rect.fLeft, rect.fTop); |
| 269 canvas->TileImageInt(*gradient_center, |
| 270 SkScalarFloor(rect.fLeft) + gradient_left->width(), |
| 271 SkScalarFloor(rect.fTop), |
| 272 SkScalarFloor(rect.width()) - gradient_left->width() - |
| 273 gradient_right->width(), |
| 274 SkScalarFloor(rect.height())); |
| 275 canvas->drawBitmap(*gradient_right, |
| 276 rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); |
| 277 |
| 278 // Finally, draw the text centered within the badge. We set a clip in case the |
| 279 // text was too large. |
| 280 rect.fLeft += kPadding; |
| 281 rect.fRight -= kPadding; |
| 282 canvas->clipRect(rect); |
| 283 canvas->drawText(text.c_str(), text.size(), |
| 284 rect.fLeft + (rect.width() - text_width) / 2, |
| 285 rect.fTop + kTextSize + 1, |
| 286 text_paint); |
| 287 canvas->restore(); |
208 } | 288 } |
209 | 289 |
210 | 290 |
211 //////////////////////////////////////////////////////////////////////////////// | 291 //////////////////////////////////////////////////////////////////////////////// |
212 // BrowserActionsContainer | 292 // BrowserActionsContainer |
213 | 293 |
214 BrowserActionsContainer::BrowserActionsContainer( | 294 BrowserActionsContainer::BrowserActionsContainer( |
215 Profile* profile, ToolbarView* toolbar) | 295 Profile* profile, ToolbarView* toolbar) |
216 : profile_(profile), | 296 : profile_(profile), |
217 toolbar_(toolbar), | 297 toolbar_(toolbar), |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 std::min(static_cast<int>(browser_action_views_.size()), | 481 std::min(static_cast<int>(browser_action_views_.size()), |
402 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; | 482 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; |
403 | 483 |
404 // Even if available_width is <= 0, we still return at least the |min_width|. | 484 // Even if available_width is <= 0, we still return at least the |min_width|. |
405 if (available_width <= 0) | 485 if (available_width <= 0) |
406 return min_width; | 486 return min_width; |
407 | 487 |
408 return std::max(min_width, available_width - available_width % kButtonSize + | 488 return std::max(min_width, available_width - available_width % kButtonSize + |
409 kHorizontalPadding * 2); | 489 kHorizontalPadding * 2); |
410 } | 490 } |
OLD | NEW |