Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / ash / wm / window_state.cc
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
30 namespace ash {
31 namespace wm {
32
33 namespace {
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.
38 class 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
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_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
84 WindowState::~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
91 bool WindowState::HasDelegate() const {
92   return delegate_;
93 }
94
95 void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) {
96   DCHECK(!delegate_.get());
97   delegate_ = delegate.Pass();
98 }
99
100 WindowStateType WindowState::GetStateType() const {
101   return current_state_->GetType();
102 }
103
104 bool WindowState::IsMinimized() const {
105   return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED;
106 }
107
108 bool WindowState::IsMaximized() const {
109   return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
110 }
111
112 bool WindowState::IsFullscreen() const {
113   return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN;
114 }
115
116 bool WindowState::IsMaximizedOrFullscreen() const {
117   return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN ||
118       GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
119 }
120
121 bool WindowState::IsSnapped() const {
122   return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED ||
123       GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED;
124 }
125
126 bool WindowState::IsNormalStateType() const {
127   return GetStateType() == WINDOW_STATE_TYPE_NORMAL ||
128       GetStateType() == WINDOW_STATE_TYPE_DEFAULT;
129 }
130
131 bool WindowState::IsNormalOrSnapped() const {
132   return IsNormalStateType() || IsSnapped();
133 }
134
135 bool WindowState::IsActive() const {
136   return IsActiveWindow(window_);
137 }
138
139 bool WindowState::IsDocked() const {
140   return window_->parent() &&
141          window_->parent()->id() == kShellWindowId_DockedContainer;
142 }
143
144 bool WindowState::CanMaximize() const {
145   return window_->GetProperty(aura::client::kCanMaximizeKey);
146 }
147
148 bool 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
160 bool WindowState::CanResize() const {
161   return window_->GetProperty(aura::client::kCanResizeKey);
162 }
163
164 bool WindowState::CanActivate() const {
165   return ::wm::CanActivateWindow(window_);
166 }
167
168 bool 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
178 bool WindowState::HasRestoreBounds() const {
179   return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL;
180 }
181
182 void WindowState::Maximize() {
183   window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
184 }
185
186 void WindowState::Minimize() {
187   window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
188 }
189
190 void WindowState::Unminimize() {
191   window_->SetProperty(
192       aura::client::kShowStateKey,
193       window_->GetProperty(aura::client::kRestoreShowStateKey));
194   window_->ClearProperty(aura::client::kRestoreShowStateKey);
195 }
196
197 void WindowState::Activate() {
198   ActivateWindow(window_);
199 }
200
201 void WindowState::Deactivate() {
202   DeactivateWindow(window_);
203 }
204
205 void WindowState::Restore() {
206   if (!IsNormalStateType()) {
207     const WMEvent event(WM_EVENT_NORMAL);
208     OnWMEvent(&event);
209   }
210 }
211
212 void WindowState::OnWMEvent(const WMEvent* event) {
213   current_state_->OnWMEvent(this, event);
214 }
215
216 void WindowState::SaveCurrentBoundsForRestore() {
217   gfx::Rect bounds_in_screen =
218       ScreenUtil::ConvertRectToScreen(window_->parent(),
219                                       window_->bounds());
220   SetRestoreBoundsInScreen(bounds_in_screen);
221 }
222
223 gfx::Rect WindowState::GetRestoreBoundsInScreen() const {
224   return *window_->GetProperty(aura::client::kRestoreBoundsKey);
225 }
226
227 gfx::Rect WindowState::GetRestoreBoundsInParent() const {
228   return ScreenUtil::ConvertRectFromScreen(window_->parent(),
229                                           GetRestoreBoundsInScreen());
230 }
231
232 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) {
233   window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
234 }
235
236 void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) {
237   SetRestoreBoundsInScreen(
238       ScreenUtil::ConvertRectToScreen(window_->parent(), bounds));
239 }
240
241 void WindowState::ClearRestoreBounds() {
242   window_->ClearProperty(aura::client::kRestoreBoundsKey);
243 }
244
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();
252 }
253
254 void WindowState::SetPreAutoManageWindowBounds(
255     const gfx::Rect& bounds) {
256   pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds));
257 }
258
259 void WindowState::AddObserver(WindowStateObserver* observer) {
260   observer_list_.AddObserver(observer);
261 }
262
263 void WindowState::RemoveObserver(WindowStateObserver* observer) {
264   observer_list_.RemoveObserver(observer);
265 }
266
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();
271 }
272
273 void 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
281 void WindowState::DeleteDragDetails() {
282   drag_details_.reset();
283 }
284
285 void WindowState::SetAndClearRestoreBounds() {
286   DCHECK(HasRestoreBounds());
287   SetBoundsInScreen(GetRestoreBoundsInScreen());
288   ClearRestoreBounds();
289 }
290
291 void 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
301 WindowState::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
319 ui::WindowShowState WindowState::GetShowState() const {
320   return window_->GetProperty(aura::client::kShowStateKey);
321 }
322
323 void 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
331 void 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
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);
350   }
351 }
352
353 void WindowState::NotifyPreStateTypeChange(
354     WindowStateType old_window_state_type) {
355   FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
356                     OnPreWindowStateTypeChange(this, old_window_state_type));
357 }
358
359 void WindowState::NotifyPostStateTypeChange(
360     WindowStateType old_window_state_type) {
361   FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
362                     OnPostWindowStateTypeChange(this, old_window_state_type));
363 }
364
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
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   SnapWindowToPixelBoundary(window_);
382 }
383
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);
390 }
391
392 void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) {
393   const int kBoundsChangeSlideDurationMs = 120;
394
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);
402 }
403
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
407   // quit.
408   if (!window_->TargetVisibility()) {
409     SetBoundsConstrained(new_bounds);
410     return;
411   }
412
413   const gfx::Rect old_bounds = window_->bounds();
414
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();
423   DCHECK(old_layer);
424   ui::Layer* new_layer = window_->layer();
425
426   // Resize the window to the new size, which will force a layout and paint.
427   SetBoundsDirect(new_bounds);
428
429   // Ensure the higher-resolution layer is on top.
430   bool old_on_top = (old_bounds.width() > new_bounds.width());
431   if (old_on_top)
432     old_layer->parent()->StackBelow(new_layer, old_layer);
433   else
434     old_layer->parent()->StackAbove(new_layer, old_layer);
435
436   CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT);
437 }
438
439 WindowState* GetActiveWindowState() {
440   aura::Window* active = GetActiveWindow();
441   return active ? GetWindowState(active) : NULL;
442 }
443
444 WindowState* GetWindowState(aura::Window* window) {
445   if (!window)
446     return NULL;
447   WindowState* settings = window->GetProperty(kWindowStateKey);
448   if(!settings) {
449     settings = new WindowState(window);
450     window->SetProperty(kWindowStateKey, settings);
451   }
452   return settings;
453 }
454
455 const WindowState* GetWindowState(const aura::Window* window) {
456   return GetWindowState(const_cast<aura::Window*>(window));
457 }
458
459 }  // namespace wm
460 }  // namespace ash