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
3 // found in the LICENSE file.
4
5 #include "ui/views/bubble/bubble_border.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "grit/ui_resources.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/image/image_skia.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/skia_util.h"
17 #include "ui/views/painter.h"
18 #include "ui/views/view.h"
19
20 namespace views {
21
22 namespace internal {
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
BorderImages(const int border_image_ids[],const int arrow_image_ids[],int border_interior_thickness,int arrow_interior_thickness,int corner_radius)49 BorderImages::BorderImages(const int border_image_ids[],
50 const int arrow_image_ids[],
51 int border_interior_thickness,
52 int arrow_interior_thickness,
53 int corner_radius)
54 : border_painter(Painter::CreateImageGridPainter(border_image_ids)),
55 border_thickness(0),
56 border_interior_thickness(border_interior_thickness),
57 arrow_thickness(0),
58 arrow_interior_thickness(arrow_interior_thickness),
59 corner_radius(corner_radius) {
60 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
61 border_thickness = rb.GetImageSkiaNamed(border_image_ids[0])->width();
62 if (arrow_image_ids[0] != 0) {
63 left_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[0]);
64 top_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[1]);
65 right_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[2]);
66 bottom_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[3]);
67 arrow_thickness = top_arrow.height();
68 }
69 }
70
71 } // namespace internal
72
73 namespace {
74
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
79 // macro because there is no center image.
80 const int kNoShadowImages[] = {
81 IDR_BUBBLE_TL, IDR_BUBBLE_T, IDR_BUBBLE_TR,
82 IDR_BUBBLE_L, 0, IDR_BUBBLE_R,
83 IDR_BUBBLE_BL, IDR_BUBBLE_B, IDR_BUBBLE_BR };
84 const int kNoShadowArrows[] = {
85 IDR_BUBBLE_L_ARROW, IDR_BUBBLE_T_ARROW,
86 IDR_BUBBLE_R_ARROW, IDR_BUBBLE_B_ARROW, };
87
88 const int kBigShadowImages[] = {
89 IDR_WINDOW_BUBBLE_SHADOW_BIG_TOP_LEFT,
90 IDR_WINDOW_BUBBLE_SHADOW_BIG_TOP,
91 IDR_WINDOW_BUBBLE_SHADOW_BIG_TOP_RIGHT,
92 IDR_WINDOW_BUBBLE_SHADOW_BIG_LEFT,
93 0,
94 IDR_WINDOW_BUBBLE_SHADOW_BIG_RIGHT,
95 IDR_WINDOW_BUBBLE_SHADOW_BIG_BOTTOM_LEFT,
96 IDR_WINDOW_BUBBLE_SHADOW_BIG_BOTTOM,
97 IDR_WINDOW_BUBBLE_SHADOW_BIG_BOTTOM_RIGHT };
98 const int kBigShadowArrows[] = {
99 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_BIG_LEFT,
100 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_BIG_TOP,
101 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_BIG_RIGHT,
102 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_BIG_BOTTOM };
103
104 const int kSmallShadowImages[] = {
105 IDR_WINDOW_BUBBLE_SHADOW_SMALL_TOP_LEFT,
106 IDR_WINDOW_BUBBLE_SHADOW_SMALL_TOP,
107 IDR_WINDOW_BUBBLE_SHADOW_SMALL_TOP_RIGHT,
108 IDR_WINDOW_BUBBLE_SHADOW_SMALL_LEFT,
109 0,
110 IDR_WINDOW_BUBBLE_SHADOW_SMALL_RIGHT,
111 IDR_WINDOW_BUBBLE_SHADOW_SMALL_BOTTOM_LEFT,
112 IDR_WINDOW_BUBBLE_SHADOW_SMALL_BOTTOM,
113 IDR_WINDOW_BUBBLE_SHADOW_SMALL_BOTTOM_RIGHT };
114 const int kSmallShadowArrows[] = {
115 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_SMALL_LEFT,
116 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_SMALL_TOP,
117 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_SMALL_RIGHT,
118 IDR_WINDOW_BUBBLE_SHADOW_SPIKE_SMALL_BOTTOM };
119
120 using internal::BorderImages;
121
122 // Returns the cached BorderImages for the given |shadow| type.
GetBorderImages(BubbleBorder::Shadow shadow)123 BorderImages* GetBorderImages(BubbleBorder::Shadow shadow) {
124 // Keep a cache of bubble border image-set painters, arrows, and metrics.
125 static BorderImages* kBorderImages[BubbleBorder::SHADOW_COUNT] = { NULL };
126
127 CHECK_LT(shadow, BubbleBorder::SHADOW_COUNT);
128 struct BorderImages*& set = kBorderImages[shadow];
129 if (set)
130 return set;
131
132 switch (shadow) {
133 case BubbleBorder::NO_SHADOW:
134 case BubbleBorder::NO_SHADOW_OPAQUE_BORDER:
135 set = new BorderImages(kNoShadowImages, kNoShadowArrows, 6, 7, 4);
136 break;
137 case BubbleBorder::BIG_SHADOW:
138 set = new BorderImages(kBigShadowImages, kBigShadowArrows, 23, 9, 2);
139 break;
140 case BubbleBorder::SMALL_SHADOW:
141 set = new BorderImages(kSmallShadowImages, kSmallShadowArrows, 5, 6, 2);
142 break;
143 case BubbleBorder::SHADOW_COUNT:
144 NOTREACHED();
145 break;
146 }
147
148 return set;
149 }
150
151 } // namespace
152
BubbleBorder(Arrow arrow,Shadow shadow,SkColor color)153 BubbleBorder::BubbleBorder(Arrow arrow, Shadow shadow, SkColor color)
154 : arrow_(arrow),
155 arrow_offset_(0),
156 arrow_paint_type_(PAINT_NORMAL),
157 alignment_(ALIGN_ARROW_TO_MID_ANCHOR),
158 shadow_(shadow),
159 background_color_(color) {
160 DCHECK(shadow < SHADOW_COUNT);
161 images_ = GetBorderImages(shadow);
162 }
163
~BubbleBorder()164 BubbleBorder::~BubbleBorder() {}
165
GetBounds(const gfx::Rect & anchor_rect,const gfx::Size & contents_size) const166 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& anchor_rect,
167 const gfx::Size& contents_size) const {
168 int x = anchor_rect.x();
169 int y = anchor_rect.y();
170 int w = anchor_rect.width();
171 int h = anchor_rect.height();
172 const gfx::Size size(GetSizeForContentsSize(contents_size));
173 const int arrow_offset = GetArrowOffset(size);
174 const int arrow_size =
175 images_->arrow_interior_thickness + kStroke - images_->arrow_thickness;
176 const bool mid_anchor = alignment_ == ALIGN_ARROW_TO_MID_ANCHOR;
177
178 // Calculate the bubble coordinates based on the border and arrow settings.
179 if (is_arrow_on_horizontal(arrow_)) {
180 if (is_arrow_on_left(arrow_)) {
181 x += mid_anchor ? w / 2 - arrow_offset : kStroke - GetBorderThickness();
182 } else if (is_arrow_at_center(arrow_)) {
183 x += w / 2 - arrow_offset;
184 } else {
185 x += mid_anchor ? w / 2 + arrow_offset - size.width() :
186 w - size.width() + GetBorderThickness() - kStroke;
187 }
188 y += is_arrow_on_top(arrow_) ? h + arrow_size : -arrow_size - size.height();
189 } else if (has_arrow(arrow_)) {
190 x += is_arrow_on_left(arrow_) ? w + arrow_size : -arrow_size - size.width();
191 if (is_arrow_on_top(arrow_)) {
192 y += mid_anchor ? h / 2 - arrow_offset : kStroke - GetBorderThickness();
193 } else if (is_arrow_at_center(arrow_)) {
194 y += h / 2 - arrow_offset;
195 } else {
196 y += mid_anchor ? h / 2 + arrow_offset - size.height() :
197 h - size.height() + GetBorderThickness() - kStroke;
198 }
199 } else {
200 x += (w - size.width()) / 2;
201 y += (arrow_ == NONE) ? h : (h - size.height()) / 2;
202 }
203
204 return gfx::Rect(x, y, size.width(), size.height());
205 }
206
GetBorderThickness() const207 int BubbleBorder::GetBorderThickness() const {
208 return images_->border_thickness - images_->border_interior_thickness;
209 }
210
GetBorderCornerRadius() const211 int BubbleBorder::GetBorderCornerRadius() const {
212 return images_->corner_radius;
213 }
214
GetArrowOffset(const gfx::Size & border_size) const215 int BubbleBorder::GetArrowOffset(const gfx::Size& border_size) const {
216 const int edge_length = is_arrow_on_horizontal(arrow_) ?
217 border_size.width() : border_size.height();
218 if (is_arrow_at_center(arrow_) && arrow_offset_ == 0)
219 return edge_length / 2;
220
221 // Calculate the minimum offset to not overlap arrow and corner images.
222 const int min = images_->border_thickness + (images_->top_arrow.width() / 2);
223 // Ensure the returned value will not cause image overlap, if possible.
224 return std::max(min, std::min(arrow_offset_, edge_length - min));
225 }
226
Paint(const views::View & view,gfx::Canvas * canvas)227 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) {
228 gfx::Rect bounds(view.GetContentsBounds());
229 bounds.Inset(-GetBorderThickness(), -GetBorderThickness());
230 const gfx::Rect arrow_bounds = GetArrowRect(view.GetLocalBounds());
231 if (arrow_bounds.IsEmpty()) {
232 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds);
233 return;
234 }
235
236 // Clip the arrow bounds out to avoid painting the overlapping edge area.
237 canvas->Save();
238 SkRect arrow_rect(gfx::RectToSkRect(arrow_bounds));
239 canvas->sk_canvas()->clipRect(arrow_rect, SkRegion::kDifference_Op);
240 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds);
241 canvas->Restore();
242
243 DrawArrow(canvas, arrow_bounds);
244 }
245
GetInsets() const246 gfx::Insets BubbleBorder::GetInsets() const {
247 // The insets contain the stroke and shadow pixels outside the bubble fill.
248 const int inset = GetBorderThickness();
249 if ((arrow_paint_type_ == PAINT_NONE) || !has_arrow(arrow_))
250 return gfx::Insets(inset, inset, inset, inset);
251
252 int first_inset = inset;
253 int second_inset = std::max(inset, images_->arrow_thickness);
254 if (is_arrow_on_horizontal(arrow_) ?
255 is_arrow_on_top(arrow_) : is_arrow_on_left(arrow_))
256 std::swap(first_inset, second_inset);
257 return is_arrow_on_horizontal(arrow_) ?
258 gfx::Insets(first_inset, inset, second_inset, inset) :
259 gfx::Insets(inset, first_inset, inset, second_inset);
260 }
261
GetMinimumSize() const262 gfx::Size BubbleBorder::GetMinimumSize() const {
263 return GetSizeForContentsSize(gfx::Size());
264 }
265
GetSizeForContentsSize(const gfx::Size & contents_size) const266 gfx::Size BubbleBorder::GetSizeForContentsSize(
267 const gfx::Size& contents_size) const {
268 // Enlarge the contents size by the thickness of the border images.
269 gfx::Size size(contents_size);
270 const gfx::Insets insets = GetInsets();
271 size.Enlarge(insets.width(), insets.height());
272
273 // Ensure the bubble is large enough to not overlap border and arrow images.
274 const int min = 2 * images_->border_thickness;
275 const int min_with_arrow_width = min + images_->top_arrow.width();
276 const int min_with_arrow_thickness = images_->border_thickness +
277 std::max(images_->arrow_thickness + images_->border_interior_thickness,
278 images_->border_thickness);
279 // Only take arrow image sizes into account when the bubble tip is shown.
280 if (arrow_paint_type_ == PAINT_TRANSPARENT || !has_arrow(arrow_))
281 size.SetToMax(gfx::Size(min, min));
282 else if (is_arrow_on_horizontal(arrow_))
283 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness));
284 else
285 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width));
286 return size;
287 }
288
GetArrowImage() const289 gfx::ImageSkia* BubbleBorder::GetArrowImage() const {
290 if (!has_arrow(arrow_))
291 return NULL;
292 if (is_arrow_on_horizontal(arrow_)) {
293 return is_arrow_on_top(arrow_) ?
294 &images_->top_arrow : &images_->bottom_arrow;
295 }
296 return is_arrow_on_left(arrow_) ?
297 &images_->left_arrow : &images_->right_arrow;
298 }
299
GetArrowRect(const gfx::Rect & bounds) const300 gfx::Rect BubbleBorder::GetArrowRect(const gfx::Rect& bounds) const {
301 if (!has_arrow(arrow_) || arrow_paint_type_ != PAINT_NORMAL)
302 return gfx::Rect();
303
304 gfx::Point origin;
305 int offset = GetArrowOffset(bounds.size());
306 const int half_length = images_->top_arrow.width() / 2;
307 const gfx::Insets insets = GetInsets();
308
309 if (is_arrow_on_horizontal(arrow_)) {
310 origin.set_x(is_arrow_on_left(arrow_) || is_arrow_at_center(arrow_) ?
311 offset : bounds.width() - offset);
312 origin.Offset(-half_length, 0);
313 if (is_arrow_on_top(arrow_))
314 origin.set_y(insets.top() - images_->arrow_thickness);
315 else
316 origin.set_y(bounds.height() - insets.bottom());
317 } else {
318 origin.set_y(is_arrow_on_top(arrow_) || is_arrow_at_center(arrow_) ?
319 offset : bounds.height() - offset);
320 origin.Offset(0, -half_length);
321 if (is_arrow_on_left(arrow_))
322 origin.set_x(insets.left() - images_->arrow_thickness);
323 else
324 origin.set_x(bounds.width() - insets.right());
325 }
326 return gfx::Rect(origin, GetArrowImage()->size());
327 }
328
DrawArrow(gfx::Canvas * canvas,const gfx::Rect & arrow_bounds) const329 void BubbleBorder::DrawArrow(gfx::Canvas* canvas,
330 const gfx::Rect& arrow_bounds) const {
331 canvas->DrawImageInt(*GetArrowImage(), arrow_bounds.x(), arrow_bounds.y());
332 const bool horizontal = is_arrow_on_horizontal(arrow_);
333 const int thickness = images_->arrow_interior_thickness;
334 float tip_x = horizontal ? arrow_bounds.CenterPoint().x() :
335 is_arrow_on_left(arrow_) ? arrow_bounds.right() - thickness :
336 arrow_bounds.x() + thickness;
337 float tip_y = !horizontal ? arrow_bounds.CenterPoint().y() + 0.5f :
338 is_arrow_on_top(arrow_) ? arrow_bounds.bottom() - thickness :
339 arrow_bounds.y() + thickness;
340 const bool positive_offset = horizontal ?
341 is_arrow_on_top(arrow_) : is_arrow_on_left(arrow_);
342 const int offset_to_next_vertex = positive_offset ?
343 images_->arrow_interior_thickness : -images_->arrow_interior_thickness;
344
345 SkPath path;
346 path.incReserve(4);
347 path.moveTo(SkDoubleToScalar(tip_x), SkDoubleToScalar(tip_y));
348 path.lineTo(SkDoubleToScalar(tip_x + offset_to_next_vertex),
349 SkDoubleToScalar(tip_y + offset_to_next_vertex));
350 const int multiplier = horizontal ? 1 : -1;
351 path.lineTo(SkDoubleToScalar(tip_x - multiplier * offset_to_next_vertex),
352 SkDoubleToScalar(tip_y + multiplier * offset_to_next_vertex));
353 path.close();
354
355 SkPaint paint;
356 paint.setStyle(SkPaint::kFill_Style);
357 paint.setColor(background_color_);
358
359 canvas->DrawPath(path, paint);
360 }
361
Paint(gfx::Canvas * canvas,views::View * view) const362 void BubbleBackground::Paint(gfx::Canvas* canvas, views::View* view) const {
363 if (border_->shadow() == BubbleBorder::NO_SHADOW_OPAQUE_BORDER)
364 canvas->DrawColor(border_->background_color());
365
366 // Fill the contents with a round-rect region to match the border images.
367 SkPaint paint;
368 paint.setAntiAlias(true);
369 paint.setStyle(SkPaint::kFill_Style);
370 paint.setColor(border_->background_color());
371 SkPath path;
372 gfx::Rect bounds(view->GetLocalBounds());
373 bounds.Inset(border_->GetInsets());
374
375 SkScalar radius = SkIntToScalar(border_->GetBorderCornerRadius());
376 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius);
377 canvas->DrawPath(path, paint);
378 }
379
380 } // namespace views
381