1 // Copyright 2013 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/window_state.h"
7 #include "ash/ash_switches.h"
8 #include "ash/root_window_controller.h"
9 #include "ash/screen_util.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/default_state.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_observer.h"
16 #include "ash/wm/window_util.h"
17 #include "ash/wm/wm_event.h"
18 #include "base/auto_reset.h"
19 #include "base/command_line.h"
20 #include "ui/aura/client/aura_constants.h"
21 #include "ui/aura/layout_manager.h"
22 #include "ui/aura/window.h"
23 #include "ui/aura/window_delegate.h"
24 #include "ui/compositor/layer_tree_owner.h"
25 #include "ui/compositor/scoped_layer_animation_settings.h"
26 #include "ui/gfx/display.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/wm/core/window_util.h"
35 // A tentative class to set the bounds on the window.
36 // TODO(oshima): Once all logic is cleaned up, move this to the real layout
37 // manager with proper friendship.
38 class BoundsSetter
: public aura::LayoutManager
{
41 ~BoundsSetter() override
{}
43 // aura::LayoutManager overrides:
44 void OnWindowResized() override
{}
45 void OnWindowAddedToLayout(aura::Window
* child
) override
{}
46 void OnWillRemoveWindowFromLayout(aura::Window
* child
) override
{}
47 void OnWindowRemovedFromLayout(aura::Window
* child
) override
{}
48 void OnChildWindowVisibilityChanged(aura::Window
* child
,
49 bool visible
) override
{}
50 void SetChildBounds(aura::Window
* child
,
51 const gfx::Rect
& requested_bounds
) override
{}
53 void SetBounds(aura::Window
* window
, const gfx::Rect
& bounds
) {
54 SetChildBoundsDirect(window
, bounds
);
58 DISALLOW_COPY_AND_ASSIGN(BoundsSetter
);
61 WMEventType
WMEventTypeFromShowState(ui::WindowShowState requested_show_state
) {
62 switch (requested_show_state
) {
63 case ui::SHOW_STATE_DEFAULT
:
64 case ui::SHOW_STATE_NORMAL
:
65 return WM_EVENT_NORMAL
;
66 case ui::SHOW_STATE_MINIMIZED
:
67 return WM_EVENT_MINIMIZE
;
68 case ui::SHOW_STATE_MAXIMIZED
:
69 return WM_EVENT_MAXIMIZE
;
70 case ui::SHOW_STATE_FULLSCREEN
:
71 return WM_EVENT_FULLSCREEN
;
72 case ui::SHOW_STATE_INACTIVE
:
73 return WM_EVENT_SHOW_INACTIVE
;
74 case ui::SHOW_STATE_END
:
75 NOTREACHED() << "No WMEvent defined for the show state:"
76 << requested_show_state
;
78 return WM_EVENT_NORMAL
;
83 WindowState::~WindowState() {
84 // WindowState is registered as an owned property of |window_|, and window
85 // unregisters all of its observers in its d'tor before destroying its
86 // properties. As a result, window_->RemoveObserver() doesn't need to (and
87 // shouldn't) be called here.
90 bool WindowState::HasDelegate() const {
94 void WindowState::SetDelegate(scoped_ptr
<WindowStateDelegate
> delegate
) {
95 DCHECK(!delegate_
.get());
96 delegate_
= delegate
.Pass();
99 WindowStateType
WindowState::GetStateType() const {
100 return current_state_
->GetType();
103 bool WindowState::IsMinimized() const {
104 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED
||
105 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED
;
108 bool WindowState::IsMaximized() const {
109 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED
;
112 bool WindowState::IsFullscreen() const {
113 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN
;
116 bool WindowState::IsMaximizedOrFullscreen() const {
117 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN
||
118 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED
;
121 bool WindowState::IsSnapped() const {
122 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED
||
123 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED
;
126 bool WindowState::IsNormalStateType() const {
127 return GetStateType() == WINDOW_STATE_TYPE_NORMAL
||
128 GetStateType() == WINDOW_STATE_TYPE_DEFAULT
;
131 bool WindowState::IsNormalOrSnapped() const {
132 return IsNormalStateType() || IsSnapped();
135 bool WindowState::IsActive() const {
136 return IsActiveWindow(window_
);
139 bool WindowState::IsDocked() const {
140 return GetStateType() == WINDOW_STATE_TYPE_DOCKED
||
141 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED
;
144 bool WindowState::CanMaximize() const {
145 // Window must have the kCanMaximizeKey and have no maximum width or height.
146 if (!window()->GetProperty(aura::client::kCanMaximizeKey
))
149 if (!window()->delegate())
152 gfx::Size max_size
= window_
->delegate()->GetMaximumSize();
153 return !max_size
.width() && !max_size
.height();
156 bool WindowState::CanMinimize() const {
157 return window()->GetProperty(aura::client::kCanMinimizeKey
);
160 bool WindowState::CanResize() const {
161 return window_
->GetProperty(aura::client::kCanResizeKey
);
164 bool WindowState::CanActivate() const {
165 return ::wm::CanActivateWindow(window_
);
168 bool WindowState::CanSnap() const {
169 if (!CanResize() || window_
->type() == ui::wm::WINDOW_TYPE_PANEL
||
170 ::wm::GetTransientParent(window_
))
172 // If a window cannot be maximized, assume it cannot snap either.
173 // TODO(oshima): We should probably snap if the maximum size is greater than
175 return CanMaximize();
178 bool WindowState::HasRestoreBounds() const {
179 return window_
->GetProperty(aura::client::kRestoreBoundsKey
) != NULL
;
182 void WindowState::Maximize() {
183 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
186 void WindowState::Minimize() {
187 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
190 void WindowState::Unminimize() {
191 window_
->SetProperty(
192 aura::client::kShowStateKey
,
193 window_
->GetProperty(aura::client::kRestoreShowStateKey
));
194 window_
->ClearProperty(aura::client::kRestoreShowStateKey
);
197 void WindowState::Activate() {
198 ActivateWindow(window_
);
201 void WindowState::Deactivate() {
202 DeactivateWindow(window_
);
205 void WindowState::Restore() {
206 if (!IsNormalStateType()) {
207 const WMEvent
event(WM_EVENT_NORMAL
);
212 void WindowState::OnWMEvent(const WMEvent
* event
) {
213 current_state_
->OnWMEvent(this, event
);
216 void WindowState::SaveCurrentBoundsForRestore() {
217 gfx::Rect bounds_in_screen
=
218 ScreenUtil::ConvertRectToScreen(window_
->parent(),
220 SetRestoreBoundsInScreen(bounds_in_screen
);
223 gfx::Rect
WindowState::GetRestoreBoundsInScreen() const {
224 return *window_
->GetProperty(aura::client::kRestoreBoundsKey
);
227 gfx::Rect
WindowState::GetRestoreBoundsInParent() const {
228 return ScreenUtil::ConvertRectFromScreen(window_
->parent(),
229 GetRestoreBoundsInScreen());
232 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect
& bounds
) {
233 window_
->SetProperty(aura::client::kRestoreBoundsKey
, new gfx::Rect(bounds
));
236 void WindowState::SetRestoreBoundsInParent(const gfx::Rect
& bounds
) {
237 SetRestoreBoundsInScreen(
238 ScreenUtil::ConvertRectToScreen(window_
->parent(), bounds
));
241 void WindowState::ClearRestoreBounds() {
242 window_
->ClearProperty(aura::client::kRestoreBoundsKey
);
245 scoped_ptr
<WindowState::State
> WindowState::SetStateObject(
246 scoped_ptr
<WindowState::State
> new_state
) {
247 current_state_
->DetachState(this);
248 scoped_ptr
<WindowState::State
> old_object
= current_state_
.Pass();
249 current_state_
= new_state
.Pass();
250 current_state_
->AttachState(this, old_object
.get());
251 return old_object
.Pass();
254 void WindowState::SetPreAutoManageWindowBounds(
255 const gfx::Rect
& bounds
) {
256 pre_auto_manage_window_bounds_
.reset(new gfx::Rect(bounds
));
259 void WindowState::AddObserver(WindowStateObserver
* observer
) {
260 observer_list_
.AddObserver(observer
);
263 void WindowState::RemoveObserver(WindowStateObserver
* observer
) {
264 observer_list_
.RemoveObserver(observer
);
267 void WindowState::set_bounds_changed_by_user(bool bounds_changed_by_user
) {
268 bounds_changed_by_user_
= bounds_changed_by_user
;
269 if (bounds_changed_by_user
)
270 pre_auto_manage_window_bounds_
.reset();
273 void WindowState::CreateDragDetails(aura::Window
* window
,
274 const gfx::Point
& point_in_parent
,
275 int window_component
,
276 aura::client::WindowMoveSource source
) {
278 new DragDetails(window
, point_in_parent
, window_component
, source
));
281 void WindowState::DeleteDragDetails() {
282 drag_details_
.reset();
285 void WindowState::SetAndClearRestoreBounds() {
286 DCHECK(HasRestoreBounds());
287 SetBoundsInScreen(GetRestoreBoundsInScreen());
288 ClearRestoreBounds();
291 void WindowState::OnWindowPropertyChanged(aura::Window
* window
,
294 DCHECK_EQ(window
, window_
);
295 if (key
== aura::client::kShowStateKey
&& !ignore_property_change_
) {
296 WMEvent
event(WMEventTypeFromShowState(GetShowState()));
301 WindowState::WindowState(aura::Window
* window
)
303 window_position_managed_(false),
304 bounds_changed_by_user_(false),
305 panel_attached_(true),
306 ignored_by_shelf_(false),
307 can_consume_system_keys_(false),
308 top_row_keys_are_function_keys_(false),
309 unminimize_to_restore_bounds_(false),
310 in_immersive_fullscreen_(false),
311 hide_shelf_when_fullscreen_(true),
312 minimum_visibility_(false),
313 can_be_dragged_(true),
314 ignore_property_change_(false),
315 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) {
316 window_
->AddObserver(this);
319 ui::WindowShowState
WindowState::GetShowState() const {
320 return window_
->GetProperty(aura::client::kShowStateKey
);
323 void WindowState::SetBoundsInScreen(
324 const gfx::Rect
& bounds_in_screen
) {
325 gfx::Rect bounds_in_parent
=
326 ScreenUtil::ConvertRectFromScreen(window_
->parent(),
328 window_
->SetBounds(bounds_in_parent
);
331 void WindowState::AdjustSnappedBounds(gfx::Rect
* bounds
) {
332 if (is_dragged() || !IsSnapped())
334 gfx::Rect maximized_bounds
= ScreenUtil::GetMaximizedWindowBoundsInParent(
336 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED
)
337 bounds
->set_x(maximized_bounds
.x());
338 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED
)
339 bounds
->set_x(maximized_bounds
.right() - bounds
->width());
340 bounds
->set_y(maximized_bounds
.y());
341 bounds
->set_height(maximized_bounds
.height());
344 void WindowState::UpdateWindowShowStateFromStateType() {
345 ui::WindowShowState new_window_state
=
346 ToWindowShowState(current_state_
->GetType());
347 if (new_window_state
!= GetShowState()) {
348 base::AutoReset
<bool> resetter(&ignore_property_change_
, true);
349 window_
->SetProperty(aura::client::kShowStateKey
, new_window_state
);
353 void WindowState::NotifyPreStateTypeChange(
354 WindowStateType old_window_state_type
) {
355 FOR_EACH_OBSERVER(WindowStateObserver
, observer_list_
,
356 OnPreWindowStateTypeChange(this, old_window_state_type
));
359 void WindowState::NotifyPostStateTypeChange(
360 WindowStateType old_window_state_type
) {
361 FOR_EACH_OBSERVER(WindowStateObserver
, observer_list_
,
362 OnPostWindowStateTypeChange(this, old_window_state_type
));
365 void WindowState::SetBoundsDirect(const gfx::Rect
& bounds
) {
366 gfx::Rect
actual_new_bounds(bounds
);
367 // Ensure we don't go smaller than our minimum bounds in "normal" window
369 if (window_
->delegate() && !IsMaximized() && !IsFullscreen()) {
370 // Get the minimum usable size of the minimum size and the screen size.
371 gfx::Size min_size
= window_
->delegate()->GetMinimumSize();
372 min_size
.SetToMin(gfx::Screen::GetScreenFor(
373 window_
)->GetDisplayNearestWindow(window_
).work_area().size());
375 actual_new_bounds
.set_width(
376 std::max(min_size
.width(), actual_new_bounds
.width()));
377 actual_new_bounds
.set_height(
378 std::max(min_size
.height(), actual_new_bounds
.height()));
380 BoundsSetter().SetBounds(window_
, actual_new_bounds
);
381 SnapWindowToPixelBoundary(window_
);
384 void WindowState::SetBoundsConstrained(const gfx::Rect
& bounds
) {
385 gfx::Rect work_area_in_parent
=
386 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_
);
387 gfx::Rect
child_bounds(bounds
);
388 AdjustBoundsSmallerThan(work_area_in_parent
.size(), &child_bounds
);
389 SetBoundsDirect(child_bounds
);
392 void WindowState::SetBoundsDirectAnimated(const gfx::Rect
& bounds
) {
393 const int kBoundsChangeSlideDurationMs
= 120;
395 ui::Layer
* layer
= window_
->layer();
396 ui::ScopedLayerAnimationSettings
slide_settings(layer
->GetAnimator());
397 slide_settings
.SetPreemptionStrategy(
398 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
399 slide_settings
.SetTransitionDuration(
400 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs
));
401 SetBoundsDirect(bounds
);
404 void WindowState::SetBoundsDirectCrossFade(const gfx::Rect
& new_bounds
) {
405 // Some test results in invoking CrossFadeToBounds when window is not visible.
406 // No animation is necessary in that case, thus just change the bounds and
408 if (!window_
->TargetVisibility()) {
409 SetBoundsConstrained(new_bounds
);
413 const gfx::Rect old_bounds
= window_
->bounds();
415 // Create fresh layers for the window and all its children to paint into.
416 // Takes ownership of the old layer and all its children, which will be
417 // cleaned up after the animation completes.
418 // Specify |set_bounds| to true here to keep the old bounds in the child
419 // windows of |window|.
420 scoped_ptr
<ui::LayerTreeOwner
> old_layer_owner
=
421 ::wm::RecreateLayers(window_
);
422 ui::Layer
* old_layer
= old_layer_owner
->root();
424 ui::Layer
* new_layer
= window_
->layer();
426 // Resize the window to the new size, which will force a layout and paint.
427 SetBoundsDirect(new_bounds
);
429 // Ensure the higher-resolution layer is on top.
430 bool old_on_top
= (old_bounds
.width() > new_bounds
.width());
432 old_layer
->parent()->StackBelow(new_layer
, old_layer
);
434 old_layer
->parent()->StackAbove(new_layer
, old_layer
);
436 CrossFadeAnimation(window_
, old_layer_owner
.Pass(), gfx::Tween::EASE_OUT
);
439 WindowState
* GetActiveWindowState() {
440 aura::Window
* active
= GetActiveWindow();
441 return active
? GetWindowState(active
) : NULL
;
444 WindowState
* GetWindowState(aura::Window
* window
) {
447 WindowState
* settings
= window
->GetProperty(kWindowStateKey
);
449 settings
= new WindowState(window
);
450 window
->SetProperty(kWindowStateKey
, settings
);
455 const WindowState
* GetWindowState(const aura::Window
* window
) {
456 return GetWindowState(const_cast<aura::Window
*>(window
));