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