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

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: Fix of Nikita's comments Created 8 years, 4 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_view.h" 7 #include "ash/desktop_background/desktop_background_view.h"
8 #include "ash/desktop_background/desktop_background_widget_controller.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 InstallComponentForAllWindows();
97 } 101 }
98 102
99 DesktopBackgroundController::~DesktopBackgroundController() { 103 DesktopBackgroundController::~DesktopBackgroundController() {
100 CancelPendingWallpaperOperation(); 104 CancelPendingWallpaperOperation();
101 } 105 }
102 106
103 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { 107 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const {
104 if (current_wallpaper_.get()) 108 if (current_wallpaper_.get())
105 return current_wallpaper_->wallpaper_image; 109 return current_wallpaper_->wallpaper_image;
106 return gfx::ImageSkia(); 110 return gfx::ImageSkia();
107 } 111 }
108 112
109 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { 113 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const {
110 if (current_wallpaper_.get()) 114 if (current_wallpaper_.get())
111 return current_wallpaper_->wallpaper_layout; 115 return current_wallpaper_->wallpaper_layout;
112 return CENTER_CROPPED; 116 return CENTER_CROPPED;
113 } 117 }
114 118
115 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() { 119 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() {
116 if (desktop_background_mode_ != BACKGROUND_IMAGE) 120 if (desktop_background_mode_ != BACKGROUND_IMAGE)
117 return SkBitmap(); 121 return SkBitmap();
118 return GetWallpaper(); 122 return GetWallpaper();
119 } 123 }
120 124
121 void DesktopBackgroundController::OnRootWindowAdded( 125 void DesktopBackgroundController::OnRootWindowAdded(
122 aura::RootWindow* root_window) { 126 aura::RootWindow* root_window) {
123 switch (desktop_background_mode_) { 127 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 } 128 }
136 129
137 void DesktopBackgroundController::SetDefaultWallpaper(int index) { 130 void DesktopBackgroundController::SetDefaultWallpaper(int index) {
138 // We should not change background when index is invalid. For instance, at 131 // We should not change background when index is invalid. For instance, at
139 // login screen or stub_user login. 132 // login screen or stub_user login.
140 if (index == ash::GetInvalidWallpaperIndex()) { 133 if (index == GetInvalidWallpaperIndex()) {
141 CreateEmptyWallpaper(); 134 CreateEmptyWallpaper();
142 return; 135 return;
143 } else if (index == ash::GetSolidColorIndex()) { 136 } else if (index == GetSolidColorIndex()) {
144 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); 137 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor);
145 return; 138 return;
146 } 139 }
147 140
148 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) 141 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index)
149 return; 142 return;
150 143
151 CancelPendingWallpaperOperation(); 144 CancelPendingWallpaperOperation();
152 145
153 wallpaper_op_ = new WallpaperOperation(index); 146 wallpaper_op_ = new WallpaperOperation(index);
154 base::WorkerPool::PostTaskAndReply( 147 base::WorkerPool::PostTaskAndReply(
155 FROM_HERE, 148 FROM_HERE,
156 base::Bind(&WallpaperOperation::Run, wallpaper_op_), 149 base::Bind(&WallpaperOperation::Run, wallpaper_op_),
157 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, 150 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted,
158 weak_ptr_factory_.GetWeakPtr(), 151 weak_ptr_factory_.GetWeakPtr(),
159 wallpaper_op_), 152 wallpaper_op_),
160 true /* task_is_slow */); 153 true /* task_is_slow */);
161 } 154 }
162 155
163 void DesktopBackgroundController::SetCustomWallpaper( 156 void DesktopBackgroundController::SetCustomWallpaper(
164 const gfx::ImageSkia& wallpaper, 157 const gfx::ImageSkia& wallpaper,
165 WallpaperLayout layout) { 158 WallpaperLayout layout) {
166 CancelPendingWallpaperOperation(); 159 CancelPendingWallpaperOperation();
167 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); 160 current_wallpaper_.reset(new WallpaperData(layout, wallpaper));
168 desktop_background_mode_ = BACKGROUND_IMAGE; 161 SetDesktopBackgroundImageMode();
169 UpdateDesktopBackgroundImageMode();
170 } 162 }
171 163
172 void DesktopBackgroundController::CancelPendingWallpaperOperation() { 164 void DesktopBackgroundController::CancelPendingWallpaperOperation() {
173 // Set canceled flag of previous request to skip unneeded loading. 165 // Set canceled flag of previous request to skip unneeded loading.
174 if (wallpaper_op_.get()) 166 if (wallpaper_op_.get())
175 wallpaper_op_->Cancel(); 167 wallpaper_op_->Cancel();
176 168
177 // Cancel reply callback for previous request. 169 // Cancel reply callback for previous request.
178 weak_ptr_factory_.InvalidateWeakPtrs(); 170 weak_ptr_factory_.InvalidateWeakPtrs();
179 } 171 }
180 172
181 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode( 173 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode(
182 SkColor color) { 174 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; 175 background_color_ = color;
189 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; 176 if (desktop_background_mode_ != BACKGROUND_SOLID_COLOR) {
177 desktop_background_mode_ = BACKGROUND_SOLID_COLOR;
178 InstallComponentForAllWindows();
179 return;
180 }
181
190 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 182 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
191 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 183 for (Shell::RootWindowList::iterator iter = root_windows.begin();
192 iter != root_windows.end(); ++iter) { 184 iter != root_windows.end(); ++iter) {
193 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
194 background_layer->SetColor(color);
195 aura::RootWindow* root_window = *iter; 185 aura::RootWindow* root_window = *iter;
196 Shell::GetContainer( 186 internal::DesktopBackgroundWidgetController* component = root_window->
197 root_window, 187 GetProperty(internal::kWindowDesktopComponent);
198 internal::kShellWindowId_DesktopBackgroundContainer)-> 188 DCHECK(component);
199 layer()->Add(background_layer); 189 DCHECK(component->layer());
200 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer( 190 component->layer()->SetColor(background_color_ );
201 background_layer);
202 GetRootWindowLayoutManager(root_window)->SetBackgroundWidget(NULL);
203 } 191 }
204 } 192 }
205 193
206 void DesktopBackgroundController::SetDesktopBackgroundImage( 194 void DesktopBackgroundController::CreateEmptyWallpaper() {
207 aura::RootWindow* root_window) { 195 current_wallpaper_.reset(NULL);
208 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer(NULL); 196 SetDesktopBackgroundImageMode();
209 if (current_wallpaper_.get() &&
210 !current_wallpaper_->wallpaper_image.empty())
211 internal::CreateDesktopBackground(root_window);
212 } 197 }
213 198
214 void DesktopBackgroundController::UpdateDesktopBackgroundImageMode() { 199 void DesktopBackgroundController::MoveDesktopToLockedContainer() {
200 if (locked_)
201 return;
202 locked_ = true;
203 ReparentBackgroundWidgets(GetBackgroundContainerId(false),
204 GetBackgroundContainerId(true));
205 }
206
207 void DesktopBackgroundController::MoveDesktopToUnlockedContainer() {
208 if (!locked_)
209 return;
210 locked_ = false;
211 ReparentBackgroundWidgets(GetBackgroundContainerId(true),
212 GetBackgroundContainerId(false));
213 }
214
215 void DesktopBackgroundController::SetDesktopBackgroundImageMode() {
216 if (desktop_background_mode_ != BACKGROUND_IMAGE) {
217 desktop_background_mode_ = BACKGROUND_IMAGE;
218 InstallComponentForAllWindows();
219 return;
220 }
221
215 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); 222 Shell::RootWindowList root_windows = Shell::GetAllRootWindows();
216
217 for (Shell::RootWindowList::iterator iter = root_windows.begin(); 223 for (Shell::RootWindowList::iterator iter = root_windows.begin();
218 iter != root_windows.end(); ++iter) { 224 iter != root_windows.end(); ++iter) {
219 SetDesktopBackgroundImage(*iter); 225 aura::RootWindow* root_window = *iter;
226 internal::DesktopBackgroundWidgetController* component = root_window->
227 GetProperty(internal::kWindowDesktopComponent);
228 DCHECK(component);
229 DCHECK(component->widget());
230 aura::Window* window = component->widget()->GetNativeView();
231 gfx::Rect bounds = window->bounds();
232 window->SchedulePaintInRect(gfx::Rect(0, 0,
233 bounds.width(), bounds.height()));
220 } 234 }
221 desktop_background_mode_ = BACKGROUND_IMAGE;
222 } 235 }
223 236
224 void DesktopBackgroundController::OnWallpaperLoadCompleted( 237 void DesktopBackgroundController::OnWallpaperLoadCompleted(
225 scoped_refptr<WallpaperOperation> wo) { 238 scoped_refptr<WallpaperOperation> wo) {
226 current_wallpaper_.reset(wo->ReleaseWallpaperData()); 239 current_wallpaper_.reset(wo->ReleaseWallpaperData());
227 240
228 UpdateDesktopBackgroundImageMode(); 241 SetDesktopBackgroundImageMode();
229 242
230 DCHECK(wo.get() == wallpaper_op_.get()); 243 DCHECK(wo.get() == wallpaper_op_.get());
231 wallpaper_op_ = NULL; 244 wallpaper_op_ = NULL;
232 } 245 }
233 246
234 void DesktopBackgroundController::CreateEmptyWallpaper() { 247 ui::Layer* DesktopBackgroundController::SetColorLayerForContainer(
235 current_wallpaper_.reset(NULL); 248 SkColor color,
236 desktop_background_mode_ = BACKGROUND_IMAGE; 249 aura::RootWindow* root_window,
250 int container_id) {
251 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR);
252 background_layer->SetColor(color);
253
254 Shell::GetContainer(root_window,container_id)->
255 layer()->Add(background_layer);
256 return background_layer;
257 }
258
259 void DesktopBackgroundController::InstallComponent(
260 aura::RootWindow* root_window) {
261 internal::DesktopBackgroundWidgetController* component = NULL;
262 int container_id = GetBackgroundContainerId(locked_);
263
264 switch (desktop_background_mode_) {
265 case BACKGROUND_IMAGE: {
266 views::Widget* widget = internal::CreateDesktopBackground(root_window,
267 container_id);
268 component = new internal::DesktopBackgroundWidgetController(widget);
269 break;
270 }
271 case BACKGROUND_SOLID_COLOR: {
272 ui::Layer* layer = SetColorLayerForContainer(background_color_,
273 root_window,
274 container_id);
275 component = new internal::DesktopBackgroundWidgetController(layer);
276 break;
277 }
278 default: {
279 NOTREACHED();
280 }
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) {
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 :
310 internal::kShellWindowId_DesktopBackgroundContainer;
311 }
312
244 } // namespace ash 313 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698