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

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

Issue 10810039: 2nd display should show the same background as login/lock screen: (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Post-review fix #2 Created 8 years, 5 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
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_controller.h" 5 #include "ash/desktop_background/desktop_background_controller.h"
6 6
7 #include "ash/desktop_background/desktop_background_widget_controller.h"
Nikita (slow) 2012/07/28 01:25:25 nit: sort
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
7 #include "ash/desktop_background/desktop_background_view.h" 8 #include "ash/desktop_background/desktop_background_view.h"
8 #include "ash/shell.h" 9 #include "ash/shell.h"
9 #include "ash/shell_factory.h" 10 #include "ash/shell_factory.h"
10 #include "ash/shell_window_ids.h" 11 #include "ash/shell_window_ids.h"
11 #include "ash/wm/root_window_layout_manager.h" 12 #include "ash/wm/root_window_layout_manager.h"
12 #include "base/bind.h" 13 #include "base/bind.h"
13 #include "base/logging.h" 14 #include "base/logging.h"
14 #include "base/synchronization/cancellation_flag.h" 15 #include "base/synchronization/cancellation_flag.h"
15 #include "base/threading/worker_pool.h" 16 #include "base/threading/worker_pool.h"
16 #include "grit/ui_resources.h" 17 #include "grit/ui_resources.h"
17 #include "ui/aura/root_window.h" 18 #include "ui/aura/root_window.h"
18 #include "ui/aura/window.h" 19 #include "ui/aura/window.h"
19 #include "ui/base/resource/resource_bundle.h" 20 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/compositor/layer.h" 21 #include "ui/compositor/layer.h"
22 #include "ui/gfx/rect.h"
21 #include "ui/gfx/image/image.h" 23 #include "ui/gfx/image/image.h"
22 #include "ui/views/widget/widget.h" 24 #include "ui/views/widget/widget.h"
23 25
24 namespace ash { 26 namespace ash {
25 namespace { 27 namespace {
26 internal::RootWindowLayoutManager* GetRootWindowLayoutManager( 28 internal::RootWindowLayoutManager* GetRootWindowLayoutManager(
27 aura::RootWindow* root_window) { 29 aura::RootWindow* root_window) {
28 return static_cast<internal::RootWindowLayoutManager*>( 30 return static_cast<internal::RootWindowLayoutManager*>(
29 root_window->layout_manager()); 31 root_window->layout_manager());
30 } 32 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 base::CancellationFlag cancel_flag_; 86 base::CancellationFlag cancel_flag_;
85 87
86 scoped_ptr<WallpaperData> wallpaper_data_; 88 scoped_ptr<WallpaperData> wallpaper_data_;
87 89
88 int index_; 90 int index_;
89 91
90 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation); 92 DISALLOW_COPY_AND_ASSIGN(WallpaperOperation);
91 }; 93 };
92 94
93 DesktopBackgroundController::DesktopBackgroundController() 95 DesktopBackgroundController::DesktopBackgroundController()
94 : desktop_background_mode_(BACKGROUND_IMAGE), 96 : locked_(false),
97 desktop_background_mode_(BACKGROUND_SOLID_COLOR),
95 background_color_(SK_ColorGRAY), 98 background_color_(SK_ColorGRAY),
96 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 99 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
100 // Install wallpaper component for all available windows.
Nikita (slow) 2012/07/28 01:25:25 nit: Not method name speaks for itself, comment sh
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
101 InstallComponentForAllWindows();
97 } 102 }
98 103
99 DesktopBackgroundController::~DesktopBackgroundController() { 104 DesktopBackgroundController::~DesktopBackgroundController() {
100 CancelPendingWallpaperOperation(); 105 CancelPendingWallpaperOperation();
101 } 106 }
102 107
103 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 108 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
104 if (current_wallpaper_.get()) 109 if (current_wallpaper_.get())
105 return current_wallpaper_->wallpaper_image; 110 return current_wallpaper_->wallpaper_image;
106 return gfx::ImageSkia(); 111 return gfx::ImageSkia();
107 } 112 }
108 113
109 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { 114 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const {
110 if (current_wallpaper_.get()) 115 if (current_wallpaper_.get())
111 return current_wallpaper_->wallpaper_layout; 116 return current_wallpaper_->wallpaper_layout;
112 return CENTER_CROPPED; 117 return CENTER_CROPPED;
113 } 118 }
114 119
115 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() { 120 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() {
116 if (desktop_background_mode_ != BACKGROUND_IMAGE) 121 if (desktop_background_mode_ != BACKGROUND_IMAGE)
117 return SkBitmap(); 122 return SkBitmap();
118 return GetWallpaper(); 123 return GetWallpaper();
119 } 124 }
120 125
121 void DesktopBackgroundController::OnRootWindowAdded( 126 void DesktopBackgroundController::OnRootWindowAdded(
122 aura::RootWindow* root_window) { 127 aura::RootWindow* root_window) {
123 switch (desktop_background_mode_) { 128 InstallComponent(root_window);
124 case BACKGROUND_IMAGE:
125 if (current_wallpaper_.get()) {
126 SetDesktopBackgroundImage(root_window);
127 } else {
128 internal::CreateDesktopBackground(root_window);
129 }
130 break;
131 case BACKGROUND_SOLID_COLOR:
132 SetDesktopBackgroundSolidColorMode(background_color_);
133 break;
134 }
135 } 129 }
136 130
137 void DesktopBackgroundController::SetDefaultWallpaper(int index) { 131 void DesktopBackgroundController::SetDefaultWallpaper(int index) {
138 // We should not change background when index is invalid. For instance, at 132 // We should not change background when index is invalid. For instance, at
139 // login screen or stub_user login. 133 // login screen or stub_user login.
140 if (index == ash::GetInvalidWallpaperIndex()) { 134 if (index == GetInvalidWallpaperIndex()) {
141 CreateEmptyWallpaper(); 135 CreateEmptyWallpaper();
142 return; 136 return;
143 } else if (index == ash::GetSolidColorIndex()) { 137 } else if (index == GetSolidColorIndex()) {
144 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); 138 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor);
145 return; 139 return;
146 } 140 }
147 141
148 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) 142 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index)
149 return; 143 return;
150 144
151 CancelPendingWallpaperOperation(); 145 CancelPendingWallpaperOperation();
152 146
153 wallpaper_op_ = new WallpaperOperation(index); 147 wallpaper_op_ = new WallpaperOperation(index);
154 base::WorkerPool::PostTaskAndReply( 148 base::WorkerPool::PostTaskAndReply(
155 FROM_HERE, 149 FROM_HERE,
156 base::Bind(&WallpaperOperation::Run, wallpaper_op_), 150 base::Bind(&WallpaperOperation::Run, wallpaper_op_),
157 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, 151 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
158 weak_ptr_factory_.GetWeakPtr(), 152 weak_ptr_factory_.GetWeakPtr(),
159 wallpaper_op_), 153 wallpaper_op_),
160 true /* task_is_slow */); 154 true /* task_is_slow */);
161 } 155 }
162 156
163 void DesktopBackgroundController::SetCustomWallpaper( 157 void DesktopBackgroundController::SetCustomWallpaper(
164 const gfx::ImageSkia& wallpaper, 158 const gfx::ImageSkia& wallpaper,
165 WallpaperLayout layout) { 159 WallpaperLayout layout) {
166 CancelPendingWallpaperOperation(); 160 CancelPendingWallpaperOperation();
167 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); 161 current_wallpaper_.reset(new WallpaperData(layout, wallpaper));
168 desktop_background_mode_ = BACKGROUND_IMAGE; 162 SetDesktopBackgroundImageMode();
169 UpdateDesktopBackgroundImageMode();
170 } 163 }
171 164
172 void DesktopBackgroundController::CancelPendingWallpaperOperation() { 165 void DesktopBackgroundController::CancelPendingWallpaperOperation() {
173 // Set canceled flag of previous request to skip unneeded loading. 166 // Set canceled flag of previous request to skip unneeded loading.
174 if (wallpaper_op_.get()) 167 if (wallpaper_op_.get())
175 wallpaper_op_->Cancel(); 168 wallpaper_op_->Cancel();
176 169
177 // Cancel reply callback for previous request. 170 // Cancel reply callback for previous request.
178 weak_ptr_factory_.InvalidateWeakPtrs(); 171 weak_ptr_factory_.InvalidateWeakPtrs();
179 } 172 }
180 173
181 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode( 174 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode(
182 SkColor color) { 175 SkColor color) {
183 // Set a solid black background.
184 // TODO(derat): Remove this in favor of having the compositor only clear the
185 // viewport when there are regions not covered by a layer:
186 // http://crbug.com/113445
187 current_wallpaper_.reset(NULL);
188 background_color_ = color; 176 background_color_ = color;
189 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; 177 if (desktop_background_mode_ != BACKGROUND_SOLID_COLOR) {
178 desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
179 InstallComponentForAllWindows();
180 return;
181 }
182
190 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 183 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
191 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 184 for (Shell::RootWindowList::iterator iter = root_windows.begin();
192 iter != root_windows.end(); ++iter) { 185 iter != root_windows.end(); ++iter) {
Nikita (slow) 2012/07/28 01:25:25 nit: Indentation is not fixed. Should be for (Sh
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
193 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
194 background_layer->SetColor(color);
195 aura::RootWindow* root_window = *iter; 186 aura::RootWindow* root_window = *iter;
196 Shell::GetContainer( 187 internal::DesktopBackgroundWidgetController* component = root_window->
197 root_window, 188 GetProperty(internal::kWindowDesktopComponent);
198 internal::kShellWindowId_DesktopBackgroundContainer)-> 189 DCHECK(component);
199 layer()->Add(background_layer); 190 DCHECK(component->layer());
200 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer( 191 component->layer()->SetColor(background_color_ );
201 background_layer);
202 GetRootWindowLayoutManager(root_window)->SetBackgroundWidget(NULL);
203 } 192 }
204 } 193 }
205 194
206 void DesktopBackgroundController::SetDesktopBackgroundImage( 195 void DesktopBackgroundController::CreateEmptyWallpaper() {
207 aura::RootWindow* root_window) { 196 current_wallpaper_.reset(NULL);
208 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer(NULL); 197 SetDesktopBackgroundImageMode();
209 if (current_wallpaper_.get() &&
210 !current_wallpaper_->wallpaper_image.empty())
211 internal::CreateDesktopBackground(root_window);
212 } 198 }
213 199
214 void DesktopBackgroundController::UpdateDesktopBackgroundImageMode() { 200 void DesktopBackgroundController::MoveDesktopToLockedContainer() {
201 if (locked_)
202 return;
203 locked_ = true;
204 ReparentBackgroundWidgets(GetBackgroundContainerId(false),
205 GetBackgroundContainerId(true));
206 }
207
208 void DesktopBackgroundController::MoveDesktopToUnlockedContainer() {
209 if (!locked_)
210 return;
211 locked_ = false;
212 ReparentBackgroundWidgets(GetBackgroundContainerId(true),
213 GetBackgroundContainerId(false));
214 }
215
216 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
217 if (desktop_background_mode_ != BACKGROUND_IMAGE) {
218 desktop_background_mode_ = BACKGROUND_IMAGE;
219 InstallComponentForAllWindows();
220 return;
221 }
222
215 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 223 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
216
217 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 224 for (Shell::RootWindowList::iterator iter = root_windows.begin();
218 iter != root_windows.end(); ++iter) { 225 iter != root_windows.end(); ++iter) {
219 SetDesktopBackgroundImage(*iter); 226 aura::RootWindow* root_window = *iter;
227 internal::DesktopBackgroundWidgetController* component = root_window->
228 GetProperty(internal::kWindowDesktopComponent);
229 DCHECK(component);
230 DCHECK(component->widget());
231 aura::Window* window = component->widget()->GetNativeView();
232 gfx::Rect bounds = window->bounds();
233 window->SchedulePaintInRect(gfx::Rect(0, 0,
234 bounds.width(), bounds.height()));
220 } 235 }
221 desktop_background_mode_ = BACKGROUND_IMAGE;
222 } 236 }
223 237
224 void DesktopBackgroundController::OnWallpaperLoadCompleted( 238 void DesktopBackgroundController::OnWallpaperLoadCompleted(
225 scoped_refptr<WallpaperOperation> wo) { 239 scoped_refptr<WallpaperOperation> wo) {
226 current_wallpaper_.reset(wo->ReleaseWallpaperData()); 240 current_wallpaper_.reset(wo->ReleaseWallpaperData());
227 241
228 UpdateDesktopBackgroundImageMode(); 242 SetDesktopBackgroundImageMode();
229 243
230 DCHECK(wo.get() == wallpaper_op_.get()); 244 DCHECK(wo.get() == wallpaper_op_.get());
231 wallpaper_op_ = NULL; 245 wallpaper_op_ = NULL;
232 } 246 }
233 247
234 void DesktopBackgroundController::CreateEmptyWallpaper() { 248 ui::Layer* DesktopBackgroundController::SetColorLayerForContainer(
235 current_wallpaper_.reset(NULL); 249 SkColor color,
236 desktop_background_mode_ = BACKGROUND_IMAGE; 250 aura::RootWindow* root_window,
251 int container_id) {
252 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
253 background_layer->SetColor(color);
254
255 Shell::GetContainer(root_window,container_id)->
256 layer()->Add(background_layer);
257 return background_layer;
258 }
259
260 void DesktopBackgroundController::InstallComponent(
261 aura::RootWindow* root_window) {
262 internal::DesktopBackgroundWidgetController* component = NULL;
263 int container_id = GetBackgroundContainerId(locked_);
264
265 switch (desktop_background_mode_) {
266 case BACKGROUND_IMAGE: {
267 views::Widget* widget = internal::CreateDesktopBackground(root_window,
268 container_id);
269 component = new internal::DesktopBackgroundWidgetController(widget);
270 break;
271 }
272 case BACKGROUND_SOLID_COLOR: {
273 ui::Layer* layer = SetColorLayerForContainer(background_color_,
274 root_window,
275 container_id);
276 component = new internal::DesktopBackgroundWidgetController(layer);
277 break;
278 }
279 default:
Nikita (slow) 2012/07/28 01:25:25 nit: Should also have {} http://google-styleguide.
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
280 NOTREACHED();
281 }
282 root_window->SetProperty(internal::kWindowDesktopComponent, component);
283 }
284
285 void DesktopBackgroundController::InstallComponentForAllWindows() {
237 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 286 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
238 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 287 for (Shell::RootWindowList::iterator iter = root_windows.begin();
239 iter != root_windows.end(); ++iter) { 288 iter != root_windows.end(); ++iter) {
Nikita (slow) 2012/07/28 01:25:25 nit: +1 indent space
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
240 internal::CreateDesktopBackground(*iter); 289 InstallComponent(*iter);
241 } 290 }
242 } 291 }
243 292
293 void DesktopBackgroundController::ReparentBackgroundWidgets(int src_container,
294 int dst_container) {
295 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
296 for (Shell::RootWindowList::iterator iter = root_windows.begin();
297 iter != root_windows.end(); ++iter) {
298 aura::RootWindow* root_window = *iter;
299 internal::DesktopBackgroundWidgetController* component = root_window->
300 GetProperty(internal::kWindowDesktopComponent);
301 DCHECK(component);
302 component->Reparent(root_window,
303 src_container,
304 dst_container);
305 }
306 }
307
308 int DesktopBackgroundController::GetBackgroundContainerId(bool locked) {
309 return locked ? internal::kShellWindowId_LockScreenBackgroundContainer :
Nikita (slow) 2012/07/28 01:25:25 nit: Remove extra space.
Denis Kuznetsov (DE-MUC) 2012/07/30 15:40:02 Done.
310 internal::kShellWindowId_DesktopBackgroundContainer;
311 }
312
244 } // namespace ash 313 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698