Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / ash / wm / maximize_mode / maximize_mode_window_state.cc
blobe8da85aa536873d1a9a14398cdbb967b7ddf7675
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/maximize_mode/maximize_mode_window_state.h"
7 #include "ash/screen_util.h"
8 #include "ash/shell.h"
9 #include "ash/shell_window_ids.h"
10 #include "ash/wm/coordinate_conversion.h"
11 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
12 #include "ash/wm/window_animations.h"
13 #include "ash/wm/window_properties.h"
14 #include "ash/wm/window_state_delegate.h"
15 #include "ash/wm/window_state_util.h"
16 #include "ash/wm/window_util.h"
17 #include "ash/wm/wm_event.h"
18 #include "ash/wm/workspace/workspace_window_resizer.h"
19 #include "ui/aura/client/aura_constants.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_delegate.h"
22 #include "ui/compositor/layer.h"
23 #include "ui/gfx/display.h"
24 #include "ui/gfx/geometry/rect.h"
25 #include "ui/views/view_constants_aura.h"
26 #include "ui/views/widget/widget.h"
28 namespace ash {
29 namespace {
31 // Returns the biggest possible size for a window which is about to be
32 // maximized.
33 gfx::Size GetMaximumSizeOfWindow(wm::WindowState* window_state) {
34 DCHECK(window_state->CanMaximize() || window_state->CanResize());
36 gfx::Size workspace_size = ScreenUtil::GetMaximizedWindowBoundsInParent(
37 window_state->window()).size();
39 aura::WindowDelegate* delegate = window_state->window()->delegate();
40 if (!delegate)
41 return workspace_size;
43 gfx::Size size = delegate->GetMaximumSize();
44 if (size.IsEmpty())
45 return workspace_size;
47 size.SetToMin(workspace_size);
48 return size;
51 // Returns the centered bounds of the given bounds in the work area.
52 gfx::Rect GetCenteredBounds(const gfx::Rect& bounds_in_parent,
53 wm::WindowState* state_object) {
54 gfx::Rect work_area_in_parent =
55 ScreenUtil::GetDisplayWorkAreaBoundsInParent(state_object->window());
56 work_area_in_parent.ClampToCenteredSize(bounds_in_parent.size());
57 return work_area_in_parent;
60 // Returns the maximized/full screen and/or centered bounds of a window.
61 gfx::Rect GetBoundsInMaximizedMode(wm::WindowState* state_object) {
62 if (state_object->IsFullscreen())
63 return ScreenUtil::GetDisplayBoundsInParent(state_object->window());
65 gfx::Rect bounds_in_parent;
66 // Make the window as big as possible.
67 if (state_object->CanMaximize() || state_object->CanResize()) {
68 bounds_in_parent.set_size(GetMaximumSizeOfWindow(state_object));
69 } else {
70 // We prefer the user given window dimensions over the current windows
71 // dimensions since they are likely to be the result from some other state
72 // object logic.
73 if (state_object->HasRestoreBounds())
74 bounds_in_parent = state_object->GetRestoreBoundsInParent();
75 else
76 bounds_in_parent = state_object->window()->bounds();
78 return GetCenteredBounds(bounds_in_parent, state_object);
81 } // namespace
83 // static
84 void MaximizeModeWindowState::UpdateWindowPosition(
85 wm::WindowState* window_state) {
86 gfx::Rect bounds_in_parent = GetBoundsInMaximizedMode(window_state);
87 if (bounds_in_parent == window_state->window()->bounds())
88 return;
89 window_state->SetBoundsDirect(bounds_in_parent);
92 MaximizeModeWindowState::MaximizeModeWindowState(
93 aura::Window* window, MaximizeModeWindowManager* creator)
94 : window_(window),
95 creator_(creator),
96 current_state_type_(wm::GetWindowState(window)->GetStateType()),
97 defer_bounds_updates_(false) {
98 old_state_.reset(
99 wm::GetWindowState(window)->SetStateObject(
100 scoped_ptr<State>(this).Pass()).release());
103 MaximizeModeWindowState::~MaximizeModeWindowState() {
104 creator_->WindowStateDestroyed(window_);
107 void MaximizeModeWindowState::LeaveMaximizeMode(wm::WindowState* window_state) {
108 // Note: When we return we will destroy ourselves with the |our_reference|.
109 scoped_ptr<wm::WindowState::State> our_reference =
110 window_state->SetStateObject(old_state_.Pass());
113 void MaximizeModeWindowState::SetDeferBoundsUpdates(bool defer_bounds_updates) {
114 if (defer_bounds_updates_ == defer_bounds_updates)
115 return;
117 defer_bounds_updates_ = defer_bounds_updates;
118 if (!defer_bounds_updates_)
119 UpdateBounds(wm::GetWindowState(window_), true);
122 void MaximizeModeWindowState::OnWMEvent(wm::WindowState* window_state,
123 const wm::WMEvent* event) {
124 switch (event->type()) {
125 case wm::WM_EVENT_TOGGLE_FULLSCREEN:
126 ToggleFullScreen(window_state, window_state->delegate());
127 break;
128 case wm::WM_EVENT_FULLSCREEN:
129 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_FULLSCREEN, true);
130 break;
131 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION:
132 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE:
133 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE:
134 case wm::WM_EVENT_TOGGLE_MAXIMIZE:
135 case wm::WM_EVENT_CYCLE_SNAP_DOCK_LEFT:
136 case wm::WM_EVENT_CYCLE_SNAP_DOCK_RIGHT:
137 case wm::WM_EVENT_CENTER:
138 case wm::WM_EVENT_SNAP_LEFT:
139 case wm::WM_EVENT_SNAP_RIGHT:
140 case wm::WM_EVENT_NORMAL:
141 case wm::WM_EVENT_MAXIMIZE:
142 case wm::WM_EVENT_DOCK:
143 UpdateWindow(window_state,
144 GetMaximizedOrCenteredWindowType(window_state),
145 true);
146 return;
147 case wm::WM_EVENT_MINIMIZE:
148 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_MINIMIZED, true);
149 return;
150 case wm::WM_EVENT_SHOW_INACTIVE:
151 return;
152 case wm::WM_EVENT_SET_BOUNDS:
153 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MAXIMIZED) {
154 // Having a maximized window, it could have been created with an empty
155 // size and the caller should get his size upon leaving the maximized
156 // mode. As such we set the restore bounds to the requested bounds.
157 gfx::Rect bounds_in_parent =
158 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds();
159 if (!bounds_in_parent.IsEmpty())
160 window_state->SetRestoreBoundsInParent(bounds_in_parent);
161 } else if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
162 current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
163 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
164 // In all other cases (except for minimized windows) we respect the
165 // requested bounds and center it to a fully visible area on the screen.
166 gfx::Rect bounds_in_parent =
167 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds();
168 bounds_in_parent = GetCenteredBounds(bounds_in_parent, window_state);
169 if (bounds_in_parent != window_state->window()->bounds()) {
170 if (window_state->window()->IsVisible())
171 window_state->SetBoundsDirectAnimated(bounds_in_parent);
172 else
173 window_state->SetBoundsDirect(bounds_in_parent);
176 break;
177 case wm::WM_EVENT_ADDED_TO_WORKSPACE:
178 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
179 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN &&
180 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) {
181 wm::WindowStateType new_state =
182 GetMaximizedOrCenteredWindowType(window_state);
183 UpdateWindow(window_state, new_state, true);
185 break;
186 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED:
187 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED)
188 UpdateBounds(window_state, true);
189 break;
190 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED:
191 // Don't animate on a screen rotation - just snap to new size.
192 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED)
193 UpdateBounds(window_state, false);
194 break;
198 wm::WindowStateType MaximizeModeWindowState::GetType() const {
199 return current_state_type_;
202 void MaximizeModeWindowState::AttachState(
203 wm::WindowState* window_state,
204 wm::WindowState::State* previous_state) {
205 current_state_type_ = previous_state->GetType();
207 views::Widget* widget =
208 views::Widget::GetWidgetForNativeWindow(window_state->window());
209 if (widget) {
210 gfx::Rect bounds = widget->GetRestoredBounds();
211 if (!bounds.IsEmpty()) {
212 // We do not want to do a session restore to our window states. Therefore
213 // we tell the window to use the current default states instead.
214 window_state->window()->SetProperty(ash::kRestoreShowStateOverrideKey,
215 window_state->GetShowState());
216 window_state->window()->SetProperty(ash::kRestoreBoundsOverrideKey,
217 new gfx::Rect(widget->GetRestoredBounds()));
221 // Initialize the state to a good preset.
222 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
223 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
224 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) {
225 UpdateWindow(window_state,
226 GetMaximizedOrCenteredWindowType(window_state),
227 true);
230 window_state->set_can_be_dragged(false);
233 void MaximizeModeWindowState::DetachState(wm::WindowState* window_state) {
234 // From now on, we can use the default session restore mechanism again.
235 window_state->window()->ClearProperty(ash::kRestoreBoundsOverrideKey);
236 window_state->set_can_be_dragged(true);
239 void MaximizeModeWindowState::UpdateWindow(wm::WindowState* window_state,
240 wm::WindowStateType target_state,
241 bool animated) {
242 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED ||
243 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED ||
244 (target_state == wm::WINDOW_STATE_TYPE_NORMAL &&
245 !window_state->CanMaximize()) ||
246 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN);
248 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) {
249 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED)
250 return;
252 current_state_type_ = target_state;
253 ::wm::SetWindowVisibilityAnimationType(
254 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
255 window_state->window()->Hide();
256 if (window_state->IsActive())
257 window_state->Deactivate();
258 return;
261 if (current_state_type_ == target_state) {
262 // If the state type did not change, update it accordingly.
263 UpdateBounds(window_state, animated);
264 return;
267 const wm::WindowStateType old_state_type = current_state_type_;
268 current_state_type_ = target_state;
269 window_state->UpdateWindowShowStateFromStateType();
270 window_state->NotifyPreStateTypeChange(old_state_type);
271 UpdateBounds(window_state, animated);
272 window_state->NotifyPostStateTypeChange(old_state_type);
274 if ((window_state->window()->TargetVisibility() ||
275 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) &&
276 !window_state->window()->layer()->visible()) {
277 // The layer may be hidden if the window was previously minimized. Make
278 // sure it's visible.
279 window_state->window()->Show();
283 wm::WindowStateType MaximizeModeWindowState::GetMaximizedOrCenteredWindowType(
284 wm::WindowState* window_state) {
285 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED :
286 wm::WINDOW_STATE_TYPE_NORMAL;
289 void MaximizeModeWindowState::UpdateBounds(wm::WindowState* window_state,
290 bool animated) {
291 if (defer_bounds_updates_)
292 return;
293 gfx::Rect bounds_in_parent = GetBoundsInMaximizedMode(window_state);
294 // If we have a target bounds rectangle, we center it and set it
295 // accordingly.
296 if (!bounds_in_parent.IsEmpty() &&
297 bounds_in_parent != window_state->window()->bounds()) {
298 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED ||
299 !window_state->window()->IsVisible() ||
300 !animated) {
301 window_state->SetBoundsDirect(bounds_in_parent);
302 } else {
303 // If we animate (to) maximized mode, we want to use the cross fade to
304 // avoid flashing.
305 if (window_state->IsMaximized())
306 window_state->SetBoundsDirectCrossFade(bounds_in_parent);
307 else
308 window_state->SetBoundsDirectAnimated(bounds_in_parent);
313 } // namespace ash