| OLD | NEW |
| (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/ui/views/dropdown_bar_view.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/memory/ptr_util.h" | |
| 9 #include "chrome/browser/themes/theme_service.h" | |
| 10 #include "chrome/browser/ui/view_ids.h" | |
| 11 #include "chrome/browser/ui/views/frame/browser_view.h" | |
| 12 #include "chrome/grit/theme_resources.h" | |
| 13 #include "third_party/skia/include/core/SkCanvas.h" | |
| 14 #include "third_party/skia/include/core/SkRect.h" | |
| 15 #include "ui/base/resource/resource_bundle.h" | |
| 16 #include "ui/gfx/canvas.h" | |
| 17 #include "ui/gfx/image/image_skia.h" | |
| 18 #include "ui/views/background.h" | |
| 19 #include "ui/views/border.h" | |
| 20 #include "ui/views/painter.h" | |
| 21 #include "ui/views/widget/widget.h" | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 25 // Background to paint toolbar background with rounded corners. | |
| 26 class DropdownBackground : public views::Background { | |
| 27 public: | |
| 28 explicit DropdownBackground(BrowserView* browser, | |
| 29 const gfx::ImageSkia* left_alpha_mask, | |
| 30 const gfx::ImageSkia* right_alpha_mask); | |
| 31 ~DropdownBackground() override {} | |
| 32 | |
| 33 // Overridden from views::Background. | |
| 34 void Paint(gfx::Canvas* canvas, views::View* view) const override; | |
| 35 | |
| 36 private: | |
| 37 BrowserView* browser_view_; | |
| 38 const gfx::ImageSkia* left_alpha_mask_; | |
| 39 const gfx::ImageSkia* right_alpha_mask_; | |
| 40 | |
| 41 DISALLOW_COPY_AND_ASSIGN(DropdownBackground); | |
| 42 }; | |
| 43 | |
| 44 DropdownBackground::DropdownBackground(BrowserView* browser_view, | |
| 45 const gfx::ImageSkia* left_alpha_mask, | |
| 46 const gfx::ImageSkia* right_alpha_mask) | |
| 47 : browser_view_(browser_view), | |
| 48 left_alpha_mask_(left_alpha_mask), | |
| 49 right_alpha_mask_(right_alpha_mask) { | |
| 50 } | |
| 51 | |
| 52 void DropdownBackground::Paint(gfx::Canvas* canvas, views::View* view) const { | |
| 53 // Find the offset from which to tile the toolbar background image. | |
| 54 // First, get the origin with respect to the screen. | |
| 55 gfx::Point origin = view->GetWidget()->GetWindowBoundsInScreen().origin(); | |
| 56 // Now convert from screen to parent coordinates. | |
| 57 views::View::ConvertPointFromScreen(browser_view_, &origin); | |
| 58 // Finally, calculate the background image tiling offset. | |
| 59 origin = browser_view_->OffsetPointForToolbarBackgroundImage(origin); | |
| 60 | |
| 61 const ui::ThemeProvider* tp = view->GetThemeProvider(); | |
| 62 gfx::ImageSkia background = *tp->GetImageSkiaNamed(IDR_THEME_TOOLBAR); | |
| 63 | |
| 64 int left_edge_width = left_alpha_mask_->width(); | |
| 65 int right_edge_width = right_alpha_mask_->width(); | |
| 66 int mask_height = left_alpha_mask_->height(); | |
| 67 int height = view->bounds().height(); | |
| 68 | |
| 69 // Stretch the middle background to cover the entire area. | |
| 70 canvas->TileImageInt(background, origin.x(), origin.y(), | |
| 71 0, 0, view->bounds().width(), height); | |
| 72 | |
| 73 SkPaint paint; | |
| 74 paint.setBlendMode(SkBlendMode::kDstIn); | |
| 75 // Draw left edge. | |
| 76 canvas->DrawImageInt(*left_alpha_mask_, 0, 0, left_edge_width, mask_height, | |
| 77 0, 0, left_edge_width, height, false, paint); | |
| 78 | |
| 79 // Draw right edge. | |
| 80 int x_right_edge = view->bounds().width() - right_edge_width; | |
| 81 canvas->DrawImageInt(*right_alpha_mask_, 0, 0, right_edge_width, | |
| 82 mask_height, x_right_edge, 0, right_edge_width, height, false, paint); | |
| 83 } | |
| 84 | |
| 85 } // namespace | |
| 86 | |
| 87 DropdownBarView::DropdownBarView(DropdownBarHost* host) : host_(host) {} | |
| 88 | |
| 89 DropdownBarView::~DropdownBarView() { | |
| 90 } | |
| 91 | |
| 92 //////////////////////////////////////////////////////////////////////////////// | |
| 93 // DropDownBarView, protected: | |
| 94 | |
| 95 void DropdownBarView::SetBackground(const gfx::ImageSkia* left_alpha_mask, | |
| 96 const gfx::ImageSkia* right_alpha_mask) { | |
| 97 set_background(new DropdownBackground(host()->browser_view(), left_alpha_mask, | |
| 98 right_alpha_mask)); | |
| 99 } | |
| 100 | |
| 101 void DropdownBarView::SetBorderFromIds(int left_border_image_id, | |
| 102 int middle_border_image_id, | |
| 103 int right_border_image_id) { | |
| 104 int border_image_ids[3] = {left_border_image_id, middle_border_image_id, | |
| 105 right_border_image_id}; | |
| 106 SetBorder(views::Border::CreateBorderPainter( | |
| 107 base::MakeUnique<views::HorizontalPainter>(border_image_ids), | |
| 108 gfx::Insets())); | |
| 109 } | |
| OLD | NEW |