Windows should animate when they are about to get docked at screen edges.
[chromium-blink-merge.git] / ash / wm / workspace_controller.cc
blob7fbbda2f7afe009bf5b57ad4f182e65a9f9492ca
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.
5 #include "ash/wm/workspace_controller.h"
7 #include "ash/shelf/shelf_layout_manager.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/base_layout_manager.h"
11 #include "ash/wm/property_util.h"
12 #include "ash/wm/window_animations.h"
13 #include "ash/wm/window_properties.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/workspace/workspace_event_handler.h"
16 #include "ash/wm/workspace/workspace_layout_manager.h"
17 #include "ui/aura/client/activation_client.h"
18 #include "ui/aura/client/aura_constants.h"
19 #include "ui/aura/root_window.h"
20 #include "ui/aura/window.h"
21 #include "ui/compositor/layer.h"
22 #include "ui/compositor/scoped_layer_animation_settings.h"
23 #include "ui/views/corewm/visibility_controller.h"
24 #include "ui/views/corewm/window_animations.h"
26 namespace ash {
27 namespace internal {
28 namespace {
30 // Amount of time to pause before animating anything. Only used during initial
31 // animation (when logging in).
32 const int kInitialPauseTimeMS = 750;
34 } // namespace
36 WorkspaceController::WorkspaceController(aura::Window* viewport)
37 : viewport_(viewport),
38 shelf_(NULL),
39 event_handler_(new WorkspaceEventHandler(viewport_)) {
40 SetWindowVisibilityAnimationTransition(
41 viewport_, views::corewm::ANIMATE_NONE);
43 // The layout-manager cannot be created in the initializer list since it
44 // depends on the window to have been initialized.
45 layout_manager_ = new WorkspaceLayoutManager(viewport_);
46 viewport_->SetLayoutManager(layout_manager_);
48 viewport_->Show();
51 WorkspaceController::~WorkspaceController() {
52 viewport_->SetLayoutManager(NULL);
53 viewport_->SetEventFilter(NULL);
54 viewport_->RemovePreTargetHandler(event_handler_.get());
55 viewport_->RemovePostTargetHandler(event_handler_.get());
58 WorkspaceWindowState WorkspaceController::GetWindowState() const {
59 if (!shelf_)
60 return WORKSPACE_WINDOW_STATE_DEFAULT;
62 const gfx::Rect shelf_bounds(shelf_->GetIdealBounds());
63 const aura::Window::Windows& windows(viewport_->children());
64 bool window_overlaps_launcher = false;
65 bool has_maximized_window = false;
66 for (aura::Window::Windows::const_iterator i = windows.begin();
67 i != windows.end(); ++i) {
68 if (GetIgnoredByShelf(*i))
69 continue;
70 ui::Layer* layer = (*i)->layer();
71 if (!layer->GetTargetVisibility() || layer->GetTargetOpacity() == 0.0f)
72 continue;
73 if (wm::IsWindowMaximized(*i)) {
74 // An untracked window may still be fullscreen so we keep iterating when
75 // we hit a maximized window.
76 has_maximized_window = true;
77 } else if (wm::IsWindowFullscreen(*i)) {
78 return WORKSPACE_WINDOW_STATE_FULL_SCREEN;
80 if (!window_overlaps_launcher && (*i)->bounds().Intersects(shelf_bounds))
81 window_overlaps_launcher = true;
83 if (has_maximized_window)
84 return WORKSPACE_WINDOW_STATE_MAXIMIZED;
86 return window_overlaps_launcher ?
87 WORKSPACE_WINDOW_STATE_WINDOW_OVERLAPS_SHELF :
88 WORKSPACE_WINDOW_STATE_DEFAULT;
91 void WorkspaceController::SetShelf(ShelfLayoutManager* shelf) {
92 shelf_ = shelf;
93 layout_manager_->SetShelf(shelf);
96 void WorkspaceController::DoInitialAnimation() {
97 viewport_->Show();
99 viewport_->layer()->SetOpacity(0.0f);
100 SetTransformForScaleAnimation(
101 viewport_->layer(), LAYER_SCALE_ANIMATION_ABOVE);
103 // In order for pause to work we need to stop animations.
104 viewport_->layer()->GetAnimator()->StopAnimating();
107 ui::ScopedLayerAnimationSettings settings(
108 viewport_->layer()->GetAnimator());
110 settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION);
111 viewport_->layer()->GetAnimator()->SchedulePauseForProperties(
112 base::TimeDelta::FromMilliseconds(kInitialPauseTimeMS),
113 ui::LayerAnimationElement::TRANSFORM,
114 ui::LayerAnimationElement::OPACITY,
115 ui::LayerAnimationElement::BRIGHTNESS,
116 ui::LayerAnimationElement::VISIBILITY,
117 -1);
119 settings.SetTweenType(ui::Tween::EASE_OUT);
120 settings.SetTransitionDuration(
121 base::TimeDelta::FromMilliseconds(kCrossFadeDurationMS));
122 viewport_->layer()->SetTransform(gfx::Transform());
123 viewport_->layer()->SetOpacity(1.0f);
127 } // namespace internal
128 } // namespace ash