Chromium Code Reviews| 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_controller.h" | 5 #include "ash/desktop_background/desktop_background_controller.h" |
| 6 | 6 |
| 7 #include "ash/desktop_background/desktop_background_component.h" | |
| 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 Loading... | |
| 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/25 20:45:24
nit: insert space between // and comment text.
sky
2012/07/25 20:47:36
nit: space after //
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 101 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 102 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 103 iter != root_windows.end(); ++iter) { | |
| 104 InstallComponent(*iter); | |
| 105 } | |
| 97 } | 106 } |
| 98 | 107 |
| 99 DesktopBackgroundController::~DesktopBackgroundController() { | 108 DesktopBackgroundController::~DesktopBackgroundController() { |
| 100 CancelPendingWallpaperOperation(); | 109 CancelPendingWallpaperOperation(); |
| 101 } | 110 } |
| 102 | 111 |
| 103 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { | 112 gfx::ImageSkia DesktopBackgroundController::GetWallpaper() const { |
| 104 if (current_wallpaper_.get()) | 113 if (current_wallpaper_.get()) |
| 105 return current_wallpaper_->wallpaper_image; | 114 return current_wallpaper_->wallpaper_image; |
| 106 return gfx::ImageSkia(); | 115 return gfx::ImageSkia(); |
| 107 } | 116 } |
| 108 | 117 |
| 109 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { | 118 WallpaperLayout DesktopBackgroundController::GetWallpaperLayout() const { |
| 110 if (current_wallpaper_.get()) | 119 if (current_wallpaper_.get()) |
| 111 return current_wallpaper_->wallpaper_layout; | 120 return current_wallpaper_->wallpaper_layout; |
| 112 return CENTER_CROPPED; | 121 return CENTER_CROPPED; |
| 113 } | 122 } |
| 114 | 123 |
| 115 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() { | 124 SkBitmap DesktopBackgroundController::GetCurrentWallpaperImage() { |
| 116 if (desktop_background_mode_ != BACKGROUND_IMAGE) | 125 if (desktop_background_mode_ != BACKGROUND_IMAGE) |
| 117 return SkBitmap(); | 126 return SkBitmap(); |
| 118 return GetWallpaper(); | 127 return GetWallpaper(); |
| 119 } | 128 } |
| 120 | 129 |
| 121 void DesktopBackgroundController::OnRootWindowAdded( | 130 void DesktopBackgroundController::OnRootWindowAdded( |
| 122 aura::RootWindow* root_window) { | 131 aura::RootWindow* root_window) { |
| 123 switch (desktop_background_mode_) { | 132 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 } | 133 } |
| 136 | 134 |
| 137 void DesktopBackgroundController::SetDefaultWallpaper(int index) { | 135 void DesktopBackgroundController::SetDefaultWallpaper(int index) { |
| 138 // We should not change background when index is invalid. For instance, at | 136 // We should not change background when index is invalid. For instance, at |
| 139 // login screen or stub_user login. | 137 // login screen or stub_user login. |
| 140 if (index == ash::GetInvalidWallpaperIndex()) { | 138 if (index == GetInvalidWallpaperIndex()) { |
| 141 CreateEmptyWallpaper(); | 139 CreateEmptyWallpaper(); |
| 142 return; | 140 return; |
| 143 } else if (index == ash::GetSolidColorIndex()) { | 141 } else if (index == GetSolidColorIndex()) { |
| 144 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); | 142 SetDesktopBackgroundSolidColorMode(kLoginWallpaperColor); |
| 145 return; | 143 return; |
| 146 } | 144 } |
| 147 | 145 |
| 148 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) | 146 if (current_wallpaper_.get() && current_wallpaper_->wallpaper_index == index) |
| 149 return; | 147 return; |
| 150 | 148 |
| 151 CancelPendingWallpaperOperation(); | 149 CancelPendingWallpaperOperation(); |
| 152 | 150 |
| 153 wallpaper_op_ = new WallpaperOperation(index); | 151 wallpaper_op_ = new WallpaperOperation(index); |
| 154 base::WorkerPool::PostTaskAndReply( | 152 base::WorkerPool::PostTaskAndReply( |
| 155 FROM_HERE, | 153 FROM_HERE, |
| 156 base::Bind(&WallpaperOperation::Run, wallpaper_op_), | 154 base::Bind(&WallpaperOperation::Run, wallpaper_op_), |
| 157 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, | 155 base::Bind(&DesktopBackgroundController::OnWallpaperLoadCompleted, |
| 158 weak_ptr_factory_.GetWeakPtr(), | 156 weak_ptr_factory_.GetWeakPtr(), |
| 159 wallpaper_op_), | 157 wallpaper_op_), |
| 160 true /* task_is_slow */); | 158 true /* task_is_slow */); |
| 161 } | 159 } |
| 162 | 160 |
| 163 void DesktopBackgroundController::SetCustomWallpaper( | 161 void DesktopBackgroundController::SetCustomWallpaper( |
| 164 const gfx::ImageSkia& wallpaper, | 162 const gfx::ImageSkia& wallpaper, |
| 165 WallpaperLayout layout) { | 163 WallpaperLayout layout) { |
| 166 CancelPendingWallpaperOperation(); | 164 CancelPendingWallpaperOperation(); |
| 167 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); | 165 current_wallpaper_.reset(new WallpaperData(layout, wallpaper)); |
| 168 desktop_background_mode_ = BACKGROUND_IMAGE; | 166 SetDesktopBackgroundImageMode(); |
| 169 UpdateDesktopBackgroundImageMode(); | |
| 170 } | 167 } |
| 171 | 168 |
| 172 void DesktopBackgroundController::CancelPendingWallpaperOperation() { | 169 void DesktopBackgroundController::CancelPendingWallpaperOperation() { |
| 173 // Set canceled flag of previous request to skip unneeded loading. | 170 // Set canceled flag of previous request to skip unneeded loading. |
| 174 if (wallpaper_op_.get()) | 171 if (wallpaper_op_.get()) |
| 175 wallpaper_op_->Cancel(); | 172 wallpaper_op_->Cancel(); |
| 176 | 173 |
| 177 // Cancel reply callback for previous request. | 174 // Cancel reply callback for previous request. |
| 178 weak_ptr_factory_.InvalidateWeakPtrs(); | 175 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 179 } | 176 } |
| 180 | 177 |
| 181 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode( | |
| 182 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; | |
| 189 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; | |
| 190 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 191 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 192 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; | |
| 196 Shell::GetContainer( | |
| 197 root_window, | |
| 198 internal::kShellWindowId_DesktopBackgroundContainer)-> | |
| 199 layer()->Add(background_layer); | |
| 200 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer( | |
| 201 background_layer); | |
| 202 GetRootWindowLayoutManager(root_window)->SetBackgroundWidget(NULL); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 void DesktopBackgroundController::SetDesktopBackgroundImage( | |
| 207 aura::RootWindow* root_window) { | |
| 208 GetRootWindowLayoutManager(root_window)->SetBackgroundLayer(NULL); | |
| 209 if (current_wallpaper_.get() && | |
| 210 !current_wallpaper_->wallpaper_image.empty()) | |
| 211 internal::CreateDesktopBackground(root_window); | |
| 212 } | |
| 213 | |
| 214 void DesktopBackgroundController::UpdateDesktopBackgroundImageMode() { | |
| 215 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 216 | |
| 217 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 218 iter != root_windows.end(); ++iter) { | |
| 219 SetDesktopBackgroundImage(*iter); | |
| 220 } | |
| 221 desktop_background_mode_ = BACKGROUND_IMAGE; | |
| 222 } | |
| 223 | |
| 224 void DesktopBackgroundController::OnWallpaperLoadCompleted( | 178 void DesktopBackgroundController::OnWallpaperLoadCompleted( |
| 225 scoped_refptr<WallpaperOperation> wo) { | 179 scoped_refptr<WallpaperOperation> wo) { |
| 226 current_wallpaper_.reset(wo->ReleaseWallpaperData()); | 180 current_wallpaper_.reset(wo->ReleaseWallpaperData()); |
| 227 | 181 |
| 228 UpdateDesktopBackgroundImageMode(); | 182 SetDesktopBackgroundImageMode(); |
| 229 | 183 |
| 230 DCHECK(wo.get() == wallpaper_op_.get()); | 184 DCHECK(wo.get() == wallpaper_op_.get()); |
| 231 wallpaper_op_ = NULL; | 185 wallpaper_op_ = NULL; |
| 232 } | 186 } |
| 233 | 187 |
| 234 void DesktopBackgroundController::CreateEmptyWallpaper() { | 188 void DesktopBackgroundController::CreateEmptyWallpaper() { |
| 235 current_wallpaper_.reset(NULL); | 189 current_wallpaper_.reset(NULL); |
| 236 desktop_background_mode_ = BACKGROUND_IMAGE; | 190 SetDesktopBackgroundImageMode(); |
| 191 } | |
| 192 | |
| 193 | |
| 194 void DesktopBackgroundController::InstallComponent( | |
| 195 aura::RootWindow* root_window) { | |
| 196 internal::DesktopBackgroundComponent* component = NULL; | |
| 197 | |
| 198 int container_id = locked_ ? | |
| 199 internal::kShellWindowId_LockScreenBackgroundContainer : | |
| 200 internal::kShellWindowId_DesktopBackgroundContainer; | |
| 201 | |
| 202 switch (desktop_background_mode_) { | |
| 203 case BACKGROUND_IMAGE: { | |
| 204 views::Widget* widget = internal::CreateDesktopBackground(root_window, | |
| 205 container_id); | |
|
Nikita (slow)
2012/07/25 20:45:24
nit: indentation is off
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 206 component = new internal::DesktopBackgroundComponent(widget); | |
| 207 break; | |
| 208 } | |
| 209 case BACKGROUND_SOLID_COLOR: { | |
| 210 ui::Layer* layer = SetColorLayerForContainer(background_color_, | |
| 211 root_window, | |
|
Nikita (slow)
2012/07/25 20:45:24
nit: indentation
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 212 container_id); | |
| 213 component = new internal::DesktopBackgroundComponent(layer); | |
| 214 break; | |
| 215 } | |
| 216 } | |
|
Nikita (slow)
2012/07/25 20:45:24
default:
NOTREACHED();
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 217 root_window->SetProperty(internal::kWindowDesktopComponent, component); | |
| 218 } | |
| 219 | |
| 220 ui::Layer* DesktopBackgroundController::SetColorLayerForContainer( | |
| 221 SkColor color, | |
| 222 aura::RootWindow* root_window, | |
| 223 int container_id) { | |
| 224 ui::Layer* background_layer = new ui::Layer(ui::LAYER_SOLID_COLOR); | |
|
Nikita (slow)
2012/07/25 20:45:24
Indentation is off.
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 225 background_layer->SetColor(color); | |
| 226 | |
| 227 Shell::GetContainer(root_window,container_id)-> | |
| 228 layer()->Add(background_layer); | |
| 229 return background_layer; | |
| 230 } | |
| 231 | |
| 232 void DesktopBackgroundController::SetDesktopBackgroundSolidColorMode( | |
| 233 SkColor color) { | |
| 234 background_color_ = color; | |
| 235 if (desktop_background_mode_ != BACKGROUND_SOLID_COLOR) { | |
| 236 desktop_background_mode_ = BACKGROUND_SOLID_COLOR; | |
| 237 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
|
Nikita (slow)
2012/07/25 20:45:24
237-240 could be extracted to a separate function.
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 238 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 239 iter != root_windows.end(); ++iter) | |
|
Nikita (slow)
2012/07/25 20:45:24
nit: add {}
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 240 InstallComponent(*iter); | |
| 241 return; | |
| 242 } | |
| 243 | |
| 237 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | 244 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); |
| 238 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | 245 for (Shell::RootWindowList::iterator iter = root_windows.begin(); |
| 239 iter != root_windows.end(); ++iter) { | 246 iter != root_windows.end(); ++iter) { |
|
Nikita (slow)
2012/07/25 20:45:24
nit: Fix indent.
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 240 internal::CreateDesktopBackground(*iter); | 247 aura::RootWindow* root_window = *iter; |
| 248 internal::DesktopBackgroundComponent* component = root_window-> | |
| 249 GetProperty(internal::kWindowDesktopComponent); | |
| 250 DCHECK(component); | |
| 251 DCHECK(component->layer()); | |
| 252 component->layer()->SetColor(background_color_ ); | |
| 241 } | 253 } |
| 242 } | 254 } |
| 243 | 255 |
| 256 | |
| 257 void DesktopBackgroundController::SetDesktopBackgroundImageMode() { | |
| 258 if (desktop_background_mode_ != BACKGROUND_IMAGE) { | |
| 259 desktop_background_mode_ = BACKGROUND_IMAGE; | |
| 260 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 261 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 262 iter != root_windows.end(); ++iter) | |
|
Nikita (slow)
2012/07/25 20:45:24
nit: Add {}
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 263 InstallComponent(*iter); | |
| 264 return; | |
| 265 } | |
| 266 | |
| 267 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 268 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 269 iter != root_windows.end(); ++iter) { | |
| 270 aura::RootWindow* root_window = *iter; | |
| 271 internal::DesktopBackgroundComponent* component = root_window-> | |
| 272 GetProperty(internal::kWindowDesktopComponent); | |
| 273 DCHECK(component); | |
| 274 DCHECK(component->widget()); | |
| 275 aura::Window* window = component->widget()->GetNativeView(); | |
| 276 gfx::Rect bounds = window->bounds(); | |
| 277 window->SchedulePaintInRect(gfx::Rect(0, 0, | |
| 278 bounds.width(), bounds.height())); | |
| 279 } | |
| 280 | |
|
Nikita (slow)
2012/07/25 20:45:24
nit: nuke empty line
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 281 } | |
| 282 | |
| 283 | |
| 284 | |
| 285 void DesktopBackgroundController::CreateLockScreenDesktops() { | |
|
sky
2012/07/25 20:47:36
Position should match that of header.
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Done.
| |
| 286 if (locked_) | |
| 287 return; | |
| 288 locked_ = true; | |
|
sky
2012/07/25 20:47:36
Why do we need locked_ cached in here?
Denis Kuznetsov (DE-MUC)
2012/07/26 13:51:13
Because we want to add ui components to correct co
| |
| 289 | |
| 290 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 291 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 292 iter != root_windows.end(); ++iter) { | |
| 293 aura::RootWindow* root_window = *iter; | |
| 294 internal::DesktopBackgroundComponent* component = root_window-> | |
| 295 GetProperty(internal::kWindowDesktopComponent); | |
| 296 DCHECK(component); | |
| 297 component->Reparent(root_window, | |
| 298 internal::kShellWindowId_DesktopBackgroundContainer, | |
| 299 internal::kShellWindowId_LockScreenBackgroundContainer); | |
| 300 } | |
| 301 } | |
| 302 | |
| 303 void DesktopBackgroundController::RemoveLockScreenDesktops() { | |
| 304 if (!locked_) | |
| 305 return; | |
| 306 locked_ = false; | |
| 307 | |
| 308 Shell::RootWindowList root_windows = Shell::GetAllRootWindows(); | |
| 309 for (Shell::RootWindowList::iterator iter = root_windows.begin(); | |
| 310 iter != root_windows.end(); ++iter) { | |
| 311 aura::RootWindow* root_window = *iter; | |
| 312 internal::DesktopBackgroundComponent* component = root_window-> | |
| 313 GetProperty(internal::kWindowDesktopComponent); | |
| 314 DCHECK(component); | |
| 315 component->Reparent(root_window, | |
| 316 internal::kShellWindowId_LockScreenBackgroundContainer, | |
| 317 internal::kShellWindowId_DesktopBackgroundContainer); | |
| 318 } | |
| 319 } | |
| 320 | |
| 244 } // namespace ash | 321 } // namespace ash |
| OLD | NEW |