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 virtual ~BoundsSetter() {}
43 // aura::LayoutManager overrides:
44 virtual void OnWindowResized() OVERRIDE
{}
45 virtual void OnWindowAddedToLayout(aura::Window
* child
) OVERRIDE
{}
46 virtual void OnWillRemoveWindowFromLayout(aura::Window
* child
) OVERRIDE
{}
47 virtual void OnWindowRemovedFromLayout(aura::Window
* child
) OVERRIDE
{}
48 virtual void OnChildWindowVisibilityChanged(
49 aura::Window
* child
, bool visible
) OVERRIDE
{}
50 virtual void SetChildBounds(
51 aura::Window
* child
, 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
;
107 bool WindowState::IsMaximized() const {
108 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED
;
111 bool WindowState::IsFullscreen() const {
112 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN
;
115 bool WindowState::IsMaximizedOrFullscreen() const {
116 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN
||
117 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED
;
120 bool WindowState::IsSnapped() const {
121 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED
||
122 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED
;
125 bool WindowState::IsNormalStateType() const {
126 return GetStateType() == WINDOW_STATE_TYPE_NORMAL
||
127 GetStateType() == WINDOW_STATE_TYPE_DEFAULT
;
130 bool WindowState::IsNormalOrSnapped() const {
131 return IsNormalStateType() || IsSnapped();
134 bool WindowState::IsActive() const {
135 return IsActiveWindow(window_
);
138 bool WindowState::IsDocked() const {
139 return window_
->parent() &&
140 window_
->parent()->id() == kShellWindowId_DockedContainer
;
143 bool WindowState::CanMaximize() const {
144 return window_
->GetProperty(aura::client::kCanMaximizeKey
);
147 bool WindowState::CanMinimize() const {
148 RootWindowController
* controller
= RootWindowController::ForWindow(window_
);
151 aura::Window
* lockscreen
=
152 controller
->GetContainer(kShellWindowId_LockScreenContainersContainer
);
153 if (lockscreen
->Contains(window_
))
159 bool WindowState::CanResize() const {
160 return window_
->GetProperty(aura::client::kCanResizeKey
);
163 bool WindowState::CanActivate() const {
164 return ::wm::CanActivateWindow(window_
);
167 bool WindowState::CanSnap() const {
168 if (!CanResize() || window_
->type() == ui::wm::WINDOW_TYPE_PANEL
||
169 ::wm::GetTransientParent(window_
))
171 // If a window has a maximum size defined, snapping may make it too big.
172 // TODO(oshima): We probably should snap if possible.
173 return window_
->delegate() ? window_
->delegate()->GetMaximumSize().IsEmpty() :
177 bool WindowState::HasRestoreBounds() const {
178 return window_
->GetProperty(aura::client::kRestoreBoundsKey
) != NULL
;
181 void WindowState::Maximize() {
182 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MAXIMIZED
);
185 void WindowState::Minimize() {
186 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_MINIMIZED
);
189 void WindowState::Unminimize() {
190 window_
->SetProperty(
191 aura::client::kShowStateKey
,
192 window_
->GetProperty(aura::client::kRestoreShowStateKey
));
193 window_
->ClearProperty(aura::client::kRestoreShowStateKey
);
196 void WindowState::Activate() {
197 ActivateWindow(window_
);
200 void WindowState::Deactivate() {
201 DeactivateWindow(window_
);
204 void WindowState::Restore() {
205 if (!IsNormalStateType()) {
206 const WMEvent
event(WM_EVENT_NORMAL
);
211 void WindowState::OnWMEvent(const WMEvent
* event
) {
212 current_state_
->OnWMEvent(this, event
);
215 void WindowState::SaveCurrentBoundsForRestore() {
216 gfx::Rect bounds_in_screen
=
217 ScreenUtil::ConvertRectToScreen(window_
->parent(),
219 SetRestoreBoundsInScreen(bounds_in_screen
);
222 gfx::Rect
WindowState::GetRestoreBoundsInScreen() const {
223 return *window_
->GetProperty(aura::client::kRestoreBoundsKey
);
226 gfx::Rect
WindowState::GetRestoreBoundsInParent() const {
227 return ScreenUtil::ConvertRectFromScreen(window_
->parent(),
228 GetRestoreBoundsInScreen());
231 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect
& bounds
) {
232 window_
->SetProperty(aura::client::kRestoreBoundsKey
, new gfx::Rect(bounds
));
235 void WindowState::SetRestoreBoundsInParent(const gfx::Rect
& bounds
) {
236 SetRestoreBoundsInScreen(
237 ScreenUtil::ConvertRectToScreen(window_
->parent(), bounds
));
240 void WindowState::ClearRestoreBounds() {
241 window_
->ClearProperty(aura::client::kRestoreBoundsKey
);
244 scoped_ptr
<WindowState::State
> WindowState::SetStateObject(
245 scoped_ptr
<WindowState::State
> new_state
) {
246 current_state_
->DetachState(this);
247 scoped_ptr
<WindowState::State
> old_object
= current_state_
.Pass();
248 current_state_
= new_state
.Pass();
249 current_state_
->AttachState(this, old_object
.get());
250 return old_object
.Pass();
253 void WindowState::SetPreAutoManageWindowBounds(
254 const gfx::Rect
& bounds
) {
255 pre_auto_manage_window_bounds_
.reset(new gfx::Rect(bounds
));
258 void WindowState::AddObserver(WindowStateObserver
* observer
) {
259 observer_list_
.AddObserver(observer
);
262 void WindowState::RemoveObserver(WindowStateObserver
* observer
) {
263 observer_list_
.RemoveObserver(observer
);
266 void WindowState::set_bounds_changed_by_user(bool bounds_changed_by_user
) {
267 bounds_changed_by_user_
= bounds_changed_by_user
;
268 if (bounds_changed_by_user
)
269 pre_auto_manage_window_bounds_
.reset();
272 void WindowState::CreateDragDetails(aura::Window
* window
,
273 const gfx::Point
& point_in_parent
,
274 int window_component
,
275 aura::client::WindowMoveSource source
) {
277 new DragDetails(window
, point_in_parent
, window_component
, source
));
280 void WindowState::DeleteDragDetails() {
281 drag_details_
.reset();
284 void WindowState::SetAndClearRestoreBounds() {
285 DCHECK(HasRestoreBounds());
286 SetBoundsInScreen(GetRestoreBoundsInScreen());
287 ClearRestoreBounds();
290 void WindowState::OnWindowPropertyChanged(aura::Window
* window
,
293 DCHECK_EQ(window
, window_
);
294 if (key
== aura::client::kShowStateKey
&& !ignore_property_change_
) {
295 WMEvent
event(WMEventTypeFromShowState(GetShowState()));
300 WindowState::WindowState(aura::Window
* window
)
302 window_position_managed_(false),
303 bounds_changed_by_user_(false),
304 panel_attached_(true),
305 ignored_by_shelf_(false),
306 can_consume_system_keys_(false),
307 top_row_keys_are_function_keys_(false),
308 unminimize_to_restore_bounds_(false),
309 in_immersive_fullscreen_(false),
310 hide_shelf_when_fullscreen_(true),
311 minimum_visibility_(false),
312 can_be_dragged_(true),
313 ignore_property_change_(false),
314 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) {
315 window_
->AddObserver(this);
318 ui::WindowShowState
WindowState::GetShowState() const {
319 return window_
->GetProperty(aura::client::kShowStateKey
);
322 void WindowState::SetBoundsInScreen(
323 const gfx::Rect
& bounds_in_screen
) {
324 gfx::Rect bounds_in_parent
=
325 ScreenUtil::ConvertRectFromScreen(window_
->parent(),
327 window_
->SetBounds(bounds_in_parent
);
330 void WindowState::AdjustSnappedBounds(gfx::Rect
* bounds
) {
331 if (is_dragged() || !IsSnapped())
333 gfx::Rect maximized_bounds
= ScreenUtil::GetMaximizedWindowBoundsInParent(
335 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED
)
336 bounds
->set_x(maximized_bounds
.x());
337 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED
)
338 bounds
->set_x(maximized_bounds
.right() - bounds
->width());
339 bounds
->set_y(maximized_bounds
.y());
340 bounds
->set_height(maximized_bounds
.height());
343 void WindowState::UpdateWindowShowStateFromStateType() {
344 ui::WindowShowState new_window_state
=
345 ToWindowShowState(current_state_
->GetType());
346 if (new_window_state
!= GetShowState()) {
347 base::AutoReset
<bool> resetter(&ignore_property_change_
, true);
348 window_
->SetProperty(aura::client::kShowStateKey
, new_window_state
);
352 void WindowState::NotifyPreStateTypeChange(
353 WindowStateType old_window_state_type
) {
354 FOR_EACH_OBSERVER(WindowStateObserver
, observer_list_
,
355 OnPreWindowStateTypeChange(this, old_window_state_type
));
358 void WindowState::NotifyPostStateTypeChange(
359 WindowStateType old_window_state_type
) {
360 FOR_EACH_OBSERVER(WindowStateObserver
, observer_list_
,
361 OnPostWindowStateTypeChange(this, old_window_state_type
));
364 void WindowState::SetBoundsDirect(const gfx::Rect
& bounds
) {
365 gfx::Rect
actual_new_bounds(bounds
);
366 // Ensure we don't go smaller than our minimum bounds in "normal" window
368 if (window_
->delegate() && !IsMaximized() && !IsFullscreen()) {
369 // Get the minimum usable size of the minimum size and the screen size.
370 gfx::Size min_size
= window_
->delegate()->GetMinimumSize();
371 min_size
.SetToMin(gfx::Screen::GetScreenFor(
372 window_
)->GetDisplayNearestWindow(window_
).work_area().size());
374 actual_new_bounds
.set_width(
375 std::max(min_size
.width(), actual_new_bounds
.width()));
376 actual_new_bounds
.set_height(
377 std::max(min_size
.height(), actual_new_bounds
.height()));
379 BoundsSetter().SetBounds(window_
, actual_new_bounds
);
380 SnapWindowToPixelBoundary(window_
);
383 void WindowState::SetBoundsConstrained(const gfx::Rect
& bounds
) {
384 gfx::Rect work_area_in_parent
=
385 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_
);
386 gfx::Rect
child_bounds(bounds
);
387 AdjustBoundsSmallerThan(work_area_in_parent
.size(), &child_bounds
);
388 SetBoundsDirect(child_bounds
);
391 void WindowState::SetBoundsDirectAnimated(const gfx::Rect
& bounds
) {
392 const int kBoundsChangeSlideDurationMs
= 120;
394 ui::Layer
* layer
= window_
->layer();
395 ui::ScopedLayerAnimationSettings
slide_settings(layer
->GetAnimator());
396 slide_settings
.SetPreemptionStrategy(
397 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET
);
398 slide_settings
.SetTransitionDuration(
399 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs
));
400 SetBoundsDirect(bounds
);
403 void WindowState::SetBoundsDirectCrossFade(const gfx::Rect
& new_bounds
) {
404 // Some test results in invoking CrossFadeToBounds when window is not visible.
405 // No animation is necessary in that case, thus just change the bounds and
407 if (!window_
->TargetVisibility()) {
408 SetBoundsConstrained(new_bounds
);
412 const gfx::Rect old_bounds
= window_
->bounds();
414 // Create fresh layers for the window and all its children to paint into.
415 // Takes ownership of the old layer and all its children, which will be
416 // cleaned up after the animation completes.
417 // Specify |set_bounds| to true here to keep the old bounds in the child
418 // windows of |window|.
419 scoped_ptr
<ui::LayerTreeOwner
> old_layer_owner
=
420 ::wm::RecreateLayers(window_
);
421 ui::Layer
* old_layer
= old_layer_owner
->root();
423 ui::Layer
* new_layer
= window_
->layer();
425 // Resize the window to the new size, which will force a layout and paint.
426 SetBoundsDirect(new_bounds
);
428 // Ensure the higher-resolution layer is on top.
429 bool old_on_top
= (old_bounds
.width() > new_bounds
.width());
431 old_layer
->parent()->StackBelow(new_layer
, old_layer
);
433 old_layer
->parent()->StackAbove(new_layer
, old_layer
);
435 CrossFadeAnimation(window_
, old_layer_owner
.Pass(), gfx::Tween::EASE_OUT
);
438 WindowState
* GetActiveWindowState() {
439 aura::Window
* active
= GetActiveWindow();
440 return active
? GetWindowState(active
) : NULL
;
443 WindowState
* GetWindowState(aura::Window
* window
) {
446 WindowState
* settings
= window
->GetProperty(kWindowStateKey
);
448 settings
= new WindowState(window
);
449 window
->SetProperty(kWindowStateKey
, settings
);
454 const WindowState
* GetWindowState(const aura::Window
* window
) {
455 return GetWindowState(const_cast<aura::Window
*>(window
));