Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_widget_controller.cc
blob5b9345285fb95e4f592a6bd21427b3038dfd787f
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
3 // found in the LICENSE file.
5 #include "ash/desktop_background/desktop_background_widget_controller.h"
7 #include "ash/ash_export.h"
8 #include "ui/aura/root_window.h"
9 #include "ui/aura/window_property.h"
10 #include "ui/views/widget/widget.h"
12 // Exported for tests.
13 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(
14 ASH_EXPORT, ash::internal::DesktopBackgroundWidgetController*);
15 DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(
16 ASH_EXPORT, ash::internal::AnimatingDesktopController*);
18 namespace ash {
19 namespace internal {
21 DEFINE_OWNED_WINDOW_PROPERTY_KEY(DesktopBackgroundWidgetController,
22 kDesktopController, NULL);
23 DEFINE_OWNED_WINDOW_PROPERTY_KEY(AnimatingDesktopController,
24 kAnimatingDesktopController, NULL);
26 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController(
27 views::Widget* widget) : widget_(widget) {
28 DCHECK(widget_);
29 widget_->AddObserver(this);
32 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController(
33 ui::Layer* layer) : widget_(NULL) {
34 layer_.reset(layer);
37 DesktopBackgroundWidgetController::~DesktopBackgroundWidgetController() {
38 if (widget_) {
39 widget_->RemoveObserver(this);
40 widget_->CloseNow();
41 widget_ = NULL;
42 } else if (layer_.get())
43 layer_.reset(NULL);
46 void DesktopBackgroundWidgetController::OnWidgetDestroying(
47 views::Widget* widget) {
48 widget_->RemoveObserver(this);
49 widget_ = NULL;
52 void DesktopBackgroundWidgetController::SetBounds(gfx::Rect bounds) {
53 if (widget_)
54 widget_->SetBounds(bounds);
55 else if (layer_.get())
56 layer_->SetBounds(bounds);
59 bool DesktopBackgroundWidgetController::Reparent(aura::RootWindow* root_window,
60 int src_container,
61 int dest_container) {
62 if (widget_) {
63 views::Widget::ReparentNativeView(widget_->GetNativeView(),
64 root_window->GetChildById(dest_container));
65 return true;
67 if (layer_.get()) {
68 ui::Layer* layer = layer_.get();
69 root_window->GetChildById(src_container)->layer()->Remove(layer);
70 root_window->GetChildById(dest_container)->layer()->Add(layer);
71 return true;
73 // Nothing to reparent.
74 return false;
77 AnimatingDesktopController::AnimatingDesktopController(
78 DesktopBackgroundWidgetController* component) {
79 controller_.reset(component);
82 AnimatingDesktopController::~AnimatingDesktopController() {
85 void AnimatingDesktopController::StopAnimating() {
86 if (controller_) {
87 ui::Layer* layer = controller_->layer() ? controller_->layer() :
88 controller_->widget()->GetNativeView()->layer();
89 layer->GetAnimator()->StopAnimating();
93 DesktopBackgroundWidgetController* AnimatingDesktopController::GetController(
94 bool pass_ownership) {
95 if (pass_ownership)
96 return controller_.release();
97 return controller_.get();
100 } // namespace internal
101 } // namespace ash