Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / ash / wm / lock_window_state.cc
blobd153fb258c8803fd8be6039fe468ceb483e77206
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/display/display_manager.h"
8 #include "ash/screen_util.h"
9 #include "ash/shell.h"
10 #include "ash/wm/lock_layout_manager.h"
11 #include "ash/wm/window_animations.h"
12 #include "ash/wm/window_state.h"
13 #include "ash/wm/window_state_delegate.h"
14 #include "ash/wm/window_state_util.h"
15 #include "ash/wm/window_util.h"
16 #include "ash/wm/wm_event.h"
17 #include "ui/aura/window.h"
18 #include "ui/aura/window_delegate.h"
19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/keyboard/keyboard_controller.h"
21 #include "ui/keyboard/keyboard_util.h"
22 #include "ui/wm/core/window_animations.h"
24 namespace ash {
26 LockWindowState::LockWindowState(aura::Window* window)
27 : current_state_type_(wm::GetWindowState(window)->GetStateType()) {
30 LockWindowState::~LockWindowState() {
33 void LockWindowState::OnWMEvent(wm::WindowState* window_state,
34 const wm::WMEvent* event) {
35 aura::Window* window = window_state->window();
36 gfx::Rect bounds = window->bounds();
38 switch (event->type()) {
39 case wm::WM_EVENT_TOGGLE_FULLSCREEN:
40 ToggleFullScreen(window_state, window_state->delegate());
41 break;
42 case wm::WM_EVENT_FULLSCREEN:
43 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_FULLSCREEN);
44 break;
45 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION:
46 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE:
47 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE:
48 case wm::WM_EVENT_TOGGLE_MAXIMIZE:
49 case wm::WM_EVENT_CYCLE_SNAP_DOCK_LEFT:
50 case wm::WM_EVENT_CYCLE_SNAP_DOCK_RIGHT:
51 case wm::WM_EVENT_CENTER:
52 case wm::WM_EVENT_SNAP_LEFT:
53 case wm::WM_EVENT_SNAP_RIGHT:
54 case wm::WM_EVENT_NORMAL:
55 case wm::WM_EVENT_MAXIMIZE:
56 case wm::WM_EVENT_DOCK:
57 UpdateWindow(window_state,
58 GetMaximizedOrCenteredWindowType(window_state));
59 return;
60 case wm::WM_EVENT_MINIMIZE:
61 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_MINIMIZED);
62 return;
63 case wm::WM_EVENT_SHOW_INACTIVE:
64 return;
65 case wm::WM_EVENT_SET_BOUNDS:
66 if (window_state->IsMaximized() || window_state->IsFullscreen()) {
67 UpdateBounds(window_state);
68 } else {
69 const ash::wm::SetBoundsEvent* bounds_event =
70 static_cast<const ash::wm::SetBoundsEvent*>(event);
71 window_state->SetBoundsConstrained(bounds_event->requested_bounds());
73 break;
74 case wm::WM_EVENT_ADDED_TO_WORKSPACE:
75 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
76 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
77 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
78 UpdateWindow(window_state,
79 GetMaximizedOrCenteredWindowType(window_state));
80 } else {
81 UpdateBounds(window_state);
83 break;
84 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED:
85 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED:
86 UpdateBounds(window_state);
87 break;
91 wm::WindowStateType LockWindowState::GetType() const {
92 return current_state_type_;
95 void LockWindowState::AttachState(wm::WindowState* window_state,
96 wm::WindowState::State* previous_state) {
97 current_state_type_ = previous_state->GetType();
99 // Initialize the state to a good preset.
100 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
101 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
102 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
103 UpdateWindow(window_state,
104 GetMaximizedOrCenteredWindowType(window_state));
108 void LockWindowState::DetachState(wm::WindowState* window_state) {
111 // static
112 wm::WindowState* LockWindowState::SetLockWindowState(aura::Window* window) {
113 scoped_ptr<wm::WindowState::State> lock_state(new LockWindowState(window));
114 scoped_ptr<wm::WindowState::State> old_state(
115 wm::GetWindowState(window)->SetStateObject(lock_state.Pass()));
116 return wm::GetWindowState(window);
119 void LockWindowState::UpdateWindow(wm::WindowState* window_state,
120 wm::WindowStateType target_state) {
121 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED ||
122 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED ||
123 (target_state == wm::WINDOW_STATE_TYPE_NORMAL &&
124 !window_state->CanMaximize()) ||
125 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN);
127 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) {
128 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED)
129 return;
131 current_state_type_ = target_state;
132 ::wm::SetWindowVisibilityAnimationType(
133 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
134 window_state->window()->Hide();
135 if (window_state->IsActive())
136 window_state->Deactivate();
137 return;
140 if (current_state_type_ == target_state) {
141 // If the state type did not change, update it accordingly.
142 UpdateBounds(window_state);
143 return;
146 const wm::WindowStateType old_state_type = current_state_type_;
147 current_state_type_ = target_state;
148 window_state->UpdateWindowShowStateFromStateType();
149 window_state->NotifyPreStateTypeChange(old_state_type);
150 UpdateBounds(window_state);
151 window_state->NotifyPostStateTypeChange(old_state_type);
153 if ((window_state->window()->TargetVisibility() ||
154 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) &&
155 !window_state->window()->layer()->visible()) {
156 // The layer may be hidden if the window was previously minimized. Make
157 // sure it's visible.
158 window_state->window()->Show();
162 wm::WindowStateType LockWindowState::GetMaximizedOrCenteredWindowType(
163 wm::WindowState* window_state) {
164 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED :
165 wm::WINDOW_STATE_TYPE_NORMAL;
168 gfx::Rect GetBoundsForLockWindow(aura::Window* window) {
169 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
170 if (display_manager->IsInUnifiedMode()) {
171 const gfx::Display& first =
172 display_manager->software_mirroring_display_list()[0];
173 return first.bounds();
174 } else {
175 return ScreenUtil::GetDisplayBoundsInParent(window);
179 void LockWindowState::UpdateBounds(wm::WindowState* window_state) {
180 if (!window_state->IsMaximized() && !window_state->IsFullscreen())
181 return;
183 keyboard::KeyboardController* keyboard_controller =
184 keyboard::KeyboardController::GetInstance();
185 gfx::Rect keyboard_bounds;
187 if (keyboard_controller &&
188 !keyboard::IsKeyboardOverscrollEnabled() &&
189 keyboard_controller->keyboard_visible()) {
190 keyboard_bounds = keyboard_controller->current_keyboard_bounds();
192 gfx::Rect bounds =
193 ScreenUtil::GetShelfDisplayBoundsInScreen(window_state->window());
195 bounds.set_height(bounds.height() - keyboard_bounds.height());
197 VLOG(1) << "Updating window bounds to: " << bounds.ToString();
198 window_state->SetBoundsDirect(bounds);
201 } // namespace ash