Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: ash/wm/maximize_mode/maximize_mode_window_state.cc

Issue 2108223002: Moves handful of classes in ash/wm/maximize_mode to ash/common (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@maximize_mode_window_manager
Patch Set: remove extraneous Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ash/wm/maximize_mode/maximize_mode_window_state.h"
6
7 #include <utility>
8
9 #include "ash/common/shell_window_ids.h"
10 #include "ash/common/wm/window_animation_types.h"
11 #include "ash/common/wm/window_state_util.h"
12 #include "ash/common/wm/wm_event.h"
13 #include "ash/common/wm/wm_screen_util.h"
14 #include "ash/common/wm_shell.h"
15 #include "ash/common/wm_window.h"
16 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h"
17 #include "ui/compositor/layer.h"
18 #include "ui/gfx/geometry/rect.h"
19
20 namespace ash {
21 namespace {
22
23 // Returns the biggest possible size for a window which is about to be
24 // maximized.
25 gfx::Size GetMaximumSizeOfWindow(wm::WindowState* window_state) {
26 DCHECK(window_state->CanMaximize() || window_state->CanResize());
27
28 gfx::Size workspace_size =
29 wm::GetMaximizedWindowBoundsInParent(window_state->window()).size();
30
31 gfx::Size size = window_state->window()->GetMaximumSize();
32 if (size.IsEmpty())
33 return workspace_size;
34
35 size.SetToMin(workspace_size);
36 return size;
37 }
38
39 // Returns the centered bounds of the given bounds in the work area.
40 gfx::Rect GetCenteredBounds(const gfx::Rect& bounds_in_parent,
41 wm::WindowState* state_object) {
42 gfx::Rect work_area_in_parent =
43 wm::GetDisplayWorkAreaBoundsInParent(state_object->window());
44 work_area_in_parent.ClampToCenteredSize(bounds_in_parent.size());
45 return work_area_in_parent;
46 }
47
48 // Returns the maximized/full screen and/or centered bounds of a window.
49 gfx::Rect GetBoundsInMaximizedMode(wm::WindowState* state_object) {
50 if (state_object->IsFullscreen() || state_object->IsPinned())
51 return wm::GetDisplayBoundsInParent(state_object->window());
52
53 gfx::Rect bounds_in_parent;
54 // Make the window as big as possible.
55 if (state_object->CanMaximize() || state_object->CanResize()) {
56 bounds_in_parent.set_size(GetMaximumSizeOfWindow(state_object));
57 } else {
58 // We prefer the user given window dimensions over the current windows
59 // dimensions since they are likely to be the result from some other state
60 // object logic.
61 if (state_object->HasRestoreBounds())
62 bounds_in_parent = state_object->GetRestoreBoundsInParent();
63 else
64 bounds_in_parent = state_object->window()->GetBounds();
65 }
66 return GetCenteredBounds(bounds_in_parent, state_object);
67 }
68
69 gfx::Rect GetRestoreBounds(wm::WindowState* window_state) {
70 if (window_state->IsMinimized() || window_state->IsMaximized() ||
71 window_state->IsFullscreen()) {
72 gfx::Rect restore_bounds = window_state->GetRestoreBoundsInScreen();
73 if (!restore_bounds.IsEmpty())
74 return restore_bounds;
75 }
76 gfx::Rect bounds = window_state->window()->GetBoundsInScreen();
77 if (window_state->IsDocked()) {
78 gfx::Rect restore_bounds = window_state->GetRestoreBoundsInScreen();
79 // Use current window horizontal offset origin in order to preserve docked
80 // alignment but preserve restored size and vertical offset for the time
81 // when the window gets undocked.
82 if (!restore_bounds.IsEmpty()) {
83 bounds.set_size(restore_bounds.size());
84 bounds.set_y(restore_bounds.y());
85 }
86 }
87 return bounds;
88 }
89
90 } // namespace
91
92 // static
93 void MaximizeModeWindowState::UpdateWindowPosition(
94 wm::WindowState* window_state) {
95 gfx::Rect bounds_in_parent = GetBoundsInMaximizedMode(window_state);
96 if (bounds_in_parent == window_state->window()->GetBounds())
97 return;
98 window_state->SetBoundsDirect(bounds_in_parent);
99 }
100
101 MaximizeModeWindowState::MaximizeModeWindowState(
102 WmWindow* window,
103 MaximizeModeWindowManager* creator)
104 : window_(window),
105 creator_(creator),
106 current_state_type_(window->GetWindowState()->GetStateType()),
107 defer_bounds_updates_(false) {
108 old_state_.reset(window_->GetWindowState()
109 ->SetStateObject(std::unique_ptr<State>(this))
110 .release());
111 }
112
113 MaximizeModeWindowState::~MaximizeModeWindowState() {
114 creator_->WindowStateDestroyed(window_);
115 }
116
117 void MaximizeModeWindowState::LeaveMaximizeMode(wm::WindowState* window_state) {
118 // Note: When we return we will destroy ourselves with the |our_reference|.
119 std::unique_ptr<wm::WindowState::State> our_reference =
120 window_state->SetStateObject(std::move(old_state_));
121 }
122
123 void MaximizeModeWindowState::SetDeferBoundsUpdates(bool defer_bounds_updates) {
124 if (defer_bounds_updates_ == defer_bounds_updates)
125 return;
126
127 defer_bounds_updates_ = defer_bounds_updates;
128 if (!defer_bounds_updates_)
129 UpdateBounds(window_->GetWindowState(), true);
130 }
131
132 void MaximizeModeWindowState::OnWMEvent(wm::WindowState* window_state,
133 const wm::WMEvent* event) {
134 switch (event->type()) {
135 case wm::WM_EVENT_TOGGLE_FULLSCREEN:
136 ToggleFullScreen(window_state, window_state->delegate());
137 break;
138 case wm::WM_EVENT_FULLSCREEN:
139 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_FULLSCREEN, true);
140 break;
141 case wm::WM_EVENT_PIN:
142 if (!WmShell::Get()->IsPinned())
143 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_PINNED, true);
144 break;
145 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION:
146 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE:
147 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE:
148 case wm::WM_EVENT_TOGGLE_MAXIMIZE:
149 case wm::WM_EVENT_CYCLE_SNAP_DOCK_LEFT:
150 case wm::WM_EVENT_CYCLE_SNAP_DOCK_RIGHT:
151 case wm::WM_EVENT_CENTER:
152 case wm::WM_EVENT_SNAP_LEFT:
153 case wm::WM_EVENT_SNAP_RIGHT:
154 case wm::WM_EVENT_NORMAL:
155 case wm::WM_EVENT_MAXIMIZE:
156 case wm::WM_EVENT_DOCK:
157 UpdateWindow(window_state, GetMaximizedOrCenteredWindowType(window_state),
158 true);
159 return;
160 case wm::WM_EVENT_MINIMIZE:
161 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_MINIMIZED, true);
162 return;
163 case wm::WM_EVENT_SHOW_INACTIVE:
164 return;
165 case wm::WM_EVENT_SET_BOUNDS:
166 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MAXIMIZED) {
167 // Having a maximized window, it could have been created with an empty
168 // size and the caller should get his size upon leaving the maximized
169 // mode. As such we set the restore bounds to the requested bounds.
170 gfx::Rect bounds_in_parent =
171 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds();
172 if (!bounds_in_parent.IsEmpty())
173 window_state->SetRestoreBoundsInParent(bounds_in_parent);
174 } else if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
175 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN &&
176 current_state_type_ != wm::WINDOW_STATE_TYPE_PINNED) {
177 // In all other cases (except for minimized windows) we respect the
178 // requested bounds and center it to a fully visible area on the screen.
179 gfx::Rect bounds_in_parent =
180 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds();
181 bounds_in_parent = GetCenteredBounds(bounds_in_parent, window_state);
182 if (bounds_in_parent != window_state->window()->GetBounds()) {
183 if (window_state->window()->IsVisible())
184 window_state->SetBoundsDirectAnimated(bounds_in_parent);
185 else
186 window_state->SetBoundsDirect(bounds_in_parent);
187 }
188 }
189 break;
190 case wm::WM_EVENT_ADDED_TO_WORKSPACE:
191 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
192 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN &&
193 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) {
194 wm::WindowStateType new_state =
195 GetMaximizedOrCenteredWindowType(window_state);
196 UpdateWindow(window_state, new_state, true);
197 }
198 break;
199 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED:
200 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED)
201 UpdateBounds(window_state, true);
202 break;
203 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED:
204 // Don't animate on a screen rotation - just snap to new size.
205 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED)
206 UpdateBounds(window_state, false);
207 break;
208 }
209 }
210
211 wm::WindowStateType MaximizeModeWindowState::GetType() const {
212 return current_state_type_;
213 }
214
215 void MaximizeModeWindowState::AttachState(
216 wm::WindowState* window_state,
217 wm::WindowState::State* previous_state) {
218 current_state_type_ = previous_state->GetType();
219
220 gfx::Rect restore_bounds = GetRestoreBounds(window_state);
221 if (!restore_bounds.IsEmpty()) {
222 // We do not want to do a session restore to our window states. Therefore
223 // we tell the window to use the current default states instead.
224 window_state->window()->SetRestoreOverrides(restore_bounds,
225 window_state->GetShowState());
226 }
227
228 // Initialize the state to a good preset.
229 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED &&
230 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED &&
231 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN &&
232 current_state_type_ != wm::WINDOW_STATE_TYPE_PINNED) {
233 UpdateWindow(window_state, GetMaximizedOrCenteredWindowType(window_state),
234 true);
235 }
236
237 window_state->set_can_be_dragged(false);
238 }
239
240 void MaximizeModeWindowState::DetachState(wm::WindowState* window_state) {
241 // From now on, we can use the default session restore mechanism again.
242 window_state->window()->SetRestoreOverrides(gfx::Rect(),
243 ui::SHOW_STATE_NORMAL);
244 window_state->set_can_be_dragged(true);
245 }
246
247 void MaximizeModeWindowState::UpdateWindow(wm::WindowState* window_state,
248 wm::WindowStateType target_state,
249 bool animated) {
250 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED ||
251 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED ||
252 target_state == wm::WINDOW_STATE_TYPE_PINNED ||
253 (target_state == wm::WINDOW_STATE_TYPE_NORMAL &&
254 !window_state->CanMaximize()) ||
255 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN);
256
257 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) {
258 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED)
259 return;
260
261 current_state_type_ = target_state;
262 window_state->window()->SetVisibilityAnimationType(
263 wm::WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE);
264 window_state->window()->Hide();
265 if (window_state->IsActive())
266 window_state->Deactivate();
267 return;
268 }
269
270 if (current_state_type_ == target_state) {
271 // If the state type did not change, update it accordingly.
272 UpdateBounds(window_state, animated);
273 return;
274 }
275
276 const wm::WindowStateType old_state_type = current_state_type_;
277 current_state_type_ = target_state;
278 window_state->UpdateWindowShowStateFromStateType();
279 window_state->NotifyPreStateTypeChange(old_state_type);
280 UpdateBounds(window_state, animated);
281 window_state->NotifyPostStateTypeChange(old_state_type);
282
283 if (old_state_type == wm::WINDOW_STATE_TYPE_PINNED ||
284 target_state == wm::WINDOW_STATE_TYPE_PINNED) {
285 WmShell::Get()->SetPinnedWindow(window_state->window());
286 }
287
288 if ((window_state->window()->GetTargetVisibility() ||
289 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) &&
290 !window_state->window()->GetLayer()->visible()) {
291 // The layer may be hidden if the window was previously minimized. Make
292 // sure it's visible.
293 window_state->window()->Show();
294 }
295 }
296
297 wm::WindowStateType MaximizeModeWindowState::GetMaximizedOrCenteredWindowType(
298 wm::WindowState* window_state) {
299 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED
300 : wm::WINDOW_STATE_TYPE_NORMAL;
301 }
302
303 void MaximizeModeWindowState::UpdateBounds(wm::WindowState* window_state,
304 bool animated) {
305 if (defer_bounds_updates_)
306 return;
307 gfx::Rect bounds_in_parent = GetBoundsInMaximizedMode(window_state);
308 // If we have a target bounds rectangle, we center it and set it
309 // accordingly.
310 if (!bounds_in_parent.IsEmpty() &&
311 bounds_in_parent != window_state->window()->GetBounds()) {
312 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED ||
313 !window_state->window()->IsVisible() || !animated) {
314 window_state->SetBoundsDirect(bounds_in_parent);
315 } else {
316 // If we animate (to) maximized mode, we want to use the cross fade to
317 // avoid flashing.
318 if (window_state->IsMaximized())
319 window_state->SetBoundsDirectCrossFade(bounds_in_parent);
320 else
321 window_state->SetBoundsDirectAnimated(bounds_in_parent);
322 }
323 }
324 }
325
326 } // namespace ash
OLDNEW
« no previous file with comments | « ash/wm/maximize_mode/maximize_mode_window_state.h ('k') | ash/wm/maximize_mode/workspace_backdrop_delegate.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698