17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved. 27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file. 47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/browser/web_contents/aura/window_slider.h" 67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 7eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <algorithm> 8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/bind.h" 107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/callback.h" 117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/browser/web_contents/aura/shadow_layer_delegate.h" 127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/public/browser/overscroll_configuration.h" 137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/aura/window.h" 147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/compositor/layer_animation_observer.h" 157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "ui/compositor/scoped_layer_animation_settings.h" 16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h" 177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace content { 197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace { 217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void DeleteLayerAndShadow(ui::Layer* layer, 237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ShadowLayerDelegate* shadow) { 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delete shadow; 257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delete layer; 267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// An animation observer that runs a callback at the end of the animation, and 297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// destroys itself. 307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class CallbackAnimationObserver : public ui::ImplicitAnimationObserver { 317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public: 327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CallbackAnimationObserver(const base::Closure& closure) 337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) : closure_(closure) { 347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) virtual ~CallbackAnimationObserver() {} 377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private: 397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Overridden from ui::ImplicitAnimationObserver: 407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) virtual void OnImplicitAnimationsCompleted() OVERRIDE { 417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (!closure_.is_null()) 427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) closure_.Run(); 437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const base::Closure closure_; 477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(CallbackAnimationObserver); 497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}; 507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} // namespace 527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WindowSlider::WindowSlider(Delegate* delegate, 547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) aura::Window* event_window, 557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) aura::Window* owner) 567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) : delegate_(delegate), 577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_(event_window), 587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_(owner), 597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delta_x_(0.f), 607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) weak_factory_(this), 61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) active_start_threshold_(0.f), 62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) start_threshold_touchscreen_(content::GetOverscrollConfig( 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHSCREEN)), 64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) start_threshold_touchpad_(content::GetOverscrollConfig( 65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_START_TOUCHPAD)), 667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) complete_threshold_(content::GetOverscrollConfig( 677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) content::OVERSCROLL_CONFIG_HORIZ_THRESHOLD_COMPLETE)) { 687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_->AddPreTargetHandler(this); 697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_->AddObserver(this); 717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->AddObserver(this); 727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)WindowSlider::~WindowSlider() { 757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (event_window_) { 767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_->RemovePreTargetHandler(this); 777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_->RemoveObserver(this); 787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (owner_) 807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->RemoveObserver(this); 81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch delegate_->OnWindowSliderDestroyed(); 827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::ChangeOwner(aura::Window* new_owner) { 857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (owner_) 867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->RemoveObserver(this); 877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_ = new_owner; 887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (owner_) { 897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->AddObserver(this); 907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForScroll(0.f, 0.f); 917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 94eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool WindowSlider::IsSlideInProgress() const { 95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) return fabs(delta_x_) >= active_start_threshold_ || slider_.get() || 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch weak_factory_.HasWeakPtrs(); 97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::SetupSliderLayer() { 1007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::Layer* parent = owner_->layer()->parent(); 1017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) parent->Add(slider_.get()); 1027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (delta_x_ < 0) 1037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) parent->StackAbove(slider_.get(), owner_->layer()); 1047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) else 1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) parent->StackBelow(slider_.get(), owner_->layer()); 1067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) slider_->SetBounds(owner_->layer()->bounds()); 1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) slider_->SetVisible(true); 1087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 1097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::UpdateForScroll(float x_offset, float y_offset) { 1117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) float old_delta = delta_x_; 1127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delta_x_ += x_offset; 113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (fabs(delta_x_) < active_start_threshold_ && !slider_.get()) 1147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return; 1157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if ((old_delta < 0 && delta_x_ > 0) || 1177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) (old_delta > 0 && delta_x_ < 0)) { 1187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) slider_.reset(); 1197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) shadow_.reset(); 1207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 1217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) float translate = 0.f; 1237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::Layer* translate_layer = NULL; 1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 125eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!slider_.get()) { 126eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch slider_.reset(delta_x_ < 0 ? delegate_->CreateFrontLayer() : 127eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch delegate_->CreateBackLayer()); 128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!slider_.get()) 129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return; 130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch SetupSliderLayer(); 131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch } 132eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) if (delta_x_ <= -active_start_threshold_) { 134eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch translate = owner_->bounds().width() + 135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) std::max(delta_x_ + active_start_threshold_, 136eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch static_cast<float>(-owner_->bounds().width())); 1377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) translate_layer = slider_.get(); 138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) } else if (delta_x_ >= active_start_threshold_) { 139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) translate = std::min(delta_x_ - active_start_threshold_, 140eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch static_cast<float>(owner_->bounds().width())); 1417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) translate_layer = owner_->layer(); 1427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } else { 143eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch return; 1447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 1457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (!shadow_.get()) 1477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) shadow_.reset(new ShadowLayerDelegate(translate_layer)); 1487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) gfx::Transform transform; 1507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) transform.Translate(translate, 0); 1517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) translate_layer->SetTransform(transform); 1527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 1537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::UpdateForFling(float x_velocity, float y_velocity) { 155eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch if (!slider_.get()) 1567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return; 1577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) int width = owner_->bounds().width(); 159424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) float ratio = (fabs(delta_x_) - active_start_threshold_) / width; 1607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (ratio < complete_threshold_) { 1617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ResetScroll(); 1627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return; 1637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 1647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::Layer* sliding = delta_x_ < 0 ? slider_.get() : owner_->layer(); 1667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::ScopedLayerAnimationSettings settings(sliding->GetAnimator()); 1677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.SetPreemptionStrategy( 1687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 169d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) settings.SetTweenType(gfx::Tween::EASE_OUT); 1707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.AddObserver(new CallbackAnimationObserver( 1717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::Bind(&WindowSlider::CompleteWindowSlideAfterAnimation, 1727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) weak_factory_.GetWeakPtr()))); 1737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) gfx::Transform transform; 1757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) transform.Translate(delta_x_ < 0 ? 0 : width, 0); 1767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) sliding->SetTransform(transform); 1777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 1787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::ResetScroll() { 1807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (!slider_.get()) 1817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return; 1827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Do not trigger any callbacks if this animation replaces any in-progress 1847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // animation. 1857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) weak_factory_.InvalidateWeakPtrs(); 1867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Reset the state of the sliding layer. 1887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (slider_.get()) { 1897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::Layer* layer = slider_.release(); 1907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::ScopedLayerAnimationSettings settings(layer->GetAnimator()); 1917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.SetPreemptionStrategy( 1927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 193d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) settings.SetTweenType(gfx::Tween::EASE_OUT); 1947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 1957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Delete the layer and the shadow at the end of the animation. 1967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.AddObserver(new CallbackAnimationObserver( 1977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::Bind(&DeleteLayerAndShadow, 1987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::Unretained(layer), 1997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::Unretained(shadow_.release())))); 2007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) gfx::Transform transform; 2027d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) transform.Translate(delta_x_ < 0 ? layer->bounds().width() : 0, 0); 2037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) layer->SetTransform(transform); 2047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Reset the state of the main layer. 2077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) { 2087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::ScopedLayerAnimationSettings settings(owner_->layer()->GetAnimator()); 2097d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.SetPreemptionStrategy( 2107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 211d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) settings.SetTweenType(gfx::Tween::EASE_OUT); 2127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) settings.AddObserver(new CallbackAnimationObserver( 2137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) base::Bind(&WindowSlider::AbortWindowSlideAfterAnimation, 2147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) weak_factory_.GetWeakPtr()))); 2157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->layer()->SetTransform(gfx::Transform()); 2167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_->layer()->SetLayerBrightness(0.f); 2177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delta_x_ = 0.f; 2207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::CancelScroll() { 2237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ResetScroll(); 2247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::CompleteWindowSlideAfterAnimation() { 227eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch weak_factory_.InvalidateWeakPtrs(); 228eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch shadow_.reset(); 229eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch slider_.reset(); 230eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch delta_x_ = 0.f; 231eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delegate_->OnWindowSlideComplete(); 2337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::AbortWindowSlideAfterAnimation() { 236eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch weak_factory_.InvalidateWeakPtrs(); 237eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 2387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) delegate_->OnWindowSlideAborted(); 2397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::OnKeyEvent(ui::KeyEvent* event) { 2427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CancelScroll(); 2437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::OnMouseEvent(ui::MouseEvent* event) { 2467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (!(event->flags() & ui::EF_IS_SYNTHESIZED)) 2477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CancelScroll(); 2487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::OnScrollEvent(ui::ScrollEvent* event) { 251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) active_start_threshold_ = start_threshold_touchpad_; 2527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (event->type() == ui::ET_SCROLL) 2537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForScroll(event->x_offset_ordinal(), event->y_offset_ordinal()); 2547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) else if (event->type() == ui::ET_SCROLL_FLING_START) 2557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForFling(event->x_offset_ordinal(), event->y_offset_ordinal()); 2567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) else 2577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CancelScroll(); 2587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event->SetHandled(); 2597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::OnGestureEvent(ui::GestureEvent* event) { 262424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) active_start_threshold_ = start_threshold_touchscreen_; 2637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const ui::GestureEventDetails& details = event->details(); 2647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) switch (event->type()) { 2657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_SCROLL_BEGIN: 2667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) ResetScroll(); 2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_SCROLL_UPDATE: 2707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForScroll(details.scroll_x(), details.scroll_y()); 2717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_SCROLL_END: 2747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForFling(0.f, 0.f); 2757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_SCROLL_FLING_START: 2787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) UpdateForFling(details.velocity_x(), details.velocity_y()); 2797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_PINCH_BEGIN: 2827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_PINCH_UPDATE: 2837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) case ui::ET_GESTURE_PINCH_END: 2847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) CancelScroll(); 2857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) default: 2887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) break; 2897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 2907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event->SetHandled(); 2927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 2937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 2947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void WindowSlider::OnWindowRemovingFromRootWindow(aura::Window* window) { 2957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (window == event_window_) { 2967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) window->RemoveObserver(this); 2977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) window->RemovePreTargetHandler(this); 2987d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) event_window_ = NULL; 2997d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } else if (window == owner_) { 3007d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) window->RemoveObserver(this); 3017d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) owner_ = NULL; 302eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch delete this; 3037d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } else { 3047d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) NOTREACHED(); 3057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 3067d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 3077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 3087d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} // namespace content 309