Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(213)

Side by Side Diff: ash/desktop_background/desktop_background_view.cc

Issue 10375010: Implement user selected wallpaper feature. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: James' review Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 layout) {
65 wallpaper_ = wallpaper; 65 wallpaper_ = wallpaper;
66 image_layout_ = layout; 66 wallpaper_layout_ = 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 layout) {
flackr 2012/05/09 21:26:16 As before, s/layout/wallpaper_layout.
bshe 2012/05/10 16:10:26 Done.
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, layout);
135 params.delegate = view; 136 params.delegate = view;
136 params.parent = 137 params.parent =
137 Shell::GetInstance()->GetContainer( 138 Shell::GetInstance()->GetContainer(
138 ash::internal::kShellWindowId_DesktopBackgroundContainer); 139 ash::internal::kShellWindowId_DesktopBackgroundContainer);
139 desktop_widget->Init(params); 140 desktop_widget->Init(params);
140 desktop_widget->SetContentsView(view); 141 desktop_widget->SetContentsView(view);
141 ash::SetWindowVisibilityAnimationType( 142 ash::SetWindowVisibilityAnimationType(
142 desktop_widget->GetNativeView(), 143 desktop_widget->GetNativeView(),
143 ash::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); 144 ash::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
144 ash::SetWindowVisibilityAnimationTransition( 145 ash::SetWindowVisibilityAnimationTransition(
145 desktop_widget->GetNativeView(), 146 desktop_widget->GetNativeView(),
146 ash::ANIMATE_SHOW); 147 ash::ANIMATE_SHOW);
147 desktop_widget->SetBounds(params.parent->bounds()); 148 desktop_widget->SetBounds(params.parent->bounds());
148 ui::ScopedLayerAnimationSettings settings(desktop_widget->GetNativeView()-> 149 ui::ScopedLayerAnimationSettings settings(desktop_widget->GetNativeView()->
149 layer()->GetAnimator()); 150 layer()->GetAnimator());
150 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION); 151 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION);
151 settings.AddObserver(new ShowWallpaperAnimationObserver(desktop_widget)); 152 settings.AddObserver(new ShowWallpaperAnimationObserver(desktop_widget));
152 desktop_widget->Show(); 153 desktop_widget->Show();
153 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView"); 154 desktop_widget->GetNativeView()->SetName("DesktopBackgroundView");
154 } 155 }
155 156
156 } // namespace internal 157 } // namespace internal
157 } // namespace ash 158 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698