OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/ui/touch/animation/screen_rotation_setter.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "chrome/browser/ui/touch/animation/screen_rotation.h" |
| 9 #include "ui/gfx/compositor/layer.h" |
| 10 #include "ui/gfx/interpolated_transform.h" |
| 11 #include "views/layer_property_setter.h" |
| 12 #include "views/view.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 static int SymmetricRound(float x) { |
| 17 return static_cast<int>( |
| 18 x > 0 |
| 19 ? std::floor(x + 0.5f) |
| 20 : std::ceil(x - 0.5f)); |
| 21 } |
| 22 |
| 23 // A screen rotation setter is a LayerPropertySetter that initiates screen |
| 24 // rotations in response to calls to |SetTransform|. Calls to |SetBounds| are |
| 25 // applied to the layer immediately. |
| 26 class ScreenRotationSetter : public views::LayerPropertySetter, |
| 27 public ScreenRotationListener { |
| 28 public: |
| 29 explicit ScreenRotationSetter(views::View* view); |
| 30 |
| 31 // implementation of LayerPropertySetter |
| 32 virtual void Installed(ui::Layer* layer) OVERRIDE; |
| 33 virtual void Uninstalled(ui::Layer* layer) OVERRIDE; |
| 34 virtual void SetTransform(ui::Layer* layer, |
| 35 const ui::Transform& transform) OVERRIDE; |
| 36 virtual void SetBounds(ui::Layer* layer, const gfx::Rect& bounds) OVERRIDE; |
| 37 |
| 38 // implementation of ScreenRotationListener |
| 39 virtual void OnScreenRotationCompleted(const ui::Transform& final_transform, |
| 40 const gfx::Rect& final_rect) OVERRIDE; |
| 41 |
| 42 private: |
| 43 // This is the currently animating rotation. We hang onto it so that if a |
| 44 // call to |SetTransform| is made during the rotation, we can update the |
| 45 // target orientation of this rotation. |
| 46 scoped_ptr<ScreenRotation> rotation_; |
| 47 |
| 48 // If a call to set bounds happens during a rotation its effect is delayed |
| 49 // until the rotation completes. If this happens several times, only the last |
| 50 // call to SetBounds will have any effect. |
| 51 scoped_ptr<gfx::Rect> pending_bounds_; |
| 52 |
| 53 // The screen rotation setter is associated with a view so that the view's |
| 54 // bounds may be set when the animation completes. |
| 55 views::View* view_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(ScreenRotationSetter); |
| 58 }; |
| 59 |
| 60 |
| 61 ScreenRotationSetter::ScreenRotationSetter(views::View* view) : view_(view) { |
| 62 } |
| 63 |
| 64 void ScreenRotationSetter::Installed(ui::Layer* layer) OVERRIDE { |
| 65 } |
| 66 |
| 67 void ScreenRotationSetter::Uninstalled(ui::Layer* layer) OVERRIDE { |
| 68 if (rotation_.get()) |
| 69 rotation_->Stop(); |
| 70 } |
| 71 |
| 72 void ScreenRotationSetter::SetTransform(ui::Layer* layer, |
| 73 const ui::Transform& transform) { |
| 74 float degrees; |
| 75 if (!ui::InterpolatedTransform::FactorTRS(transform, NULL, °rees, NULL)) |
| 76 return; |
| 77 |
| 78 if (rotation_.get()) { |
| 79 rotation_->SetTarget(SymmetricRound(degrees)); |
| 80 } else { |
| 81 float old_degrees; |
| 82 if (ui::InterpolatedTransform::FactorTRS(layer->transform(), |
| 83 NULL, |
| 84 &old_degrees, |
| 85 NULL)) { |
| 86 rotation_.reset(new ScreenRotation(view_, |
| 87 this, |
| 88 SymmetricRound(old_degrees), |
| 89 SymmetricRound(degrees))); |
| 90 } |
| 91 } |
| 92 } |
| 93 |
| 94 void ScreenRotationSetter::SetBounds(ui::Layer* layer, |
| 95 const gfx::Rect& bounds) { |
| 96 // cache bounds changes during an animation. |
| 97 if (rotation_.get()) |
| 98 pending_bounds_.reset(new gfx::Rect(bounds)); |
| 99 else |
| 100 layer->SetBounds(bounds); |
| 101 } |
| 102 |
| 103 void ScreenRotationSetter::OnScreenRotationCompleted( |
| 104 const ui::Transform& final_transform, |
| 105 const gfx::Rect& final_bounds) { |
| 106 // destroy the animation. |
| 107 rotation_.reset(); |
| 108 if (pending_bounds_.get() && view_->layer()) { |
| 109 // If there are any pending bounds changes, they will have already been |
| 110 // applied to the view, and are waiting to be applied to the layer. |
| 111 view_->layer()->SetBounds(*pending_bounds_); |
| 112 pending_bounds_.reset(); |
| 113 } else { |
| 114 // Otherwise we may have new bounds as the result of the completed screen |
| 115 // rotation. Apply these to the view. |
| 116 view_->SetBoundsRect(final_bounds); |
| 117 } |
| 118 } |
| 119 |
| 120 } // namespace |
| 121 |
| 122 views::LayerPropertySetter* ScreenRotationSetterFactory::Create( |
| 123 views::View* view) { |
| 124 return new ScreenRotationSetter(view); |
| 125 } |
OLD | NEW |