| 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 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 button_ = new BrowserActionButton(browser_action, extension, panel); | 292 button_ = new BrowserActionButton(browser_action, extension, panel); |
| 293 AddChildView(button_); | 293 AddChildView(button_); |
| 294 } | 294 } |
| 295 | 295 |
| 296 void BrowserActionView::Layout() { | 296 void BrowserActionView::Layout() { |
| 297 button_->SetBounds(0, kControlVertOffset, width(), kButtonSize); | 297 button_->SetBounds(0, kControlVertOffset, width(), kButtonSize); |
| 298 } | 298 } |
| 299 | 299 |
| 300 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 300 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
| 301 View::PaintChildren(canvas); | 301 View::PaintChildren(canvas); |
| 302 | 302 button_->browser_action_state()->PaintBadge(canvas, |
| 303 const std::string& text = button_->browser_action_state()->badge_text(); | 303 gfx::Rect(width(), height())); |
| 304 if (text.empty()) | |
| 305 return; | |
| 306 | |
| 307 const int kTextSize = 8; | |
| 308 const int kBottomMargin = 5; | |
| 309 const int kPadding = 2; | |
| 310 const int kBadgeHeight = 11; | |
| 311 const int kMaxTextWidth = 23; | |
| 312 const int kCenterAlignThreshold = 20; // at than width, we center align | |
| 313 | |
| 314 canvas->save(); | |
| 315 | |
| 316 SkTypeface* typeface = SkTypeface::CreateFromName("Arial", SkTypeface::kBold); | |
| 317 SkPaint text_paint; | |
| 318 text_paint.setAntiAlias(true); | |
| 319 text_paint.setColor(SK_ColorWHITE); | |
| 320 text_paint.setFakeBoldText(true); | |
| 321 text_paint.setTextAlign(SkPaint::kLeft_Align); | |
| 322 text_paint.setTextSize(SkIntToScalar(kTextSize)); | |
| 323 text_paint.setTypeface(typeface); | |
| 324 | |
| 325 // Calculate text width. We clamp it to a max size. | |
| 326 SkScalar text_width = text_paint.measureText(text.c_str(), text.size()); | |
| 327 text_width = SkIntToScalar( | |
| 328 std::min(kMaxTextWidth, SkScalarFloor(text_width))); | |
| 329 | |
| 330 // Cacluate badge size. It is clamped to a min width just because it looks | |
| 331 // silly if it is too skinny. | |
| 332 int badge_width = SkScalarFloor(text_width) + kPadding * 2; | |
| 333 badge_width = std::max(kBadgeHeight, badge_width); | |
| 334 | |
| 335 // Paint the badge background color in the right location. It is usually | |
| 336 // right-aligned, but it can also be center-aligned if it is large. | |
| 337 SkRect rect; | |
| 338 rect.fBottom = SkIntToScalar(height() - kBottomMargin); | |
| 339 rect.fTop = rect.fBottom - SkIntToScalar(kBadgeHeight); | |
| 340 if (badge_width >= kCenterAlignThreshold) { | |
| 341 rect.fLeft = SkIntToScalar((width() - badge_width) / 2); | |
| 342 rect.fRight = rect.fLeft + SkIntToScalar(badge_width); | |
| 343 } else { | |
| 344 rect.fRight = SkIntToScalar(width()); | |
| 345 rect.fLeft = rect.fRight - badge_width; | |
| 346 } | |
| 347 | |
| 348 SkPaint rect_paint; | |
| 349 rect_paint.setStyle(SkPaint::kFill_Style); | |
| 350 rect_paint.setAntiAlias(true); | |
| 351 rect_paint.setColor( | |
| 352 button_->browser_action_state()->badge_background_color()); | |
| 353 canvas->drawRoundRect(rect, SkIntToScalar(2), SkIntToScalar(2), rect_paint); | |
| 354 | |
| 355 // Overlay the gradient. It is stretchy, so we do this in three parts. | |
| 356 ResourceBundle& resource_bundle = ResourceBundle::GetSharedInstance(); | |
| 357 SkBitmap* gradient_left = resource_bundle.GetBitmapNamed( | |
| 358 IDR_BROWSER_ACTION_BADGE_LEFT); | |
| 359 SkBitmap* gradient_right = resource_bundle.GetBitmapNamed( | |
| 360 IDR_BROWSER_ACTION_BADGE_RIGHT); | |
| 361 SkBitmap* gradient_center = resource_bundle.GetBitmapNamed( | |
| 362 IDR_BROWSER_ACTION_BADGE_CENTER); | |
| 363 | |
| 364 canvas->drawBitmap(*gradient_left, rect.fLeft, rect.fTop); | |
| 365 canvas->TileImageInt(*gradient_center, | |
| 366 SkScalarFloor(rect.fLeft) + gradient_left->width(), | |
| 367 SkScalarFloor(rect.fTop), | |
| 368 SkScalarFloor(rect.width()) - gradient_left->width() - | |
| 369 gradient_right->width(), | |
| 370 SkScalarFloor(rect.height())); | |
| 371 canvas->drawBitmap(*gradient_right, | |
| 372 rect.fRight - SkIntToScalar(gradient_right->width()), rect.fTop); | |
| 373 | |
| 374 // Finally, draw the text centered within the badge. We set a clip in case the | |
| 375 // text was too large. | |
| 376 rect.fLeft += kPadding; | |
| 377 rect.fRight -= kPadding; | |
| 378 canvas->clipRect(rect); | |
| 379 canvas->drawText(text.c_str(), text.size(), | |
| 380 rect.fLeft + (rect.width() - text_width) / 2, | |
| 381 rect.fTop + kTextSize + 1, | |
| 382 text_paint); | |
| 383 canvas->restore(); | |
| 384 } | 304 } |
| 385 | 305 |
| 386 | 306 |
| 387 //////////////////////////////////////////////////////////////////////////////// | 307 //////////////////////////////////////////////////////////////////////////////// |
| 388 // BrowserActionsContainer | 308 // BrowserActionsContainer |
| 389 | 309 |
| 390 BrowserActionsContainer::BrowserActionsContainer( | 310 BrowserActionsContainer::BrowserActionsContainer( |
| 391 Profile* profile, ToolbarView* toolbar) | 311 Profile* profile, ToolbarView* toolbar) |
| 392 : profile_(profile), | 312 : profile_(profile), |
| 393 toolbar_(toolbar), | 313 toolbar_(toolbar), |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 std::min(static_cast<int>(browser_action_views_.size()), | 497 std::min(static_cast<int>(browser_action_views_.size()), |
| 578 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; | 498 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; |
| 579 | 499 |
| 580 // Even if available_width is <= 0, we still return at least the |min_width|. | 500 // Even if available_width is <= 0, we still return at least the |min_width|. |
| 581 if (available_width <= 0) | 501 if (available_width <= 0) |
| 582 return min_width; | 502 return min_width; |
| 583 | 503 |
| 584 return std::max(min_width, available_width - available_width % kButtonSize + | 504 return std::max(min_width, available_width - available_width % kButtonSize + |
| 585 kHorizontalPadding * 2); | 505 kHorizontalPadding * 2); |
| 586 } | 506 } |
| OLD | NEW |