Index: ash/desktop_background/desktop_background_view.cc |
diff --git a/ash/desktop_background/desktop_background_view.cc b/ash/desktop_background/desktop_background_view.cc |
index bd535a8a23811006d8ed01759a4749986972e26b..45a4c60500aaeaaf93271820354a50c744b647b1 100644 |
--- a/ash/desktop_background/desktop_background_view.cc |
+++ b/ash/desktop_background/desktop_background_view.cc |
@@ -33,10 +33,50 @@ DesktopBackgroundView::~DesktopBackgroundView() { |
// DesktopBackgroundView, views::View overrides: |
void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { |
- canvas->DrawBitmapInt(wallpaper_, |
- 0, 0, wallpaper_.width(), wallpaper_.height(), |
- 0, 0, width(), height(), |
- true); |
+ // Scale the image while maintaining the aspect ratio, cropping as |
+ // necessary to fill the background. Ideally the image should be larger |
+ // than the largest display supported, if not we will center it rather than |
+ // streching to avoid upsampling artifacts (Note that we could tile too, but |
+ // decided not to do this at the moment). |
+ gfx::Rect wallpaper_rect(0, 0, wallpaper_.width(), wallpaper_.height()); |
+ if (wallpaper_.width() > width() && wallpaper_.height() > height()) { |
+ // The dimension with the smallest ratio must be cropped, the other one |
+ // is preserved. Both are set in gfx::Size cropped_size. |
+ double horizontal_ratio = static_cast<double>(width()) / |
+ static_cast<double>(wallpaper_.width()); |
+ double vertical_ratio = static_cast<double>(height()) / |
+ static_cast<double>(wallpaper_.height()); |
+ |
+ gfx::Size cropped_size; |
+ if (vertical_ratio > horizontal_ratio) { |
+ cropped_size = gfx::Size( |
+ static_cast<int>(static_cast<double>(width()) / vertical_ratio), |
Daniel Erat
2012/02/29 21:41:40
nit: round this?
Emmanuel Saint-loubert-Bié
2012/02/29 22:04:16
Do you mean using floor() or similar function and
Daniel Erat
2012/02/29 22:10:40
i meant doing static_cast<int>(round(...)) on the
|
+ wallpaper_.height()); |
+ } else { |
+ cropped_size = gfx::Size(wallpaper_.width(), |
+ static_cast<int>(static_cast<double>(height()) / horizontal_ratio)); |
Daniel Erat
2012/02/29 21:41:40
nit: round?
|
+ } |
+ |
+ gfx::Rect wallpaper_cropped_rect = wallpaper_rect.Center(cropped_size); |
+ canvas->DrawBitmapInt(wallpaper_, |
+ wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), |
+ wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), |
+ 0, 0, width(), height(), |
+ true); |
+ } |
+ else { |
+ // Center the wallpaper and crop to the destination rectangle. |
Daniel Erat
2012/02/29 21:41:40
have you tested whether it's okay for the destinat
Emmanuel Saint-loubert-Bié
2012/02/29 22:04:16
Ha yes that works too, much clearer. I applied you
|
+ gfx::Rect wallpaper_centered_rect = wallpaper_rect.Center(gfx::Size( |
+ std::min(wallpaper_.width(), width()), |
+ std::min(wallpaper_.height(), height()))); |
+ canvas->DrawBitmapInt(wallpaper_, |
+ wallpaper_centered_rect.x(), wallpaper_centered_rect.y(), |
+ wallpaper_centered_rect.width(), wallpaper_centered_rect.height(), |
+ std::max(0, (width() - wallpaper_centered_rect.width()) / 2), |
+ std::max(0, (height() - wallpaper_centered_rect.height()) / 2), |
+ wallpaper_centered_rect.width(), wallpaper_centered_rect.height(), |
+ true); |
+ } |
} |
bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { |