window_state.cc revision f8ee788a64d60abd8f2d742a5fdedde054ecd910
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.
4
5#include "ash/wm/window_state.h"
6
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"
29
30namespace ash {
31namespace wm {
32
33namespace {
34
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.
38class BoundsSetter : public aura::LayoutManager {
39 public:
40  BoundsSetter() {}
41  virtual ~BoundsSetter() {}
42
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 {}
52
53  void SetBounds(aura::Window* window, const gfx::Rect& bounds) {
54    SetChildBoundsDirect(window, bounds);
55  }
56
57 private:
58  DISALLOW_COPY_AND_ASSIGN(BoundsSetter);
59};
60
61WMEventType 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_DETACHED:
75    case ui::SHOW_STATE_END:
76      NOTREACHED() << "No WMEvent defined for the show state:"
77                   << requested_show_state;
78  }
79  return WM_EVENT_NORMAL;
80}
81
82}  // namespace
83
84WindowState::~WindowState() {
85  // WindowState is registered as an owned property of |window_|, and window
86  // unregisters all of its observers in its d'tor before destroying its
87  // properties. As a result, window_->RemoveObserver() doesn't need to (and
88  // shouldn't) be called here.
89}
90
91bool WindowState::HasDelegate() const {
92  return delegate_;
93}
94
95void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) {
96  DCHECK(!delegate_.get());
97  delegate_ = delegate.Pass();
98}
99
100WindowStateType WindowState::GetStateType() const {
101  return current_state_->GetType();
102}
103
104bool WindowState::IsMinimized() const {
105  return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED;
106}
107
108bool WindowState::IsMaximized() const {
109  return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
110}
111
112bool WindowState::IsFullscreen() const {
113  return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN;
114}
115
116bool WindowState::IsMaximizedOrFullscreen() const {
117  return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN ||
118      GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
119}
120
121bool WindowState::IsSnapped() const {
122  return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED ||
123      GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED;
124}
125
126bool WindowState::IsNormalStateType() const {
127  return GetStateType() == WINDOW_STATE_TYPE_NORMAL ||
128      GetStateType() == WINDOW_STATE_TYPE_DEFAULT;
129}
130
131bool WindowState::IsNormalOrSnapped() const {
132  return IsNormalStateType() || IsSnapped();
133}
134
135bool WindowState::IsActive() const {
136  return IsActiveWindow(window_);
137}
138
139bool WindowState::IsDocked() const {
140  return window_->parent() &&
141         window_->parent()->id() == kShellWindowId_DockedContainer;
142}
143
144bool WindowState::CanMaximize() const {
145  return window_->GetProperty(aura::client::kCanMaximizeKey);
146}
147
148bool WindowState::CanMinimize() const {
149  RootWindowController* controller = RootWindowController::ForWindow(window_);
150  if (!controller)
151    return false;
152  aura::Window* lockscreen =
153      controller->GetContainer(kShellWindowId_LockScreenContainersContainer);
154  if (lockscreen->Contains(window_))
155    return false;
156
157  return true;
158}
159
160bool WindowState::CanResize() const {
161  return window_->GetProperty(aura::client::kCanResizeKey);
162}
163
164bool WindowState::CanActivate() const {
165  return ::wm::CanActivateWindow(window_);
166}
167
168bool WindowState::CanSnap() const {
169  if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL ||
170      ::wm::GetTransientParent(window_))
171    return false;
172  // If a window has a maximum size defined, snapping may make it too big.
173  // TODO(oshima): We probably should snap if possible.
174  return window_->delegate() ? window_->delegate()->GetMaximumSize().IsEmpty() :
175                              true;
176}
177
178bool WindowState::HasRestoreBounds() const {
179  return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL;
180}
181
182void WindowState::Maximize() {
183  window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
184}
185
186void WindowState::Minimize() {
187  window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
188}
189
190void WindowState::Unminimize() {
191  window_->SetProperty(
192      aura::client::kShowStateKey,
193      window_->GetProperty(aura::client::kRestoreShowStateKey));
194  window_->ClearProperty(aura::client::kRestoreShowStateKey);
195}
196
197void WindowState::Activate() {
198  ActivateWindow(window_);
199}
200
201void WindowState::Deactivate() {
202  DeactivateWindow(window_);
203}
204
205void WindowState::Restore() {
206  if (!IsNormalStateType()) {
207    const WMEvent event(WM_EVENT_NORMAL);
208    OnWMEvent(&event);
209  }
210}
211
212void WindowState::OnWMEvent(const WMEvent* event) {
213  current_state_->OnWMEvent(this, event);
214}
215
216void WindowState::SaveCurrentBoundsForRestore() {
217  gfx::Rect bounds_in_screen =
218      ScreenUtil::ConvertRectToScreen(window_->parent(),
219                                      window_->bounds());
220  SetRestoreBoundsInScreen(bounds_in_screen);
221}
222
223gfx::Rect WindowState::GetRestoreBoundsInScreen() const {
224  return *window_->GetProperty(aura::client::kRestoreBoundsKey);
225}
226
227gfx::Rect WindowState::GetRestoreBoundsInParent() const {
228  return ScreenUtil::ConvertRectFromScreen(window_->parent(),
229                                          GetRestoreBoundsInScreen());
230}
231
232void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) {
233  window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
234}
235
236void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) {
237  SetRestoreBoundsInScreen(
238      ScreenUtil::ConvertRectToScreen(window_->parent(), bounds));
239}
240
241void WindowState::ClearRestoreBounds() {
242  window_->ClearProperty(aura::client::kRestoreBoundsKey);
243}
244
245scoped_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();
252}
253
254void WindowState::SetPreAutoManageWindowBounds(
255    const gfx::Rect& bounds) {
256  pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds));
257}
258
259void WindowState::AddObserver(WindowStateObserver* observer) {
260  observer_list_.AddObserver(observer);
261}
262
263void WindowState::RemoveObserver(WindowStateObserver* observer) {
264  observer_list_.RemoveObserver(observer);
265}
266
267void 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();
271}
272
273void WindowState::CreateDragDetails(aura::Window* window,
274                                    const gfx::Point& point_in_parent,
275                                    int window_component,
276                                    aura::client::WindowMoveSource source) {
277  drag_details_.reset(
278      new DragDetails(window, point_in_parent, window_component, source));
279}
280
281void WindowState::DeleteDragDetails() {
282  drag_details_.reset();
283}
284
285void WindowState::SetAndClearRestoreBounds() {
286  DCHECK(HasRestoreBounds());
287  SetBoundsInScreen(GetRestoreBoundsInScreen());
288  ClearRestoreBounds();
289}
290
291void WindowState::OnWindowPropertyChanged(aura::Window* window,
292                                          const void* key,
293                                          intptr_t old) {
294  DCHECK_EQ(window, window_);
295  if (key == aura::client::kShowStateKey && !ignore_property_change_) {
296    WMEvent event(WMEventTypeFromShowState(GetShowState()));
297    OnWMEvent(&event);
298  }
299}
300
301WindowState::WindowState(aura::Window* window)
302    : 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);
317}
318
319ui::WindowShowState WindowState::GetShowState() const {
320  return window_->GetProperty(aura::client::kShowStateKey);
321}
322
323void WindowState::SetBoundsInScreen(
324    const gfx::Rect& bounds_in_screen) {
325  gfx::Rect bounds_in_parent =
326      ScreenUtil::ConvertRectFromScreen(window_->parent(),
327                                       bounds_in_screen);
328  window_->SetBounds(bounds_in_parent);
329}
330
331void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) {
332  if (is_dragged() || !IsSnapped())
333    return;
334  gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent(
335      window_);
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());
342}
343
344void 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);
350  }
351}
352
353void WindowState::NotifyPreStateTypeChange(
354    WindowStateType old_window_state_type) {
355  FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
356                    OnPreWindowStateTypeChange(this, old_window_state_type));
357}
358
359void WindowState::NotifyPostStateTypeChange(
360    WindowStateType old_window_state_type) {
361  FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
362                    OnPostWindowStateTypeChange(this, old_window_state_type));
363}
364
365void 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
368  // modes
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());
374
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()));
379  }
380  BoundsSetter().SetBounds(window_, actual_new_bounds);
381}
382
383void 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);
389}
390
391void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) {
392  const int kBoundsChangeSlideDurationMs = 120;
393
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);
401}
402
403void 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
406  // quit.
407  if (!window_->TargetVisibility()) {
408    SetBoundsConstrained(new_bounds);
409    return;
410  }
411
412  const gfx::Rect old_bounds = window_->bounds();
413
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();
422  DCHECK(old_layer);
423  ui::Layer* new_layer = window_->layer();
424
425  // Resize the window to the new size, which will force a layout and paint.
426  SetBoundsDirect(new_bounds);
427
428  // Ensure the higher-resolution layer is on top.
429  bool old_on_top = (old_bounds.width() > new_bounds.width());
430  if (old_on_top)
431    old_layer->parent()->StackBelow(new_layer, old_layer);
432  else
433    old_layer->parent()->StackAbove(new_layer, old_layer);
434
435  CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT);
436}
437
438WindowState* GetActiveWindowState() {
439  aura::Window* active = GetActiveWindow();
440  return active ? GetWindowState(active) : NULL;
441}
442
443WindowState* GetWindowState(aura::Window* window) {
444  if (!window)
445    return NULL;
446  WindowState* settings = window->GetProperty(kWindowStateKey);
447  if(!settings) {
448    settings = new WindowState(window);
449    window->SetProperty(kWindowStateKey, settings);
450  }
451  return settings;
452}
453
454const WindowState* GetWindowState(const aura::Window* window) {
455  return GetWindowState(const_cast<aura::Window*>(window));
456}
457
458}  // namespace wm
459}  // namespace ash
460