Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / ash / desktop_background / desktop_background_widget_controller.cc
blob72351c34c44fe14b4080567f8b90aca6aca3114c
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 "ash/desktop_background/user_wallpaper_delegate.h"
9 #include "ash/root_window_controller.h"
10 #include "ash/shell.h"
11 #include "ui/aura/window_event_dispatcher.h"
12 #include "ui/compositor/layer_animation_observer.h"
13 #include "ui/compositor/scoped_layer_animation_settings.h"
14 #include "ui/views/widget/widget.h"
15 #include "ui/views/widget/widget_observer.h"
17 namespace ash {
18 namespace {
20 class ShowWallpaperAnimationObserver : public ui::ImplicitAnimationObserver,
21 public views::WidgetObserver {
22 public:
23 ShowWallpaperAnimationObserver(RootWindowController* root_window_controller,
24 views::Widget* desktop_widget,
25 bool is_initial_animation)
26 : root_window_controller_(root_window_controller),
27 desktop_widget_(desktop_widget),
28 is_initial_animation_(is_initial_animation) {
29 DCHECK(desktop_widget_);
30 desktop_widget_->AddObserver(this);
33 ~ShowWallpaperAnimationObserver() override {
34 StopObservingImplicitAnimations();
35 if (desktop_widget_)
36 desktop_widget_->RemoveObserver(this);
39 private:
40 // Overridden from ui::ImplicitAnimationObserver:
41 void OnImplicitAnimationsScheduled() override {
42 if (is_initial_animation_) {
43 root_window_controller_->
44 HandleInitialDesktopBackgroundAnimationStarted();
48 void OnImplicitAnimationsCompleted() override {
49 root_window_controller_->OnWallpaperAnimationFinished(desktop_widget_);
50 delete this;
53 // Overridden from views::WidgetObserver.
54 void OnWidgetDestroying(views::Widget* widget) override { delete this; }
56 RootWindowController* root_window_controller_;
57 views::Widget* desktop_widget_;
59 // Is this object observing the initial brightness/grayscale animation?
60 const bool is_initial_animation_;
62 DISALLOW_COPY_AND_ASSIGN(ShowWallpaperAnimationObserver);
65 } // namespace
67 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController(
68 views::Widget* widget) : widget_(widget) {
69 DCHECK(widget_);
70 widget_->AddObserver(this);
73 DesktopBackgroundWidgetController::~DesktopBackgroundWidgetController() {
74 if (widget_) {
75 widget_->RemoveObserver(this);
76 widget_->CloseNow();
77 widget_ = NULL;
81 void DesktopBackgroundWidgetController::OnWidgetDestroying(
82 views::Widget* widget) {
83 widget_->RemoveObserver(this);
84 widget_ = NULL;
87 void DesktopBackgroundWidgetController::SetBounds(gfx::Rect bounds) {
88 if (widget_)
89 widget_->SetBounds(bounds);
92 bool DesktopBackgroundWidgetController::Reparent(aura::Window* root_window,
93 int src_container,
94 int dest_container) {
95 if (widget_) {
96 views::Widget::ReparentNativeView(widget_->GetNativeView(),
97 root_window->GetChildById(dest_container));
98 return true;
100 // Nothing to reparent.
101 return false;
104 void DesktopBackgroundWidgetController::StartAnimating(
105 RootWindowController* root_window_controller) {
106 if (widget_) {
107 ui::ScopedLayerAnimationSettings settings(
108 widget_->GetNativeView()->layer()->GetAnimator());
109 settings.AddObserver(new ShowWallpaperAnimationObserver(
110 root_window_controller, widget_,
111 Shell::GetInstance()->user_wallpaper_delegate()->
112 ShouldShowInitialAnimation()));
113 // When |widget_| shows, AnimateShowWindowCommon() is called to do the
114 // animation. Sets transition duration to 0 to avoid animating to the
115 // show animation's initial values.
116 settings.SetTransitionDuration(base::TimeDelta());
117 widget_->Show();
118 widget_->GetNativeView()->SetName("DesktopBackgroundView");
122 AnimatingDesktopController::AnimatingDesktopController(
123 DesktopBackgroundWidgetController* component) {
124 controller_.reset(component);
127 AnimatingDesktopController::~AnimatingDesktopController() {
130 void AnimatingDesktopController::StopAnimating() {
131 if (controller_) {
132 ui::Layer* layer = controller_->widget()->GetNativeView()->layer();
133 layer->GetAnimator()->StopAnimating();
137 DesktopBackgroundWidgetController* AnimatingDesktopController::GetController(
138 bool pass_ownership) {
139 if (pass_ownership)
140 return controller_.release();
141 return controller_.get();
144 } // namespace ash