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..948bb38702823b385e29200fb75257d587d67b58 100644 |
--- a/ash/desktop_background/desktop_background_view.cc |
+++ b/ash/desktop_background/desktop_background_view.cc |
@@ -33,10 +33,45 @@ 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), |
+ wallpaper_.height()); |
+ } else { |
+ cropped_size = gfx::Size(wallpaper_.width(), |
+ static_cast<int>(static_cast<double>(height()) / horizontal_ratio)); |
+ } |
+ |
+ 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. |
+ gfx::Rect wallpaper_centered_rect = wallpaper_rect.Center(gfx::Size( |
Daniel Erat
2012/02/29 22:10:40
you don't need this anymore
Emmanuel Saint-loubert-Bié
2012/02/29 22:32:58
Done.
|
+ std::min(wallpaper_.width(), width()), |
+ std::min(wallpaper_.height(), height()))); |
+ canvas->DrawBitmapInt(wallpaper_, (width() - wallpaper_.width()) / 2, |
+ (height() - wallpaper_.height()) / 2); |
+ } |
} |
bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { |