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/skia_util.h" 16 #include "ui/views/painter.h" 17 18 namespace views { 19 20 namespace internal { 21 22 // A helper that combines each border image-set painter with arrows and metrics. 23 struct BorderImages { 24 BorderImages(const int border_image_ids[], 25 const int arrow_image_ids[], 26 int border_interior_thickness, 27 int arrow_interior_thickness, 28 int corner_radius); 29 30 scoped_ptr<Painter> border_painter; 31 gfx::ImageSkia left_arrow; 32 gfx::ImageSkia top_arrow; 33 gfx::ImageSkia right_arrow; 34 gfx::ImageSkia bottom_arrow; 35 36 // The thickness of border and arrow images and their interior areas. 37 // Thickness is the width of left/right and the height of top/bottom images. 38 // The interior is measured without including stroke or shadow pixels. 39 int border_thickness; 40 int border_interior_thickness; 41 int arrow_thickness; 42 int arrow_interior_thickness; 43 // The corner radius of the bubble's rounded-rect interior area. 44 int corner_radius; 45 }; 46 47 BorderImages::BorderImages(const int border_image_ids[], 48 const int arrow_image_ids[], 49 int border_interior_thickness, 50 int arrow_interior_thickness, 51 int corner_radius) 52 : border_painter(Painter::CreateImageGridPainter(border_image_ids)), 53 border_thickness(0), 54 border_interior_thickness(border_interior_thickness), 55 arrow_thickness(0), 56 arrow_interior_thickness(arrow_interior_thickness), 57 corner_radius(corner_radius) { 58 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 59 border_thickness = rb.GetImageSkiaNamed(border_image_ids[0])->width(); 60 if (arrow_image_ids[0] != 0) { 61 left_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[0]); 62 top_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[1]); 63 right_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[2]); 64 bottom_arrow = *rb.GetImageSkiaNamed(arrow_image_ids[3]); 65 arrow_thickness = top_arrow.height(); 66 } 67 } 68 69 } // namespace internal 70 71 namespace { 72 73 // The border and arrow stroke size used in image assets, in pixels. 74 const int kStroke = 1; 75 76 // Macros to define arrays of IDR constants used with CreateImageGridPainter. 77 #define IMAGE_BORDER(x) { x ## _TOP_LEFT, x ## _TOP, x ## _TOP_RIGHT, \ 78 x ## _LEFT, 0, x ## _RIGHT, \ 79 x ## _BOTTOM_LEFT, x ## _BOTTOM, x ## _BOTTOM_RIGHT, } 80 #define IMAGE_BORDER_ACRONYM(x) { x ## _TL, x ## _T, x ## _TR, \ 81 x ## _L, 0, x ## _R, \ 82 x ## _BL, x ## _B, x ## _BR, } 83 #define ARROWS(x) { x ## _LEFT, x ## _TOP, x ## _RIGHT, x ## _BOTTOM, } 84 85 // Bubble border and arrow image resource ids. 86 const int kShadowImages[] = IMAGE_BORDER_ACRONYM(IDR_BUBBLE_SHADOW); 87 const int kShadowArrows[] = { 0, 0, 0, 0 }; 88 const int kNoShadowImages[] = IMAGE_BORDER_ACRONYM(IDR_BUBBLE); 89 const int kNoShadowArrows[] = { IDR_BUBBLE_L_ARROW, IDR_BUBBLE_T_ARROW, 90 IDR_BUBBLE_R_ARROW, IDR_BUBBLE_B_ARROW, }; 91 const int kBigShadowImages[] = IMAGE_BORDER(IDR_WINDOW_BUBBLE_SHADOW_BIG); 92 const int kBigShadowArrows[] = ARROWS(IDR_WINDOW_BUBBLE_SHADOW_SPIKE_BIG); 93 const int kSmallShadowImages[] = IMAGE_BORDER(IDR_WINDOW_BUBBLE_SHADOW_SMALL); 94 const int kSmallShadowArrows[] = ARROWS(IDR_WINDOW_BUBBLE_SHADOW_SPIKE_SMALL); 95 96 using internal::BorderImages; 97 98 // Returns the cached BorderImages for the given |shadow| type. 99 BorderImages* GetBorderImages(BubbleBorder::Shadow shadow) { 100 // Keep a cache of bubble border image-set painters, arrows, and metrics. 101 static BorderImages* kBorderImages[BubbleBorder::SHADOW_COUNT] = { NULL }; 102 103 CHECK_LT(shadow, BubbleBorder::SHADOW_COUNT); 104 struct BorderImages*& set = kBorderImages[shadow]; 105 if (set) 106 return set; 107 108 switch (shadow) { 109 case BubbleBorder::SHADOW: 110 // Note: SHADOW's border interior thickness is actually 10, but 0 is used 111 // here to match the legacy appearance of SHADOW's extra large inset. 112 set = new BorderImages(kShadowImages, kShadowArrows, 0, 0, 3); 113 break; 114 case BubbleBorder::NO_SHADOW: 115 case BubbleBorder::NO_SHADOW_OPAQUE_BORDER: 116 set = new BorderImages(kNoShadowImages, kNoShadowArrows, 6, 7, 4); 117 break; 118 case BubbleBorder::BIG_SHADOW: 119 set = new BorderImages(kBigShadowImages, kBigShadowArrows, 23, 9, 2); 120 break; 121 case BubbleBorder::SMALL_SHADOW: 122 set = new BorderImages(kSmallShadowImages, kSmallShadowArrows, 5, 6, 2); 123 break; 124 case BubbleBorder::SHADOW_COUNT: 125 NOTREACHED(); 126 break; 127 } 128 129 return set; 130 } 131 132 } // namespace 133 134 BubbleBorder::BubbleBorder(Arrow arrow, Shadow shadow, SkColor color) 135 : arrow_(arrow), 136 arrow_offset_(0), 137 arrow_paint_type_(PAINT_NORMAL), 138 alignment_(ALIGN_ARROW_TO_MID_ANCHOR), 139 shadow_(shadow), 140 background_color_(color) { 141 DCHECK(shadow < SHADOW_COUNT); 142 images_ = GetBorderImages(shadow); 143 } 144 145 BubbleBorder::~BubbleBorder() {} 146 147 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& anchor_rect, 148 const gfx::Size& contents_size) const { 149 // Enlarge the contents size by the thickness of the border images. 150 gfx::Size size(contents_size); 151 const gfx::Insets insets = GetInsets(); 152 size.Enlarge(insets.width(), insets.height()); 153 154 // Ensure the bubble is large enough to not overlap border and arrow images. 155 const int min = 2 * images_->border_thickness; 156 const int min_with_arrow_width = min + images_->top_arrow.width(); 157 const int min_with_arrow_thickness = images_->border_thickness + 158 std::max(images_->arrow_thickness + images_->border_interior_thickness, 159 images_->border_thickness); 160 // Only take arrow image sizes into account when the bubble tip is shown. 161 if (arrow_paint_type_ == PAINT_TRANSPARENT || !has_arrow(arrow_)) 162 size.SetToMax(gfx::Size(min, min)); 163 else if (is_arrow_on_horizontal(arrow_)) 164 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness)); 165 else 166 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width)); 167 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 int arrow_offset = GetArrowOffset(size); 173 const int arrow_size = 174 images_->arrow_interior_thickness + kStroke - images_->arrow_thickness; 175 const bool mid_anchor = alignment_ == ALIGN_ARROW_TO_MID_ANCHOR; 176 177 // Calculate the bubble coordinates based on the border and arrow settings. 178 if (is_arrow_on_horizontal(arrow_)) { 179 if (is_arrow_on_left(arrow_)) { 180 x += mid_anchor ? w / 2 - arrow_offset : kStroke - GetBorderThickness(); 181 } else if (is_arrow_at_center(arrow_)) { 182 x += w / 2 - arrow_offset; 183 } else { 184 x += mid_anchor ? w / 2 + arrow_offset - size.width() : 185 w - size.width() + GetBorderThickness() - kStroke; 186 } 187 y += is_arrow_on_top(arrow_) ? h + arrow_size : -arrow_size - size.height(); 188 } else if (has_arrow(arrow_)) { 189 x += is_arrow_on_left(arrow_) ? w + arrow_size : -arrow_size - size.width(); 190 if (is_arrow_on_top(arrow_)) { 191 y += mid_anchor ? h / 2 - arrow_offset : kStroke - GetBorderThickness(); 192 } else if (is_arrow_at_center(arrow_)) { 193 y += h / 2 - arrow_offset; 194 } else { 195 y += mid_anchor ? h / 2 + arrow_offset - size.height() : 196 h - size.height() + GetBorderThickness() - kStroke; 197 } 198 } else { 199 x += (w - size.width()) / 2; 200 y += (arrow_ == NONE) ? h : (h - size.height()) / 2; 201 } 202 203 return gfx::Rect(x, y, size.width(), size.height()); 204 } 205 206 int BubbleBorder::GetBorderThickness() const { 207 return images_->border_thickness - images_->border_interior_thickness; 208 } 209 210 int BubbleBorder::GetBorderCornerRadius() const { 211 return images_->corner_radius; 212 } 213 214 int BubbleBorder::GetArrowOffset(const gfx::Size& border_size) const { 215 const int edge_length = is_arrow_on_horizontal(arrow_) ? 216 border_size.width() : border_size.height(); 217 if (is_arrow_at_center(arrow_) && arrow_offset_ == 0) 218 return edge_length / 2; 219 220 // Calculate the minimum offset to not overlap arrow and corner images. 221 const int min = images_->border_thickness + (images_->top_arrow.width() / 2); 222 // Ensure the returned value will not cause image overlap, if possible. 223 return std::max(min, std::min(arrow_offset_, edge_length - min)); 224 } 225 226 gfx::Insets BubbleBorder::GetInsets() const { 227 // The insets contain the stroke and shadow pixels outside the bubble fill. 228 const int inset = GetBorderThickness(); 229 if (arrow_paint_type_ == PAINT_NONE) 230 return gfx::Insets(inset, inset, inset, inset); 231 const int inset_with_arrow = std::max(inset, images_->arrow_thickness); 232 if (is_arrow_on_horizontal(arrow_)) { 233 if (is_arrow_on_top(arrow_)) 234 return gfx::Insets(inset_with_arrow, inset, inset, inset); 235 return gfx::Insets(inset, inset, inset_with_arrow, inset); 236 } else if (has_arrow(arrow_)) { 237 if (is_arrow_on_left(arrow_)) 238 return gfx::Insets(inset, inset_with_arrow, inset, inset); 239 return gfx::Insets(inset, inset, inset, inset_with_arrow); 240 } 241 return gfx::Insets(inset, inset, inset, inset); 242 } 243 244 void BubbleBorder::Paint(const views::View& view, gfx::Canvas* canvas) { 245 gfx::Rect bounds(view.GetContentsBounds()); 246 bounds.Inset(-GetBorderThickness(), -GetBorderThickness()); 247 const gfx::Rect arrow_bounds = GetArrowRect(view.GetLocalBounds()); 248 if (arrow_bounds.IsEmpty()) { 249 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds); 250 return; 251 } 252 253 // Clip the arrow bounds out to avoid painting the overlapping edge area. 254 canvas->Save(); 255 SkRect arrow_rect(gfx::RectToSkRect(arrow_bounds)); 256 canvas->sk_canvas()->clipRect(arrow_rect, SkRegion::kDifference_Op); 257 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds); 258 canvas->Restore(); 259 260 DrawArrow(canvas, arrow_bounds); 261 } 262 263 gfx::ImageSkia* BubbleBorder::GetArrowImage() const { 264 if (is_arrow_on_horizontal(arrow_)) { 265 return is_arrow_on_top(arrow_) ? 266 &images_->top_arrow : &images_->bottom_arrow; 267 } else if (has_arrow(arrow_)) { 268 return is_arrow_on_left(arrow_) ? 269 &images_->left_arrow : &images_->right_arrow; 270 } 271 return NULL; 272 } 273 274 gfx::Rect BubbleBorder::GetArrowRect(const gfx::Rect& bounds) const { 275 if (!has_arrow(arrow_) || arrow_paint_type_ != PAINT_NORMAL) 276 return gfx::Rect(); 277 278 gfx::Point origin; 279 int offset = GetArrowOffset(bounds.size()); 280 const int half_length = images_->top_arrow.width() / 2; 281 const gfx::Insets insets = GetInsets(); 282 283 if (is_arrow_on_horizontal(arrow_)) { 284 origin.set_x(is_arrow_on_left(arrow_) || is_arrow_at_center(arrow_) ? 285 offset : bounds.width() - offset); 286 origin.Offset(-half_length, 0); 287 if (is_arrow_on_top(arrow_)) 288 origin.set_y(insets.top() - images_->arrow_thickness); 289 else 290 origin.set_y(bounds.height() - insets.bottom()); 291 } else { 292 origin.set_y(is_arrow_on_top(arrow_) || is_arrow_at_center(arrow_) ? 293 offset : bounds.height() - offset); 294 origin.Offset(0, -half_length); 295 if (is_arrow_on_left(arrow_)) 296 origin.set_x(insets.left() - images_->arrow_thickness); 297 else 298 origin.set_x(bounds.width() - insets.right()); 299 } 300 return gfx::Rect(origin, GetArrowImage()->size()); 301 } 302 303 void BubbleBorder::DrawArrow(gfx::Canvas* canvas, 304 const gfx::Rect& arrow_bounds) const { 305 canvas->DrawImageInt(*GetArrowImage(), arrow_bounds.x(), arrow_bounds.y()); 306 const bool horizontal = is_arrow_on_horizontal(arrow_); 307 const int thickness = images_->arrow_interior_thickness; 308 float tip_x = horizontal ? arrow_bounds.CenterPoint().x() : 309 is_arrow_on_left(arrow_) ? arrow_bounds.right() - thickness : 310 arrow_bounds.x() + thickness; 311 float tip_y = !horizontal ? arrow_bounds.CenterPoint().y() + 0.5f : 312 is_arrow_on_top(arrow_) ? arrow_bounds.bottom() - thickness : 313 arrow_bounds.y() + thickness; 314 const bool positive_offset = horizontal ? 315 is_arrow_on_top(arrow_) : is_arrow_on_left(arrow_); 316 const int offset_to_next_vertex = positive_offset ? 317 images_->arrow_interior_thickness : -images_->arrow_interior_thickness; 318 319 SkPath path; 320 path.incReserve(4); 321 path.moveTo(SkDoubleToScalar(tip_x), SkDoubleToScalar(tip_y)); 322 path.lineTo(SkDoubleToScalar(tip_x + offset_to_next_vertex), 323 SkDoubleToScalar(tip_y + offset_to_next_vertex)); 324 const int multiplier = horizontal ? 1 : -1; 325 path.lineTo(SkDoubleToScalar(tip_x - multiplier * offset_to_next_vertex), 326 SkDoubleToScalar(tip_y + multiplier * offset_to_next_vertex)); 327 path.close(); 328 329 SkPaint paint; 330 paint.setStyle(SkPaint::kFill_Style); 331 paint.setColor(background_color_); 332 333 canvas->DrawPath(path, paint); 334 } 335 336 void BubbleBackground::Paint(gfx::Canvas* canvas, views::View* view) const { 337 if (border_->shadow() == BubbleBorder::NO_SHADOW_OPAQUE_BORDER) 338 canvas->DrawColor(border_->background_color()); 339 340 // Clip out the client bounds to prevent overlapping transparent widgets. 341 if (!border_->client_bounds().IsEmpty()) { 342 SkRect client_rect(gfx::RectToSkRect(border_->client_bounds())); 343 canvas->sk_canvas()->clipRect(client_rect, SkRegion::kDifference_Op); 344 } 345 346 // Fill the contents with a round-rect region to match the border images. 347 SkPaint paint; 348 paint.setAntiAlias(true); 349 paint.setStyle(SkPaint::kFill_Style); 350 paint.setColor(border_->background_color()); 351 SkPath path; 352 gfx::Rect bounds(view->GetLocalBounds()); 353 bounds.Inset(border_->GetInsets()); 354 355 // Note: This matches the legacy appearance of SHADOW's extra large inset. 356 if (border_->shadow() == BubbleBorder::SHADOW) 357 bounds.Inset(-10, -10); 358 359 SkScalar radius = SkIntToScalar(border_->GetBorderCornerRadius()); 360 path.addRoundRect(gfx::RectToSkRect(bounds), radius, radius); 361 canvas->DrawPath(path, paint); 362 } 363 364 } // namespace views 365