detachable_toolbar_view.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang// Use of this source code is governed by a BSD-style license that can be 3ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang// found in the LICENSE file. 4ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang 5ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "chrome/browser/ui/views/detachable_toolbar_view.h" 6ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang 7ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "chrome/browser/themes/theme_properties.h" 8ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "grit/theme_resources.h" 9ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "third_party/skia/include/core/SkShader.h" 10ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/base/resource/resource_bundle.h" 11ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/base/theme_provider.h" 12ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/gfx/canvas.h" 13ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/gfx/image/image_skia.h" 14ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/gfx/skia_util.h" 15ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#include "ui/views/window/non_client_view.h" 16ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang 17ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang// How round the 'new tab' style bookmarks bar is. 18ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Changstatic const int kNewtabBarRoundness = 5; 19ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang 20ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Changconst SkColor DetachableToolbarView::kEdgeDividerColor = 21ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang SkColorSetRGB(222, 234, 248); 22ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Changconst SkColor DetachableToolbarView::kMiddleDividerColor = 232d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines SkColorSetRGB(194, 205, 212); 24ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang 25ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang// static 26ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Changvoid DetachableToolbarView::PaintBackgroundAttachedMode( 27ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang gfx::Canvas* canvas, 28ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang ui::ThemeProvider* theme_provider, 29ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang const gfx::Rect& bounds, 30ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang const gfx::Point& background_origin, 31ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang chrome::HostDesktopType host_desktop_type) { 32ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang canvas->FillRect(bounds, 332d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines theme_provider->GetColor(ThemeProperties::COLOR_TOOLBAR)); 342d1fdb26e458c4ddc04155c1d421bced3ba90cd0Stephen Hines canvas->TileImageInt(*theme_provider->GetImageSkiaNamed(IDR_THEME_TOOLBAR), 35ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang background_origin.x(), background_origin.y(), bounds.x(), 36ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang bounds.y(), bounds.width(), bounds.height()); 37ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang#if defined(USE_ASH) 38ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang if (host_desktop_type == chrome::HOST_DESKTOP_TYPE_ASH) { 39ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang // Ash provides additional lightening at the edges of the toolbar. 40ce2f1840c8a15318e6e8958a8e36bc1ceaccba75Zonr Chang gfx::ImageSkia* toolbar_left = 41 theme_provider->GetImageSkiaNamed(IDR_TOOLBAR_SHADE_LEFT); 42 canvas->TileImageInt(*toolbar_left, 43 bounds.x(), bounds.y(), 44 toolbar_left->width(), bounds.height()); 45 gfx::ImageSkia* toolbar_right = 46 theme_provider->GetImageSkiaNamed(IDR_TOOLBAR_SHADE_RIGHT); 47 canvas->TileImageInt(*toolbar_right, 48 bounds.right() - toolbar_right->width(), bounds.y(), 49 toolbar_right->width(), bounds.height()); 50 } 51#endif 52} 53 54// static 55void DetachableToolbarView::CalculateContentArea( 56 double animation_state, double horizontal_padding, 57 double vertical_padding, SkRect* rect, 58 double* roundness, views::View* view) { 59 // The 0.5 is to correct for Skia's "draw on pixel boundaries"ness. 60 rect->set(SkDoubleToScalar(horizontal_padding - 0.5), 61 SkDoubleToScalar(vertical_padding - 0.5), 62 SkDoubleToScalar(view->width() - horizontal_padding - 0.5), 63 SkDoubleToScalar(view->height() - vertical_padding - 0.5)); 64 65 *roundness = static_cast<double>(kNewtabBarRoundness) * animation_state; 66} 67 68// static 69void DetachableToolbarView::PaintHorizontalBorderForState( 70 gfx::Canvas* canvas, 71 DetachableToolbarView* view) { 72 // Border can be at the top or at the bottom of the view depending on whether 73 // the view (bar/shelf) is attached or detached. 74 PaintHorizontalBorder(canvas, view, view->IsDetached(), 75 ThemeProperties::GetDefaultColor( 76 ThemeProperties::COLOR_TOOLBAR_SEPARATOR)); 77} 78 79void DetachableToolbarView::PaintHorizontalBorder( 80 gfx::Canvas* canvas, 81 DetachableToolbarView* view, 82 bool at_top, 83 SkColor color) { 84 int thickness = views::NonClientFrameView::kClientEdgeThickness; 85 int y = at_top ? 0 : (view->height() - thickness); 86 canvas->FillRect(gfx::Rect(0, y, view->width(), thickness), color); 87} 88 89// static 90void DetachableToolbarView::PaintContentAreaBackground( 91 gfx::Canvas* canvas, 92 ui::ThemeProvider* theme_provider, 93 const SkRect& rect, 94 double roundness) { 95 SkPaint paint; 96 paint.setAntiAlias(true); 97 paint.setColor(theme_provider->GetColor(ThemeProperties::COLOR_TOOLBAR)); 98 99 canvas->sk_canvas()->drawRoundRect( 100 rect, SkDoubleToScalar(roundness), SkDoubleToScalar(roundness), paint); 101} 102 103// static 104void DetachableToolbarView::PaintContentAreaBorder( 105 gfx::Canvas* canvas, ui::ThemeProvider* theme_provider, 106 const SkRect& rect, double roundness) { 107 SkPaint border_paint; 108 border_paint.setColor( 109 theme_provider->GetColor(ThemeProperties::COLOR_NTP_HEADER)); 110 border_paint.setStyle(SkPaint::kStroke_Style); 111 border_paint.setAlpha(96); 112 border_paint.setAntiAlias(true); 113 114 canvas->sk_canvas()->drawRoundRect( 115 rect, SkDoubleToScalar(roundness), SkDoubleToScalar(roundness), 116 border_paint); 117} 118 119// static 120void DetachableToolbarView::PaintVerticalDivider(gfx::Canvas* canvas, 121 int x, 122 int height, 123 int vertical_padding, 124 SkColor top_color, 125 SkColor middle_color, 126 SkColor bottom_color) { 127 // Draw the upper half of the divider. 128 SkPaint paint; 129 skia::RefPtr<SkShader> shader = gfx::CreateGradientShader( 130 vertical_padding + 1, height / 2, top_color, middle_color); 131 paint.setShader(shader.get()); 132 SkRect rc = { SkIntToScalar(x), 133 SkIntToScalar(vertical_padding + 1), 134 SkIntToScalar(x + 1), 135 SkIntToScalar(height / 2) }; 136 canvas->sk_canvas()->drawRect(rc, paint); 137 138 // Draw the lower half of the divider. 139 SkPaint paint_down; 140 shader = gfx::CreateGradientShader( 141 height / 2, height - vertical_padding, middle_color, bottom_color); 142 paint_down.setShader(shader.get()); 143 SkRect rc_down = { SkIntToScalar(x), 144 SkIntToScalar(height / 2), 145 SkIntToScalar(x + 1), 146 SkIntToScalar(height - vertical_padding) }; 147 canvas->sk_canvas()->drawRect(rc_down, paint_down); 148} 149