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"
20 class ShowWallpaperAnimationObserver
: public ui::ImplicitAnimationObserver
,
21 public views::WidgetObserver
{
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();
36 desktop_widget_
->RemoveObserver(this);
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_
);
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
);
67 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController(
68 views::Widget
* widget
) : widget_(widget
) {
70 widget_
->AddObserver(this);
73 DesktopBackgroundWidgetController::~DesktopBackgroundWidgetController() {
75 widget_
->RemoveObserver(this);
81 void DesktopBackgroundWidgetController::OnWidgetDestroying(
82 views::Widget
* widget
) {
83 widget_
->RemoveObserver(this);
87 void DesktopBackgroundWidgetController::SetBounds(gfx::Rect bounds
) {
89 widget_
->SetBounds(bounds
);
92 bool DesktopBackgroundWidgetController::Reparent(aura::Window
* root_window
,
96 views::Widget::ReparentNativeView(widget_
->GetNativeView(),
97 root_window
->GetChildById(dest_container
));
100 // Nothing to reparent.
104 void DesktopBackgroundWidgetController::StartAnimating(
105 RootWindowController
* root_window_controller
) {
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());
118 widget_
->GetNativeView()->SetName("DesktopBackgroundView");
122 AnimatingDesktopController::AnimatingDesktopController(
123 DesktopBackgroundWidgetController
* component
) {
124 controller_
.reset(component
);
127 AnimatingDesktopController::~AnimatingDesktopController() {
130 void AnimatingDesktopController::StopAnimating() {
132 ui::Layer
* layer
= controller_
->widget()->GetNativeView()->layer();
133 layer
->GetAnimator()->StopAnimating();
137 DesktopBackgroundWidgetController
* AnimatingDesktopController::GetController(
138 bool pass_ownership
) {
140 return controller_
.release();
141 return controller_
.get();