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 44526cb89a1adee344acc78023614709d848d2c7..79470f0dbddb8f9ddf40cab326ee3758554f3604 100644 |
--- a/ash/desktop_background/desktop_background_view.cc |
+++ b/ash/desktop_background/desktop_background_view.cc |
@@ -4,6 +4,8 @@ |
#include "ash/desktop_background/desktop_background_view.h" |
+#include <limits> |
+ |
#include "ash/ash_export.h" |
#include "ash/shell.h" |
#include "ash/shell_window_ids.h" |
@@ -18,6 +20,11 @@ |
namespace ash { |
namespace internal { |
+// For our scaling ratios we need to round positive numbers. |
+static int RoundPositive(double x) { |
+ return static_cast<int>(floor(x + 0.5)); |
+} |
+ |
//////////////////////////////////////////////////////////////////////////////// |
// DesktopBackgroundView, public: |
@@ -34,10 +41,43 @@ 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( |
+ RoundPositive(static_cast<double>(width()) / vertical_ratio), |
+ wallpaper_.height()); |
+ } else { |
+ cropped_size = gfx::Size(wallpaper_.width(), |
+ RoundPositive(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 { |
tfarina
2012/03/01 17:24:12
nit: else should be in the of the previous line.
|
+ // Center the wallpaper in the destination rectangle (Skia will crop |
+ // as needed). We might decide later to tile small solid color images. |
+ canvas->DrawBitmapInt(wallpaper_, (width() - wallpaper_.width()) / 2, |
+ (height() - wallpaper_.height()) / 2); |
+ } |
} |
bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { |