Chromium Code Reviews| Index: ui/message_center/views/notification_view.cc |
| diff --git a/ui/message_center/views/notification_view.cc b/ui/message_center/views/notification_view.cc |
| index 1ed5c8388ab9b1387d5c3fc1258395d2ed1c9b12..f733a4613fc61fce22b15a4fcb356d83e32fdc6f 100644 |
| --- a/ui/message_center/views/notification_view.cc |
| +++ b/ui/message_center/views/notification_view.cc |
| @@ -208,19 +208,24 @@ ProportionalImageView::~ProportionalImageView() { |
| } |
| gfx::Size ProportionalImageView::GetPreferredSize() { |
| - gfx::Size size = GetImageSizeForWidth(image_.width()); |
| - return gfx::Size(size.width() + GetInsets().width(), |
| - size.height() + GetInsets().height()); |
| + gfx::Insets insets = GetInsets(); |
| + gfx::Rect rect = gfx::Rect(GetImageSizeForWidth(image_.width())); |
| + rect.Inset(-insets); |
| + return rect.size(); |
| } |
| int ProportionalImageView::GetHeightForWidth(int width) { |
| - return GetImageSizeForWidth(width).height(); |
| + // The border will count against the width available for the image |
| + // and towards the height taken by the image. |
| + gfx::Insets insets = GetInsets(); |
| + int inset_width = width - insets.width(); |
| + return GetImageSizeForWidth(inset_width).height() + insets.height(); |
| } |
| void ProportionalImageView::OnPaint(gfx::Canvas* canvas) { |
| views::View::OnPaint(canvas); |
| - gfx::Size draw_size(GetImageSizeForWidth(width())); |
| + gfx::Size draw_size(GetImageSizeForWidth(width() - GetInsets().width())); |
| if (!draw_size.IsEmpty()) { |
| gfx::Rect draw_bounds = GetContentsBounds(); |
| draw_bounds.ClampToCenteredSize(draw_size); |
| @@ -246,6 +251,51 @@ gfx::Size ProportionalImageView::GetImageSizeForWidth(int width) { |
| return message_center::GetImageSizeForWidth(width, size); |
| } |
| +// NotificationImage /////////////////////////////////////////////////////// |
| + |
| +// The NotificationImage is the view representing the area covered by the |
| +// notification's image, including background and border. Its size can be |
| +// specified in advance and images will be scaled to fit including a border if |
| +// necessary. |
| +class NotificationImage : public views::View { |
|
Dmitry Titov
2013/12/06 19:29:11
Is it possible to avoid creating new class?
Can we
dewittj
2013/12/10 02:47:04
Done.
|
| + public: |
| + NotificationImage(const gfx::Image& image, gfx::Size size); |
| + virtual ~NotificationImage(); |
| + |
| + private: |
| + virtual gfx::Size GetPreferredSize() OVERRIDE; |
| + |
| + gfx::Image image_; |
| + gfx::Size size_; |
| +}; |
|
Dmitry Titov
2013/12/06 19:29:11
DISALLOW_COPY_AND_ASSIGN...
dewittj
2013/12/10 02:47:04
Done.
|
| + |
| +NotificationImage::NotificationImage(const gfx::Image& image, gfx::Size size) |
| + : image_(image), size_(size) { |
| + SetLayoutManager(new views::FillLayout()); |
| + set_background(views::Background::CreateSolidBackground( |
| + message_center::kImageBackgroundColor)); |
| + gfx::Size image_size = image_.Size(); |
| + float image_aspect = 0; |
| + if (image_size.height() > 0) |
| + image_aspect = static_cast<float>(image_size.width()) / image_size.height(); |
| + |
| + views::View* proportional_image_view = |
| + new ProportionalImageView(image.AsImageSkia()); |
| + |
| + // This calculation determines that the new image would have the correct |
| + // height for width. |
| + if (static_cast<int>(image_aspect * size_.height()) != size_.width()) { |
| + proportional_image_view->set_border(views::Border::CreateSolidBorder( |
| + message_center::kNotificationImageBorderSize, SK_ColorTRANSPARENT)); |
| + } |
| + |
| + AddChildView(proportional_image_view); |
| +} |
| + |
| +NotificationImage::~NotificationImage() {} |
| + |
| +gfx::Size NotificationImage::GetPreferredSize() { return size_; } |
| + |
| // NotificationProgressBar ///////////////////////////////////////////////////// |
| class NotificationProgressBar : public views::ProgressBar { |
| @@ -583,7 +633,10 @@ NotificationView::NotificationView(const Notification& notification, |
| // Create the image view if appropriate. |
| image_view_ = NULL; |
| if (!notification.image().IsEmpty()) { |
| - image_view_ = new ProportionalImageView(notification.image().AsImageSkia()); |
| + gfx::Size image_size( |
| + kNotificationPreferredImageSize, |
| + kNotificationPreferredImageSize * kNotificationPreferredImageRatio); |
| + image_view_ = new NotificationImage(notification.image(), image_size); |
| image_view_->SetVisible(is_expanded_); |
| bottom_view_->AddChildView(image_view_); |
| } |