• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "chrome/browser/chromeos/login/username_view.h"
6 
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/chromeos/login/rounded_view.h"
10 #include "grit/generated_resources.h"
11 #include "third_party/skia/include/core/SkColorShader.h"
12 #include "third_party/skia/include/core/SkComposeShader.h"
13 #include "third_party/skia/include/effects/SkGradientShader.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/resource/resource_bundle.h"
16 #include "ui/gfx/canvas.h"
17 #include "ui/gfx/canvas_skia.h"
18 #include "ui/gfx/gtk_util.h"
19 #include "ui/gfx/rect.h"
20 
21 namespace chromeos {
22 
23 namespace {
24 // Username label background color.
25 const SkColor kLabelBackgoundColor = 0x55000000;
26 // Holds margin to height ratio.
27 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 // Class that sets up half rounded rectangle (only the bottom corners are
32 // rounded) as a clip region of the view.
33 // For more info see the file "chrome/browser/chromeos/login/rounded_view.h".
34 template<typename C>
35 class HalfRoundedView : public RoundedView<C> {
36  public:
HalfRoundedView(const std::wstring & text,bool use_small_shape)37   HalfRoundedView(const std::wstring &text, bool use_small_shape)
38       : RoundedView<C>(text, use_small_shape) {
39   }
40 
41  protected:
42   // Overrides ViewFilter.
GetClipPath() const43   virtual SkPath GetClipPath() const {
44     if (!C::use_small_shape()) {
45       return RoundedView<C>::GetClipPath();
46     } else {
47       SkPath path;
48       gfx::Rect frame_bounds = this->bounds();
49       frame_bounds.Inset(kSmallShapeFrameWidth, kSmallShapeFrameWidth,
50                    kSmallShapeFrameWidth, kSmallShapeFrameWidth);
51       path.addRect(SkIntToScalar(frame_bounds.x()),
52                    SkIntToScalar(frame_bounds.y()),
53                    SkIntToScalar(frame_bounds.x() + frame_bounds.width()),
54                    SkIntToScalar(frame_bounds.y() + frame_bounds.height()));
55       return path;
56     }
57   }
58 
DrawFrame(gfx::Canvas * canvas)59   virtual void DrawFrame(gfx::Canvas* canvas) {
60     // No frame is needed.
61   }
62 
GetViewRect() const63   virtual SkRect GetViewRect() const {
64     SkRect view_rect;
65     // The rectangle will be intersected with the bounds, so the correct half
66     // of the round rectangle will be obtained.
67     view_rect.iset(this->x(),
68                    this->y() - this->height(),
69                    this->x() + this->width(),
70                    this->y() + this->height());
71     return view_rect;
72   }
73 };
74 
75 }  // namespace
76 
UsernameView(const std::wstring & username,bool use_small_shape)77 UsernameView::UsernameView(const std::wstring& username, bool use_small_shape)
78     : views::Label(username.empty()
79           ? UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST)) : username),
80       use_small_shape_(use_small_shape),
81       is_guest_(username.empty()) {
82 }
83 
OnPaint(gfx::Canvas * canvas)84 void UsernameView::OnPaint(gfx::Canvas* canvas) {
85   gfx::Rect bounds = GetContentsBounds();
86   if (text_image_ == NULL)
87     PaintUsername(bounds);
88   DCHECK(text_image_ != NULL);
89   DCHECK(bounds.size() ==
90          gfx::Size(text_image_->width(), text_image_->height()));
91   canvas->DrawBitmapInt(*text_image_, bounds.x(), bounds.y());
92 }
93 
94 // static
CreateShapedUsernameView(const std::wstring & username,bool use_small_shape)95 UsernameView* UsernameView::CreateShapedUsernameView(
96     const std::wstring& username, bool use_small_shape) {
97   return new HalfRoundedView<UsernameView>(username, use_small_shape);
98 }
99 
GetCursorForPoint(ui::EventType event_type,const gfx::Point & p)100 gfx::NativeCursor UsernameView::GetCursorForPoint(
101     ui::EventType event_type,
102     const gfx::Point& p) {
103   return use_small_shape_ ? gfx::GetCursor(GDK_HAND2) : NULL;
104 }
105 
PaintUsername(const gfx::Rect & bounds)106 void UsernameView::PaintUsername(const gfx::Rect& bounds) {
107   margin_width_ = bounds.height() * kMarginRatio;
108   gfx::CanvasSkia canvas(bounds.width(), bounds.height(), false);
109   // Draw transparent background.
110   canvas.drawColor(0);
111 
112   // Calculate needed space.
113   int flags = gfx::Canvas::TEXT_ALIGN_LEFT |
114       gfx::Canvas::TEXT_VALIGN_MIDDLE |
115       gfx::Canvas::NO_ELLIPSIS;
116   int text_height, text_width;
117   gfx::CanvasSkia::SizeStringInt(WideToUTF16Hack(GetText()), font(),
118                                  &text_width, &text_height,
119                                  flags);
120   text_width += margin_width_;
121 
122   // Also leave the right margin.
123   bool use_fading_for_text = text_width + margin_width_ >= bounds.width();
124 
125   // Only alpha channel counts.
126   SkColor gradient_colors[2];
127   gradient_colors[0] = 0xFFFFFFFF;
128   gradient_colors[1] = 0x00FFFFFF;
129 
130   int gradient_start = use_fading_for_text ?
131                        bounds.width() - bounds.height() - margin_width_ :
132                        text_width;
133   int gradient_end = std::min(gradient_start + bounds.height(),
134                               bounds.width() - margin_width_);
135 
136   SkPoint gradient_borders[2];
137   gradient_borders[0].set(SkIntToScalar(gradient_start), SkIntToScalar(0));
138   gradient_borders[1].set(SkIntToScalar(gradient_end), SkIntToScalar(0));
139 
140   SkShader* gradient_shader =
141       SkGradientShader::CreateLinear(gradient_borders, gradient_colors, NULL, 2,
142                                      SkShader::kClamp_TileMode, NULL);
143 
144   if (!use_fading_for_text) {
145     // Draw the final background with the fading in the end.
146     SkShader* solid_shader = new SkColorShader(kLabelBackgoundColor);
147     SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode);
148     SkShader* composite_shader = new SkComposeShader(gradient_shader,
149                                                      solid_shader, mode);
150     gradient_shader->unref();
151     solid_shader->unref();
152 
153     SkPaint paint;
154     paint.setShader(composite_shader)->unref();
155     canvas.drawPaint(paint);
156   }
157 
158   // Draw the text.
159   // Note, direct call of the DrawStringInt method produces the green dots
160   // along the text perimeter (when the label is place on the white background).
161   SkColor kInvisibleHaloColor = 0x00000000;
162   canvas.DrawStringWithHalo(WideToUTF16Hack(GetText()), font(), GetColor(),
163                             kInvisibleHaloColor, bounds.x() + margin_width_,
164                             bounds.y(), bounds.width() - 2 * margin_width_,
165                             bounds.height(), flags);
166 
167   text_image_.reset(new SkBitmap(canvas.ExtractBitmap()));
168 
169   if (use_fading_for_text) {
170     // Fade out only the text in the end. Use regular background.
171     canvas.drawColor(kLabelBackgoundColor, SkXfermode::kSrc_Mode);
172     SkShader* image_shader = SkShader::CreateBitmapShader(
173         *text_image_,
174         SkShader::kRepeat_TileMode,
175         SkShader::kRepeat_TileMode);
176     SkXfermode* mode = SkXfermode::Create(SkXfermode::kSrcIn_Mode);
177     SkShader* composite_shader = new SkComposeShader(gradient_shader,
178                                                      image_shader, mode);
179     gradient_shader->unref();
180     image_shader->unref();
181 
182     SkPaint paint;
183     paint.setShader(composite_shader)->unref();
184     canvas.drawPaint(paint);
185     text_image_.reset(new SkBitmap(canvas.ExtractBitmap()));
186   }
187 }
188 
OnLocaleChanged()189 void UsernameView::OnLocaleChanged() {
190   if (is_guest_) {
191     SetText(UTF16ToWide(l10n_util::GetStringUTF16(IDS_GUEST)));
192   }
193   // Repaint because the font may have changed.
194   text_image_.reset();
195   SchedulePaint();
196 }
197 
198 }  // namespace chromeos
199