Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace_controller.cc
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/wm/workspace_controller.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/window_animations.h"
12 #include "ash/wm/window_state.h"
13 #include "ash/wm/window_util.h"
14 #include "ash/wm/workspace/workspace_event_handler.h"
15 #include "ash/wm/workspace/workspace_layout_manager.h"
16 #include "ui/aura/client/activation_client.h"
17 #include "ui/aura/client/aura_constants.h"
18 #include "ui/aura/root_window.h"
19 #include "ui/aura/window.h"
20 #include "ui/compositor/layer.h"
21 #include "ui/compositor/scoped_layer_animation_settings.h"
22 #include "ui/views/corewm/visibility_controller.h"
23 #include "ui/views/corewm/window_animations.h"
24
25 namespace ash {
26 namespace internal {
27 namespace {
28
29 // Amount of time to pause before animating anything. Only used during initial
30 // animation (when logging in).
31 const int kInitialPauseTimeMS = 750;
32
33 // Returns true if there are visible docked windows in the same screen as the
34 // |shelf|.
35 bool IsDockedAreaVisible(const ShelfLayoutManager* shelf) {
36   return shelf->dock_bounds().width() > 0;
37 }
38
39 }  // namespace
40
41 WorkspaceController::WorkspaceController(aura::Window* viewport)
42     : viewport_(viewport),
43       shelf_(NULL),
44       event_handler_(new WorkspaceEventHandler),
45       layout_manager_(new WorkspaceLayoutManager(viewport)) {
46   SetWindowVisibilityAnimationTransition(
47       viewport_, views::corewm::ANIMATE_NONE);
48
49   viewport_->SetLayoutManager(layout_manager_);
50   viewport_->AddPreTargetHandler(event_handler_.get());
51   viewport_->AddPostTargetHandler(event_handler_.get());
52 }
53
54 WorkspaceController::~WorkspaceController() {
55   viewport_->SetLayoutManager(NULL);
56   viewport_->RemovePreTargetHandler(event_handler_.get());
57   viewport_->RemovePostTargetHandler(event_handler_.get());
58 }
59
60 WorkspaceWindowState WorkspaceController::GetWindowState() const {
61   if (!shelf_)
62     return WORKSPACE_WINDOW_STATE_DEFAULT;
63   const aura::Window* topmost_fullscreen_window = GetRootWindowController(
64       viewport_->GetRootWindow())->GetWindowForFullscreenMode();
65   if (topmost_fullscreen_window &&
66       !wm::GetWindowState(topmost_fullscreen_window)->ignored_by_shelf()) {
67     return WORKSPACE_WINDOW_STATE_FULL_SCREEN;
68   }
69
70   // These are the container ids of containers which may contain windows that
71   // may overlap the launcher shelf and affect its transparency.
72   const int kWindowContainerIds[] = {
73       internal::kShellWindowId_DefaultContainer,
74       internal::kShellWindowId_DockedContainer,
75   };
76   const gfx::Rect shelf_bounds(shelf_->GetIdealBounds());
77   bool window_overlaps_launcher = false;
78   for (size_t idx = 0; idx < arraysize(kWindowContainerIds); idx++) {
79     const aura::Window* container = Shell::GetContainer(
80         viewport_->GetRootWindow(), kWindowContainerIds[idx]);
81     const aura::Window::Windows& windows(container->children());
82     for (aura::Window::Windows::const_iterator i = windows.begin();
83          i != windows.end(); ++i) {
84       wm::WindowState* window_state = wm::GetWindowState(*i);
85       if (window_state->ignored_by_shelf())
86         continue;
87       ui::Layer* layer = (*i)->layer();
88       if (!layer->GetTargetVisibility())
89         continue;
90       if (window_state->IsMaximized())
91         return WORKSPACE_WINDOW_STATE_MAXIMIZED;
92       if (!window_overlaps_launcher &&
93           ((*i)->bounds().Intersects(shelf_bounds))) {
94         window_overlaps_launcher = true;
95       }
96     }
97   }
98
99   return (window_overlaps_launcher || IsDockedAreaVisible(shelf_)) ?
100       WORKSPACE_WINDOW_STATE_WINDOW_OVERLAPS_SHELF :
101       WORKSPACE_WINDOW_STATE_DEFAULT;
102 }
103
104 void WorkspaceController::SetShelf(ShelfLayoutManager* shelf) {
105   shelf_ = shelf;
106   layout_manager_->SetShelf(shelf);
107 }
108
109 void WorkspaceController::DoInitialAnimation() {
110   viewport_->Show();
111
112   viewport_->layer()->SetOpacity(0.0f);
113   SetTransformForScaleAnimation(
114       viewport_->layer(), LAYER_SCALE_ANIMATION_ABOVE);
115
116   // In order for pause to work we need to stop animations.
117   viewport_->layer()->GetAnimator()->StopAnimating();
118
119   {
120     ui::ScopedLayerAnimationSettings settings(
121         viewport_->layer()->GetAnimator());
122
123     settings.SetPreemptionStrategy(ui::LayerAnimator::ENQUEUE_NEW_ANIMATION);
124     viewport_->layer()->GetAnimator()->SchedulePauseForProperties(
125         base::TimeDelta::FromMilliseconds(kInitialPauseTimeMS),
126         ui::LayerAnimationElement::TRANSFORM |
127             ui::LayerAnimationElement::OPACITY |
128             ui::LayerAnimationElement::BRIGHTNESS |
129             ui::LayerAnimationElement::VISIBILITY);
130     settings.SetTweenType(gfx::Tween::EASE_OUT);
131     settings.SetTransitionDuration(
132         base::TimeDelta::FromMilliseconds(kCrossFadeDurationMS));
133     viewport_->layer()->SetTransform(gfx::Transform());
134     viewport_->layer()->SetOpacity(1.0f);
135   }
136 }
137
138 }  // namespace internal
139 }  // namespace ash