Chromium Code Reviews| 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/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/geometry/rect_f.h" | 14 #include "ui/gfx/geometry/rect_f.h" |
| 15 #include "ui/gfx/scoped_canvas.h" | |
| 15 #include "ui/views/painter.h" | 16 #include "ui/views/painter.h" |
| 16 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
| 17 | 18 |
| 18 namespace views { | 19 namespace views { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // A simple border with different thicknesses on each side and single color. | 23 // A simple border with different thicknesses on each side and single color. |
| 23 class SolidSidedBorder : public Border { | 24 class SolidSidedBorder : public Border { |
| 24 public: | 25 public: |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 35 | 36 |
| 36 DISALLOW_COPY_AND_ASSIGN(SolidSidedBorder); | 37 DISALLOW_COPY_AND_ASSIGN(SolidSidedBorder); |
| 37 }; | 38 }; |
| 38 | 39 |
| 39 SolidSidedBorder::SolidSidedBorder(const gfx::Insets& insets, SkColor color) | 40 SolidSidedBorder::SolidSidedBorder(const gfx::Insets& insets, SkColor color) |
| 40 : insets_(insets), | 41 : insets_(insets), |
| 41 color_(color) { | 42 color_(color) { |
| 42 } | 43 } |
| 43 | 44 |
| 44 void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) { | 45 void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) { |
| 45 // Top border. | 46 // Undo DSF so that we can be sure to draw an integral number of pixels for |
| 46 canvas->FillRect(gfx::Rect(0, 0, view.width(), insets_.top()), color_); | 47 // the border. Integral scale factors should be unaffected by this, but for |
| 47 // Left border. | 48 // fractional scale factors this ensures sharp lines. |
| 48 canvas->FillRect(gfx::Rect(0, insets_.top(), insets_.left(), | 49 gfx::ScopedCanvas scoped(canvas); |
| 49 view.height() - insets_.height()), color_); | 50 float dsf = canvas->UndoDeviceScaleFactor(); |
| 50 // Bottom border. | 51 |
| 51 canvas->FillRect(gfx::Rect(0, view.height() - insets_.bottom(), view.width(), | 52 gfx::RectF bounds(view.GetLocalBounds()); |
| 52 insets_.bottom()), color_); | 53 bounds.Scale(dsf); |
| 53 // Right border. | 54 // This scaling operation floors the inset values. |
| 54 canvas->FillRect(gfx::Rect(view.width() - insets_.right(), insets_.top(), | 55 bounds.Inset(insets_.Scale(dsf)); |
| 55 insets_.right(), view.height() - insets_.height()), | 56 // If the view has a layer, it will be snapped to pixel bounds, giving the |
| 56 color_); | 57 // canvas integral dimensions. Therefore we have to round up. Otherwise, no |
| 58 // rounding is necessary as the view's canvas won't be integral. | |
| 59 if (view.layer()) | |
| 60 bounds = gfx::RectF(gfx::ToEnclosingRect(bounds)); | |
|
Peter Kasting
2017/02/18 00:08:28
Why ToEnclosingRect() and not ToEnclosedRect()? T
ericrk
2017/02/18 00:37:25
Agreed.
Interestingly, it appears that the view.l
| |
| 61 canvas->ClipRect(bounds, SkClipOp::kDifference); | |
| 62 canvas->DrawColor(color_); | |
| 57 } | 63 } |
| 58 | 64 |
| 59 gfx::Insets SolidSidedBorder::GetInsets() const { | 65 gfx::Insets SolidSidedBorder::GetInsets() const { |
| 60 return insets_; | 66 return insets_; |
| 61 } | 67 } |
| 62 | 68 |
| 63 gfx::Size SolidSidedBorder::GetMinimumSize() const { | 69 gfx::Size SolidSidedBorder::GetMinimumSize() const { |
| 64 return gfx::Size(insets_.width(), insets_.height()); | 70 return gfx::Size(insets_.width(), insets_.height()); |
| 65 } | 71 } |
| 66 | 72 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 const gfx::Insets& insets) { | 255 const gfx::Insets& insets) { |
| 250 return base::MakeUnique<ExtraInsetsBorder>(std::move(border), insets); | 256 return base::MakeUnique<ExtraInsetsBorder>(std::move(border), insets); |
| 251 } | 257 } |
| 252 | 258 |
| 253 std::unique_ptr<Border> CreateBorderPainter(std::unique_ptr<Painter> painter, | 259 std::unique_ptr<Border> CreateBorderPainter(std::unique_ptr<Painter> painter, |
| 254 const gfx::Insets& insets) { | 260 const gfx::Insets& insets) { |
| 255 return base::WrapUnique(new BorderPainter(std::move(painter), insets)); | 261 return base::WrapUnique(new BorderPainter(std::move(painter), insets)); |
| 256 } | 262 } |
| 257 | 263 |
| 258 } // namespace views | 264 } // namespace views |
| OLD | NEW |