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> | 7 #include <limits> |
8 | 8 |
9 #include "ash/ash_export.h" | 9 #include "ash/ash_export.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 | 54 |
55 // For our scaling ratios we need to round positive numbers. | 55 // For our scaling ratios we need to round positive numbers. |
56 static int RoundPositive(double x) { | 56 static int RoundPositive(double x) { |
57 return static_cast<int>(floor(x + 0.5)); | 57 return static_cast<int>(floor(x + 0.5)); |
58 } | 58 } |
59 | 59 |
60 //////////////////////////////////////////////////////////////////////////////// | 60 //////////////////////////////////////////////////////////////////////////////// |
61 // DesktopBackgroundView, public: | 61 // DesktopBackgroundView, public: |
62 | 62 |
63 DesktopBackgroundView::DesktopBackgroundView(const SkBitmap& wallpaper, | 63 DesktopBackgroundView::DesktopBackgroundView(const SkBitmap& wallpaper, |
64 ImageLayout layout) { | 64 WallpaperLayout wallpaper_layout) { |
65 wallpaper_ = wallpaper; | 65 wallpaper_ = wallpaper; |
66 image_layout_ = layout; | 66 wallpaper_layout_ = wallpaper_layout; |
67 wallpaper_.buildMipMap(false); | 67 wallpaper_.buildMipMap(false); |
68 } | 68 } |
69 | 69 |
70 DesktopBackgroundView::~DesktopBackgroundView() { | 70 DesktopBackgroundView::~DesktopBackgroundView() { |
71 } | 71 } |
72 | 72 |
73 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
74 // DesktopBackgroundView, views::View overrides: | 74 // DesktopBackgroundView, views::View overrides: |
75 | 75 |
76 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { | 76 void DesktopBackgroundView::OnPaint(gfx::Canvas* canvas) { |
77 // Scale the image while maintaining the aspect ratio, cropping as | 77 // Scale the image while maintaining the aspect ratio, cropping as |
78 // necessary to fill the background. Ideally the image should be larger | 78 // necessary to fill the background. Ideally the image should be larger |
79 // than the largest display supported, if not we will center it rather than | 79 // than the largest display supported, if not we will center it rather than |
80 // streching to avoid upsampling artifacts (Note that we could tile too, but | 80 // streching to avoid upsampling artifacts (Note that we could tile too, but |
81 // decided not to do this at the moment). | 81 // decided not to do this at the moment). |
82 gfx::Rect wallpaper_rect(0, 0, wallpaper_.width(), wallpaper_.height()); | 82 gfx::Rect wallpaper_rect(0, 0, wallpaper_.width(), wallpaper_.height()); |
83 if (image_layout_ == ash::CENTER_CROPPED && wallpaper_.width() > width() | 83 if (wallpaper_layout_ == ash::CENTER_CROPPED && wallpaper_.width() > width() |
84 && wallpaper_.height() > height()) { | 84 && wallpaper_.height() > height()) { |
85 // The dimension with the smallest ratio must be cropped, the other one | 85 // The dimension with the smallest ratio must be cropped, the other one |
86 // is preserved. Both are set in gfx::Size cropped_size. | 86 // is preserved. Both are set in gfx::Size cropped_size. |
87 double horizontal_ratio = static_cast<double>(width()) / | 87 double horizontal_ratio = static_cast<double>(width()) / |
88 static_cast<double>(wallpaper_.width()); | 88 static_cast<double>(wallpaper_.width()); |
89 double vertical_ratio = static_cast<double>(height()) / | 89 double vertical_ratio = static_cast<double>(height()) / |
90 static_cast<double>(wallpaper_.height()); | 90 static_cast<double>(wallpaper_.height()); |
91 | 91 |
92 gfx::Size cropped_size; | 92 gfx::Size cropped_size; |
93 if (vertical_ratio > horizontal_ratio) { | 93 if (vertical_ratio > horizontal_ratio) { |
94 cropped_size = gfx::Size( | 94 cropped_size = gfx::Size( |
95 RoundPositive(static_cast<double>(width()) / vertical_ratio), | 95 RoundPositive(static_cast<double>(width()) / vertical_ratio), |
96 wallpaper_.height()); | 96 wallpaper_.height()); |
97 } else { | 97 } else { |
98 cropped_size = gfx::Size(wallpaper_.width(), | 98 cropped_size = gfx::Size(wallpaper_.width(), |
99 RoundPositive(static_cast<double>(height()) / horizontal_ratio)); | 99 RoundPositive(static_cast<double>(height()) / horizontal_ratio)); |
100 } | 100 } |
101 | 101 |
102 gfx::Rect wallpaper_cropped_rect = wallpaper_rect.Center(cropped_size); | 102 gfx::Rect wallpaper_cropped_rect = wallpaper_rect.Center(cropped_size); |
103 canvas->DrawBitmapInt(wallpaper_, | 103 canvas->DrawBitmapInt(wallpaper_, |
104 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), | 104 wallpaper_cropped_rect.x(), wallpaper_cropped_rect.y(), |
105 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), | 105 wallpaper_cropped_rect.width(), wallpaper_cropped_rect.height(), |
106 0, 0, width(), height(), | 106 0, 0, width(), height(), |
107 true); | 107 true); |
108 } else if (image_layout_ == ash::TILE) { | 108 } else if (wallpaper_layout_ == ash::TILE) { |
109 canvas->TileImageInt(wallpaper_, 0, 0, width(), height()); | 109 canvas->TileImageInt(wallpaper_, 0, 0, width(), height()); |
110 } else if (image_layout_ == ash::STRETCH) { | 110 } else if (wallpaper_layout_ == ash::STRETCH) { |
111 // This is generally not recommended as it may show artifacts. | 111 // This is generally not recommended as it may show artifacts. |
112 canvas->DrawBitmapInt(wallpaper_, 0, 0, wallpaper_.width(), | 112 canvas->DrawBitmapInt(wallpaper_, 0, 0, wallpaper_.width(), |
113 wallpaper_.height(), 0, 0, width(), height(), true); | 113 wallpaper_.height(), 0, 0, width(), height(), true); |
114 } else { | 114 } else { |
115 // All other are simply centered, and not scaled (but may be clipped). | 115 // All other are simply centered, and not scaled (but may be clipped). |
116 canvas->DrawBitmapInt(wallpaper_, (width() - wallpaper_.width()) / 2, | 116 canvas->DrawBitmapInt(wallpaper_, (width() - wallpaper_.width()) / 2, |
117 (height() - wallpaper_.height()) / 2); | 117 (height() - wallpaper_.height()) / 2); |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { | 121 bool DesktopBackgroundView::OnMousePressed(const views::MouseEvent& event) { |
122 return true; | 122 return true; |
123 } | 123 } |
124 | 124 |
125 void DesktopBackgroundView::OnMouseReleased(const views::MouseEvent& event) { | 125 void DesktopBackgroundView::OnMouseReleased(const views::MouseEvent& event) { |
126 if (event.IsRightMouseButton()) | 126 if (event.IsRightMouseButton()) |
127 Shell::GetInstance()->ShowBackgroundMenu(GetWidget(), event.location()); | 127 Shell::GetInstance()->ShowBackgroundMenu(GetWidget(), event.location()); |
128 } | 128 } |
129 | 129 |
130 void CreateDesktopBackground(const SkBitmap& wallpaper, ImageLayout layout) { | 130 void CreateDesktopBackground(const SkBitmap& wallpaper, |
| 131 WallpaperLayout wallpaper_layout) { |
131 views::Widget* desktop_widget = new views::Widget; | 132 views::Widget* desktop_widget = new views::Widget; |
132 views::Widget::InitParams params( | 133 views::Widget::InitParams params( |
133 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 134 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
134 DesktopBackgroundView* view = new DesktopBackgroundView(wallpaper, layout); | 135 DesktopBackgroundView* view = new DesktopBackgroundView(wallpaper, |
| 136 wallpaper_layout); |
135 params.delegate = view; | 137 params.delegate = view; |
136 params.parent = | 138 params.parent = |
137 Shell::GetInstance()->GetContainer( | 139 Shell::GetInstance()->GetContainer( |
138 ash::internal::kShellWindowId_DesktopBackgroundContainer); | 140 ash::internal::kShellWindowId_DesktopBackgroundContainer); |
139 desktop_widget->Init(params); | 141 desktop_widget->Init(params); |
140 desktop_widget->SetContentsView(view); | 142 desktop_widget->SetContentsView(view); |
141 ash::SetWindowVisibilityAnimationType( | 143 ash::SetWindowVisibilityAnimationType( |
142 desktop_widget->GetNativeView(), | 144 desktop_widget->GetNativeView(), |
143 ash::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); | 145 ash::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); |
144 ash::SetWindowVisibilityAnimationTransition( | 146 ash::SetWindowVisibilityAnimationTransition( |
145 desktop_widget->GetNativeView(), | 147 desktop_widget->GetNativeView(), |
146 ash::ANIMATE_SHOW); | 148 ash::ANIMATE_SHOW); |
147 desktop_widget->SetBounds(params.parent->bounds()); | 149 desktop_widget->SetBounds(params.parent->bounds()); |
148 ui::ScopedLayerAnimationSettings settings(desktop_widget->GetNativeView()-> | 150 ui::ScopedLayerAnimationSettings settings(desktop_widget->GetNativeView()-> |
149 layer()->GetAnimator()); | 151 layer()->GetAnimator()); |
150 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); | 152 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); |
151 settings.AddObserver(new ShowWallpaperAnimationObserver(desktop_widget)); | 153 settings.AddObserver(new ShowWallpaperAnimationObserver(desktop_widget)); |
152 desktop_widget->Show(); | 154 desktop_widget->Show(); |
153 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView"); | 155 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView"); |
154 } | 156 } |
155 | 157 |
156 } // namespace internal | 158 } // namespace internal |
157 } // namespace ash | 159 } // namespace ash |
OLD | NEW |