Home | History | Annotate | Download | only in corewm
      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 "ui/views/corewm/visibility_controller.h"
      6 
      7 #include "ui/aura/window.h"
      8 #include "ui/aura/window_property.h"
      9 #include "ui/compositor/layer.h"
     10 #include "ui/views/corewm/window_animations.h"
     11 
     12 namespace views {
     13 namespace corewm {
     14 
     15 namespace {
     16 
     17 // Property set on all windows whose child windows' visibility changes are
     18 // animated.
     19 DEFINE_WINDOW_PROPERTY_KEY(
     20     bool, kChildWindowVisibilityChangesAnimatedKey, false);
     21 
     22 bool ShouldAnimateWindow(aura::Window* window) {
     23   return window->parent() && window->parent()->GetProperty(
     24       kChildWindowVisibilityChangesAnimatedKey);
     25 }
     26 
     27 }  // namespace
     28 
     29 VisibilityController::VisibilityController() {
     30 }
     31 
     32 VisibilityController::~VisibilityController() {
     33 }
     34 
     35 bool VisibilityController::CallAnimateOnChildWindowVisibilityChanged(
     36     aura::Window* window,
     37     bool visible) {
     38   return AnimateOnChildWindowVisibilityChanged(window, visible);
     39 }
     40 
     41 void VisibilityController::UpdateLayerVisibility(aura::Window* window,
     42                                                  bool visible) {
     43   bool animated = window->type() != aura::client::WINDOW_TYPE_CONTROL &&
     44                   window->type() != aura::client::WINDOW_TYPE_UNKNOWN &&
     45                   ShouldAnimateWindow(window);
     46   animated = animated &&
     47       CallAnimateOnChildWindowVisibilityChanged(window, visible);
     48 
     49   if (!visible) {
     50     // For window hiding animation, we want to check if the window is already
     51     // animating, and not do SetVisible(false) if it is.
     52     // TODO(vollick): remove this.
     53     animated = animated || (window->layer()->GetAnimator()->
     54         IsAnimatingProperty(ui::LayerAnimationElement::OPACITY) &&
     55         window->layer()->GetTargetOpacity() == 0.0f);
     56   }
     57 
     58   // When a window is made visible, we always make its layer visible
     59   // immediately. When a window is hidden, the layer must be left visible and
     60   // only made not visible once the animation is complete.
     61   if (!animated || visible)
     62     window->layer()->SetVisible(visible);
     63 }
     64 
     65 SuspendChildWindowVisibilityAnimations::SuspendChildWindowVisibilityAnimations(
     66     aura::Window* window)
     67     : window_(window),
     68       original_enabled_(window->GetProperty(
     69           kChildWindowVisibilityChangesAnimatedKey)) {
     70   window_->ClearProperty(kChildWindowVisibilityChangesAnimatedKey);
     71 }
     72 
     73 SuspendChildWindowVisibilityAnimations::
     74     ~SuspendChildWindowVisibilityAnimations() {
     75   if (original_enabled_)
     76     window_->SetProperty(kChildWindowVisibilityChangesAnimatedKey, true);
     77   else
     78     window_->ClearProperty(kChildWindowVisibilityChangesAnimatedKey);
     79 }
     80 
     81 void SetChildWindowVisibilityChangesAnimated(aura::Window* window) {
     82   window->SetProperty(kChildWindowVisibilityChangesAnimatedKey, true);
     83 }
     84 
     85 }  // namespace corewm
     86 }  // namespace views
     87 
     88