Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(852)

Unified Diff: ui/views/border.cc

Issue 2718533004: Add SolidSidedWidgetBorder
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/views/border.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/border.cc
diff --git a/ui/views/border.cc b/ui/views/border.cc
index 61b3b4a399423e1cf52c3f433597cb8493fe33de..129f903d9ec98e31aeab1c4dc26ef9fd19406bde 100644
--- a/ui/views/border.cc
+++ b/ui/views/border.cc
@@ -12,10 +12,12 @@
#include "cc/paint/paint_flags.h"
#include "ui/compositor/dip_util.h"
#include "ui/gfx/canvas.h"
+#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/views/painter.h"
#include "ui/views/view.h"
+#include "ui/views/widget/widget.h"
namespace views {
@@ -31,6 +33,11 @@ class SolidSidedBorder : public Border {
gfx::Insets GetInsets() const override;
gfx::Size GetMinimumSize() const override;
+ protected:
+ // Returns the scaled bounds to inset from. Default implementation works
+ // well for views that are not drawn directly to a layer / widget.
+ virtual gfx::RectF ScaledBounds(const View& view, float dsf) const;
+
private:
const gfx::Insets insets_;
const SkColor color_;
@@ -49,15 +56,7 @@ void SolidSidedBorder::Paint(const View& view, gfx::Canvas* canvas) {
// fractional scale factors this ensures sharp lines.
gfx::ScopedCanvas scoped(canvas);
float dsf = canvas->UndoDeviceScaleFactor();
-
- gfx::RectF scaled_bounds;
- if (view.layer()) {
- scaled_bounds =
- gfx::RectF(ui::ConvertRectToPixel(view.layer(), view.GetLocalBounds()));
- } else {
- scaled_bounds = gfx::RectF(view.GetLocalBounds());
- scaled_bounds.Scale(dsf);
- }
+ gfx::RectF scaled_bounds = ScaledBounds(view, dsf);
// This scaling operation floors the inset values.
scaled_bounds.Inset(insets_.Scale(dsf));
@@ -74,6 +73,39 @@ gfx::Size SolidSidedBorder::GetMinimumSize() const {
return gfx::Size(insets_.width(), insets_.height());
}
+gfx::RectF SolidSidedBorder::ScaledBounds(const View& view, float dsf) const {
+ gfx::RectF scaled_bounds;
+ if (view.layer()) {
+ scaled_bounds =
+ gfx::RectF(ui::ConvertRectToPixel(view.layer(), view.GetLocalBounds()));
+ } else {
+ scaled_bounds = gfx::RectF(view.GetLocalBounds());
+ scaled_bounds.Scale(dsf);
+ }
+
+ return scaled_bounds;
+}
+
+// Subclass of SolidSidedBorder which handles rounding correctly for a view
+// drawn to a root widget.
+class SolidSidedWidgetBorder : public SolidSidedBorder {
+ public:
+ SolidSidedWidgetBorder(const gfx::Insets& insets, SkColor color);
+
+ private:
+ // Overridden from SolidSidedBorder:
+ gfx::RectF ScaledBounds(const View& view, float dsf) const override;
+};
+
+SolidSidedWidgetBorder::SolidSidedWidgetBorder(const gfx::Insets& insets,
+ SkColor color)
+ : SolidSidedBorder(insets, color) {}
+
+gfx::RectF SolidSidedWidgetBorder::ScaledBounds(const View& view,
+ float dsf) const {
+ return gfx::RectF(gfx::ScaleToEnclosedRect(view.GetLocalBounds(), dsf));
+}
+
// A border with a rounded rectangle and single color.
class RoundedRectBorder : public Border {
public:
@@ -229,6 +261,11 @@ std::unique_ptr<Border> CreateSolidBorder(int thickness, SkColor color) {
return base::MakeUnique<SolidSidedBorder>(gfx::Insets(thickness), color);
}
+std::unique_ptr<Border> CreateSolidWidgetBorder(int thickness, SkColor color) {
+ return base::MakeUnique<SolidSidedWidgetBorder>(gfx::Insets(thickness),
+ color);
+}
+
std::unique_ptr<Border> CreateEmptyBorder(const gfx::Insets& insets) {
return base::MakeUnique<EmptyBorder>(insets);
}
« no previous file with comments | « ui/views/border.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698