| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 <algorithm> |
| 8 |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/chromeos/login/rounded_view.h" | 11 #include "chrome/browser/chromeos/login/rounded_view.h" |
| 10 #include "grit/generated_resources.h" | 12 #include "grit/generated_resources.h" |
| 11 #include "third_party/skia/include/core/SkColorShader.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 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 17 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 17 #include "ui/gfx/canvas_skia.h" | 19 #include "ui/gfx/canvas_skia.h" |
| 18 #include "ui/gfx/gtk_util.h" | 20 #include "ui/gfx/gtk_util.h" |
| 19 #include "ui/gfx/rect.h" | 21 #include "ui/gfx/rect.h" |
| 20 | 22 |
| 21 namespace chromeos { | 23 namespace chromeos { |
| 22 | 24 |
| 23 namespace { | 25 namespace { |
| 24 // Username label background color. | 26 // Username label background color. |
| 25 const SkColor kLabelBackgoundColor = 0x55000000; | 27 const SkColor kLabelBackgoundColor = 0x55000000; |
| 26 // Holds margin to height ratio. | 28 // Holds margin to height ratio. |
| 27 const double kMarginRatio = 1.0 / 3.0; | 29 const double kMarginRatio = 1.0 / 3.0; |
| 28 // Holds the frame width for the small shaped username view. | |
| 29 const SkScalar kSmallShapeFrameWidth = SkIntToScalar(1); | |
| 30 | |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape) | 32 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape) |
| 34 : views::Label(username.empty() | 33 : views::Label(username.empty() |
| 35 ? UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST)) : username), | 34 ? l10n_util::GetStringUTF16(IDS_GUEST) : WideToUTF16Hack(username)), |
| 36 use_small_shape_(use_small_shape), | 35 use_small_shape_(use_small_shape), |
| 37 is_guest_(username.empty()) { | 36 is_guest_(username.empty()) { |
| 38 } | 37 } |
| 39 | 38 |
| 40 UsernameView::~UsernameView() {} | 39 UsernameView::~UsernameView() {} |
| 41 | 40 |
| 42 void UsernameView::OnPaint(gfx::Canvas* canvas) { | 41 void UsernameView::OnPaint(gfx::Canvas* canvas) { |
| 43 gfx::Rect bounds = GetContentsBounds(); | 42 gfx::Rect bounds = GetContentsBounds(); |
| 44 if (text_image_ == NULL) | 43 if (text_image_ == NULL) |
| 45 PaintUsername(bounds); | 44 PaintUsername(bounds); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 63 margin_width_ = bounds.height() * kMarginRatio; | 62 margin_width_ = bounds.height() * kMarginRatio; |
| 64 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); | 63 gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false); |
| 65 // Draw transparent background. | 64 // Draw transparent background. |
| 66 canvas.drawColor(0); | 65 canvas.drawColor(0); |
| 67 | 66 |
| 68 // Calculate needed space. | 67 // Calculate needed space. |
| 69 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | | 68 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | |
| 70 gfx::Canvas::TEXT_VALIGN_MIDDLE | | 69 gfx::Canvas::TEXT_VALIGN_MIDDLE | |
| 71 gfx::Canvas::NO_ELLIPSIS; | 70 gfx::Canvas::NO_ELLIPSIS; |
| 72 int text_height, text_width; | 71 int text_height, text_width; |
| 73 gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(GetText()), font(), | 72 gfx::CanvasSkia::SizeStringInt(GetText(), font(), |
| 74 &text_width, &text_height, | 73 &text_width, &text_height, |
| 75 flags); | 74 flags); |
| 76 text_width += margin_width_; | 75 text_width += margin_width_; |
| 77 | 76 |
| 78 // Also leave the right margin. | 77 // Also leave the right margin. |
| 79 bool use_fading_for_text = text_width + margin_width_ >= bounds.width(); | 78 bool use_fading_for_text = text_width + margin_width_ >= bounds.width(); |
| 80 | 79 |
| 81 // Only alpha channel counts. | 80 // Only alpha channel counts. |
| 82 SkColor gradient_colors[2]; | 81 SkColor gradient_colors[2]; |
| 83 gradient_colors[0] = 0xFFFFFFFF; | 82 gradient_colors[0] = 0xFFFFFFFF; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 108 | 107 |
| 109 SkPaint paint; | 108 SkPaint paint; |
| 110 paint.setShader(composite_shader)->unref(); | 109 paint.setShader(composite_shader)->unref(); |
| 111 canvas.drawPaint(paint); | 110 canvas.drawPaint(paint); |
| 112 } | 111 } |
| 113 | 112 |
| 114 // Draw the text. | 113 // Draw the text. |
| 115 // Note, direct call of the DrawStringInt method produces the green dots | 114 // Note, direct call of the DrawStringInt method produces the green dots |
| 116 // along the text perimeter (when the label is place on the white background). | 115 // along the text perimeter (when the label is place on the white background). |
| 117 SkColor kInvisibleHaloColor = 0x00000000; | 116 SkColor kInvisibleHaloColor = 0x00000000; |
| 118 canvas.DrawStringWithHalo(WideToUTF16Hack(GetText()), font(), GetColor(), | 117 canvas.DrawStringWithHalo(GetText(), font(), GetColor(), |
| 119 kInvisibleHaloColor, bounds.x() + margin_width_, | 118 kInvisibleHaloColor, bounds.x() + margin_width_, |
| 120 bounds.y(), bounds.width() - 2 * margin_width_, | 119 bounds.y(), bounds.width() - 2 * margin_width_, |
| 121 bounds.height(), flags); | 120 bounds.height(), flags); |
| 122 | 121 |
| 123 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); | 122 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); |
| 124 | 123 |
| 125 if (use_fading_for_text) { | 124 if (use_fading_for_text) { |
| 126 // Fade out only the text in the end. Use regular background. | 125 // Fade out only the text in the end. Use regular background. |
| 127 canvas.drawColor(kLabelBackgoundColor, SkXfermode::kSrc_Mode); | 126 canvas.drawColor(kLabelBackgoundColor, SkXfermode::kSrc_Mode); |
| 128 SkShader* image_shader = SkShader::CreateBitmapShader( | 127 SkShader* image_shader = SkShader::CreateBitmapShader( |
| 129 *text_image_, | 128 *text_image_, |
| 130 SkShader::kRepeat_TileMode, | 129 SkShader::kRepeat_TileMode, |
| 131 SkShader::kRepeat_TileMode); | 130 SkShader::kRepeat_TileMode); |
| 132 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode); | 131 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode); |
| 133 SkShader* composite_shader = new SkComposeShader(gradient_shader, | 132 SkShader* composite_shader = new SkComposeShader(gradient_shader, |
| 134 image_shader, mode); | 133 image_shader, mode); |
| 135 gradient_shader->unref(); | 134 gradient_shader->unref(); |
| 136 image_shader->unref(); | 135 image_shader->unref(); |
| 137 | 136 |
| 138 SkPaint paint; | 137 SkPaint paint; |
| 139 paint.setShader(composite_shader)->unref(); | 138 paint.setShader(composite_shader)->unref(); |
| 140 canvas.drawPaint(paint); | 139 canvas.drawPaint(paint); |
| 141 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); | 140 text_image_.reset(new SkBitmap(canvas.ExtractBitmap())); |
| 142 } | 141 } |
| 143 } | 142 } |
| 144 | 143 |
| 145 void UsernameView::OnLocaleChanged() { | 144 void UsernameView::OnLocaleChanged() { |
| 146 if (is_guest_) { | 145 if (is_guest_) { |
| 147 SetText(UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST))); | 146 SetText(l10n_util::GetStringUTF16(IDS_GUEST)); |
| 148 } | 147 } |
| 149 // Repaint because the font may have changed. | 148 // Repaint because the font may have changed. |
| 150 text_image_.reset(); | 149 text_image_.reset(); |
| 151 SchedulePaint(); | 150 SchedulePaint(); |
| 152 } | 151 } |
| 153 | 152 |
| 154 } // namespace chromeos | 153 } // namespace chromeos |
| OLD | NEW |