| 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 "third_party/skia/include/core/SkPaint.h" | 10 #include "third_party/skia/include/core/SkPaint.h" |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 BubbleBorder::~BubbleBorder() {} | 157 BubbleBorder::~BubbleBorder() {} |
| 158 | 158 |
| 159 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& anchor_rect, | 159 gfx::Rect BubbleBorder::GetBounds(const gfx::Rect& anchor_rect, |
| 160 const gfx::Size& contents_size) const { | 160 const gfx::Size& contents_size) const { |
| 161 int x = anchor_rect.x(); | 161 int x = anchor_rect.x(); |
| 162 int y = anchor_rect.y(); | 162 int y = anchor_rect.y(); |
| 163 int w = anchor_rect.width(); | 163 int w = anchor_rect.width(); |
| 164 int h = anchor_rect.height(); | 164 int h = anchor_rect.height(); |
| 165 const gfx::Size size(GetSizeForContentsSize(contents_size)); | 165 const gfx::Size size(GetSizeForContentsSize(contents_size)); |
| 166 const int arrow_offset = GetArrowOffset(size); | 166 const int arrow_offset = GetArrowOffset(size); |
| 167 const int arrow_size = | 167 // |arrow_shift| is necessary to visually align the tip of the bubble arrow |
| 168 // with the anchor point. This shift is an inverse of the shadow thickness. |
| 169 int arrow_shift = |
| 168 images_->arrow_interior_thickness + kStroke - images_->arrow_thickness; | 170 images_->arrow_interior_thickness + kStroke - images_->arrow_thickness; |
| 171 // When arrow is painted transparently the visible border of the bubble needs |
| 172 // to be positioned at the same bounds as when the arrow is shown. |
| 173 if (arrow_paint_type_ == PAINT_TRANSPARENT) |
| 174 arrow_shift += images_->arrow_interior_thickness; |
| 169 const bool mid_anchor = alignment_ == ALIGN_ARROW_TO_MID_ANCHOR; | 175 const bool mid_anchor = alignment_ == ALIGN_ARROW_TO_MID_ANCHOR; |
| 170 | 176 |
| 171 // Calculate the bubble coordinates based on the border and arrow settings. | 177 // Calculate the bubble coordinates based on the border and arrow settings. |
| 172 if (is_arrow_on_horizontal(arrow_)) { | 178 if (is_arrow_on_horizontal(arrow_)) { |
| 173 if (is_arrow_on_left(arrow_)) { | 179 if (is_arrow_on_left(arrow_)) { |
| 174 x += mid_anchor ? w / 2 - arrow_offset : kStroke - GetBorderThickness(); | 180 x += mid_anchor ? w / 2 - arrow_offset : kStroke - GetBorderThickness(); |
| 175 } else if (is_arrow_at_center(arrow_)) { | 181 } else if (is_arrow_at_center(arrow_)) { |
| 176 x += w / 2 - arrow_offset; | 182 x += w / 2 - arrow_offset; |
| 177 } else { | 183 } else { |
| 178 x += mid_anchor ? w / 2 + arrow_offset - size.width() : | 184 x += mid_anchor ? w / 2 + arrow_offset - size.width() : |
| 179 w - size.width() + GetBorderThickness() - kStroke; | 185 w - size.width() + GetBorderThickness() - kStroke; |
| 180 } | 186 } |
| 181 y += is_arrow_on_top(arrow_) ? h + arrow_size : -arrow_size - size.height(); | 187 y += is_arrow_on_top(arrow_) ? h + arrow_shift |
| 188 : -arrow_shift - size.height(); |
| 182 } else if (has_arrow(arrow_)) { | 189 } else if (has_arrow(arrow_)) { |
| 183 x += is_arrow_on_left(arrow_) ? w + arrow_size : -arrow_size - size.width(); | 190 x += is_arrow_on_left(arrow_) ? w + arrow_shift |
| 191 : -arrow_shift - size.width(); |
| 184 if (is_arrow_on_top(arrow_)) { | 192 if (is_arrow_on_top(arrow_)) { |
| 185 y += mid_anchor ? h / 2 - arrow_offset : kStroke - GetBorderThickness(); | 193 y += mid_anchor ? h / 2 - arrow_offset : kStroke - GetBorderThickness(); |
| 186 } else if (is_arrow_at_center(arrow_)) { | 194 } else if (is_arrow_at_center(arrow_)) { |
| 187 y += h / 2 - arrow_offset; | 195 y += h / 2 - arrow_offset; |
| 188 } else { | 196 } else { |
| 189 y += mid_anchor ? h / 2 + arrow_offset - size.height() : | 197 y += mid_anchor ? h / 2 + arrow_offset - size.height() : |
| 190 h - size.height() + GetBorderThickness() - kStroke; | 198 h - size.height() + GetBorderThickness() - kStroke; |
| 191 } | 199 } |
| 192 } else { | 200 } else { |
| 193 x += (w - size.width()) / 2; | 201 x += (w - size.width()) / 2; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 canvas->sk_canvas()->clipRect(arrow_rect, SkRegion::kDifference_Op); | 254 canvas->sk_canvas()->clipRect(arrow_rect, SkRegion::kDifference_Op); |
| 247 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds); | 255 Painter::PaintPainterAt(canvas, images_->border_painter.get(), bounds); |
| 248 canvas->Restore(); | 256 canvas->Restore(); |
| 249 | 257 |
| 250 DrawArrow(canvas, arrow_bounds); | 258 DrawArrow(canvas, arrow_bounds); |
| 251 } | 259 } |
| 252 | 260 |
| 253 gfx::Insets BubbleBorder::GetInsets() const { | 261 gfx::Insets BubbleBorder::GetInsets() const { |
| 254 // The insets contain the stroke and shadow pixels outside the bubble fill. | 262 // The insets contain the stroke and shadow pixels outside the bubble fill. |
| 255 const int inset = GetBorderThickness(); | 263 const int inset = GetBorderThickness(); |
| 256 if ((arrow_paint_type_ == PAINT_NONE) || !has_arrow(arrow_)) | 264 if (arrow_paint_type_ != PAINT_NORMAL || !has_arrow(arrow_)) |
| 257 return gfx::Insets(inset); | 265 return gfx::Insets(inset); |
| 258 | 266 |
| 259 int first_inset = inset; | 267 int first_inset = inset; |
| 260 int second_inset = std::max(inset, images_->arrow_thickness); | 268 int second_inset = std::max(inset, images_->arrow_thickness); |
| 261 if (is_arrow_on_horizontal(arrow_) ? | 269 if (is_arrow_on_horizontal(arrow_) ? |
| 262 is_arrow_on_top(arrow_) : is_arrow_on_left(arrow_)) | 270 is_arrow_on_top(arrow_) : is_arrow_on_left(arrow_)) |
| 263 std::swap(first_inset, second_inset); | 271 std::swap(first_inset, second_inset); |
| 264 return is_arrow_on_horizontal(arrow_) ? | 272 return is_arrow_on_horizontal(arrow_) ? |
| 265 gfx::Insets(first_inset, inset, second_inset, inset) : | 273 gfx::Insets(first_inset, inset, second_inset, inset) : |
| 266 gfx::Insets(inset, first_inset, inset, second_inset); | 274 gfx::Insets(inset, first_inset, inset, second_inset); |
| 267 } | 275 } |
| 268 | 276 |
| 269 gfx::Size BubbleBorder::GetMinimumSize() const { | 277 gfx::Size BubbleBorder::GetMinimumSize() const { |
| 270 return GetSizeForContentsSize(gfx::Size()); | 278 return GetSizeForContentsSize(gfx::Size()); |
| 271 } | 279 } |
| 272 | 280 |
| 273 gfx::Size BubbleBorder::GetSizeForContentsSize( | 281 gfx::Size BubbleBorder::GetSizeForContentsSize( |
| 274 const gfx::Size& contents_size) const { | 282 const gfx::Size& contents_size) const { |
| 275 // Enlarge the contents size by the thickness of the border images. | 283 // Enlarge the contents size by the thickness of the border images. |
| 276 gfx::Size size(contents_size); | 284 gfx::Size size(contents_size); |
| 277 const gfx::Insets insets = GetInsets(); | 285 const gfx::Insets insets = GetInsets(); |
| 278 size.Enlarge(insets.width(), insets.height()); | 286 size.Enlarge(insets.width(), insets.height()); |
| 279 | 287 |
| 280 // Ensure the bubble is large enough to not overlap border and arrow images. | 288 // Ensure the bubble is large enough to not overlap border and arrow images. |
| 281 const int min = 2 * images_->border_thickness; | 289 const int min = 2 * images_->border_thickness; |
| 290 // Only take arrow image sizes into account when the bubble tip is shown. |
| 291 if (arrow_paint_type_ != PAINT_NORMAL || !has_arrow(arrow_)) { |
| 292 size.SetToMax(gfx::Size(min, min)); |
| 293 return size; |
| 294 } |
| 282 const int min_with_arrow_width = min + images_->arrow_width; | 295 const int min_with_arrow_width = min + images_->arrow_width; |
| 283 const int min_with_arrow_thickness = images_->border_thickness + | 296 const int min_with_arrow_thickness = images_->border_thickness + |
| 284 std::max(images_->arrow_thickness + images_->border_interior_thickness, | 297 std::max(images_->arrow_thickness + images_->border_interior_thickness, |
| 285 images_->border_thickness); | 298 images_->border_thickness); |
| 286 // Only take arrow image sizes into account when the bubble tip is shown. | 299 if (is_arrow_on_horizontal(arrow_)) |
| 287 if (arrow_paint_type_ == PAINT_NONE || !has_arrow(arrow_)) | |
| 288 size.SetToMax(gfx::Size(min, min)); | |
| 289 else if (is_arrow_on_horizontal(arrow_)) | |
| 290 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness)); | 300 size.SetToMax(gfx::Size(min_with_arrow_width, min_with_arrow_thickness)); |
| 291 else | 301 else |
| 292 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width)); | 302 size.SetToMax(gfx::Size(min_with_arrow_thickness, min_with_arrow_width)); |
| 293 return size; | 303 return size; |
| 294 } | 304 } |
| 295 | 305 |
| 296 gfx::ImageSkia* BubbleBorder::GetArrowImage() const { | 306 gfx::ImageSkia* BubbleBorder::GetArrowImage() const { |
| 297 if (!has_arrow(arrow_)) | 307 if (!has_arrow(arrow_)) |
| 298 return NULL; | 308 return NULL; |
| 299 if (is_arrow_on_horizontal(arrow_)) { | 309 if (is_arrow_on_horizontal(arrow_)) { |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 paint.setStyle(SkPaint::kFill_Style); | 404 paint.setStyle(SkPaint::kFill_Style); |
| 395 paint.setColor(border_->background_color()); | 405 paint.setColor(border_->background_color()); |
| 396 SkPath path; | 406 SkPath path; |
| 397 gfx::Rect bounds(view->GetLocalBounds()); | 407 gfx::Rect bounds(view->GetLocalBounds()); |
| 398 bounds.Inset(border_->GetInsets()); | 408 bounds.Inset(border_->GetInsets()); |
| 399 | 409 |
| 400 canvas->DrawRoundRect(bounds, border_->GetBorderCornerRadius(), paint); | 410 canvas->DrawRoundRect(bounds, border_->GetBorderCornerRadius(), paint); |
| 401 } | 411 } |
| 402 | 412 |
| 403 } // namespace views | 413 } // namespace views |
| OLD | NEW |