| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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 "ui/aura_shell/app_list/drop_shadow_label.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 9 #include "ui/gfx/canvas_skia.h" |
| 10 #include "ui/gfx/color_utils.h" |
| 11 #include "ui/gfx/skbitmap_operations.h" |
| 12 |
| 13 using views::Label; |
| 14 |
| 15 namespace aura_shell { |
| 16 |
| 17 static const int kDefaultDropShadowSize = 2; |
| 18 |
| 19 DropShadowLabel::DropShadowLabel() : drop_shadow_size_(kDefaultDropShadowSize) { |
| 20 } |
| 21 |
| 22 void DropShadowLabel::SetDropShadowSize(int drop_shadow_size) { |
| 23 if (drop_shadow_size != drop_shadow_size_) { |
| 24 drop_shadow_size_ = drop_shadow_size; |
| 25 invalidate_text_size(); |
| 26 SchedulePaint(); |
| 27 } |
| 28 } |
| 29 |
| 30 void DropShadowLabel::PaintText(gfx::Canvas* canvas, |
| 31 const string16& text, |
| 32 const gfx::Rect& text_bounds, |
| 33 int flags) { |
| 34 SkColor text_color = enabled() ? enabled_color() : disabled_color(); |
| 35 if (drop_shadow_size_ > 0) { |
| 36 // To properly render shadow with elliding fade effect, text and shadow |
| 37 // is rendered to this canvas first with elliding disable so that underlying |
| 38 // code would not mix shadow color into text area because of elliding fade. |
| 39 // When that is done and if we need elliding fade, an alpha mask is applied |
| 40 // when transfering contents on this canvas to target canvas. |
| 41 gfx::Size canvas_size(text_bounds.width() + drop_shadow_size_, |
| 42 text_bounds.height() + drop_shadow_size_); |
| 43 gfx::CanvasSkia text_canvas(canvas_size.width(), canvas_size.height(), |
| 44 false); |
| 45 |
| 46 const float kShadowOpacity = 0.2; |
| 47 const SkColor shadow_color = |
| 48 SkColorSetA(SK_ColorBLACK, kShadowOpacity * SkColorGetA(text_color)); |
| 49 gfx::Size text_size = GetTextSize(); |
| 50 for (int i = 0; i < drop_shadow_size_; i++) { |
| 51 text_canvas.DrawStringInt(text, font(), shadow_color, i, 0, |
| 52 text_size.width(), text_size.height(), |
| 53 flags | gfx::Canvas::NO_ELLIPSIS); |
| 54 text_canvas.DrawStringInt(text, font(), shadow_color, i, i, |
| 55 text_size.width(), text_size.height(), |
| 56 flags | gfx::Canvas::NO_ELLIPSIS); |
| 57 text_canvas.DrawStringInt(text, font(), shadow_color, 0, i, |
| 58 text_size.width(), text_size.height(), |
| 59 flags | gfx::Canvas::NO_ELLIPSIS); |
| 60 } |
| 61 text_canvas.DrawStringInt(text, font(), text_color, 0, 0, |
| 62 text_size.width(), text_size.height(), |
| 63 flags | gfx::Canvas::NO_ELLIPSIS); |
| 64 |
| 65 const SkBitmap& text_bitmap = const_cast<SkBitmap&>( |
| 66 skia::GetTopDevice(*text_canvas.sk_canvas())->accessBitmap(false)); |
| 67 |
| 68 if (text_size.width() > text_bounds.width() && |
| 69 !(flags & gfx::Canvas::NO_ELLIPSIS)) { |
| 70 // Apply an gradient alpha mask for elliding fade effect. |
| 71 const double kFadeWidthFactor = 1.5; |
| 72 int fade_width = std::min(text_size.width() / 2, |
| 73 static_cast<int>(text_size.height() * kFadeWidthFactor)); |
| 74 |
| 75 const SkColor kColors[] = { SK_ColorWHITE, 0 }; |
| 76 const SkScalar kPoints[] = { SkIntToScalar(0), SkIntToScalar(1) }; |
| 77 SkPoint p[2]; |
| 78 p[0].set(SkIntToScalar(text_bounds.width() - fade_width), |
| 79 SkIntToScalar(0)); |
| 80 p[1].set(SkIntToScalar(text_bounds.width()), |
| 81 SkIntToScalar(0)); |
| 82 SkShader* s = SkGradientShader::CreateLinear( |
| 83 p, kColors, kPoints, 2, SkShader::kClamp_TileMode, NULL); |
| 84 |
| 85 SkPaint paint; |
| 86 paint.setShader(s)->unref(); |
| 87 |
| 88 gfx::CanvasSkia alpha_canvas(canvas_size.width(), canvas_size.height(), |
| 89 false); |
| 90 alpha_canvas.DrawRect(gfx::Rect(canvas_size), paint); |
| 91 |
| 92 const SkBitmap& alpha_bitmap = const_cast<SkBitmap&>( |
| 93 skia::GetTopDevice(*alpha_canvas.sk_canvas())->accessBitmap(false)); |
| 94 SkBitmap blended = SkBitmapOperations::CreateMaskedBitmap(text_bitmap, |
| 95 alpha_bitmap); |
| 96 canvas->DrawBitmapInt(blended, text_bounds.x(), text_bounds.y()); |
| 97 } else { |
| 98 canvas->DrawBitmapInt(text_bitmap, text_bounds.x(), text_bounds.y()); |
| 99 } |
| 100 } else { |
| 101 canvas->DrawStringInt(text, font(), text_color, text_bounds.x(), |
| 102 text_bounds.y(), text_bounds.width(), text_bounds.height(), flags); |
| 103 } |
| 104 |
| 105 if (HasFocus() || paint_as_focused()) { |
| 106 gfx::Rect focus_bounds = text_bounds; |
| 107 focus_bounds.Inset(-Label::kFocusBorderPadding, |
| 108 -Label::kFocusBorderPadding); |
| 109 canvas->DrawFocusRect(focus_bounds); |
| 110 } |
| 111 } |
| 112 |
| 113 gfx::Size DropShadowLabel::GetTextSize() const { |
| 114 gfx::Size text_size = Label::GetTextSize(); |
| 115 text_size.SetSize(text_size.width() + drop_shadow_size_, |
| 116 text_size.height() + drop_shadow_size_); |
| 117 return text_size; |
| 118 } |
| 119 |
| 120 } // namespace aura_shell |
| OLD | NEW |