Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(834)

Side by Side Diff: chrome/browser/chromeos/login/username_view.cc

Issue 9664072: Removing WmIpc and related files from ChromeOS (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Copyright Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/login/username_view.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/utf_string_conversions.h"
11 #include "grit/generated_resources.h"
12 #include "third_party/skia/include/core/SkColorShader.h"
13 #include "third_party/skia/include/core/SkComposeShader.h"
14 #include "third_party/skia/include/effects/SkGradientShader.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/gtk_util.h"
20 #include "ui/gfx/rect.h"
21
22 namespace chromeos {
23
24 namespace {
25 // Username label background color.
26 const SkColor kLabelBackgoundColor = 0x55000000;
27 // Holds margin to height ratio.
28 const double kMarginRatio = 1.0 / 3.0;
29 } // namespace
30
31 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape)
32 : views::Label(username.empty()
33 ? l10n_util::GetStringUTF16(IDS_GUEST) : WideToUTF16Hack(username)),
34 use_small_shape_(use_small_shape),
35 is_guest_(username.empty()) {
36 }
37
38 UsernameView::~UsernameView() {}
39
40 void UsernameView::OnPaint(gfx::Canvas* canvas) {
41 gfx::Rect bounds = GetContentsBounds();
42 if (text_image_ == NULL)
43 PaintUsername(bounds);
44 DCHECK(text_image_ != NULL);
45 DCHECK(bounds.size() ==
46 gfx::Size(text_image_->width(), text_image_->height()));
47 canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y());
48 }
49
50 // static
51 UsernameView* UsernameView::CreateShapedUsernameView(
52 const std::wstring& username, bool use_small_shape) {
53 return new UsernameView(username, use_small_shape);
54 }
55
56 gfx::NativeCursor UsernameView::GetCursor(const views::MouseEvent& event) {
57 return use_small_shape_ ? gfx::GetCursor(GDK_HAND2) : NULL;
58 }
59
60 void UsernameView::PaintUsername(const gfx::Rect& bounds) {
61 margin_width_ = bounds.height() * kMarginRatio;
62 gfx::Canvas canvas(bounds.size(), false);
63 // Draw transparent background.
64 canvas.sk_canvas()->drawColor(0);
65
66 // Calculate needed space.
67 int flags = gfx::Canvas::TEXT_ALIGN_LEFT | gfx::Canvas::TEXT_VALIGN_MIDDLE |
68 gfx::Canvas::NO_ELLIPSIS;
69 int text_height, text_width;
70 gfx::Canvas::SizeStringInt(GetText(), font(), &text_width, &text_height,
71 flags);
72 text_width += margin_width_;
73
74 // Also leave the right margin.
75 bool use_fading_for_text = text_width + margin_width_ >= bounds.width();
76
77 // Only alpha channel counts.
78 SkColor gradient_colors[2];
79 gradient_colors[0] = 0xFFFFFFFF;
80 gradient_colors[1] = 0x00FFFFFF;
81
82 int gradient_start = use_fading_for_text ?
83 bounds.width() - bounds.height() - margin_width_ :
84 text_width;
85 int gradient_end = std::min(gradient_start + bounds.height(),
86 bounds.width() - margin_width_);
87
88 SkPoint gradient_borders[2];
89 gradient_borders[0].set(SkIntToScalar(gradient_start), SkIntToScalar(0));
90 gradient_borders[1].set(SkIntToScalar(gradient_end), SkIntToScalar(0));
91
92 SkShader* gradient_shader =
93 SkGradientShader::CreateLinear(gradient_borders, gradient_colors, NULL, 2,
94 SkShader::kClamp_TileMode, NULL);
95
96 if (!use_fading_for_text) {
97 // Draw the final background with the fading in the end.
98 SkShader* solid_shader = new SkColorShader(kLabelBackgoundColor);
99 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode);
100 SkShader* composite_shader = new SkComposeShader(gradient_shader,
101 solid_shader, mode);
102 gradient_shader->unref();
103 solid_shader->unref();
104
105 SkPaint paint;
106 paint.setShader(composite_shader)->unref();
107 canvas.sk_canvas()->drawPaint(paint);
108 }
109
110 // Draw the text.
111 // Note, direct call of the DrawStringInt method produces the green dots
112 // along the text perimeter (when the label is place on the white background).
113 SkColor text_color = enabled() ? enabled_color() : disabled_color();
114 SkColor kInvisibleHaloColor = 0x00000000;
115 canvas.DrawStringWithHalo(GetText(), font(), text_color,
116 kInvisibleHaloColor, bounds.x() + margin_width_,
117 bounds.y(), bounds.width() - 2 * margin_width_,
118 bounds.height(), flags);
119
120 text_image_.reset(new SkBitmap(canvas.ExtractBitmap()));
121
122 if (use_fading_for_text) {
123 // Fade out only the text in the end. Use regular background.
124 canvas.sk_canvas()->drawColor(kLabelBackgoundColor, SkXfermode::kSrc_Mode);
125 SkShader* image_shader = SkShader::CreateBitmapShader(
126 *text_image_,
127 SkShader::kRepeat_TileMode,
128 SkShader::kRepeat_TileMode);
129 SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode);
130 SkShader* composite_shader = new SkComposeShader(gradient_shader,
131 image_shader, mode);
132 gradient_shader->unref();
133 image_shader->unref();
134
135 SkPaint paint;
136 paint.setShader(composite_shader)->unref();
137 canvas.sk_canvas()->drawPaint(paint);
138 text_image_.reset(new SkBitmap(canvas.ExtractBitmap()));
139 }
140 }
141
142 void UsernameView::OnLocaleChanged() {
143 if (is_guest_) {
144 SetText(l10n_util::GetStringUTF16(IDS_GUEST));
145 }
146 // Repaint because the font may have changed.
147 text_image_.reset();
148 SchedulePaint();
149 }
150
151 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/login/user_manager_impl.cc ('k') | chrome/browser/chromeos/login/version_info_updater.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698