Move login windows to/from unified desktop. This is necessary because in Unified...
[chromium-blink-merge.git] / ash / wm / lock_window_state.cc
blob06fb8b3f143ecdf5a198e0a1b8a879b82bef4ceb
1 // Copyright 2014 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/lock_window_state.h"
7 #include "ash/screen_util.h"
8 #include "ash/shell.h"
9 #include "ash/wm/lock_layout_manager.h"
10 #include "ash/wm/window_animations.h"
11 #include "ash/wm/window_state.h"
12 #include "ash/wm/window_state_delegate.h"
13 #include "ash/wm/window_state_util.h"
14 #include "ash/wm/window_util.h"
15 #include "ash/wm/wm_event.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/keyboard/keyboard_controller.h"
20 #include "ui/keyboard/keyboard_util.h"
21 #include "ui/wm/core/window_animations.h"
23 namespace ash {
25 LockWindowState::LockWindowState(aura::Window* window)
26 : current_state_type_(wm::GetWindowState(window)->GetStateType()) {
29 LockWindowState::~LockWindowState() {
32 void LockWindowState::OnWMEvent(wm::WindowState* window_state,
33 const wm::WMEvent* event) {
34 aura::Window* window = window_state->window();
35 gfx::Rect bounds = window->bounds();
37 switch (event->type()) {
38 case wm::WM_EVENT_TOGGLE_FULLSCREEN:
39 ToggleFullScreen(window_state, window_state->delegate());
40 break;
41 case wm::WM_EVENT_FULLSCREEN:
42 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_FULLSCREEN);
43 break;
44 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION:
45 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE:
46 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE:
47 case wm::WM_EVENT_TOGGLE_MAXIMIZE:
48 case wm::WM_EVENT_CYCLE_SNAP_DOCK_LEFT:
49 case wm::WM_EVENT_CYCLE_SNAP_DOCK_RIGHT:
50 case wm::WM_EVENT_CENTER:
51 case wm::WM_EVENT_SNAP_LEFT:
52 case wm::WM_EVENT_SNAP_RIGHT:
53 case wm::WM_EVENT_NORMAL:
54 case wm::WM_EVENT_MAXIMIZE:
55 case wm::WM_EVENT_DOCK:
56 UpdateWindow(window_state,
57 GetMaximizedOrCenteredWindowType(window_state));
58 return;
59 case wm::WM_EVENT_MINIMIZE:
60 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_MINIMIZED);
61 return;
62 case wm::WM_EVENT_SHOW_INACTIVE:
63 return;
64 case wm::WM_EVENT_SET_BOUNDS:
65 if (window_state->IsMaximized() || window_state->IsFullscreen()) {
66 UpdateBounds(window_state);
67 } else {
68 const ash::wm::SetBoundsEvent* bounds_event =
69 static_cast<const ash::wm::SetBoundsEvent*>(event);
70 window_state->SetBoundsConstrained(bounds_event->requested_bounds());
72 break;
73 case wm::WM_EVENT_ADDED_TO_WORKSPACE:
74 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
75 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
76 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
77 UpdateWindow(window_state,
78 GetMaximizedOrCenteredWindowType(window_state));
79 } else {
80 UpdateBounds(window_state);
82 break;
83 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED:
84 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED:
85 UpdateBounds(window_state);
86 break;
90 wm::WindowStateType LockWindowState::GetType() const {
91 return current_state_type_;
94 void LockWindowState::AttachState(wm::WindowState* window_state,
95 wm::WindowState::State* previous_state) {
96 current_state_type_ = previous_state->GetType();
98 // Initialize the state to a good preset.
99 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
100 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
101 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
102 UpdateWindow(window_state,
103 GetMaximizedOrCenteredWindowType(window_state));
107 void LockWindowState::DetachState(wm::WindowState* window_state) {
110 // static
111 wm::WindowState* LockWindowState::SetLockWindowState(aura::Window* window) {
112 scoped_ptr<wm::WindowState::State> lock_state(new LockWindowState(window));
113 scoped_ptr<wm::WindowState::State> old_state(
114 wm::GetWindowState(window)->SetStateObject(lock_state.Pass()));
115 return wm::GetWindowState(window);
118 void LockWindowState::UpdateWindow(wm::WindowState* window_state,
119 wm::WindowStateType target_state) {
120 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED ||
121 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED ||
122 (target_state == wm::WINDOW_STATE_TYPE_NORMAL &&
123 !window_state->CanMaximize()) ||
124 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN);
126 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) {
127 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED)
128 return;
130 current_state_type_ = target_state;
131 ::wm::SetWindowVisibilityAnimationType(
132 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
133 window_state->window()->Hide();
134 if (window_state->IsActive())
135 window_state->Deactivate();
136 return;
139 if (current_state_type_ == target_state) {
140 // If the state type did not change, update it accordingly.
141 UpdateBounds(window_state);
142 return;
145 const wm::WindowStateType old_state_type = current_state_type_;
146 current_state_type_ = target_state;
147 window_state->UpdateWindowShowStateFromStateType();
148 window_state->NotifyPreStateTypeChange(old_state_type);
149 UpdateBounds(window_state);
150 window_state->NotifyPostStateTypeChange(old_state_type);
152 if ((window_state->window()->TargetVisibility() ||
153 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) &&
154 !window_state->window()->layer()->visible()) {
155 // The layer may be hidden if the window was previously minimized. Make
156 // sure it's visible.
157 window_state->window()->Show();
161 wm::WindowStateType LockWindowState::GetMaximizedOrCenteredWindowType(
162 wm::WindowState* window_state) {
163 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED :
164 wm::WINDOW_STATE_TYPE_NORMAL;
167 void LockWindowState::UpdateBounds(wm::WindowState* window_state) {
168 if (!window_state->IsMaximized() && !window_state->IsFullscreen())
169 return;
171 keyboard::KeyboardController* keyboard_controller =
172 keyboard::KeyboardController::GetInstance();
173 gfx::Rect keyboard_bounds;
175 if (keyboard_controller &&
176 !keyboard::IsKeyboardOverscrollEnabled() &&
177 keyboard_controller->keyboard_visible()) {
178 keyboard_bounds = keyboard_controller->current_keyboard_bounds();
181 gfx::Rect bounds =
182 ScreenUtil::GetDisplayBoundsInParent(window_state->window());
183 bounds.set_height(bounds.height() - keyboard_bounds.height());
185 VLOG(1) << "Updating window bounds to: " << bounds.ToString();
186 window_state->SetBoundsDirect(bounds);
189 } // namespace ash