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/background.h" | 5 #include "ui/views/background.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "skia/ext/skia_utils_win.h" | 8 #include "skia/ext/skia_utils_win.h" |
| 9 #include "third_party/skia/include/core/SkPaint.h" | 9 #include "third_party/skia/include/core/SkPaint.h" |
| 10 #include "third_party/skia/include/core/SkPath.h" | |
| 11 #include "third_party/skia/include/pathops/SkPathOps.h" | |
| 10 #include "ui/gfx/canvas.h" | 12 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/color_utils.h" | 13 #include "ui/gfx/color_utils.h" |
| 12 #include "ui/views/painter.h" | 14 #include "ui/views/painter.h" |
| 13 #include "ui/views/view.h" | 15 #include "ui/views/view.h" |
| 14 | 16 |
| 15 namespace views { | 17 namespace views { |
| 16 | 18 |
| 17 // SolidBackground is a trivial Background implementation that fills the | 19 // SolidBackground is a trivial Background implementation that fills the |
| 18 // background in a solid color. | 20 // background in a solid color. |
| 19 class SolidBackground : public Background { | 21 class SolidBackground : public Background { |
| 20 public: | 22 public: |
| 21 explicit SolidBackground(SkColor color) { | 23 explicit SolidBackground(SkColor color) { |
| 22 SetNativeControlColor(color); | 24 SetNativeControlColor(color); |
| 23 } | 25 } |
| 24 | 26 |
| 25 void Paint(gfx::Canvas* canvas, View* view) const override { | 27 void Paint(gfx::Canvas* canvas, View* view) const override { |
| 26 // Fill the background. Note that we don't constrain to the bounds as | 28 // Fill the background. Note that we don't constrain to the bounds as |
| 27 // canvas is already clipped for us. | 29 // canvas is already clipped for us. |
| 28 canvas->DrawColor(get_color()); | 30 canvas->DrawColor(get_color()); |
| 29 } | 31 } |
| 30 | 32 |
| 31 private: | 33 private: |
| 32 DISALLOW_COPY_AND_ASSIGN(SolidBackground); | 34 DISALLOW_COPY_AND_ASSIGN(SolidBackground); |
| 33 }; | 35 }; |
| 34 | 36 |
| 37 // SolidScaledBackground renders a solid background color, with a one pixel | |
| 38 // border. This accounts for the scaling of the canvas, so that the border is | |
| 39 // one pixel regardless of display scaling. Optionally the border can have | |
| 40 // rounded corners. | |
| 41 class SolidScaledBackground : public Background { | |
| 42 public: | |
| 43 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.
| |
| 44 : border_color_(border), round_(round) { | |
| 45 SetNativeControlColor(background); | |
| 46 } | |
| 47 | |
| 48 void Paint(gfx::Canvas* canvas, View* view) const override { | |
| 49 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.
| |
| 50 const float scale = canvas->SaveAndUnscale(); | |
| 51 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.
| |
| 52 border_rect_f.Scale(scale); | |
| 53 | |
| 54 SkPaint stroke_paint; | |
| 55 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.
| |
| 56 stroke_paint.setStrokeWidth(1); | |
| 57 stroke_paint.setColor(border_color_); | |
| 58 stroke_paint.setAntiAlias(true); | |
| 59 | |
| 60 SkPaint fill_paint; | |
| 61 fill_paint.setStyle(SkPaint::kFill_Style); | |
| 62 fill_paint.setColor(get_color()); | |
| 63 fill_paint.setAntiAlias(true); | |
| 64 | |
| 65 SkPath path; | |
| 66 if (round_) { | |
| 67 border_rect_f.Inset(kOffset, kOffset); | |
| 68 const SkScalar kCornerRadius = SkDoubleToScalar(2.5f * scale); | |
| 69 path.addRoundRect(gfx::RectFToSkRect(border_rect_f), kCornerRadius, | |
| 70 kCornerRadius); | |
| 71 } else { | |
| 72 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.
| |
| 73 path.addRect(gfx::RectFToSkRect(border_rect_f)); | |
| 74 } | |
| 75 | |
| 76 SkPath stroke_path; | |
| 77 stroke_paint.getFillPath(path, &stroke_path); | |
| 78 | |
| 79 SkPath fill_path; | |
| 80 Op(path, stroke_path, kDifference_SkPathOp, &fill_path); | |
| 81 canvas->sk_canvas()->drawPath(fill_path, fill_paint); | |
| 82 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.
| |
| 83 canvas->Restore(); | |
| 84 } | |
| 85 | |
| 86 private: | |
| 87 // Color for the one pixel border. | |
| 88 SkColor border_color_; | |
| 89 | |
| 90 // 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.
| |
| 91 bool round_; | |
| 92 | |
| 93 DISALLOW_COPY_AND_ASSIGN(SolidScaledBackground); | |
| 94 }; | |
| 95 | |
| 35 class BackgroundPainter : public Background { | 96 class BackgroundPainter : public Background { |
| 36 public: | 97 public: |
| 37 BackgroundPainter(bool owns_painter, Painter* painter) | 98 BackgroundPainter(bool owns_painter, Painter* painter) |
| 38 : owns_painter_(owns_painter), painter_(painter) { | 99 : owns_painter_(owns_painter), painter_(painter) { |
| 39 DCHECK(painter); | 100 DCHECK(painter); |
| 40 } | 101 } |
| 41 | 102 |
| 42 ~BackgroundPainter() override { | 103 ~BackgroundPainter() override { |
| 43 if (owns_painter_) | 104 if (owns_painter_) |
| 44 delete painter_; | 105 delete painter_; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 native_control_brush_ = CreateSolidBrush(skia::SkColorToCOLORREF(color_)); | 144 native_control_brush_ = CreateSolidBrush(skia::SkColorToCOLORREF(color_)); |
| 84 return native_control_brush_; | 145 return native_control_brush_; |
| 85 } | 146 } |
| 86 #endif | 147 #endif |
| 87 | 148 |
| 88 // static | 149 // static |
| 89 Background* Background::CreateSolidBackground(SkColor color) { | 150 Background* Background::CreateSolidBackground(SkColor color) { |
| 90 return new SolidBackground(color); | 151 return new SolidBackground(color); |
| 91 } | 152 } |
| 92 | 153 |
| 154 Background* Background::CreateSolidScaledBackground(SkColor background, | |
| 155 SkColor border, | |
| 156 bool round) { | |
| 157 return new SolidScaledBackground(background, border, round); | |
| 158 } | |
| 159 | |
| 93 // static | 160 // static |
| 94 Background* Background::CreateStandardPanelBackground() { | 161 Background* Background::CreateStandardPanelBackground() { |
| 95 // TODO(beng): Should be in NativeTheme. | 162 // TODO(beng): Should be in NativeTheme. |
| 96 return CreateSolidBackground(SK_ColorWHITE); | 163 return CreateSolidBackground(SK_ColorWHITE); |
| 97 } | 164 } |
| 98 | 165 |
| 99 // static | 166 // static |
| 100 Background* Background::CreateVerticalGradientBackground(SkColor color1, | 167 Background* Background::CreateVerticalGradientBackground(SkColor color1, |
| 101 SkColor color2) { | 168 SkColor color2) { |
| 102 Background* background = CreateBackgroundPainter( | 169 Background* background = CreateBackgroundPainter( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 120 return background; | 187 return background; |
| 121 } | 188 } |
| 122 | 189 |
| 123 // static | 190 // static |
| 124 Background* Background::CreateBackgroundPainter(bool owns_painter, | 191 Background* Background::CreateBackgroundPainter(bool owns_painter, |
| 125 Painter* painter) { | 192 Painter* painter) { |
| 126 return new BackgroundPainter(owns_painter, painter); | 193 return new BackgroundPainter(owns_painter, painter); |
| 127 } | 194 } |
| 128 | 195 |
| 129 } // namespace views | 196 } // namespace views |
| OLD | NEW |