Home | History | Annotate | Download | only in views
      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/ui/views/detachable_toolbar_view.h"
      6 
      7 #include "chrome/browser/themes/theme_service.h"
      8 #include "grit/theme_resources.h"
      9 #include "third_party/skia/include/core/SkBitmap.h"
     10 #include "third_party/skia/include/core/SkShader.h"
     11 #include "ui/base/resource/resource_bundle.h"
     12 #include "ui/gfx/canvas_skia.h"
     13 #include "ui/gfx/skia_util.h"
     14 #include "views/window/non_client_view.h"
     15 
     16 // How round the 'new tab' style bookmarks bar is.
     17 static const int kNewtabBarRoundness = 5;
     18 
     19 const SkColor DetachableToolbarView::kEdgeDividerColor =
     20     SkColorSetRGB(222, 234, 248);
     21 const SkColor DetachableToolbarView::kMiddleDividerColor =
     22     SkColorSetRGB(194, 205, 212);
     23 
     24 // static
     25 void DetachableToolbarView::PaintBackgroundAttachedMode(
     26     gfx::Canvas* canvas,
     27     views::View* view,
     28     const gfx::Point& background_origin) {
     29   ui::ThemeProvider* tp = view->GetThemeProvider();
     30   SkColor theme_toolbar_color =
     31       tp->GetColor(ThemeService::COLOR_TOOLBAR);
     32   canvas->FillRectInt(theme_toolbar_color, 0, 0,
     33                       view->width(), view->height());
     34   canvas->TileImageInt(*tp->GetBitmapNamed(IDR_THEME_TOOLBAR),
     35                        background_origin.x(), background_origin.y(), 0, 0,
     36                        view->width(), view->height());
     37 }
     38 
     39 // static
     40 void DetachableToolbarView::CalculateContentArea(
     41     double animation_state, double horizontal_padding,
     42     double vertical_padding, SkRect* rect,
     43     double* roundness, views::View* view) {
     44   // The 0.5 is to correct for Skia's "draw on pixel boundaries"ness.
     45   rect->set(SkDoubleToScalar(horizontal_padding - 0.5),
     46            SkDoubleToScalar(vertical_padding - 0.5),
     47            SkDoubleToScalar(view->width() - horizontal_padding - 0.5),
     48            SkDoubleToScalar(view->height() - vertical_padding - 0.5));
     49 
     50   *roundness = static_cast<double>(kNewtabBarRoundness) * animation_state;
     51 }
     52 
     53 // static
     54 void DetachableToolbarView::PaintHorizontalBorder(gfx::Canvas* canvas,
     55                                                   DetachableToolbarView* view) {
     56   // Border can be at the top or at the bottom of the view depending on whether
     57   // the view (bar/shelf) is attached or detached.
     58   int thickness = views::NonClientFrameView::kClientEdgeThickness;
     59   int y = view->IsDetached() ? 0 : (view->height() - thickness);
     60   canvas->FillRectInt(ResourceBundle::toolbar_separator_color,
     61       0, y, view->width(), thickness);
     62 }
     63 
     64 // static
     65 void DetachableToolbarView::PaintContentAreaBackground(
     66     gfx::Canvas* canvas, ui::ThemeProvider* theme_provider,
     67     const SkRect& rect, double roundness) {
     68   SkPaint paint;
     69   paint.setAntiAlias(true);
     70   paint.setColor(theme_provider->GetColor(ThemeService::COLOR_TOOLBAR));
     71 
     72   canvas->AsCanvasSkia()->drawRoundRect(
     73       rect, SkDoubleToScalar(roundness), SkDoubleToScalar(roundness), paint);
     74 }
     75 
     76 // static
     77 void DetachableToolbarView::PaintContentAreaBorder(
     78     gfx::Canvas* canvas, ui::ThemeProvider* theme_provider,
     79     const SkRect& rect, double roundness) {
     80   SkPaint border_paint;
     81   border_paint.setColor(
     82       theme_provider->GetColor(ThemeService::COLOR_NTP_HEADER));
     83   border_paint.setStyle(SkPaint::kStroke_Style);
     84   border_paint.setAlpha(96);
     85   border_paint.setAntiAlias(true);
     86 
     87   canvas->AsCanvasSkia()->drawRoundRect(
     88       rect, SkDoubleToScalar(roundness), SkDoubleToScalar(roundness),
     89       border_paint);
     90 }
     91 
     92 // static
     93 void DetachableToolbarView::PaintVerticalDivider(
     94     gfx::Canvas* canvas, int x, int height, int vertical_padding,
     95     const SkColor& top_color,
     96     const SkColor& middle_color,
     97     const SkColor& bottom_color) {
     98   // Draw the upper half of the divider.
     99   SkPaint paint;
    100   SkSafeUnref(paint.setShader(gfx::CreateGradientShader(vertical_padding + 1,
    101                                                         height / 2,
    102                                                         top_color,
    103                                                         middle_color)));
    104   SkRect rc = { SkIntToScalar(x),
    105                 SkIntToScalar(vertical_padding + 1),
    106                 SkIntToScalar(x + 1),
    107                 SkIntToScalar(height / 2) };
    108   canvas->AsCanvasSkia()->drawRect(rc, paint);
    109 
    110   // Draw the lower half of the divider.
    111   SkPaint paint_down;
    112   SkSafeUnref(paint_down.setShader(gfx::CreateGradientShader(
    113           height / 2, height - vertical_padding, middle_color, bottom_color)));
    114   SkRect rc_down = { SkIntToScalar(x),
    115                      SkIntToScalar(height / 2),
    116                      SkIntToScalar(x + 1),
    117                      SkIntToScalar(height - vertical_padding) };
    118   canvas->AsCanvasSkia()->drawRect(rc_down, paint_down);
    119 }
    120