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