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 "ash/desktop_background/desktop_background_view.h" | 5 #include "ash/desktop_background/desktop_background_view.h" |
6 | 6 |
7 #include <limits> | |
8 | |
7 #include "ash/ash_export.h" | 9 #include "ash/ash_export.h" |
8 #include "ash/shell.h" | 10 #include "ash/shell.h" |
9 #include "ash/shell_window_ids.h" | 11 #include "ash/shell_window_ids.h" |
10 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
11 #include "grit/ui_resources.h" | 13 #include "grit/ui_resources.h" |
12 #include "ui/aura/root_window.h" | 14 #include "ui/aura/root_window.h" |
13 #include "ui/base/resource/resource_bundle.h" | 15 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
15 #include "ui/gfx/image/image.h" | 17 #include "ui/gfx/image/image.h" |
16 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
17 | 19 |
18 namespace ash { | 20 namespace ash { |
19 namespace internal { | 21 namespace internal { |
20 | 22 |
23 // For our scaling ratios we need to round positive numbers. | |
24 static int RoundPositive(double x) { | |
25 return static_cast<int>(floor(x + 0.5)); | |
26 } | |
27 | |
21 //////////////////////////////////////////////////////////////////////////////// | 28 //////////////////////////////////////////////////////////////////////////////// |
22 // DesktopBackgroundView, public: | 29 // DesktopBackgroundView, public: |
23 | 30 |
24 DesktopBackgroundView::DesktopBackgroundView() { | 31 DesktopBackgroundView::DesktopBackgroundView() { |
25 wallpaper_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 32 wallpaper_ = *ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
26 IDR_AURA_WALLPAPER).ToSkBitmap(); | 33 IDR_AURA_WALLPAPER).ToSkBitmap(); |
27 wallpaper_.buildMipMap(false); | 34 wallpaper_.buildMipMap(false); |
28 } | 35 } |
29 | 36 |
30 DesktopBackgroundView::~DesktopBackgroundView() { | 37 DesktopBackgroundView::~DesktopBackgroundView() { |
31 } | 38 } |
32 | 39 |
33 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
34 // DesktopBackgroundView, views::View overrides: | 41 // DesktopBackgroundView, views::View overrides: |
35 | 42 |
36 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { | 43 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { |
37 canvas->DrawBitmapInt(wallpaper_, | 44 // Scale the image while maintaining the aspect ratio, cropping as |
38 0, 0, wallpaper_.width(), wallpaper_.height(), | 45 // necessary to fill the background. Ideally the image should be larger |
39 0, 0, width(), height(), | 46 // than the largest display supported, if not we will center it rather than |
40 true); | 47 // streching to avoid upsampling artifacts (Note that we could tile too, but |
48 // decided not to do this at the moment). | |
49 gfx::Rect wallpaper_rect(0, 0, wallpaper_.width(), wallpaper_.height()); | |
50 if (wallpaper_.width() > width() && wallpaper_.height() > height()) { | |
51 // The dimension with the smallest ratio must be cropped, the other one | |
52 // is preserved. Both are set in gfx::Size cropped_size. | |
53 double horizontal_ratio = static_cast<double>(width()) / | |
54 static_cast<double>(wallpaper_.width()); | |
55 double vertical_ratio = static_cast<double>(height()) / | |
56 static_cast<double>(wallpaper_.height()); | |
57 | |
58 gfx::Size cropped_size; | |
59 if (vertical_ratio > horizontal_ratio) { | |
60 cropped_size = gfx::Size( | |
61 RoundPositive(static_cast<double>(width()) / vertical_ratio), | |
62 wallpaper_.height()); | |
63 } else { | |
64 cropped_size = gfx::Size(wallpaper_.width(), | |
65 RoundPositive(static_cast<double>(height()) / horizontal_ratio)); | |
66 } | |
67 | |
68 gfx::Rect wallpaper_cropped_rect = wallpaper_rect.Center(cropped_size); | |
69 canvas->DrawBitmapInt(wallpaper_, | |
70 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), | |
71 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), | |
72 0, 0, width(), height(), | |
73 true); | |
74 } | |
75 else { | |
tfarina
2012/03/01 17:24:12
nit: else should be in the of the previous line.
| |
76 // Center the wallpaper in the destination rectangle (Skia will crop | |
77 // as needed). We might decide later to tile small solid color images. | |
78 canvas->DrawBitmapInt(wallpaper_, (width() - wallpaper_.width()) / 2, | |
79 (height() - wallpaper_.height()) / 2); | |
80 } | |
41 } | 81 } |
42 | 82 |
43 bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { | 83 bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { |
44 return true; | 84 return true; |
45 } | 85 } |
46 | 86 |
47 void DesktopBackgroundView::OnMouseReleased(const views::MouseEvent& event) { | 87 void DesktopBackgroundView::OnMouseReleased(const views::MouseEvent& event) { |
48 if (event.IsRightMouseButton()) | 88 if (event.IsRightMouseButton()) |
49 Shell::GetInstance()->ShowBackgroundMenu(GetWidget(), event.location()); | 89 Shell::GetInstance()->ShowBackgroundMenu(GetWidget(), event.location()); |
50 } | 90 } |
51 | 91 |
52 views::Widget* CreateDesktopBackground() { | 92 views::Widget* CreateDesktopBackground() { |
53 views::Widget* desktop_widget = new views::Widget; | 93 views::Widget* desktop_widget = new views::Widget; |
54 views::Widget::InitParams params( | 94 views::Widget::InitParams params( |
55 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 95 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
56 DesktopBackgroundView* view = new DesktopBackgroundView; | 96 DesktopBackgroundView* view = new DesktopBackgroundView; |
57 params.delegate = view; | 97 params.delegate = view; |
58 params.parent = | 98 params.parent = |
59 Shell::GetInstance()->GetContainer( | 99 Shell::GetInstance()->GetContainer( |
60 ash::internal::kShellWindowId_DesktopBackgroundContainer); | 100 ash::internal::kShellWindowId_DesktopBackgroundContainer); |
61 desktop_widget->Init(params); | 101 desktop_widget->Init(params); |
62 desktop_widget->SetContentsView(view); | 102 desktop_widget->SetContentsView(view); |
63 desktop_widget->Show(); | 103 desktop_widget->Show(); |
64 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView"); | 104 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView"); |
65 return desktop_widget; | 105 return desktop_widget; |
66 } | 106 } |
67 | 107 |
68 } // namespace internal | 108 } // namespace internal |
69 } // namespace ash | 109 } // namespace ash |
OLD | NEW |