Correctly link HttpStreams to QuicSessions in net-internals.
[chromium-blink-merge.git] / ash / wm / lock_window_state.cc
blobfae47085d7e9f3da6e97942bd289cca58d2355e3
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));
80 break;
81 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED:
82 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED:
83 UpdateBounds(window_state);
84 break;
88 wm::WindowStateType LockWindowState::GetType() const {
89 return current_state_type_;
92 void LockWindowState::AttachState(wm::WindowState* window_state,
93 wm::WindowState::State* previous_state) {
94 current_state_type_ = previous_state->GetType();
96 // Initialize the state to a good preset.
97 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
98 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
99 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
100 UpdateWindow(window_state,
101 GetMaximizedOrCenteredWindowType(window_state));
105 void LockWindowState::DetachState(wm::WindowState* window_state) {
108 // static
109 wm::WindowState* LockWindowState::SetLockWindowState(aura::Window* window) {
110 scoped_ptr<wm::WindowState::State> lock_state(new LockWindowState(window));
111 scoped_ptr<wm::WindowState::State> old_state(
112 wm::GetWindowState(window)->SetStateObject(lock_state.Pass()));
113 return wm::GetWindowState(window);
116 void LockWindowState::UpdateWindow(wm::WindowState* window_state,
117 wm::WindowStateType target_state) {
118 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED ||
119 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED ||
120 (target_state == wm::WINDOW_STATE_TYPE_NORMAL &&
121 !window_state->CanMaximize()) ||
122 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN);
124 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) {
125 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED)
126 return;
128 current_state_type_ = target_state;
129 ::wm::SetWindowVisibilityAnimationType(
130 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
131 window_state->window()->Hide();
132 if (window_state->IsActive())
133 window_state->Deactivate();
134 return;
137 if (current_state_type_ == target_state) {
138 // If the state type did not change, update it accordingly.
139 UpdateBounds(window_state);
140 return;
143 const wm::WindowStateType old_state_type = current_state_type_;
144 current_state_type_ = target_state;
145 window_state->UpdateWindowShowStateFromStateType();
146 window_state->NotifyPreStateTypeChange(old_state_type);
147 UpdateBounds(window_state);
148 window_state->NotifyPostStateTypeChange(old_state_type);
150 if ((window_state->window()->TargetVisibility() ||
151 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) &&
152 !window_state->window()->layer()->visible()) {
153 // The layer may be hidden if the window was previously minimized. Make
154 // sure it's visible.
155 window_state->window()->Show();
159 wm::WindowStateType LockWindowState::GetMaximizedOrCenteredWindowType(
160 wm::WindowState* window_state) {
161 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED :
162 wm::WINDOW_STATE_TYPE_NORMAL;
165 void LockWindowState::UpdateBounds(wm::WindowState* window_state) {
166 if (!window_state->IsMaximized() && !window_state->IsFullscreen())
167 return;
169 keyboard::KeyboardController* keyboard_controller =
170 keyboard::KeyboardController::GetInstance();
171 gfx::Rect keyboard_bounds;
173 if (keyboard_controller &&
174 !keyboard::IsKeyboardOverscrollEnabled() &&
175 keyboard_controller->keyboard_visible()) {
176 keyboard_bounds = keyboard_controller->current_keyboard_bounds();
179 gfx::Rect bounds =
180 ScreenUtil::GetDisplayBoundsInParent(window_state->window());
181 bounds.set_height(bounds.height() - keyboard_bounds.height());
183 VLOG(1) << "Updating window bounds to: " << bounds.ToString();
184 window_state->SetBoundsDirect(bounds);
187 } // namespace ash