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

Unified Diff: ui/views/background.cc

Issue 1394763003: Update LocationBarView Background (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactor Painting into a Background class Created 5 years, 2 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
« ui/views/background.h ('K') | « ui/views/background.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/background.cc
diff --git a/ui/views/background.cc b/ui/views/background.cc
index 62cdc3b4cc613058f53f083cccaa7cfde8e808de..ac28ca8abb99c53ce1f00edfd7091bb0c86c32bc 100644
--- a/ui/views/background.cc
+++ b/ui/views/background.cc
@@ -7,6 +7,8 @@
#include "base/logging.h"
#include "skia/ext/skia_utils_win.h"
#include "third_party/skia/include/core/SkPaint.h"
+#include "third_party/skia/include/core/SkPath.h"
+#include "third_party/skia/include/pathops/SkPathOps.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_utils.h"
#include "ui/views/painter.h"
@@ -32,6 +34,65 @@ class SolidBackground : public Background {
DISALLOW_COPY_AND_ASSIGN(SolidBackground);
};
+// SolidScaledBackground renders a solid background color, with a one pixel
+// border. This accounts for the scaling of the canvas, so that the border is
+// one pixel regardless of display scaling. Optionally the border can have
+// rounded corners.
+class SolidScaledBackground : public Background {
+ public:
+ explicit SolidScaledBackground(SkColor background, SkColor border, bool round)
Peter Kasting 2015/10/19 21:09:44 Nit: No explicit (only used for 1-arg constructors
jonross 2015/10/20 18:54:53 Done.
+ : border_color_(border), round_(round) {
+ SetNativeControlColor(background);
+ }
+
+ void Paint(gfx::Canvas* canvas, View* view) const override {
+ gfx::RectF border_rect_f(view->GetContentsBounds());
Peter Kasting 2015/10/19 21:09:44 Nit: Declare |border_rect_f| and |kOffset| right a
jonross 2015/10/20 18:54:54 Done.
+ const float scale = canvas->SaveAndUnscale();
+ const float kOffset = scale > 1.0f ? 1.5f : 0.5f;
Peter Kasting 2015/10/19 21:09:44 I don't think this gets the right result for scale
jonross 2015/10/20 18:54:54 I spoke with him, he wants the 200% inset by one p
Peter Kasting 2015/10/20 23:04:05 Yes, and my suggestions should accomplish that, bu
jonross 2015/10/21 14:52:55 Done.
+ border_rect_f.Scale(scale);
+
+ SkPaint stroke_paint;
+ stroke_paint.setStyle(SkPaint::Style::kStroke_Style);
Peter Kasting 2015/10/19 21:09:44 Nit: No need for "Style::"
jonross 2015/10/20 18:54:53 Done.
+ stroke_paint.setStrokeWidth(1);
+ stroke_paint.setColor(border_color_);
+ stroke_paint.setAntiAlias(true);
+
+ SkPaint fill_paint;
+ fill_paint.setStyle(SkPaint::kFill_Style);
+ fill_paint.setColor(get_color());
+ fill_paint.setAntiAlias(true);
+
+ SkPath path;
+ if (round_) {
+ border_rect_f.Inset(kOffset, kOffset);
+ const SkScalar kCornerRadius = SkDoubleToScalar(2.5f * scale);
+ path.addRoundRect(gfx::RectFToSkRect(border_rect_f), kCornerRadius,
+ kCornerRadius);
+ } else {
+ border_rect_f.Inset(-kOffset, kOffset);
Peter Kasting 2015/10/19 21:09:44 I don't think this is right for non-1x. It seems
jonross 2015/10/20 18:54:54 Done.
+ path.addRect(gfx::RectFToSkRect(border_rect_f));
+ }
+
+ SkPath stroke_path;
+ stroke_paint.getFillPath(path, &stroke_path);
+
+ SkPath fill_path;
+ Op(path, stroke_path, kDifference_SkPathOp, &fill_path);
+ canvas->sk_canvas()->drawPath(fill_path, fill_paint);
+ canvas->sk_canvas()->drawPath(path, stroke_paint);
Peter Kasting 2015/10/19 21:09:44 Nit: I think it might be slightly more performant
jonross 2015/10/20 18:54:53 Done.
+ canvas->Restore();
+ }
+
+ private:
+ // Color for the one pixel border.
+ SkColor border_color_;
+
+ // If true the rect will be a rounded rect.
Peter Kasting 2015/10/19 21:09:44 Nit: Maybe "If true the border and interior corner
Evan Stade 2015/10/19 23:21:30 I don't really care what this comment says because
jonross 2015/10/20 18:54:54 Done.
+ bool round_;
+
+ DISALLOW_COPY_AND_ASSIGN(SolidScaledBackground);
+};
+
class BackgroundPainter : public Background {
public:
BackgroundPainter(bool owns_painter, Painter* painter)
@@ -90,6 +151,12 @@ Background* Background::CreateSolidBackground(SkColor color) {
return new SolidBackground(color);
}
+Background* Background::CreateSolidScaledBackground(SkColor background,
+ SkColor border,
+ bool round) {
+ return new SolidScaledBackground(background, border, round);
+}
+
// static
Background* Background::CreateStandardPanelBackground() {
// TODO(beng): Should be in NativeTheme.
« ui/views/background.h ('K') | « ui/views/background.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698