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, false); | |
44 | |
45 const double kShadowOpacity = 0.2; | |
46 const SkColor shadow_color = | |
47 SkColorSetA(SK_ColorBLACK, kShadowOpacity * SkColorGetA(text_color)); | |
48 gfx::Size text_size = GetTextSize(); | |
49 for (int i = 0; i < drop_shadow_size_; i++) { | |
50 text_canvas.DrawStringInt(text, font(), shadow_color, i, 0, | |
51 text_size.width(), text_size.height(), | |
52 flags | gfx::Canvas::NO_ELLIPSIS); | |
53 text_canvas.DrawStringInt(text, font(), shadow_color, i, i, | |
54 text_size.width(), text_size.height(), | |
55 flags | gfx::Canvas::NO_ELLIPSIS); | |
56 text_canvas.DrawStringInt(text, font(), shadow_color, 0, i, | |
57 text_size.width(), text_size.height(), | |
58 flags | gfx::Canvas::NO_ELLIPSIS); | |
59 } | |
60 text_canvas.DrawStringInt(text, font(), text_color, 0, 0, | |
61 text_size.width(), text_size.height(), | |
62 flags | gfx::Canvas::NO_ELLIPSIS); | |
63 | |
64 const SkBitmap& text_bitmap = const_cast<SkBitmap&>( | |
65 skia::GetTopDevice(*text_canvas.sk_canvas())->accessBitmap(false)); | |
66 | |
67 if (text_size.width() > text_bounds.width() && | |
68 !(flags & gfx::Canvas::NO_ELLIPSIS)) { | |
69 // Apply an gradient alpha mask for elliding fade effect. | |
70 const double kFadeWidthFactor = 1.5; | |
71 int fade_width = std::min(text_size.width() / 2, | |
72 static_cast<int>(text_size.height() * kFadeWidthFactor)); | |
73 | |
74 const SkColor kColors[] = { SK_ColorWHITE, 0 }; | |
75 const SkScalar kPoints[] = { SkIntToScalar(0), SkIntToScalar(1) }; | |
76 SkPoint p[2]; | |
77 p[0].set(SkIntToScalar(text_bounds.width() - fade_width), | |
78 SkIntToScalar(0)); | |
79 p[1].set(SkIntToScalar(text_bounds.width()), | |
80 SkIntToScalar(0)); | |
81 SkShader* s = SkGradientShader::CreateLinear( | |
82 p, kColors, kPoints, 2, SkShader::kClamp_TileMode, NULL); | |
83 | |
84 SkPaint paint; | |
85 paint.setShader(s)->unref(); | |
86 | |
87 gfx::CanvasSkia alpha_canvas(canvas_size, false); | |
88 alpha_canvas.DrawRect(gfx::Rect(canvas_size), paint); | |
89 | |
90 const SkBitmap& alpha_bitmap = const_cast<SkBitmap&>( | |
91 skia::GetTopDevice(*alpha_canvas.sk_canvas())->accessBitmap(false)); | |
92 SkBitmap blended = SkBitmapOperations::CreateMaskedBitmap(text_bitmap, | |
93 alpha_bitmap); | |
94 canvas->DrawBitmapInt(blended, text_bounds.x(), text_bounds.y()); | |
95 } else { | |
96 canvas->DrawBitmapInt(text_bitmap, text_bounds.x(), text_bounds.y()); | |
97 } | |
98 } else { | |
99 canvas->DrawStringInt(text, font(), text_color, text_bounds.x(), | |
100 text_bounds.y(), text_bounds.width(), text_bounds.height(), flags); | |
101 } | |
102 | |
103 if (HasFocus() || paint_as_focused()) { | |
104 gfx::Rect focus_bounds = text_bounds; | |
105 focus_bounds.Inset(-Label::kFocusBorderPadding, | |
106 -Label::kFocusBorderPadding); | |
107 canvas->DrawFocusRect(focus_bounds); | |
108 } | |
109 } | |
110 | |
111 gfx::Size DropShadowLabel::GetTextSize() const { | |
112 gfx::Size text_size = Label::GetTextSize(); | |
113 text_size.SetSize(text_size.width() + drop_shadow_size_, | |
114 text_size.height() + drop_shadow_size_); | |
115 return text_size; | |
116 } | |
117 | |
118 } // namespace aura_shell | |
OLD | NEW |