OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/chromeos/login/username_view.h" | 5 #include "chrome/browser/chromeos/login/username_view.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/chromeos/login/rounded_view.h" |
9 #include "gfx/canvas.h" | 10 #include "gfx/canvas.h" |
10 #include "gfx/canvas_skia.h" | 11 #include "gfx/canvas_skia.h" |
11 #include "gfx/rect.h" | 12 #include "gfx/rect.h" |
| 13 #include "third_party/skia/include/core/SkColorShader.h" |
12 #include "third_party/skia/include/core/SkComposeShader.h" | 14 #include "third_party/skia/include/core/SkComposeShader.h" |
13 #include "third_party/skia/include/effects/SkGradientShader.h" | 15 #include "third_party/skia/include/effects/SkGradientShader.h" |
14 | 16 |
| 17 namespace chromeos { |
| 18 |
15 namespace { | 19 namespace { |
16 // Username label background color. | 20 // Username label background color. |
17 const SkColor kLabelBackgoundColor = 0x55000000; | 21 const SkColor kLabelBackgoundColor = 0x55000000; |
18 // Holds margin to height ratio. | 22 // Holds margin to height ratio. |
19 const double kMarginRatio = 1.0 / 3.0; | 23 const double kMarginRatio = 1.0 / 3.0; |
| 24 |
| 25 // Class that sets up half rounded rectangle (only the bottom corners are |
| 26 // rounded) as a clip region of the view. |
| 27 // For more info see the file "chrome/browser/chromeos/login/rounded_view.h". |
| 28 template<typename C> |
| 29 class HalfRoundedView : public RoundedView<C> { |
| 30 public: |
| 31 explicit HalfRoundedView(const std::wstring &text): RoundedView<C>(text) { |
| 32 } |
| 33 |
| 34 protected: |
| 35 // Overrides ViewFilter. |
| 36 virtual void DrawFrame(gfx::Canvas* canvas) { |
| 37 // No frame is needed. |
| 38 } |
| 39 |
| 40 virtual SkRect GetViewRect() const { |
| 41 gfx::Rect bounds = C::GetLocalBounds(false); |
| 42 SkRect view_rect; |
| 43 // The rectangle will be intersected with the bounds, so the correct half |
| 44 // of the round rectangle will be obtained. |
| 45 view_rect.iset(bounds.x(), bounds.y() - bounds.width(), |
| 46 bounds.x() + bounds.width(), |
| 47 bounds.y() + bounds.height()); |
| 48 return view_rect; |
| 49 } |
| 50 }; |
| 51 |
20 } // namespace | 52 } // namespace |
21 | |
22 namespace chromeos { | |
23 | |
24 UsernameView::UsernameView(const std::wstring& username) | 53 UsernameView::UsernameView(const std::wstring& username) |
25 : views::Label(username) { | 54 : views::Label(username) { |
26 } | 55 } |
27 | 56 |
28 void UsernameView::Paint(gfx::Canvas* canvas) { | 57 void UsernameView::Paint(gfx::Canvas* canvas) { |
29 gfx::Rect bounds = GetLocalBounds(false); | 58 gfx::Rect bounds = GetLocalBounds(false); |
30 if (!text_image_.get()) | 59 if (!text_image_.get()) |
31 PaintUsername(bounds); | 60 PaintUsername(bounds); |
32 | 61 |
33 DCHECK(bounds.size() == | 62 DCHECK(bounds.size() == |
34 gfx::Size(text_image_->width(), text_image_->height())); | 63 gfx::Size(text_image_->width(), text_image_->height())); |
35 | 64 |
| 65 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y()); |
| 66 } |
| 67 |
| 68 void UsernameView::PaintUsername(const gfx::Rect& bounds) { |
| 69 margin_width_ = bounds.height() * kMarginRatio; |
| 70 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); |
| 71 // Draw transparent background. |
| 72 canvas.drawColor(0); |
| 73 |
| 74 // Calculate needed space. |
| 75 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | |
| 76 gfx::Canvas::TEXT_VALIGN_MIDDLE | |
| 77 gfx::Canvas::NO_ELLIPSIS; |
| 78 int text_height, text_width; |
| 79 gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(GetText()), font(), |
| 80 &text_width, &text_height, |
| 81 flags); |
| 82 text_width += margin_width_; |
| 83 |
| 84 // Also leave the right margin. |
| 85 bool use_fading_for_text = text_width + margin_width_ >= bounds.width(); |
| 86 |
36 // Only alpha channel counts. | 87 // Only alpha channel counts. |
37 SkColor gradient_colors[2]; | 88 SkColor gradient_colors[2]; |
38 gradient_colors[0] = 0xFFFFFFFF; | 89 gradient_colors[0] = 0xFFFFFFFF; |
39 gradient_colors[1] = 0x00FFFFFF; | 90 gradient_colors[1] = 0x00FFFFFF; |
40 | 91 |
41 int gradient_start = std::min(margin_width_ + text_width_, | 92 int gradient_start = use_fading_for_text ? |
42 bounds.width() - bounds.height()); | 93 bounds.width() - bounds.height() - margin_width_ : |
| 94 text_width; |
| 95 int gradient_end = std::min(gradient_start + bounds.height(), |
| 96 bounds.width() - margin_width_); |
43 | 97 |
44 SkPoint gradient_borders[2]; | 98 SkPoint gradient_borders[2]; |
45 gradient_borders[0].set(SkIntToScalar(gradient_start), SkIntToScalar(0)); | 99 gradient_borders[0].set(SkIntToScalar(gradient_start), SkIntToScalar(0)); |
46 gradient_borders[1].set(SkIntToScalar( | 100 gradient_borders[1].set(SkIntToScalar(gradient_end), SkIntToScalar(0)); |
47 gradient_start + bounds.height()), SkIntToScalar(0)); | |
48 | 101 |
49 SkShader* gradient_shader = | 102 SkShader* gradient_shader = |
50 SkGradientShader::CreateLinear(gradient_borders, gradient_colors, NULL, 2, | 103 SkGradientShader::CreateLinear(gradient_borders, gradient_colors, NULL, 2, |
51 SkShader::kClamp_TileMode, NULL); | 104 SkShader::kClamp_TileMode, NULL); |
52 SkShader* image_shader = SkShader::CreateBitmapShader( | |
53 *text_image_, | |
54 SkShader::kRepeat_TileMode, | |
55 SkShader::kRepeat_TileMode); | |
56 | 105 |
57 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode); | 106 if (!use_fading_for_text) { |
58 SkShader* composite_shader = new SkComposeShader(gradient_shader, | 107 // Draw the final background with the fading in the end. |
59 image_shader, mode); | 108 SkShader* solid_shader = new SkColorShader(kLabelBackgoundColor); |
60 gradient_shader->unref(); | 109 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode); |
61 image_shader->unref(); | 110 SkShader* composite_shader = new SkComposeShader(gradient_shader, |
| 111 solid_shader, mode); |
| 112 gradient_shader->unref(); |
| 113 solid_shader->unref(); |
62 | 114 |
63 SkPaint paint; | 115 SkPaint paint; |
64 paint.setAntiAlias(true); | 116 paint.setShader(composite_shader)->unref(); |
65 paint.setFilterBitmap(true); | 117 canvas.drawPaint(paint); |
66 paint.setShader(composite_shader)->unref(); | 118 } |
67 canvas->DrawRectInt(bounds.x(), bounds.y(), | |
68 bounds.width(), bounds.height(), paint); | |
69 } | |
70 | 119 |
71 void UsernameView::PaintUsername(const gfx::Rect& bounds) { | |
72 margin_width_ = bounds.height() * kMarginRatio; | |
73 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); | |
74 // Draw background. | |
75 canvas.drawColor(kLabelBackgoundColor); | |
76 // Calculate needed space. | |
77 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | | |
78 gfx::Canvas::TEXT_VALIGN_MIDDLE | | |
79 gfx::Canvas::NO_ELLIPSIS; | |
80 int text_height; | |
81 gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(GetText()), font(), | |
82 &text_width_, &text_height, | |
83 flags); | |
84 text_width_ = std::min(text_width_, bounds.width() - margin_width_); | |
85 // Draw the text. | 120 // Draw the text. |
86 // Note, direct call of the DrawStringInt method produces the green dots | 121 // Note, direct call of the DrawStringInt method produces the green dots |
87 // along the text perimeter (when the label is place on the white background). | 122 // along the text perimeter (when the label is place on the white background). |
88 SkColor kInvisibleHaloColor = 0x00000000; | 123 SkColor kInvisibleHaloColor = 0x00000000; |
89 canvas.DrawStringWithHalo(GetText(), font(), GetColor(), kInvisibleHaloColor, | 124 canvas.DrawStringWithHalo(GetText(), font(), GetColor(), kInvisibleHaloColor, |
90 bounds.x() + margin_width_, bounds.y(), | 125 bounds.x() + margin_width_, bounds.y(), |
91 bounds.width() - margin_width_, bounds.height(), | 126 bounds.width() - 2 * margin_width_, bounds.height(), |
92 flags); | 127 flags); |
93 | 128 |
94 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); | 129 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); |
95 text_image_->buildMipMap(false); | 130 |
| 131 if (use_fading_for_text) { |
| 132 // Fade out only the text in the end. Use regualar background. |
| 133 canvas.drawColor(kLabelBackgoundColor, SkXfermode::kSrc_Mode); |
| 134 SkShader* image_shader = SkShader::CreateBitmapShader( |
| 135 *text_image_, |
| 136 SkShader::kRepeat_TileMode, |
| 137 SkShader::kRepeat_TileMode); |
| 138 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode); |
| 139 SkShader* composite_shader = new SkComposeShader(gradient_shader, |
| 140 image_shader, mode); |
| 141 gradient_shader->unref(); |
| 142 image_shader->unref(); |
| 143 |
| 144 SkPaint paint; |
| 145 paint.setShader(composite_shader)->unref(); |
| 146 canvas.drawPaint(paint); |
| 147 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); |
| 148 } |
| 149 } |
| 150 |
| 151 UsernameView* UsernameView::CreateShapedUsernameView( |
| 152 const std::wstring& username) { |
| 153 return new HalfRoundedView<UsernameView>(username); |
96 } | 154 } |
97 | 155 |
98 } // namespace chromeos | 156 } // namespace chromeos |
OLD | NEW |