| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "cc/paint/paint_flags.h" | 12 #include "cc/paint/paint_flags.h" |
| 13 #include "ui/compositor/dip_util.h" | 13 #include "ui/compositor/dip_util.h" |
| 14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 15 #include "ui/gfx/geometry/dip_util.h" |
| 15 #include "ui/gfx/geometry/rect_f.h" | 16 #include "ui/gfx/geometry/rect_f.h" |
| 16 #include "ui/gfx/scoped_canvas.h" | 17 #include "ui/gfx/scoped_canvas.h" |
| 17 #include "ui/views/painter.h" | 18 #include "ui/views/painter.h" |
| 18 #include "ui/views/view.h" | 19 #include "ui/views/view.h" |
| 20 #include "ui/views/widget/widget.h" |
| 19 | 21 |
| 20 namespace views { | 22 namespace views { |
| 21 | 23 |
| 22 namespace { | 24 namespace { |
| 23 | 25 |
| 24 // A simple border with different thicknesses on each side and single color. | 26 // A simple border with different thicknesses on each side and single color. |
| 25 class SolidSidedBorder : public Border { | 27 class SolidSidedBorder : public Border { |
| 26 public: | 28 public: |
| 27 SolidSidedBorder(const gfx::Insets& insets, SkColor color); | 29 SolidSidedBorder(const gfx::Insets& insets, SkColor color); |
| 28 | 30 |
| 29 // Overridden from Border: | 31 // Overridden from Border: |
| 30 void Paint(const View& view, gfx::Canvas* canvas) override; | 32 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 31 gfx::Insets GetInsets() const override; | 33 gfx::Insets GetInsets() const override; |
| 32 gfx::Size GetMinimumSize() const override; | 34 gfx::Size GetMinimumSize() const override; |
| 33 | 35 |
| 36 protected: |
| 37 // Returns the scaled bounds to inset from. Default implementation works |
| 38 // well for views that are not drawn directly to a layer / widget. |
| 39 virtual gfx::RectF ScaledBounds(const View& view, float dsf) const; |
| 40 |
| 34 private: | 41 private: |
| 35 const gfx::Insets insets_; | 42 const gfx::Insets insets_; |
| 36 const SkColor color_; | 43 const SkColor color_; |
| 37 | 44 |
| 38 DISALLOW_COPY_AND_ASSIGN(SolidSidedBorder); | 45 DISALLOW_COPY_AND_ASSIGN(SolidSidedBorder); |
| 39 }; | 46 }; |
| 40 | 47 |
| 41 SolidSidedBorder::SolidSidedBorder(const gfx::Insets& insets, SkColor color) | 48 SolidSidedBorder::SolidSidedBorder(const gfx::Insets& insets, SkColor color) |
| 42 : insets_(insets), | 49 : insets_(insets), |
| 43 color_(color) { | 50 color_(color) { |
| 44 } | 51 } |
| 45 | 52 |
| 46 void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) { | 53 void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 47 // Undo DSF so that we can be sure to draw an integral number of pixels for | 54 // Undo DSF so that we can be sure to draw an integral number of pixels for |
| 48 // the border. Integral scale factors should be unaffected by this, but for | 55 // the border. Integral scale factors should be unaffected by this, but for |
| 49 // fractional scale factors this ensures sharp lines. | 56 // fractional scale factors this ensures sharp lines. |
| 50 gfx::ScopedCanvas scoped(canvas); | 57 gfx::ScopedCanvas scoped(canvas); |
| 51 float dsf = canvas->UndoDeviceScaleFactor(); | 58 float dsf = canvas->UndoDeviceScaleFactor(); |
| 52 | 59 gfx::RectF scaled_bounds = ScaledBounds(view, dsf); |
| 53 gfx::RectF scaled_bounds; | |
| 54 if (view.layer()) { | |
| 55 scaled_bounds = | |
| 56 gfx::RectF(ui::ConvertRectToPixel(view.layer(), view.GetLocalBounds())); | |
| 57 } else { | |
| 58 scaled_bounds = gfx::RectF(view.GetLocalBounds()); | |
| 59 scaled_bounds.Scale(dsf); | |
| 60 } | |
| 61 | 60 |
| 62 // This scaling operation floors the inset values. | 61 // This scaling operation floors the inset values. |
| 63 scaled_bounds.Inset(insets_.Scale(dsf)); | 62 scaled_bounds.Inset(insets_.Scale(dsf)); |
| 64 canvas->sk_canvas()->clipRect(gfx::RectFToSkRect(scaled_bounds), | 63 canvas->sk_canvas()->clipRect(gfx::RectFToSkRect(scaled_bounds), |
| 65 SkClipOp::kDifference, true); | 64 SkClipOp::kDifference, true); |
| 66 canvas->DrawColor(color_); | 65 canvas->DrawColor(color_); |
| 67 } | 66 } |
| 68 | 67 |
| 69 gfx::Insets SolidSidedBorder::GetInsets() const { | 68 gfx::Insets SolidSidedBorder::GetInsets() const { |
| 70 return insets_; | 69 return insets_; |
| 71 } | 70 } |
| 72 | 71 |
| 73 gfx::Size SolidSidedBorder::GetMinimumSize() const { | 72 gfx::Size SolidSidedBorder::GetMinimumSize() const { |
| 74 return gfx::Size(insets_.width(), insets_.height()); | 73 return gfx::Size(insets_.width(), insets_.height()); |
| 75 } | 74 } |
| 76 | 75 |
| 76 gfx::RectF SolidSidedBorder::ScaledBounds(const View& view, float dsf) const { |
| 77 gfx::RectF scaled_bounds; |
| 78 if (view.layer()) { |
| 79 scaled_bounds = |
| 80 gfx::RectF(ui::ConvertRectToPixel(view.layer(), view.GetLocalBounds())); |
| 81 } else { |
| 82 scaled_bounds = gfx::RectF(view.GetLocalBounds()); |
| 83 scaled_bounds.Scale(dsf); |
| 84 } |
| 85 |
| 86 return scaled_bounds; |
| 87 } |
| 88 |
| 89 // Subclass of SolidSidedBorder which handles rounding correctly for a view |
| 90 // drawn to a root widget. |
| 91 class SolidSidedWidgetBorder : public SolidSidedBorder { |
| 92 public: |
| 93 SolidSidedWidgetBorder(const gfx::Insets& insets, SkColor color); |
| 94 |
| 95 private: |
| 96 // Overridden from SolidSidedBorder: |
| 97 gfx::RectF ScaledBounds(const View& view, float dsf) const override; |
| 98 }; |
| 99 |
| 100 SolidSidedWidgetBorder::SolidSidedWidgetBorder(const gfx::Insets& insets, |
| 101 SkColor color) |
| 102 : SolidSidedBorder(insets, color) {} |
| 103 |
| 104 gfx::RectF SolidSidedWidgetBorder::ScaledBounds(const View& view, |
| 105 float dsf) const { |
| 106 return gfx::RectF(gfx::ScaleToEnclosedRect(view.GetLocalBounds(), dsf)); |
| 107 } |
| 108 |
| 77 // A border with a rounded rectangle and single color. | 109 // A border with a rounded rectangle and single color. |
| 78 class RoundedRectBorder : public Border { | 110 class RoundedRectBorder : public Border { |
| 79 public: | 111 public: |
| 80 RoundedRectBorder(int thickness, int corner_radius, SkColor color); | 112 RoundedRectBorder(int thickness, int corner_radius, SkColor color); |
| 81 | 113 |
| 82 // Overridden from Border: | 114 // Overridden from Border: |
| 83 void Paint(const View& view, gfx::Canvas* canvas) override; | 115 void Paint(const View& view, gfx::Canvas* canvas) override; |
| 84 gfx::Insets GetInsets() const override; | 116 gfx::Insets GetInsets() const override; |
| 85 gfx::Size GetMinimumSize() const override; | 117 gfx::Size GetMinimumSize() const override; |
| 86 | 118 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 Border::~Border() {} | 254 Border::~Border() {} |
| 223 | 255 |
| 224 std::unique_ptr<Border> NullBorder() { | 256 std::unique_ptr<Border> NullBorder() { |
| 225 return nullptr; | 257 return nullptr; |
| 226 } | 258 } |
| 227 | 259 |
| 228 std::unique_ptr<Border> CreateSolidBorder(int thickness, SkColor color) { | 260 std::unique_ptr<Border> CreateSolidBorder(int thickness, SkColor color) { |
| 229 return base::MakeUnique<SolidSidedBorder>(gfx::Insets(thickness), color); | 261 return base::MakeUnique<SolidSidedBorder>(gfx::Insets(thickness), color); |
| 230 } | 262 } |
| 231 | 263 |
| 264 std::unique_ptr<Border> CreateSolidWidgetBorder(int thickness, SkColor color) { |
| 265 return base::MakeUnique<SolidSidedWidgetBorder>(gfx::Insets(thickness), |
| 266 color); |
| 267 } |
| 268 |
| 232 std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) { | 269 std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) { |
| 233 return base::MakeUnique<EmptyBorder>(insets); | 270 return base::MakeUnique<EmptyBorder>(insets); |
| 234 } | 271 } |
| 235 | 272 |
| 236 std::unique_ptr<Border> CreateRoundedRectBorder(int thickness, | 273 std::unique_ptr<Border> CreateRoundedRectBorder(int thickness, |
| 237 int corner_radius, | 274 int corner_radius, |
| 238 SkColor color) { | 275 SkColor color) { |
| 239 return base::MakeUnique<RoundedRectBorder>(thickness, corner_radius, color); | 276 return base::MakeUnique<RoundedRectBorder>(thickness, corner_radius, color); |
| 240 } | 277 } |
| 241 | 278 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 259 const gfx::Insets& insets) { | 296 const gfx::Insets& insets) { |
| 260 return base::MakeUnique<ExtraInsetsBorder>(std::move(border), insets); | 297 return base::MakeUnique<ExtraInsetsBorder>(std::move(border), insets); |
| 261 } | 298 } |
| 262 | 299 |
| 263 std::unique_ptr<Border> CreateBorderPainter(std::unique_ptr<Painter> painter, | 300 std::unique_ptr<Border> CreateBorderPainter(std::unique_ptr<Painter> painter, |
| 264 const gfx::Insets& insets) { | 301 const gfx::Insets& insets) { |
| 265 return base::WrapUnique(new BorderPainter(std::move(painter), insets)); | 302 return base::WrapUnique(new BorderPainter(std::move(painter), insets)); |
| 266 } | 303 } |
| 267 | 304 |
| 268 } // namespace views | 305 } // namespace views |
| OLD | NEW |