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/bubble/bubble_border.h" | 5 #include "ui/views/bubble/bubble_border.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
msw
2014/08/22 20:54:44
nit: remove this it should be added to the header.
bruthig
2014/08/22 22:55:02
Done.
| |
11 #include "grit/ui_resources.h" | 11 #include "grit/ui_resources.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
14 #include "ui/gfx/image/image_skia.h" | 14 #include "ui/gfx/image/image_skia.h" |
msw
2014/08/22 20:54:44
nit: remove this, it's now in the header.
bruthig
2014/08/22 22:55:02
Done.
| |
15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
16 #include "ui/gfx/skia_util.h" | 16 #include "ui/gfx/skia_util.h" |
17 #include "ui/views/painter.h" | 17 #include "ui/views/painter.h" |
18 #include "ui/views/view.h" | 18 #include "ui/views/view.h" |
19 | 19 |
20 namespace views { | 20 namespace views { |
21 | 21 |
22 namespace internal { | 22 namespace internal { |
23 | 23 |
24 // A helper that combines each border image-set painter with arrows and metrics. | |
25 struct BorderImages { | |
26 BorderImages(const int border_image_ids[], | |
27 const int arrow_image_ids[], | |
28 int border_interior_thickness, | |
29 int arrow_interior_thickness, | |
30 int corner_radius); | |
31 | |
32 scoped_ptr<Painter> border_painter; | |
33 gfx::ImageSkia left_arrow; | |
34 gfx::ImageSkia top_arrow; | |
35 gfx::ImageSkia right_arrow; | |
36 gfx::ImageSkia bottom_arrow; | |
37 | |
38 // The thickness of border and arrow images and their interior areas. | |
39 // Thickness is the width of left/right and the height of top/bottom images. | |
40 // The interior is measured without including stroke or shadow pixels. | |
41 int border_thickness; | |
42 int border_interior_thickness; | |
43 int arrow_thickness; | |
44 int arrow_interior_thickness; | |
45 // The corner radius of the bubble's rounded-rect interior area. | |
46 int corner_radius; | |
47 }; | |
48 | |
49 BorderImages::BorderImages(const int border_image_ids[], | 24 BorderImages::BorderImages(const int border_image_ids[], |
50 const int arrow_image_ids[], | 25 const int arrow_image_ids[], |
51 int border_interior_thickness, | 26 int border_interior_thickness, |
52 int arrow_interior_thickness, | 27 int arrow_interior_thickness, |
53 int corner_radius) | 28 int corner_radius) |
54 : border_painter(Painter::CreateImageGridPainter(border_image_ids)), | 29 : border_painter(Painter::CreateImageGridPainter(border_image_ids)), |
55 border_thickness(0), | 30 border_thickness(0), |
56 border_interior_thickness(border_interior_thickness), | 31 border_interior_thickness(border_interior_thickness), |
57 arrow_thickness(0), | 32 arrow_thickness(0), |
58 arrow_interior_thickness(arrow_interior_thickness), | 33 arrow_interior_thickness(arrow_interior_thickness), |
59 corner_radius(corner_radius) { | 34 corner_radius(corner_radius) { |
60 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 35 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
61 border_thickness = rb.GetImageSkiaNamed(border_image_ids[0])->width(); | 36 border_thickness = rb.GetImageSkiaNamed(border_image_ids[0])->width(); |
62 if (arrow_image_ids[0] != 0) { | 37 if (arrow_image_ids[0] != 0) { |
63 left_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[0]); | 38 left_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[0]); |
64 top_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[1]); | 39 top_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[1]); |
65 right_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[2]); | 40 right_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[2]); |
66 bottom_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[3]); | 41 bottom_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[3]); |
67 arrow_thickness = top_arrow.height(); | 42 arrow_thickness = top_arrow.height(); |
68 } | 43 } |
69 } | 44 } |
70 | 45 |
46 BorderImages::~BorderImages() {} | |
47 | |
71 } // namespace internal | 48 } // namespace internal |
72 | 49 |
73 namespace { | 50 namespace { |
74 | 51 |
75 // The border and arrow stroke size used in image assets, in pixels. | |
76 const int kStroke = 1; | |
77 | |
78 // Bubble border and arrow image resource ids. They don't use the IMAGE_GRID | 52 // Bubble border and arrow image resource ids. They don't use the IMAGE_GRID |
79 // macro because there is no center image. | 53 // macro because there is no center image. |
80 const int kNoShadowImages[] = { | 54 const int kNoShadowImages[] = { |
81 IDR_BUBBLE_TL, IDR_BUBBLE_T, IDR_BUBBLE_TR, | 55 IDR_BUBBLE_TL, IDR_BUBBLE_T, IDR_BUBBLE_TR, |
82 IDR_BUBBLE_L, 0, IDR_BUBBLE_R, | 56 IDR_BUBBLE_L, 0, IDR_BUBBLE_R, |
83 IDR_BUBBLE_BL, IDR_BUBBLE_B, IDR_BUBBLE_BR }; | 57 IDR_BUBBLE_BL, IDR_BUBBLE_B, IDR_BUBBLE_BR }; |
84 const int kNoShadowArrows[] = { | 58 const int kNoShadowArrows[] = { |
85 IDR_BUBBLE_L_ARROW, IDR_BUBBLE_T_ARROW, | 59 IDR_BUBBLE_L_ARROW, IDR_BUBBLE_T_ARROW, |
86 IDR_BUBBLE_R_ARROW, IDR_BUBBLE_B_ARROW, }; | 60 IDR_BUBBLE_R_ARROW, IDR_BUBBLE_B_ARROW, }; |
87 | 61 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 case BubbleBorder::SHADOW_COUNT: | 117 case BubbleBorder::SHADOW_COUNT: |
144 NOTREACHED(); | 118 NOTREACHED(); |
145 break; | 119 break; |
146 } | 120 } |
147 | 121 |
148 return set; | 122 return set; |
149 } | 123 } |
150 | 124 |
151 } // namespace | 125 } // namespace |
152 | 126 |
127 const int BubbleBorder::kStroke = 1; | |
128 | |
153 BubbleBorder::BubbleBorder(Arrow arrow, Shadow shadow, SkColor color) | 129 BubbleBorder::BubbleBorder(Arrow arrow, Shadow shadow, SkColor color) |
154 : arrow_(arrow), | 130 : arrow_(arrow), |
155 arrow_offset_(0), | 131 arrow_offset_(0), |
156 arrow_paint_type_(PAINT_NORMAL), | 132 arrow_paint_type_(PAINT_NORMAL), |
157 alignment_(ALIGN_ARROW_TO_MID_ANCHOR), | 133 alignment_(ALIGN_ARROW_TO_MID_ANCHOR), |
158 shadow_(shadow), | 134 shadow_(shadow), |
159 background_color_(color), | 135 background_color_(color), |
160 use_theme_background_color_(false) { | 136 use_theme_background_color_(false) { |
161 DCHECK(shadow < SHADOW_COUNT); | 137 DCHECK(shadow < SHADOW_COUNT); |
162 images_ = GetBorderImages(shadow); | 138 images_ = GetBorderImages(shadow); |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
271 const gfx::Insets insets = GetInsets(); | 247 const gfx::Insets insets = GetInsets(); |
272 size.Enlarge(insets.width(), insets.height()); | 248 size.Enlarge(insets.width(), insets.height()); |
273 | 249 |
274 // Ensure the bubble is large enough to not overlap border and arrow images. | 250 // Ensure the bubble is large enough to not overlap border and arrow images. |
275 const int min = 2 * images_->border_thickness; | 251 const int min = 2 * images_->border_thickness; |
276 const int min_with_arrow_width = min + images_->top_arrow.width(); | 252 const int min_with_arrow_width = min + images_->top_arrow.width(); |
277 const int min_with_arrow_thickness = images_->border_thickness + | 253 const int min_with_arrow_thickness = images_->border_thickness + |
278 std::max(images_->arrow_thickness + images_->border_interior_thickness, | 254 std::max(images_->arrow_thickness + images_->border_interior_thickness, |
279 images_->border_thickness); | 255 images_->border_thickness); |
280 // Only take arrow image sizes into account when the bubble tip is shown. | 256 // Only take arrow image sizes into account when the bubble tip is shown. |
281 if (arrow_paint_type_ == PAINT_TRANSPARENT || !has_arrow(arrow_)) | 257 if (arrow_paint_type_ == PAINT_NONE || !has_arrow(arrow_)) |
282 size.SetToMax(gfx::Size(min, min)); | 258 size.SetToMax(gfx::Size(min, min)); |
283 else if (is_arrow_on_horizontal(arrow_)) | 259 else if (is_arrow_on_horizontal(arrow_)) |
284 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness)); | 260 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness)); |
285 else | 261 else |
286 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width)); | 262 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width)); |
287 return size; | 263 return size; |
288 } | 264 } |
289 | 265 |
290 gfx::ImageSkia* BubbleBorder::GetArrowImage() const { | 266 gfx::ImageSkia* BubbleBorder::GetArrowImage() const { |
291 if (!has_arrow(arrow_)) | 267 if (!has_arrow(arrow_)) |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
372 SkPath path; | 348 SkPath path; |
373 gfx::Rect bounds(view->GetLocalBounds()); | 349 gfx::Rect bounds(view->GetLocalBounds()); |
374 bounds.Inset(border_->GetInsets()); | 350 bounds.Inset(border_->GetInsets()); |
375 | 351 |
376 SkScalar radius = SkIntToScalar(border_->GetBorderCornerRadius()); | 352 SkScalar radius = SkIntToScalar(border_->GetBorderCornerRadius()); |
377 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius); | 353 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius); |
378 canvas->DrawPath(path, paint); | 354 canvas->DrawPath(path, paint); |
379 } | 355 } |
380 | 356 |
381 } // namespace views | 357 } // namespace views |
OLD | NEW |