1b4b4afd67c04167922621d1d571e2ddb7ea14d2
[platform/framework/web/crosswalk.git] / src / ash / wm / workspace / workspace_layout_manager.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/workspace_layout_manager.h"
6
7 #include "ash/display/display_controller.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/screen_util.h"
10 #include "ash/session_state_delegate.h"
11 #include "ash/shelf/shelf_layout_manager.h"
12 #include "ash/shell.h"
13 #include "ash/wm/always_on_top_controller.h"
14 #include "ash/wm/window_animations.h"
15 #include "ash/wm/window_positioner.h"
16 #include "ash/wm/window_properties.h"
17 #include "ash/wm/window_state.h"
18 #include "ash/wm/window_util.h"
19 #include "ash/wm/wm_event.h"
20 #include "ash/wm/workspace/workspace_layout_manager_delegate.h"
21 #include "ui/aura/client/aura_constants.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_observer.h"
24 #include "ui/base/ui_base_types.h"
25 #include "ui/compositor/layer.h"
26 #include "ui/events/event.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/wm/core/window_util.h"
29 #include "ui/wm/public/activation_client.h"
30
31 using aura::Window;
32
33 namespace ash {
34
35 namespace internal {
36
37 WorkspaceLayoutManager::WorkspaceLayoutManager(aura::Window* window)
38     : shelf_(NULL),
39       window_(window),
40       root_window_(window->GetRootWindow()),
41       work_area_in_parent_(ScreenUtil::ConvertRectFromScreen(
42           window_,
43           Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area())),
44       is_fullscreen_(GetRootWindowController(
45           window->GetRootWindow())->GetWindowForFullscreenMode() != NULL) {
46   Shell::GetInstance()->activation_client()->AddObserver(this);
47   Shell::GetInstance()->AddShellObserver(this);
48   root_window_->AddObserver(this);
49 }
50
51 WorkspaceLayoutManager::~WorkspaceLayoutManager() {
52   if (root_window_)
53     root_window_->RemoveObserver(this);
54   for (WindowSet::const_iterator i = windows_.begin(); i != windows_.end(); ++i)
55     (*i)->RemoveObserver(this);
56   Shell::GetInstance()->RemoveShellObserver(this);
57   Shell::GetInstance()->activation_client()->RemoveObserver(this);
58 }
59
60 void WorkspaceLayoutManager::SetShelf(internal::ShelfLayoutManager* shelf) {
61   shelf_ = shelf;
62 }
63
64 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
65     scoped_ptr<WorkspaceLayoutManagerDelegate> delegate) {
66   backdrop_delegate_.reset(delegate.release());
67 }
68
69 //////////////////////////////////////////////////////////////////////////////
70 // WorkspaceLayoutManager, aura::LayoutManager implementation:
71
72 void WorkspaceLayoutManager::OnWindowAddedToLayout(Window* child) {
73   wm::WindowState* window_state = wm::GetWindowState(child);
74   wm::WMEvent event(wm::WM_EVENT_ADDED_TO_WORKSPACE);
75   window_state->OnWMEvent(&event);
76   windows_.insert(child);
77   child->AddObserver(this);
78   window_state->AddObserver(this);
79   UpdateShelfVisibility();
80   UpdateFullscreenState();
81   if (backdrop_delegate_)
82     backdrop_delegate_->OnWindowAddedToLayout(child);
83   WindowPositioner::RearrangeVisibleWindowOnShow(child);
84 }
85
86 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window* child) {
87   windows_.erase(child);
88   child->RemoveObserver(this);
89   wm::GetWindowState(child)->RemoveObserver(this);
90
91   if (child->TargetVisibility())
92     WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child);
93 }
94
95 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window* child) {
96   UpdateShelfVisibility();
97   UpdateFullscreenState();
98   if (backdrop_delegate_)
99     backdrop_delegate_->OnWindowRemovedFromLayout(child);
100 }
101
102 void WorkspaceLayoutManager::OnChildWindowVisibilityChanged(Window* child,
103                                                             bool visible) {
104   wm::WindowState* window_state = wm::GetWindowState(child);
105   // Attempting to show a minimized window. Unminimize it.
106   if (visible && window_state->IsMinimized())
107     window_state->Unminimize();
108
109   if (child->TargetVisibility())
110     WindowPositioner::RearrangeVisibleWindowOnShow(child);
111   else
112     WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child);
113   UpdateFullscreenState();
114   UpdateShelfVisibility();
115   if (backdrop_delegate_)
116     backdrop_delegate_->OnChildWindowVisibilityChanged(child, visible);
117 }
118
119 void WorkspaceLayoutManager::SetChildBounds(
120     Window* child,
121     const gfx::Rect& requested_bounds) {
122   wm::WindowState* window_state = wm::GetWindowState(child);
123   wm::SetBoundsEvent event(wm::WM_EVENT_SET_BOUNDS, requested_bounds);
124   window_state->OnWMEvent(&event);
125   UpdateShelfVisibility();
126 }
127
128 //////////////////////////////////////////////////////////////////////////////
129 // WorkspaceLayoutManager, ash::ShellObserver implementation:
130
131 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
132   const gfx::Rect work_area(ScreenUtil::ConvertRectFromScreen(
133       window_,
134       Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area()));
135   if (work_area != work_area_in_parent_) {
136     const wm::WMEvent event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
137     AdjustAllWindowsBoundsForWorkAreaChange(&event);
138   }
139 }
140
141 //////////////////////////////////////////////////////////////////////////////
142 // WorkspaceLayoutManager, aura::WindowObserver implementation:
143
144 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
145     const WindowObserver::HierarchyChangeParams& params) {
146   if (!wm::GetWindowState(params.target)->IsActive())
147     return;
148   // If the window is already tracked by the workspace this update would be
149   // redundant as the fullscreen and shelf state would have been handled in
150   // OnWindowAddedToLayout.
151   if (windows_.find(params.target) != windows_.end())
152     return;
153
154   // If the active window has moved to this root window then update the
155   // fullscreen state.
156   // TODO(flackr): Track the active window leaving this root window and update
157   // the fullscreen state accordingly.
158   if (params.new_parent && params.new_parent->GetRootWindow() == root_window_) {
159     UpdateFullscreenState();
160     UpdateShelfVisibility();
161   }
162 }
163
164 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window* window,
165                                                      const void* key,
166                                                      intptr_t old) {
167   if (key == aura::client::kAlwaysOnTopKey &&
168       window->GetProperty(aura::client::kAlwaysOnTopKey)) {
169     GetRootWindowController(window->GetRootWindow())->
170         always_on_top_controller()->GetContainer(window)->AddChild(window);
171   }
172 }
173
174 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window* window) {
175   UpdateShelfVisibility();
176   UpdateFullscreenState();
177   if (backdrop_delegate_)
178     backdrop_delegate_->OnWindowStackingChanged(window);
179 }
180
181 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window* window) {
182   if (root_window_ == window) {
183     root_window_->RemoveObserver(this);
184     root_window_ = NULL;
185   }
186 }
187
188 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window* window,
189                                               const gfx::Rect& old_bounds,
190                                               const gfx::Rect& new_bounds) {
191   if (root_window_ == window) {
192     const wm::WMEvent wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED);
193     AdjustAllWindowsBoundsForWorkAreaChange(&wm_event);
194   }
195 }
196
197 //////////////////////////////////////////////////////////////////////////////
198 // WorkspaceLayoutManager,
199 // aura::client::ActivationChangeObserver implementation:
200
201 void WorkspaceLayoutManager::OnWindowActivated(aura::Window* gained_active,
202                                                aura::Window* lost_active) {
203   wm::WindowState* window_state = wm::GetWindowState(gained_active);
204   if (window_state && window_state->IsMinimized() &&
205       !gained_active->IsVisible()) {
206     window_state->Unminimize();
207     DCHECK(!window_state->IsMinimized());
208   }
209   UpdateFullscreenState();
210   UpdateShelfVisibility();
211 }
212
213 //////////////////////////////////////////////////////////////////////////////
214 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
215
216 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
217     wm::WindowState* window_state,
218     wm::WindowStateType old_type) {
219
220   // Notify observers that fullscreen state may be changing.
221   if (window_state->IsFullscreen() ||
222       old_type == wm::WINDOW_STATE_TYPE_FULLSCREEN) {
223     UpdateFullscreenState();
224   }
225
226   UpdateShelfVisibility();
227   if (backdrop_delegate_)
228     backdrop_delegate_->OnPostWindowStateTypeChange(window_state, old_type);
229 }
230
231 //////////////////////////////////////////////////////////////////////////////
232 // WorkspaceLayoutManager, private:
233
234 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
235     const wm::WMEvent* event) {
236   DCHECK(event->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED ||
237          event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
238
239   work_area_in_parent_ = ScreenUtil::ConvertRectFromScreen(
240       window_,
241       Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area());
242
243   // Don't do any adjustments of the insets while we are in screen locked mode.
244   // This would happen if the launcher was auto hidden before the login screen
245   // was shown and then gets shown when the login screen gets presented.
246   if (event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED &&
247       Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
248     return;
249
250   // If a user plugs an external display into a laptop running Aura the
251   // display size will change.  Maximized windows need to resize to match.
252   // We also do this when developers running Aura on a desktop manually resize
253   // the host window.
254   // We also need to do this when the work area insets changes.
255   for (WindowSet::const_iterator it = windows_.begin();
256        it != windows_.end();
257        ++it) {
258     wm::GetWindowState(*it)->OnWMEvent(event);
259   }
260 }
261
262 void WorkspaceLayoutManager::UpdateShelfVisibility() {
263   if (shelf_)
264     shelf_->UpdateVisibilityState();
265 }
266
267 void WorkspaceLayoutManager::UpdateFullscreenState() {
268   // TODO(flackr): The fullscreen state is currently tracked per workspace
269   // but the shell notification implies a per root window state. Currently
270   // only windows in the default workspace container will go fullscreen but
271   // this should really be tracked by the RootWindowController since
272   // technically any container could get a fullscreen window.
273   if (!shelf_)
274     return;
275   bool is_fullscreen = GetRootWindowController(
276       window_->GetRootWindow())->GetWindowForFullscreenMode() != NULL;
277   if (is_fullscreen != is_fullscreen_) {
278     ash::Shell::GetInstance()->NotifyFullscreenStateChange(
279         is_fullscreen, window_->GetRootWindow());
280     is_fullscreen_ = is_fullscreen;
281   }
282 }
283
284 }  // namespace internal
285 }  // namespace ash