Resize BrowserFrameAsh and not RenderWidgetHostViewAura when keyboard bounds change.
[chromium-blink-merge.git] / ash / wm / workspace / workspace_layout_manager.cc
blobc7e6c4c1e70a5a1a7605f06837d4aa000bb073c5
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/workspace_layout_manager.h"
7 #include <algorithm>
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"
36 using aura::Window;
38 namespace ash {
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));
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);
64 void WorkspaceLayoutManager::SetShelf(ShelfLayoutManager* shelf) {
65 shelf_ = shelf;
68 void WorkspaceLayoutManager::SetMaximizeBackdropDelegate(
69 scoped_ptr<WorkspaceLayoutManagerDelegate> delegate) {
70 backdrop_delegate_.reset(delegate.release());
73 //////////////////////////////////////////////////////////////////////////////
74 // WorkspaceLayoutManager, aura::LayoutManager implementation:
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);
90 void WorkspaceLayoutManager::OnWillRemoveWindowFromLayout(Window* child) {
91 windows_.erase(child);
92 child->RemoveObserver(this);
93 wm::GetWindowState(child)->RemoveObserver(this);
95 if (child->TargetVisibility())
96 WindowPositioner::RearrangeVisibleWindowOnHideOrRemove(child);
99 void WorkspaceLayoutManager::OnWindowRemovedFromLayout(Window* child) {
100 UpdateShelfVisibility();
101 UpdateFullscreenState();
102 if (backdrop_delegate_)
103 backdrop_delegate_->OnWindowRemovedFromLayout(child);
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();
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);
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();
132 //////////////////////////////////////////////////////////////////////////////
133 // WorkspaceLayoutManager, keyboard::KeyboardControllerObserver implementation:
135 void WorkspaceLayoutManager::OnKeyboardBoundsChanging(
136 const gfx::Rect& new_bounds) {
137 ui::InputMethod* input_method =
138 root_window_->GetProperty(aura::client::kRootWindowInputMethodKey);
139 ui::TextInputClient* text_input_client = input_method->GetTextInputClient();
140 if (!text_input_client)
141 return;
142 aura::Window *window =
143 text_input_client->GetAttachedWindow()->GetToplevelWindow();
144 if (!window || !window_->Contains(window))
145 return;
146 wm::WindowState* window_state = wm::GetWindowState(window);
147 if (!new_bounds.IsEmpty()) {
148 // Store existing bounds to be restored before resizing for keyboard if it
149 // is not already stored.
150 if (!window_state->HasRestoreBounds())
151 window_state->SaveCurrentBoundsForRestore();
153 gfx::Rect window_bounds = ScreenUtil::ConvertRectToScreen(
154 window_,
155 window->GetTargetBounds());
156 gfx::Rect intersect = gfx::IntersectRects(window_bounds, new_bounds);
157 int shift = std::min(intersect.height(),
158 window->bounds().y() - work_area_in_parent_.y());
159 if (shift > 0) {
160 gfx::Point origin(window->bounds().x(), window->bounds().y() - shift);
161 SetChildBounds(window, gfx::Rect(origin, window->bounds().size()));
163 } else if (window_state->HasRestoreBounds()) {
164 // Keyboard hidden, restore original bounds if they exist.
165 window_state->SetAndClearRestoreBounds();
169 //////////////////////////////////////////////////////////////////////////////
170 // WorkspaceLayoutManager, ash::ShellObserver implementation:
172 void WorkspaceLayoutManager::OnDisplayWorkAreaInsetsChanged() {
173 const gfx::Rect work_area(ScreenUtil::ConvertRectFromScreen(
174 window_,
175 Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area()));
176 if (work_area != work_area_in_parent_) {
177 const wm::WMEvent event(wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
178 AdjustAllWindowsBoundsForWorkAreaChange(&event);
180 if (backdrop_delegate_)
181 backdrop_delegate_->OnDisplayWorkAreaInsetsChanged();
184 //////////////////////////////////////////////////////////////////////////////
185 // WorkspaceLayoutManager, aura::WindowObserver implementation:
187 void WorkspaceLayoutManager::OnWindowHierarchyChanged(
188 const WindowObserver::HierarchyChangeParams& params) {
189 if (!wm::GetWindowState(params.target)->IsActive())
190 return;
191 // If the window is already tracked by the workspace this update would be
192 // redundant as the fullscreen and shelf state would have been handled in
193 // OnWindowAddedToLayout.
194 if (windows_.find(params.target) != windows_.end())
195 return;
197 // If the active window has moved to this root window then update the
198 // fullscreen state.
199 // TODO(flackr): Track the active window leaving this root window and update
200 // the fullscreen state accordingly.
201 if (params.new_parent && params.new_parent->GetRootWindow() == root_window_) {
202 UpdateFullscreenState();
203 UpdateShelfVisibility();
207 void WorkspaceLayoutManager::OnWindowPropertyChanged(Window* window,
208 const void* key,
209 intptr_t old) {
210 if (key == aura::client::kAlwaysOnTopKey &&
211 window->GetProperty(aura::client::kAlwaysOnTopKey)) {
212 GetRootWindowController(window->GetRootWindow())->
213 always_on_top_controller()->GetContainer(window)->AddChild(window);
217 void WorkspaceLayoutManager::OnWindowStackingChanged(aura::Window* window) {
218 UpdateShelfVisibility();
219 UpdateFullscreenState();
220 if (backdrop_delegate_)
221 backdrop_delegate_->OnWindowStackingChanged(window);
224 void WorkspaceLayoutManager::OnWindowDestroying(aura::Window* window) {
225 if (root_window_ == window) {
226 root_window_->RemoveObserver(this);
227 root_window_ = NULL;
231 void WorkspaceLayoutManager::OnWindowBoundsChanged(aura::Window* window,
232 const gfx::Rect& old_bounds,
233 const gfx::Rect& new_bounds) {
234 if (root_window_ == window) {
235 const wm::WMEvent wm_event(wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED);
236 AdjustAllWindowsBoundsForWorkAreaChange(&wm_event);
240 //////////////////////////////////////////////////////////////////////////////
241 // WorkspaceLayoutManager,
242 // aura::client::ActivationChangeObserver implementation:
244 void WorkspaceLayoutManager::OnWindowActivated(aura::Window* gained_active,
245 aura::Window* lost_active) {
246 wm::WindowState* window_state = wm::GetWindowState(gained_active);
247 if (window_state && window_state->IsMinimized() &&
248 !gained_active->IsVisible()) {
249 window_state->Unminimize();
250 DCHECK(!window_state->IsMinimized());
252 UpdateFullscreenState();
253 UpdateShelfVisibility();
256 //////////////////////////////////////////////////////////////////////////////
257 // WorkspaceLayoutManager, wm::WindowStateObserver implementation:
259 void WorkspaceLayoutManager::OnPostWindowStateTypeChange(
260 wm::WindowState* window_state,
261 wm::WindowStateType old_type) {
263 // Notify observers that fullscreen state may be changing.
264 if (window_state->IsFullscreen() ||
265 old_type == wm::WINDOW_STATE_TYPE_FULLSCREEN) {
266 UpdateFullscreenState();
269 UpdateShelfVisibility();
270 if (backdrop_delegate_)
271 backdrop_delegate_->OnPostWindowStateTypeChange(window_state, old_type);
274 //////////////////////////////////////////////////////////////////////////////
275 // WorkspaceLayoutManager, private:
277 void WorkspaceLayoutManager::AdjustAllWindowsBoundsForWorkAreaChange(
278 const wm::WMEvent* event) {
279 DCHECK(event->type() == wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED ||
280 event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED);
282 work_area_in_parent_ = ScreenUtil::ConvertRectFromScreen(
283 window_,
284 Shell::GetScreen()->GetDisplayNearestWindow(window_).work_area());
286 // Don't do any adjustments of the insets while we are in screen locked mode.
287 // This would happen if the launcher was auto hidden before the login screen
288 // was shown and then gets shown when the login screen gets presented.
289 if (event->type() == wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED &&
290 Shell::GetInstance()->session_state_delegate()->IsScreenLocked())
291 return;
293 // If a user plugs an external display into a laptop running Aura the
294 // display size will change. Maximized windows need to resize to match.
295 // We also do this when developers running Aura on a desktop manually resize
296 // the host window.
297 // We also need to do this when the work area insets changes.
298 for (WindowSet::const_iterator it = windows_.begin();
299 it != windows_.end();
300 ++it) {
301 wm::GetWindowState(*it)->OnWMEvent(event);
305 void WorkspaceLayoutManager::UpdateShelfVisibility() {
306 if (shelf_)
307 shelf_->UpdateVisibilityState();
310 void WorkspaceLayoutManager::UpdateFullscreenState() {
311 // TODO(flackr): The fullscreen state is currently tracked per workspace
312 // but the shell notification implies a per root window state. Currently
313 // only windows in the default workspace container will go fullscreen but
314 // this should really be tracked by the RootWindowController since
315 // technically any container could get a fullscreen window.
316 if (!shelf_)
317 return;
318 bool is_fullscreen = GetRootWindowController(
319 window_->GetRootWindow())->GetWindowForFullscreenMode() != NULL;
320 if (is_fullscreen != is_fullscreen_) {
321 ash::Shell::GetInstance()->NotifyFullscreenStateChange(
322 is_fullscreen, window_->GetRootWindow());
323 is_fullscreen_ = is_fullscreen;
327 } // namespace ash