Home | History | Annotate | Download | only in desktop_background
      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/desktop_background/user_wallpaper_delegate.h"
      9 #include "ash/root_window_controller.h"
     10 #include "ash/shell.h"
     11 #include "ui/aura/root_window.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"
     16 
     17 namespace ash {
     18 namespace internal {
     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   virtual ~ShowWallpaperAnimationObserver() {
     35     StopObservingImplicitAnimations();
     36     if (desktop_widget_)
     37       desktop_widget_->RemoveObserver(this);
     38   }
     39 
     40  private:
     41   // Overridden from ui::ImplicitAnimationObserver:
     42   virtual void OnImplicitAnimationsScheduled() OVERRIDE {
     43     if (is_initial_animation_) {
     44       root_window_controller_->
     45           HandleInitialDesktopBackgroundAnimationStarted();
     46     }
     47   }
     48 
     49   virtual void OnImplicitAnimationsCompleted() OVERRIDE {
     50     root_window_controller_->OnWallpaperAnimationFinished(desktop_widget_);
     51     delete this;
     52   }
     53 
     54   // Overridden from views::WidgetObserver.
     55   virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE {
     56     delete this;
     57   }
     58 
     59   RootWindowController* root_window_controller_;
     60   views::Widget* desktop_widget_;
     61 
     62   // Is this object observing the initial brightness/grayscale animation?
     63   const bool is_initial_animation_;
     64 
     65   DISALLOW_COPY_AND_ASSIGN(ShowWallpaperAnimationObserver);
     66 };
     67 
     68 }  // namespace
     69 
     70 DesktopBackgroundWidgetController::DesktopBackgroundWidgetController(
     71     views::Widget* widget) : widget_(widget) {
     72   DCHECK(widget_);
     73   widget_->AddObserver(this);
     74 }
     75 
     76 DesktopBackgroundWidgetController::~DesktopBackgroundWidgetController() {
     77   if (widget_) {
     78     widget_->RemoveObserver(this);
     79     widget_->CloseNow();
     80     widget_ = NULL;
     81   }
     82 }
     83 
     84 void DesktopBackgroundWidgetController::OnWidgetDestroying(
     85     views::Widget* widget) {
     86   widget_->RemoveObserver(this);
     87   widget_ = NULL;
     88 }
     89 
     90 void DesktopBackgroundWidgetController::SetBounds(gfx::Rect bounds) {
     91   if (widget_)
     92     widget_->SetBounds(bounds);
     93 }
     94 
     95 bool DesktopBackgroundWidgetController::Reparent(aura::Window* root_window,
     96                                                  int src_container,
     97                                                  int dest_container) {
     98   if (widget_) {
     99     views::Widget::ReparentNativeView(widget_->GetNativeView(),
    100         root_window->GetChildById(dest_container));
    101     return true;
    102   }
    103   // Nothing to reparent.
    104   return false;
    105 }
    106 
    107 void DesktopBackgroundWidgetController::StartAnimating(
    108     RootWindowController* root_window_controller) {
    109   if (widget_) {
    110     ui::ScopedLayerAnimationSettings settings(
    111         widget_->GetNativeView()->layer()->GetAnimator());
    112     settings.AddObserver(new ShowWallpaperAnimationObserver(
    113         root_window_controller, widget_,
    114         Shell::GetInstance()->user_wallpaper_delegate()->
    115             ShouldShowInitialAnimation()));
    116     // When |widget_| shows, AnimateShowWindowCommon() is called to do the
    117     // animation. Sets transition duration to 0 to avoid animating to the
    118     // show animation's initial values.
    119     settings.SetTransitionDuration(base::TimeDelta());
    120     widget_->Show();
    121     widget_->GetNativeView()->SetName("DesktopBackgroundView");
    122   }
    123 }
    124 
    125 AnimatingDesktopController::AnimatingDesktopController(
    126     DesktopBackgroundWidgetController* component) {
    127   controller_.reset(component);
    128 }
    129 
    130 AnimatingDesktopController::~AnimatingDesktopController() {
    131 }
    132 
    133 void AnimatingDesktopController::StopAnimating() {
    134   if (controller_) {
    135     ui::Layer* layer = controller_->widget()->GetNativeView()->layer();
    136     layer->GetAnimator()->StopAnimating();
    137   }
    138 }
    139 
    140 DesktopBackgroundWidgetController* AnimatingDesktopController::GetController(
    141     bool pass_ownership) {
    142   if (pass_ownership)
    143     return controller_.release();
    144   return controller_.get();
    145 }
    146 
    147 }  // namespace internal
    148 }  // namespace ash
    149