OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/desktop_background/desktop_background_widget_controller.h" | |
6 | |
7 #include "ash/ash_export.h" | |
8 #include "ash/common/wallpaper/wallpaper_delegate.h" | |
9 #include "ash/common/wm_lookup.h" | |
10 #include "ash/common/wm_shell.h" | |
11 #include "ash/common/wm_window.h" | |
12 #include "ash/root_window_controller.h" | |
13 #include "ui/aura/window.h" | |
14 #include "ui/compositor/layer_animation_observer.h" | |
15 #include "ui/compositor/scoped_layer_animation_settings.h" | |
16 #include "ui/views/widget/widget.h" | |
17 | |
18 namespace ash { | |
19 namespace { | |
20 | |
21 class ShowWallpaperAnimationObserver : public ui::ImplicitAnimationObserver, | |
22 public views::WidgetObserver { | |
23 public: | |
24 ShowWallpaperAnimationObserver(RootWindowController* root_window_controller, | |
25 views::Widget* desktop_widget, | |
26 bool is_initial_animation) | |
27 : root_window_controller_(root_window_controller), | |
28 desktop_widget_(desktop_widget), | |
29 is_initial_animation_(is_initial_animation) { | |
30 DCHECK(desktop_widget_); | |
31 desktop_widget_->AddObserver(this); | |
32 } | |
33 | |
34 ~ShowWallpaperAnimationObserver() override { | |
35 StopObservingImplicitAnimations(); | |
36 if (desktop_widget_) | |
37 desktop_widget_->RemoveObserver(this); | |
38 } | |
39 | |
40 private: | |
41 // Overridden from ui::ImplicitAnimationObserver: | |
42 void OnImplicitAnimationsScheduled() override { | |
43 if (is_initial_animation_) { | |
44 root_window_controller_->HandleInitialDesktopBackgroundAnimationStarted(); | |
45 } | |
46 } | |
47 | |
48 void OnImplicitAnimationsCompleted() override { | |
49 root_window_controller_->OnWallpaperAnimationFinished(desktop_widget_); | |
50 delete this; | |
51 } | |
52 | |
53 // Overridden from views::WidgetObserver. | |
54 void OnWidgetDestroying(views::Widget* widget) override { delete this; } | |
55 | |
56 RootWindowController* root_window_controller_; | |
57 views::Widget* desktop_widget_; | |
58 | |
59 // Is this object observing the initial brightness/grayscale animation? | |
60 const bool is_initial_animation_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(ShowWallpaperAnimationObserver); | |
63 }; | |
64 | |
65 } // namespace | |
66 | |
67 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController( | |
68 views::Widget* widget) | |
69 : widget_(widget), | |
70 widget_parent_(WmLookup::Get()->GetWindowForWidget(widget)->GetParent()) { | |
71 DCHECK(widget_); | |
72 widget_->AddObserver(this); | |
73 widget_parent_->AddObserver(this); | |
74 } | |
75 | |
76 DesktopBackgroundWidgetController::~DesktopBackgroundWidgetController() { | |
77 if (widget_) { | |
78 views::Widget* widget = widget_; | |
79 RemoveObservers(); | |
80 widget->CloseNow(); | |
81 } | |
82 } | |
83 | |
84 void DesktopBackgroundWidgetController::OnWidgetDestroying( | |
85 views::Widget* widget) { | |
86 RemoveObservers(); | |
87 } | |
88 | |
89 void DesktopBackgroundWidgetController::SetBounds(const gfx::Rect& bounds) { | |
90 if (widget_) | |
91 widget_->SetBounds(bounds); | |
92 } | |
93 | |
94 bool DesktopBackgroundWidgetController::Reparent(aura::Window* root_window, | |
95 int src_container, | |
96 int dest_container) { | |
97 if (widget_) { | |
98 widget_parent_->RemoveObserver(this); | |
99 views::Widget::ReparentNativeView( | |
100 widget_->GetNativeView(), root_window->GetChildById(dest_container)); | |
101 widget_parent_ = WmLookup::Get()->GetWindowForWidget(widget_)->GetParent(); | |
102 widget_parent_->AddObserver(this); | |
103 return true; | |
104 } | |
105 // Nothing to reparent. | |
106 return false; | |
107 } | |
108 | |
109 void DesktopBackgroundWidgetController::RemoveObservers() { | |
110 widget_parent_->RemoveObserver(this); | |
111 widget_->RemoveObserver(this); | |
112 widget_ = nullptr; | |
113 } | |
114 | |
115 void DesktopBackgroundWidgetController::OnWindowBoundsChanged( | |
116 WmWindow* window, | |
117 const gfx::Rect& old_bounds, | |
118 const gfx::Rect& new_bounds) { | |
119 SetBounds(new_bounds); | |
120 } | |
121 | |
122 void DesktopBackgroundWidgetController::StartAnimating( | |
123 RootWindowController* root_window_controller) { | |
124 if (widget_) { | |
125 ui::ScopedLayerAnimationSettings settings( | |
126 widget_->GetNativeView()->layer()->GetAnimator()); | |
127 settings.AddObserver(new ShowWallpaperAnimationObserver( | |
128 root_window_controller, widget_, | |
129 WmShell::Get()->wallpaper_delegate()->ShouldShowInitialAnimation())); | |
130 // When |widget_| shows, AnimateShowWindowCommon() is called to do the | |
131 // animation. Sets transition duration to 0 to avoid animating to the | |
132 // show animation's initial values. | |
133 settings.SetTransitionDuration(base::TimeDelta()); | |
134 widget_->Show(); | |
135 } | |
136 } | |
137 | |
138 AnimatingDesktopController::AnimatingDesktopController( | |
139 DesktopBackgroundWidgetController* component) { | |
140 controller_.reset(component); | |
141 } | |
142 | |
143 AnimatingDesktopController::~AnimatingDesktopController() {} | |
144 | |
145 void AnimatingDesktopController::StopAnimating() { | |
146 if (controller_) { | |
147 ui::Layer* layer = controller_->widget()->GetNativeView()->layer(); | |
148 layer->GetAnimator()->StopAnimating(); | |
149 } | |
150 } | |
151 | |
152 DesktopBackgroundWidgetController* AnimatingDesktopController::GetController( | |
153 bool pass_ownership) { | |
154 if (pass_ownership) | |
155 return controller_.release(); | |
156 return controller_.get(); | |
157 } | |
158 | |
159 } // namespace ash | |
OLD | NEW |