| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/views/border.h" | 5 #include "ui/views/border.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" |
| 9 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/geometry/rect_f.h" |
| 10 #include "ui/views/painter.h" | 12 #include "ui/views/painter.h" |
| 11 #include "ui/views/view.h" | 13 #include "ui/views/view.h" |
| 12 | 14 |
| 13 namespace views { | 15 namespace views { |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 // A simple border with different thicknesses on each side and single color. | 19 // A simple border with different thicknesses on each side and single color. |
| 18 class SolidSidedBorder : public Border { | 20 class SolidSidedBorder : public Border { |
| 19 public: | 21 public: |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 } | 54 } |
| 53 | 55 |
| 54 gfx::Insets SolidSidedBorder::GetInsets() const { | 56 gfx::Insets SolidSidedBorder::GetInsets() const { |
| 55 return insets_; | 57 return insets_; |
| 56 } | 58 } |
| 57 | 59 |
| 58 gfx::Size SolidSidedBorder::GetMinimumSize() const { | 60 gfx::Size SolidSidedBorder::GetMinimumSize() const { |
| 59 return gfx::Size(insets_.width(), insets_.height()); | 61 return gfx::Size(insets_.width(), insets_.height()); |
| 60 } | 62 } |
| 61 | 63 |
| 64 // A border with a rounded rectangle and single color. |
| 65 class RoundedRectBorder : public Border { |
| 66 public: |
| 67 RoundedRectBorder(int thickness, int corner_radius, SkColor color); |
| 68 |
| 69 // Overridden from Border: |
| 70 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 71 gfx::Insets GetInsets() const override; |
| 72 gfx::Size GetMinimumSize() const override; |
| 73 |
| 74 private: |
| 75 const int thickness_; |
| 76 const int corner_radius_; |
| 77 const SkColor color_; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(RoundedRectBorder); |
| 80 }; |
| 81 |
| 82 RoundedRectBorder::RoundedRectBorder(int thickness, |
| 83 int corner_radius, |
| 84 SkColor color) |
| 85 : thickness_(thickness), corner_radius_(corner_radius), color_(color) {} |
| 86 |
| 87 void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 88 SkPaint paint; |
| 89 paint.setStrokeWidth(thickness_); |
| 90 paint.setColor(color_); |
| 91 paint.setStyle(SkPaint::kStroke_Style); |
| 92 paint.setAntiAlias(true); |
| 93 |
| 94 float half_thickness = thickness_ / 2; |
| 95 gfx::RectF bounds(view.GetLocalBounds()); |
| 96 bounds.Inset(half_thickness, half_thickness); |
| 97 canvas->DrawRoundRect(bounds, corner_radius_, paint); |
| 98 } |
| 99 |
| 100 gfx::Insets RoundedRectBorder::GetInsets() const { |
| 101 return gfx::Insets(thickness_, thickness_, thickness_, thickness_); |
| 102 } |
| 103 |
| 104 gfx::Size RoundedRectBorder::GetMinimumSize() const { |
| 105 return gfx::Size(thickness_ * 2, thickness_ * 2); |
| 106 } |
| 107 |
| 62 class EmptyBorder : public Border { | 108 class EmptyBorder : public Border { |
| 63 public: | 109 public: |
| 64 explicit EmptyBorder(const gfx::Insets& insets); | 110 explicit EmptyBorder(const gfx::Insets& insets); |
| 65 | 111 |
| 66 // Overridden from Border: | 112 // Overridden from Border: |
| 67 void Paint(const View& view, gfx::Canvas* canvas) override; | 113 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 68 gfx::Insets GetInsets() const override ; | 114 gfx::Insets GetInsets() const override ; |
| 69 gfx::Size GetMinimumSize() const override; | 115 gfx::Size GetMinimumSize() const override; |
| 70 | 116 |
| 71 private: | 117 private: |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 return make_scoped_ptr(new SolidSidedBorder( | 186 return make_scoped_ptr(new SolidSidedBorder( |
| 141 gfx::Insets(thickness, thickness, thickness, thickness), color)); | 187 gfx::Insets(thickness, thickness, thickness, thickness), color)); |
| 142 } | 188 } |
| 143 | 189 |
| 144 // static | 190 // static |
| 145 scoped_ptr<Border> Border::CreateEmptyBorder(const gfx::Insets& insets) { | 191 scoped_ptr<Border> Border::CreateEmptyBorder(const gfx::Insets& insets) { |
| 146 return make_scoped_ptr(new EmptyBorder(insets)); | 192 return make_scoped_ptr(new EmptyBorder(insets)); |
| 147 } | 193 } |
| 148 | 194 |
| 149 // static | 195 // static |
| 196 scoped_ptr<Border> Border::CreateRoundedRectBorder(int thickness, |
| 197 int corner_radius, |
| 198 SkColor color) { |
| 199 return make_scoped_ptr( |
| 200 new RoundedRectBorder(thickness, corner_radius, color)); |
| 201 } |
| 202 |
| 203 // static |
| 150 scoped_ptr<Border> Border::CreateEmptyBorder(int top, | 204 scoped_ptr<Border> Border::CreateEmptyBorder(int top, |
| 151 int left, | 205 int left, |
| 152 int bottom, | 206 int bottom, |
| 153 int right) { | 207 int right) { |
| 154 return CreateEmptyBorder(gfx::Insets(top, left, bottom, right)); | 208 return CreateEmptyBorder(gfx::Insets(top, left, bottom, right)); |
| 155 } | 209 } |
| 156 | 210 |
| 157 // static | 211 // static |
| 158 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, | 212 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, |
| 159 int left, | 213 int left, |
| 160 int bottom, | 214 int bottom, |
| 161 int right, | 215 int right, |
| 162 SkColor color) { | 216 SkColor color) { |
| 163 return make_scoped_ptr(new SolidSidedBorder( | 217 return make_scoped_ptr(new SolidSidedBorder( |
| 164 gfx::Insets(top, left, bottom, right), color)); | 218 gfx::Insets(top, left, bottom, right), color)); |
| 165 } | 219 } |
| 166 | 220 |
| 167 // static | 221 // static |
| 168 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, | 222 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, |
| 169 const gfx::Insets& insets) { | 223 const gfx::Insets& insets) { |
| 170 return make_scoped_ptr(new BorderPainter(painter, insets)); | 224 return make_scoped_ptr(new BorderPainter(painter, insets)); |
| 171 } | 225 } |
| 172 | 226 |
| 173 } // namespace views | 227 } // namespace views |
| OLD | NEW |