window_state.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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/wm/core/window_util.h" 28 29namespace ash { 30namespace wm { 31 32namespace { 33 34// A tentative class to set the bounds on the window. 35// TODO(oshima): Once all logic is cleaned up, move this to the real layout 36// manager with proper friendship. 37class BoundsSetter : public aura::LayoutManager { 38 public: 39 BoundsSetter() {} 40 virtual ~BoundsSetter() {} 41 42 // aura::LayoutManager overrides: 43 virtual void OnWindowResized() OVERRIDE {} 44 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {} 45 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} 46 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} 47 virtual void OnChildWindowVisibilityChanged( 48 aura::Window* child, bool visible) OVERRIDE {} 49 virtual void SetChildBounds( 50 aura::Window* child, const gfx::Rect& requested_bounds) OVERRIDE {} 51 52 void SetBounds(aura::Window* window, const gfx::Rect& bounds) { 53 SetChildBoundsDirect(window, bounds); 54 } 55 56 private: 57 DISALLOW_COPY_AND_ASSIGN(BoundsSetter); 58}; 59 60WMEventType WMEventTypeFromShowState(ui::WindowShowState requested_show_state) { 61 switch (requested_show_state) { 62 case ui::SHOW_STATE_DEFAULT: 63 case ui::SHOW_STATE_NORMAL: 64 return WM_EVENT_NORMAL; 65 case ui::SHOW_STATE_MINIMIZED: 66 return WM_EVENT_MINIMIZE; 67 case ui::SHOW_STATE_MAXIMIZED: 68 return WM_EVENT_MAXIMIZE; 69 case ui::SHOW_STATE_FULLSCREEN: 70 return WM_EVENT_FULLSCREEN; 71 case ui::SHOW_STATE_INACTIVE: 72 return WM_EVENT_SHOW_INACTIVE; 73 case ui::SHOW_STATE_DETACHED: 74 case ui::SHOW_STATE_END: 75 NOTREACHED() << "No WMEvent defined for the show state:" 76 << requested_show_state; 77 } 78 return WM_EVENT_NORMAL; 79} 80 81} // namespace 82 83WindowState::WindowState(aura::Window* window) 84 : window_(window), 85 window_position_managed_(false), 86 bounds_changed_by_user_(false), 87 panel_attached_(true), 88 ignored_by_shelf_(false), 89 can_consume_system_keys_(false), 90 top_row_keys_are_function_keys_(false), 91 unminimize_to_restore_bounds_(false), 92 hide_shelf_when_fullscreen_(true), 93 minimum_visibility_(false), 94 can_be_dragged_(true), 95 ignore_property_change_(false), 96 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) { 97 window_->AddObserver(this); 98} 99 100WindowState::~WindowState() { 101} 102 103bool WindowState::HasDelegate() const { 104 return delegate_; 105} 106 107void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) { 108 DCHECK(!delegate_.get()); 109 delegate_ = delegate.Pass(); 110} 111 112WindowStateType WindowState::GetStateType() const { 113 return current_state_->GetType(); 114} 115 116bool WindowState::IsMinimized() const { 117 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED; 118} 119 120bool WindowState::IsMaximized() const { 121 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 122} 123 124bool WindowState::IsFullscreen() const { 125 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN; 126} 127 128bool WindowState::IsMaximizedOrFullscreen() const { 129 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN || 130 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 131} 132 133bool WindowState::IsSnapped() const { 134 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED || 135 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED; 136} 137 138bool WindowState::IsNormalStateType() const { 139 return GetStateType() == WINDOW_STATE_TYPE_NORMAL || 140 GetStateType() == WINDOW_STATE_TYPE_DEFAULT; 141} 142 143bool WindowState::IsNormalOrSnapped() const { 144 return IsNormalStateType() || IsSnapped(); 145} 146 147bool WindowState::IsActive() const { 148 return IsActiveWindow(window_); 149} 150 151bool WindowState::IsDocked() const { 152 return window_->parent() && 153 window_->parent()->id() == internal::kShellWindowId_DockedContainer; 154} 155 156bool WindowState::CanMaximize() const { 157 return window_->GetProperty(aura::client::kCanMaximizeKey); 158} 159 160bool WindowState::CanMinimize() const { 161 internal::RootWindowController* controller = 162 internal::RootWindowController::ForWindow(window_); 163 if (!controller) 164 return false; 165 aura::Window* lockscreen = controller->GetContainer( 166 internal::kShellWindowId_LockScreenContainersContainer); 167 if (lockscreen->Contains(window_)) 168 return false; 169 170 return true; 171} 172 173bool WindowState::CanResize() const { 174 return window_->GetProperty(aura::client::kCanResizeKey); 175} 176 177bool WindowState::CanActivate() const { 178 return ::wm::CanActivateWindow(window_); 179} 180 181bool WindowState::CanSnap() const { 182 if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL || 183 ::wm::GetTransientParent(window_)) 184 return false; 185 // If a window has a maximum size defined, snapping may make it too big. 186 // TODO(oshima): We probably should snap if possible. 187 return window_->delegate() ? window_->delegate()->GetMaximumSize().IsEmpty() : 188 true; 189} 190 191bool WindowState::HasRestoreBounds() const { 192 return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL; 193} 194 195void WindowState::Maximize() { 196 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED); 197} 198 199void WindowState::Minimize() { 200 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED); 201} 202 203void WindowState::Unminimize() { 204 window_->SetProperty( 205 aura::client::kShowStateKey, 206 window_->GetProperty(aura::client::kRestoreShowStateKey)); 207 window_->ClearProperty(aura::client::kRestoreShowStateKey); 208} 209 210void WindowState::Activate() { 211 ActivateWindow(window_); 212} 213 214void WindowState::Deactivate() { 215 DeactivateWindow(window_); 216} 217 218void WindowState::Restore() { 219 if (!IsNormalStateType()) { 220 const WMEvent event(WM_EVENT_NORMAL); 221 OnWMEvent(&event); 222 } 223} 224 225void WindowState::OnWMEvent(const WMEvent* event) { 226 current_state_->OnWMEvent(this, event); 227} 228 229void WindowState::SaveCurrentBoundsForRestore() { 230 gfx::Rect bounds_in_screen = 231 ScreenUtil::ConvertRectToScreen(window_->parent(), 232 window_->bounds()); 233 SetRestoreBoundsInScreen(bounds_in_screen); 234} 235 236gfx::Rect WindowState::GetRestoreBoundsInScreen() const { 237 return *window_->GetProperty(aura::client::kRestoreBoundsKey); 238} 239 240gfx::Rect WindowState::GetRestoreBoundsInParent() const { 241 return ScreenUtil::ConvertRectFromScreen(window_->parent(), 242 GetRestoreBoundsInScreen()); 243} 244 245void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) { 246 window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds)); 247} 248 249void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) { 250 SetRestoreBoundsInScreen( 251 ScreenUtil::ConvertRectToScreen(window_->parent(), bounds)); 252} 253 254void WindowState::ClearRestoreBounds() { 255 window_->ClearProperty(aura::client::kRestoreBoundsKey); 256} 257 258scoped_ptr<WindowState::State> WindowState::SetStateObject( 259 scoped_ptr<WindowState::State> new_state) { 260 current_state_->DetachState(this); 261 scoped_ptr<WindowState::State> old_object = current_state_.Pass(); 262 current_state_ = new_state.Pass(); 263 current_state_->AttachState(this, old_object.get()); 264 return old_object.Pass(); 265} 266 267void WindowState::SetPreAutoManageWindowBounds( 268 const gfx::Rect& bounds) { 269 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds)); 270} 271 272void WindowState::AddObserver(WindowStateObserver* observer) { 273 observer_list_.AddObserver(observer); 274} 275 276void WindowState::RemoveObserver(WindowStateObserver* observer) { 277 observer_list_.RemoveObserver(observer); 278} 279 280void WindowState::CreateDragDetails(aura::Window* window, 281 const gfx::Point& point_in_parent, 282 int window_component, 283 aura::client::WindowMoveSource source) { 284 drag_details_.reset( 285 new DragDetails(window, point_in_parent, window_component, source)); 286} 287 288void WindowState::DeleteDragDetails() { 289 drag_details_.reset(); 290} 291 292void WindowState::SetAndClearRestoreBounds() { 293 DCHECK(HasRestoreBounds()); 294 SetBoundsInScreen(GetRestoreBoundsInScreen()); 295 ClearRestoreBounds(); 296} 297 298void WindowState::OnWindowPropertyChanged(aura::Window* window, 299 const void* key, 300 intptr_t old) { 301 DCHECK_EQ(window, window_); 302 if (key == aura::client::kShowStateKey && !ignore_property_change_) { 303 WMEvent event(WMEventTypeFromShowState(GetShowState())); 304 OnWMEvent(&event); 305 } 306} 307 308void WindowState::SetBoundsInScreen( 309 const gfx::Rect& bounds_in_screen) { 310 gfx::Rect bounds_in_parent = 311 ScreenUtil::ConvertRectFromScreen(window_->parent(), 312 bounds_in_screen); 313 window_->SetBounds(bounds_in_parent); 314} 315 316ui::WindowShowState WindowState::GetShowState() const { 317 return window_->GetProperty(aura::client::kShowStateKey); 318} 319 320void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) { 321 if (is_dragged() || !IsSnapped()) 322 return; 323 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent( 324 window_); 325 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED) 326 bounds->set_x(maximized_bounds.x()); 327 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED) 328 bounds->set_x(maximized_bounds.right() - bounds->width()); 329 bounds->set_y(maximized_bounds.y()); 330 bounds->set_height(maximized_bounds.height()); 331} 332 333void WindowState::UpdateWindowShowStateFromStateType() { 334 ui::WindowShowState new_window_state = 335 ToWindowShowState(current_state_->GetType()); 336 if (new_window_state != GetShowState()) { 337 base::AutoReset<bool> resetter(&ignore_property_change_, true); 338 window_->SetProperty(aura::client::kShowStateKey, new_window_state); 339 } 340} 341 342void WindowState::NotifyPreStateTypeChange( 343 WindowStateType old_window_state_type) { 344 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 345 OnPreWindowStateTypeChange(this, old_window_state_type)); 346} 347 348void WindowState::NotifyPostStateTypeChange( 349 WindowStateType old_window_state_type) { 350 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 351 OnPostWindowStateTypeChange(this, old_window_state_type)); 352} 353 354void WindowState::SetBoundsDirect(const gfx::Rect& bounds) { 355 BoundsSetter().SetBounds(window_, bounds); 356} 357 358void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) { 359 gfx::Rect work_area_in_parent = 360 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_); 361 gfx::Rect child_bounds(bounds); 362 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds); 363 SetBoundsDirect(child_bounds); 364} 365 366void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) { 367 const int kBoundsChangeSlideDurationMs = 120; 368 369 ui::Layer* layer = window_->layer(); 370 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator()); 371 slide_settings.SetPreemptionStrategy( 372 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 373 slide_settings.SetTransitionDuration( 374 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs)); 375 SetBoundsDirect(bounds); 376} 377 378void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) { 379 // Some test results in invoking CrossFadeToBounds when window is not visible. 380 // No animation is necessary in that case, thus just change the bounds and 381 // quit. 382 if (!window_->TargetVisibility()) { 383 SetBoundsConstrained(new_bounds); 384 return; 385 } 386 387 const gfx::Rect old_bounds = window_->bounds(); 388 389 // Create fresh layers for the window and all its children to paint into. 390 // Takes ownership of the old layer and all its children, which will be 391 // cleaned up after the animation completes. 392 // Specify |set_bounds| to true here to keep the old bounds in the child 393 // windows of |window|. 394 scoped_ptr<ui::LayerTreeOwner> old_layer_owner = 395 ::wm::RecreateLayers(window_); 396 ui::Layer* old_layer = old_layer_owner->root(); 397 DCHECK(old_layer); 398 ui::Layer* new_layer = window_->layer(); 399 400 // Resize the window to the new size, which will force a layout and paint. 401 SetBoundsDirect(new_bounds); 402 403 // Ensure the higher-resolution layer is on top. 404 bool old_on_top = (old_bounds.width() > new_bounds.width()); 405 if (old_on_top) 406 old_layer->parent()->StackBelow(new_layer, old_layer); 407 else 408 old_layer->parent()->StackAbove(new_layer, old_layer); 409 410 CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT); 411} 412 413WindowState* GetActiveWindowState() { 414 aura::Window* active = GetActiveWindow(); 415 return active ? GetWindowState(active) : NULL; 416} 417 418WindowState* GetWindowState(aura::Window* window) { 419 if (!window) 420 return NULL; 421 WindowState* settings = window->GetProperty(internal::kWindowStateKey); 422 if(!settings) { 423 settings = new WindowState(window); 424 window->SetProperty(internal::kWindowStateKey, settings); 425 } 426 return settings; 427} 428 429const WindowState* GetWindowState(const aura::Window* window) { 430 return GetWindowState(const_cast<aura::Window*>(window)); 431} 432 433} // namespace wm 434} // namespace ash 435