OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/maximize_mode/maximize_mode_window_state.h" | 5 #include "ash/wm/maximize_mode/maximize_mode_window_state.h" |
6 | 6 |
7 #include "ash/display/display_controller.h" | 7 #include "ash/display/display_controller.h" |
8 #include "ash/screen_util.h" | 8 #include "ash/screen_util.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 // Returns the centered bounds of the given bounds in the work area. | 48 // Returns the centered bounds of the given bounds in the work area. |
49 gfx::Rect GetCenteredBounds(const gfx::Rect& bounds_in_parent, | 49 gfx::Rect GetCenteredBounds(const gfx::Rect& bounds_in_parent, |
50 wm::WindowState* state_object) { | 50 wm::WindowState* state_object) { |
51 gfx::Rect work_area_in_parent = | 51 gfx::Rect work_area_in_parent = |
52 ScreenUtil::GetDisplayWorkAreaBoundsInParent(state_object->window()); | 52 ScreenUtil::GetDisplayWorkAreaBoundsInParent(state_object->window()); |
53 work_area_in_parent.ClampToCenteredSize(bounds_in_parent.size()); | 53 work_area_in_parent.ClampToCenteredSize(bounds_in_parent.size()); |
54 return work_area_in_parent; | 54 return work_area_in_parent; |
55 } | 55 } |
56 | 56 |
57 // Returns the maximized and centered bounds of a window. | 57 // Returns the maximized/full screen and/or centered bounds of a window. |
58 gfx::Rect GetMaximizedAndCenteredBounds(wm::WindowState* state_object) { | 58 gfx::Rect GetMaximizedAndCenteredBounds(wm::WindowState* state_object) { |
| 59 if (state_object->IsFullscreen()) |
| 60 return ScreenUtil::GetDisplayBoundsInParent(state_object->window()); |
| 61 |
59 gfx::Rect bounds_in_parent; | 62 gfx::Rect bounds_in_parent; |
60 // Make the window as big as possible. | 63 // Make the window as big as possible. |
61 if (state_object->CanMaximize() || state_object->CanResize()) { | 64 if (state_object->CanMaximize() || state_object->CanResize()) { |
62 bounds_in_parent.set_size(GetMaximumSizeOfWindow(state_object)); | 65 bounds_in_parent.set_size(GetMaximumSizeOfWindow(state_object)); |
63 } else { | 66 } else { |
64 // We prefer the user given window dimensions over the current windows | 67 // We prefer the user given window dimensions over the current windows |
65 // dimensions since they are likely to be the result from some other state | 68 // dimensions since they are likely to be the result from some other state |
66 // object logic. | 69 // object logic. |
67 if (state_object->HasRestoreBounds()) | 70 if (state_object->HasRestoreBounds()) |
68 bounds_in_parent = state_object->GetRestoreBoundsInParent(); | 71 bounds_in_parent = state_object->GetRestoreBoundsInParent(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 107 |
105 void MaximizeModeWindowState::LeaveMaximizeMode(wm::WindowState* window_state) { | 108 void MaximizeModeWindowState::LeaveMaximizeMode(wm::WindowState* window_state) { |
106 // Note: When we return we will destroy ourselves with the |our_reference|. | 109 // Note: When we return we will destroy ourselves with the |our_reference|. |
107 scoped_ptr<wm::WindowState::State> our_reference = | 110 scoped_ptr<wm::WindowState::State> our_reference = |
108 window_state->SetStateObject(old_state_.Pass()); | 111 window_state->SetStateObject(old_state_.Pass()); |
109 } | 112 } |
110 | 113 |
111 void MaximizeModeWindowState::OnWMEvent(wm::WindowState* window_state, | 114 void MaximizeModeWindowState::OnWMEvent(wm::WindowState* window_state, |
112 const wm::WMEvent* event) { | 115 const wm::WMEvent* event) { |
113 switch (event->type()) { | 116 switch (event->type()) { |
| 117 case wm::WM_EVENT_TOGGLE_FULLSCREEN: { |
| 118 // Window which cannot be maximized should not be fullscreened. |
| 119 // It can, however, be restored if it was fullscreened. |
| 120 bool is_fullscreen = window_state->IsFullscreen(); |
| 121 if (!is_fullscreen && !window_state->CanMaximize()) |
| 122 return; |
| 123 |
| 124 if (window_state->delegate() && |
| 125 window_state->delegate()->ToggleFullscreen(window_state)) { |
| 126 return; |
| 127 } |
| 128 UpdateWindow(window_state, |
| 129 is_fullscreen ? |
| 130 GetMaximizedOrCenteredWindowType(window_state) : |
| 131 wm::WINDOW_STATE_TYPE_FULLSCREEN, |
| 132 true); |
| 133 break; |
| 134 } |
| 135 case wm::WM_EVENT_FULLSCREEN: |
| 136 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_FULLSCREEN, true); |
| 137 break; |
114 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION: | 138 case wm::WM_EVENT_TOGGLE_MAXIMIZE_CAPTION: |
115 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE: | 139 case wm::WM_EVENT_TOGGLE_VERTICAL_MAXIMIZE: |
116 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE: | 140 case wm::WM_EVENT_TOGGLE_HORIZONTAL_MAXIMIZE: |
117 case wm::WM_EVENT_TOGGLE_FULLSCREEN: | |
118 case wm::WM_EVENT_TOGGLE_MAXIMIZE: | 141 case wm::WM_EVENT_TOGGLE_MAXIMIZE: |
119 case wm::WM_EVENT_CENTER: | 142 case wm::WM_EVENT_CENTER: |
120 case wm::WM_EVENT_FULLSCREEN: | |
121 case wm::WM_EVENT_SNAP_LEFT: | 143 case wm::WM_EVENT_SNAP_LEFT: |
122 case wm::WM_EVENT_SNAP_RIGHT: | 144 case wm::WM_EVENT_SNAP_RIGHT: |
123 case wm::WM_EVENT_NORMAL: | 145 case wm::WM_EVENT_NORMAL: |
124 case wm::WM_EVENT_MAXIMIZE: | 146 case wm::WM_EVENT_MAXIMIZE: |
125 MaximizeOrCenterWindow(window_state, true); | 147 UpdateWindow(window_state, |
| 148 GetMaximizedOrCenteredWindowType(window_state), |
| 149 true); |
126 return; | 150 return; |
127 case wm::WM_EVENT_MINIMIZE: | 151 case wm::WM_EVENT_MINIMIZE: |
128 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) { | 152 UpdateWindow(window_state, wm::WINDOW_STATE_TYPE_MINIMIZED, true); |
129 current_state_type_ = wm::WINDOW_STATE_TYPE_MINIMIZED; | |
130 Minimize(window_state); | |
131 } | |
132 return; | 153 return; |
133 case wm::WM_EVENT_SHOW_INACTIVE: | 154 case wm::WM_EVENT_SHOW_INACTIVE: |
134 return; | 155 return; |
135 case wm::WM_EVENT_SET_BOUNDS: | 156 case wm::WM_EVENT_SET_BOUNDS: |
136 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MAXIMIZED || | 157 if (window_state->CanResize()) { |
137 window_state->CanResize()) { | |
138 // In case the window is resizable and / or maximized we ignore the | 158 // In case the window is resizable and / or maximized we ignore the |
139 // requested bounds change and resize to the biggest possible size. | 159 // requested bounds change and resize to the biggest possible size. |
140 MaximizeOrCenterWindow(window_state, true); | 160 UpdateBounds(window_state, true); |
141 } else if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) { | 161 } else |
| 162 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED && |
| 163 current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED) { |
142 // In all other cases (except for minimized windows) we respect the | 164 // In all other cases (except for minimized windows) we respect the |
143 // requested bounds and center it to a fully visible area on the screen. | 165 // requested bounds and center it to a fully visible area on the screen. |
144 gfx::Rect bounds_in_parent = | 166 gfx::Rect bounds_in_parent = |
145 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds(); | 167 (static_cast<const wm::SetBoundsEvent*>(event))->requested_bounds(); |
146 bounds_in_parent = GetCenteredBounds(bounds_in_parent, window_state); | 168 bounds_in_parent = GetCenteredBounds(bounds_in_parent, window_state); |
147 if (bounds_in_parent != window_state->window()->bounds()) { | 169 if (bounds_in_parent != window_state->window()->bounds()) { |
148 if (window_state->window()->IsVisible()) | 170 if (window_state->window()->IsVisible()) |
149 window_state->SetBoundsDirectAnimated(bounds_in_parent); | 171 window_state->SetBoundsDirectAnimated(bounds_in_parent); |
150 else | 172 else |
151 window_state->SetBoundsDirect(bounds_in_parent); | 173 window_state->SetBoundsDirect(bounds_in_parent); |
152 } | 174 } |
153 } | 175 } |
154 break; | 176 break; |
155 case wm::WM_EVENT_ADDED_TO_WORKSPACE: | 177 case wm::WM_EVENT_ADDED_TO_WORKSPACE: |
156 MaximizeOrCenterWindow(window_state, true); | 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); |
| 184 } |
157 break; | 185 break; |
158 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED: | 186 case wm::WM_EVENT_WORKAREA_BOUNDS_CHANGED: |
159 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED: | 187 case wm::WM_EVENT_DISPLAY_BOUNDS_CHANGED: |
160 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) | 188 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) |
161 MaximizeOrCenterWindow(window_state, false); | 189 UpdateBounds(window_state, true); |
162 break; | 190 break; |
163 } | 191 } |
164 } | 192 } |
165 | 193 |
166 wm::WindowStateType MaximizeModeWindowState::GetType() const { | 194 wm::WindowStateType MaximizeModeWindowState::GetType() const { |
167 return current_state_type_; | 195 return current_state_type_; |
168 } | 196 } |
169 | 197 |
170 void MaximizeModeWindowState::AttachState( | 198 void MaximizeModeWindowState::AttachState( |
171 wm::WindowState* window_state, | 199 wm::WindowState* window_state, |
172 wm::WindowState::State* previous_state) { | 200 wm::WindowState::State* previous_state) { |
173 current_state_type_ = previous_state->GetType(); | 201 current_state_type_ = previous_state->GetType(); |
174 | 202 |
175 // Initialize the state to a good preset. | 203 // Initialize the state to a good preset. |
176 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED && | 204 if (current_state_type_ != wm::WINDOW_STATE_TYPE_MAXIMIZED && |
177 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED) { | 205 current_state_type_ != wm::WINDOW_STATE_TYPE_MINIMIZED && |
178 MaximizeOrCenterWindow(window_state, true); | 206 current_state_type_ != wm::WINDOW_STATE_TYPE_FULLSCREEN) { |
| 207 UpdateWindow(window_state, |
| 208 GetMaximizedOrCenteredWindowType(window_state), |
| 209 true); |
179 } | 210 } |
180 | 211 |
181 window_state->set_can_be_dragged(false); | 212 window_state->set_can_be_dragged(false); |
182 } | 213 } |
183 | 214 |
184 void MaximizeModeWindowState::DetachState(wm::WindowState* window_state) { | 215 void MaximizeModeWindowState::DetachState(wm::WindowState* window_state) { |
185 window_state->set_can_be_dragged(true); | 216 window_state->set_can_be_dragged(true); |
186 } | 217 } |
187 | 218 |
188 void MaximizeModeWindowState::MaximizeOrCenterWindow( | 219 void MaximizeModeWindowState::UpdateWindow(wm::WindowState* window_state, |
189 wm::WindowState* window_state, | 220 wm::WindowStateType target_state, |
190 bool animated) { | 221 bool animated) { |
191 const wm::WindowStateType target_state = | 222 DCHECK(target_state == wm::WINDOW_STATE_TYPE_MINIMIZED || |
192 window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED : | 223 target_state == wm::WINDOW_STATE_TYPE_MAXIMIZED || |
193 wm::WINDOW_STATE_TYPE_NORMAL; | 224 (target_state == wm::WINDOW_STATE_TYPE_NORMAL && |
| 225 !window_state->CanMaximize()) || |
| 226 target_state == wm::WINDOW_STATE_TYPE_FULLSCREEN); |
| 227 |
| 228 if (target_state == wm::WINDOW_STATE_TYPE_MINIMIZED) { |
| 229 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED) |
| 230 return; |
| 231 |
| 232 current_state_type_ = target_state; |
| 233 ::wm::SetWindowVisibilityAnimationType( |
| 234 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE); |
| 235 window_state->window()->Hide(); |
| 236 if (window_state->IsActive()) |
| 237 window_state->Deactivate(); |
| 238 return; |
| 239 } |
| 240 |
| 241 if (current_state_type_ == target_state) { |
| 242 // If the state type did not change, update it accordingly. |
| 243 UpdateBounds(window_state, animated); |
| 244 return; |
| 245 } |
| 246 |
194 const wm::WindowStateType old_state_type = current_state_type_; | 247 const wm::WindowStateType old_state_type = current_state_type_; |
195 gfx::Rect bounds_in_parent = GetMaximizedAndCenteredBounds(window_state); | 248 current_state_type_ = target_state; |
196 | 249 window_state->UpdateWindowShowStateFromStateType(); |
197 if (current_state_type_ != target_state) { | 250 window_state->NotifyPreStateTypeChange(old_state_type); |
198 current_state_type_ = target_state; | 251 UpdateBounds(window_state, animated); |
199 window_state->UpdateWindowShowStateFromStateType(); | 252 window_state->NotifyPostStateTypeChange(old_state_type); |
200 window_state->NotifyPreStateTypeChange(old_state_type); | |
201 // If we have a target bounds rectangle, we center it and set it | |
202 // accordingly. | |
203 if (!bounds_in_parent.IsEmpty()) { | |
204 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED || !animated) | |
205 window_state->SetBoundsDirect(bounds_in_parent); | |
206 else | |
207 window_state->SetBoundsDirectAnimated(bounds_in_parent); | |
208 } | |
209 window_state->NotifyPostStateTypeChange(old_state_type); | |
210 } else if (!bounds_in_parent.IsEmpty() && | |
211 bounds_in_parent != window_state->window()->bounds()) { | |
212 // Coming here, we were most probably called through a display change. | |
213 // Do not animate. | |
214 if (animated) | |
215 window_state->SetBoundsDirectAnimated(bounds_in_parent); | |
216 else | |
217 window_state->SetBoundsDirect(bounds_in_parent); | |
218 } | |
219 | 253 |
220 if ((window_state->window()->TargetVisibility() || | 254 if ((window_state->window()->TargetVisibility() || |
221 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) && | 255 old_state_type == wm::WINDOW_STATE_TYPE_MINIMIZED) && |
222 !window_state->window()->layer()->visible()) { | 256 !window_state->window()->layer()->visible()) { |
223 // The layer may be hidden if the window was previously minimized. Make | 257 // The layer may be hidden if the window was previously minimized. Make |
224 // sure it's visible. | 258 // sure it's visible. |
225 window_state->window()->Show(); | 259 window_state->window()->Show(); |
226 } | 260 } |
227 } | 261 } |
228 | 262 |
229 void MaximizeModeWindowState::Minimize(wm::WindowState* window_state) { | 263 wm::WindowStateType MaximizeModeWindowState::GetMaximizedOrCenteredWindowType( |
230 ::wm::SetWindowVisibilityAnimationType( | 264 wm::WindowState* window_state) { |
231 window_state->window(), WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE); | 265 return window_state->CanMaximize() ? wm::WINDOW_STATE_TYPE_MAXIMIZED : |
| 266 wm::WINDOW_STATE_TYPE_NORMAL; |
| 267 } |
232 | 268 |
233 // Hide the window. | 269 void MaximizeModeWindowState::UpdateBounds(wm::WindowState* window_state, |
234 window_state->window()->Hide(); | 270 bool animated) { |
235 // Activate another window. | 271 gfx::Rect bounds_in_parent = GetMaximizedAndCenteredBounds(window_state); |
236 if (window_state->IsActive()) | 272 // If we have a target bounds rectangle, we center it and set it |
237 window_state->Deactivate(); | 273 // accordingly. |
| 274 if (!bounds_in_parent.IsEmpty() && |
| 275 bounds_in_parent != window_state->window()->bounds()) { |
| 276 if (current_state_type_ == wm::WINDOW_STATE_TYPE_MINIMIZED || |
| 277 !window_state->window()->IsVisible() || |
| 278 !animated) { |
| 279 window_state->SetBoundsDirect(bounds_in_parent); |
| 280 } else { |
| 281 window_state->SetBoundsDirectAnimated(bounds_in_parent); |
| 282 } |
| 283 } |
238 } | 284 } |
239 | 285 |
240 } // namespace ash | 286 } // namespace ash |
OLD | NEW |