Home | History | Annotate | Download | only in infobars
      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/ui/views/infobars/infobar_background.h"
      6 
      7 #include "chrome/browser/infobars/infobar.h"
      8 #include "chrome/browser/ui/views/infobars/infobar_view.h"
      9 #include "third_party/skia/include/effects/SkGradientShader.h"
     10 #include "ui/gfx/canvas.h"
     11 #include "ui/gfx/color_utils.h"
     12 #include "ui/views/view.h"
     13 
     14 InfoBarBackground::InfoBarBackground(InfoBarDelegate::Type infobar_type)
     15     : separator_color_(SK_ColorBLACK),
     16       top_color_(InfoBar::GetTopColor(infobar_type)),
     17       bottom_color_(InfoBar::GetBottomColor(infobar_type)) {
     18   SetNativeControlColor(
     19       color_utils::AlphaBlend(top_color_, bottom_color_, 128));
     20 }
     21 
     22 InfoBarBackground::~InfoBarBackground() {
     23 }
     24 
     25 void InfoBarBackground::Paint(gfx::Canvas* canvas, views::View* view) const {
     26   SkPoint gradient_points[2];
     27   gradient_points[0].iset(0, 0);
     28   gradient_points[1].iset(0, view->height());
     29   SkColor gradient_colors[2] = { top_color_, bottom_color_ };
     30   skia::RefPtr<SkShader> gradient_shader = skia::AdoptRef(
     31       SkGradientShader::CreateLinear(gradient_points, gradient_colors, NULL, 2,
     32                                      SkShader::kClamp_TileMode));
     33   SkPaint paint;
     34   paint.setStrokeWidth(SkIntToScalar(InfoBar::kSeparatorLineHeight));
     35   paint.setStyle(SkPaint::kFill_Style);
     36   paint.setStrokeCap(SkPaint::kRound_Cap);
     37   paint.setShader(gradient_shader.get());
     38 
     39   InfoBarView* infobar = static_cast<InfoBarView*>(view);
     40   SkCanvas* canvas_skia = canvas->sk_canvas();
     41   canvas_skia->drawPath(infobar->fill_path(), paint);
     42 
     43   paint.setShader(NULL);
     44   paint.setColor(SkColorSetA(separator_color_,
     45                              SkColorGetA(gradient_colors[0])));
     46   paint.setStyle(SkPaint::kStroke_Style);
     47   // Anti-alias the path so it doesn't look goofy when the edges are not at 45
     48   // degree angles, but don't anti-alias anything else, especially not the fill,
     49   // lest we get weird color bleeding problems.
     50   paint.setAntiAlias(true);
     51   canvas_skia->drawPath(infobar->stroke_path(), paint);
     52   paint.setAntiAlias(false);
     53 
     54   // Now draw the separator at the bottom.
     55   canvas->FillRect(gfx::Rect(0, view->height() - InfoBar::kSeparatorLineHeight,
     56                              view->width(), InfoBar::kSeparatorLineHeight),
     57                    separator_color_);
     58 }
     59