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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "third_party/skia/include/core/SkPaint.h" | |
9 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
10 #include "ui/views/painter.h" | 11 #include "ui/views/painter.h" |
11 #include "ui/views/view.h" | 12 #include "ui/views/view.h" |
12 | 13 |
13 namespace views { | 14 namespace views { |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // A simple border with different thicknesses on each side and single color. | 18 // A simple border with different thicknesses on each side and single color. |
18 class SolidSidedBorder : public Border { | 19 class SolidSidedBorder : public Border { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 } | 53 } |
53 | 54 |
54 gfx::Insets SolidSidedBorder::GetInsets() const { | 55 gfx::Insets SolidSidedBorder::GetInsets() const { |
55 return insets_; | 56 return insets_; |
56 } | 57 } |
57 | 58 |
58 gfx::Size SolidSidedBorder::GetMinimumSize() const { | 59 gfx::Size SolidSidedBorder::GetMinimumSize() const { |
59 return gfx::Size(insets_.width(), insets_.height()); | 60 return gfx::Size(insets_.width(), insets_.height()); |
60 } | 61 } |
61 | 62 |
63 // A border with a rounded rectangle and single color. | |
64 class RoundedRectBorder : public Border { | |
65 public: | |
66 RoundedRectBorder(int thickness, int corner_radius, SkColor color); | |
67 | |
68 // Overridden from Border: | |
69 void Paint(const View& view, gfx::Canvas* canvas) override; | |
70 gfx::Insets GetInsets() const override; | |
71 gfx::Size GetMinimumSize() const override; | |
72 | |
73 private: | |
74 const int thickness_; | |
75 const int corner_radius_; | |
76 const SkColor color_; | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(RoundedRectBorder); | |
79 }; | |
80 | |
81 RoundedRectBorder::RoundedRectBorder(int thickness, | |
82 int corner_radius, | |
83 SkColor color) | |
84 : thickness_(thickness), corner_radius_(corner_radius), color_(color) {} | |
85 | |
86 void RoundedRectBorder::Paint(const View& view, gfx::Canvas* canvas) { | |
87 SkPaint paint; | |
88 paint.setStrokeWidth(thickness_); | |
89 paint.setColor(color_); | |
90 paint.setStyle(SkPaint::kStroke_Style); | |
91 paint.setAntiAlias(true); | |
92 int half_thickness = thickness_ / 2; | |
Evan Stade
2015/10/29 18:36:18
I don't think this is correct. For thickness_ == 1
Matt Giuca
2015/11/09 04:44:57
Ah good catch. Well I can't use floating point num
Evan Stade
2015/11/09 18:41:48
No, you really do need to use floats, or any odd-n
Matt Giuca
2015/11/10 00:15:06
Oh, yeah that fixes the 1px problem I was having (
Evan Stade
2015/11/10 00:22:06
Yea, I was thinking the same thing, although I'd t
Matt Giuca
2015/11/10 00:28:28
OK, I am "weakly" deprecating it by adding a comme
| |
93 canvas->DrawRoundRect( | |
94 gfx::Rect(half_thickness, half_thickness, view.width() - thickness_, | |
95 view.height() - thickness_), | |
Evan Stade
2015/10/29 18:36:18
I would do
gfx::Rect bounds = view.GetLocalBounds
Matt Giuca
2015/11/09 04:44:57
That's neat. Done.
| |
96 corner_radius_, paint); | |
97 } | |
98 | |
99 gfx::Insets RoundedRectBorder::GetInsets() const { | |
100 return gfx::Insets(thickness_, thickness_, thickness_, thickness_); | |
101 } | |
102 | |
103 gfx::Size RoundedRectBorder::GetMinimumSize() const { | |
104 return gfx::Size(thickness_ * 2, thickness_ * 2); | |
105 } | |
106 | |
62 class EmptyBorder : public Border { | 107 class EmptyBorder : public Border { |
63 public: | 108 public: |
64 explicit EmptyBorder(const gfx::Insets& insets); | 109 explicit EmptyBorder(const gfx::Insets& insets); |
65 | 110 |
66 // Overridden from Border: | 111 // Overridden from Border: |
67 void Paint(const View& view, gfx::Canvas* canvas) override; | 112 void Paint(const View& view, gfx::Canvas* canvas) override; |
68 gfx::Insets GetInsets() const override ; | 113 gfx::Insets GetInsets() const override ; |
69 gfx::Size GetMinimumSize() const override; | 114 gfx::Size GetMinimumSize() const override; |
70 | 115 |
71 private: | 116 private: |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 return make_scoped_ptr(new SolidSidedBorder( | 185 return make_scoped_ptr(new SolidSidedBorder( |
141 gfx::Insets(thickness, thickness, thickness, thickness), color)); | 186 gfx::Insets(thickness, thickness, thickness, thickness), color)); |
142 } | 187 } |
143 | 188 |
144 // static | 189 // static |
145 scoped_ptr<Border> Border::CreateEmptyBorder(const gfx::Insets& insets) { | 190 scoped_ptr<Border> Border::CreateEmptyBorder(const gfx::Insets& insets) { |
146 return make_scoped_ptr(new EmptyBorder(insets)); | 191 return make_scoped_ptr(new EmptyBorder(insets)); |
147 } | 192 } |
148 | 193 |
149 // static | 194 // static |
195 scoped_ptr<Border> Border::CreateRoundedRectBorder(int thickness, | |
196 int corner_radius, | |
197 SkColor color) { | |
198 return make_scoped_ptr( | |
199 new RoundedRectBorder(thickness, corner_radius, color)); | |
200 } | |
201 | |
202 // static | |
150 scoped_ptr<Border> Border::CreateEmptyBorder(int top, | 203 scoped_ptr<Border> Border::CreateEmptyBorder(int top, |
151 int left, | 204 int left, |
152 int bottom, | 205 int bottom, |
153 int right) { | 206 int right) { |
154 return CreateEmptyBorder(gfx::Insets(top, left, bottom, right)); | 207 return CreateEmptyBorder(gfx::Insets(top, left, bottom, right)); |
155 } | 208 } |
156 | 209 |
157 // static | 210 // static |
158 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, | 211 scoped_ptr<Border> Border::CreateSolidSidedBorder(int top, |
159 int left, | 212 int left, |
160 int bottom, | 213 int bottom, |
161 int right, | 214 int right, |
162 SkColor color) { | 215 SkColor color) { |
163 return make_scoped_ptr(new SolidSidedBorder( | 216 return make_scoped_ptr(new SolidSidedBorder( |
164 gfx::Insets(top, left, bottom, right), color)); | 217 gfx::Insets(top, left, bottom, right), color)); |
165 } | 218 } |
166 | 219 |
167 // static | 220 // static |
168 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, | 221 scoped_ptr<Border> Border::CreateBorderPainter(Painter* painter, |
169 const gfx::Insets& insets) { | 222 const gfx::Insets& insets) { |
170 return make_scoped_ptr(new BorderPainter(painter, insets)); | 223 return make_scoped_ptr(new BorderPainter(painter, insets)); |
171 } | 224 } |
172 | 225 |
173 } // namespace views | 226 } // namespace views |
OLD | NEW |