window_state.cc revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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(aura::Window* window) 85 : window_(window), 86 window_position_managed_(false), 87 bounds_changed_by_user_(false), 88 panel_attached_(true), 89 ignored_by_shelf_(false), 90 can_consume_system_keys_(false), 91 top_row_keys_are_function_keys_(false), 92 unminimize_to_restore_bounds_(false), 93 hide_shelf_when_fullscreen_(true), 94 minimum_visibility_(false), 95 can_be_dragged_(true), 96 ignore_property_change_(false), 97 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) { 98 window_->AddObserver(this); 99} 100 101WindowState::~WindowState() { 102} 103 104bool WindowState::HasDelegate() const { 105 return delegate_; 106} 107 108void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) { 109 DCHECK(!delegate_.get()); 110 delegate_ = delegate.Pass(); 111} 112 113WindowStateType WindowState::GetStateType() const { 114 return current_state_->GetType(); 115} 116 117bool WindowState::IsMinimized() const { 118 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED; 119} 120 121bool WindowState::IsMaximized() const { 122 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 123} 124 125bool WindowState::IsFullscreen() const { 126 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN; 127} 128 129bool WindowState::IsMaximizedOrFullscreen() const { 130 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN || 131 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 132} 133 134bool WindowState::IsSnapped() const { 135 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED || 136 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED; 137} 138 139bool WindowState::IsNormalStateType() const { 140 return GetStateType() == WINDOW_STATE_TYPE_NORMAL || 141 GetStateType() == WINDOW_STATE_TYPE_DEFAULT; 142} 143 144bool WindowState::IsNormalOrSnapped() const { 145 return IsNormalStateType() || IsSnapped(); 146} 147 148bool WindowState::IsActive() const { 149 return IsActiveWindow(window_); 150} 151 152bool WindowState::IsDocked() const { 153 return window_->parent() && 154 window_->parent()->id() == kShellWindowId_DockedContainer; 155} 156 157bool WindowState::CanMaximize() const { 158 return window_->GetProperty(aura::client::kCanMaximizeKey); 159} 160 161bool WindowState::CanMinimize() const { 162 RootWindowController* controller = RootWindowController::ForWindow(window_); 163 if (!controller) 164 return false; 165 aura::Window* lockscreen = 166 controller->GetContainer(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::set_bounds_changed_by_user(bool bounds_changed_by_user) { 281 bounds_changed_by_user_ = bounds_changed_by_user; 282 if (bounds_changed_by_user) 283 pre_auto_manage_window_bounds_.reset(); 284} 285 286void WindowState::CreateDragDetails(aura::Window* window, 287 const gfx::Point& point_in_parent, 288 int window_component, 289 aura::client::WindowMoveSource source) { 290 drag_details_.reset( 291 new DragDetails(window, point_in_parent, window_component, source)); 292} 293 294void WindowState::DeleteDragDetails() { 295 drag_details_.reset(); 296} 297 298void WindowState::SetAndClearRestoreBounds() { 299 DCHECK(HasRestoreBounds()); 300 SetBoundsInScreen(GetRestoreBoundsInScreen()); 301 ClearRestoreBounds(); 302} 303 304void WindowState::OnWindowPropertyChanged(aura::Window* window, 305 const void* key, 306 intptr_t old) { 307 DCHECK_EQ(window, window_); 308 if (key == aura::client::kShowStateKey && !ignore_property_change_) { 309 WMEvent event(WMEventTypeFromShowState(GetShowState())); 310 OnWMEvent(&event); 311 } 312} 313 314void WindowState::SetBoundsInScreen( 315 const gfx::Rect& bounds_in_screen) { 316 gfx::Rect bounds_in_parent = 317 ScreenUtil::ConvertRectFromScreen(window_->parent(), 318 bounds_in_screen); 319 window_->SetBounds(bounds_in_parent); 320} 321 322ui::WindowShowState WindowState::GetShowState() const { 323 return window_->GetProperty(aura::client::kShowStateKey); 324} 325 326void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) { 327 if (is_dragged() || !IsSnapped()) 328 return; 329 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent( 330 window_); 331 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED) 332 bounds->set_x(maximized_bounds.x()); 333 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED) 334 bounds->set_x(maximized_bounds.right() - bounds->width()); 335 bounds->set_y(maximized_bounds.y()); 336 bounds->set_height(maximized_bounds.height()); 337} 338 339void WindowState::UpdateWindowShowStateFromStateType() { 340 ui::WindowShowState new_window_state = 341 ToWindowShowState(current_state_->GetType()); 342 if (new_window_state != GetShowState()) { 343 base::AutoReset<bool> resetter(&ignore_property_change_, true); 344 window_->SetProperty(aura::client::kShowStateKey, new_window_state); 345 } 346} 347 348void WindowState::NotifyPreStateTypeChange( 349 WindowStateType old_window_state_type) { 350 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 351 OnPreWindowStateTypeChange(this, old_window_state_type)); 352} 353 354void WindowState::NotifyPostStateTypeChange( 355 WindowStateType old_window_state_type) { 356 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 357 OnPostWindowStateTypeChange(this, old_window_state_type)); 358} 359 360void WindowState::SetBoundsDirect(const gfx::Rect& bounds) { 361 gfx::Rect actual_new_bounds(bounds); 362 // Ensure we don't go smaller than our minimum bounds in "normal" window 363 // modes 364 if (window_->delegate() && !IsMaximized() && !IsFullscreen()) { 365 // Get the minimum usable size of the minimum size and the screen size. 366 gfx::Size min_size = window_->delegate()->GetMinimumSize(); 367 min_size.SetToMin(gfx::Screen::GetScreenFor( 368 window_)->GetDisplayNearestWindow(window_).work_area().size()); 369 370 actual_new_bounds.set_width( 371 std::max(min_size.width(), actual_new_bounds.width())); 372 actual_new_bounds.set_height( 373 std::max(min_size.height(), actual_new_bounds.height())); 374 } 375 BoundsSetter().SetBounds(window_, actual_new_bounds); 376} 377 378void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) { 379 gfx::Rect work_area_in_parent = 380 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_); 381 gfx::Rect child_bounds(bounds); 382 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds); 383 SetBoundsDirect(child_bounds); 384} 385 386void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) { 387 const int kBoundsChangeSlideDurationMs = 120; 388 389 ui::Layer* layer = window_->layer(); 390 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator()); 391 slide_settings.SetPreemptionStrategy( 392 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 393 slide_settings.SetTransitionDuration( 394 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs)); 395 SetBoundsDirect(bounds); 396} 397 398void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) { 399 // Some test results in invoking CrossFadeToBounds when window is not visible. 400 // No animation is necessary in that case, thus just change the bounds and 401 // quit. 402 if (!window_->TargetVisibility()) { 403 SetBoundsConstrained(new_bounds); 404 return; 405 } 406 407 const gfx::Rect old_bounds = window_->bounds(); 408 409 // Create fresh layers for the window and all its children to paint into. 410 // Takes ownership of the old layer and all its children, which will be 411 // cleaned up after the animation completes. 412 // Specify |set_bounds| to true here to keep the old bounds in the child 413 // windows of |window|. 414 scoped_ptr<ui::LayerTreeOwner> old_layer_owner = 415 ::wm::RecreateLayers(window_); 416 ui::Layer* old_layer = old_layer_owner->root(); 417 DCHECK(old_layer); 418 ui::Layer* new_layer = window_->layer(); 419 420 // Resize the window to the new size, which will force a layout and paint. 421 SetBoundsDirect(new_bounds); 422 423 // Ensure the higher-resolution layer is on top. 424 bool old_on_top = (old_bounds.width() > new_bounds.width()); 425 if (old_on_top) 426 old_layer->parent()->StackBelow(new_layer, old_layer); 427 else 428 old_layer->parent()->StackAbove(new_layer, old_layer); 429 430 CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT); 431} 432 433WindowState* GetActiveWindowState() { 434 aura::Window* active = GetActiveWindow(); 435 return active ? GetWindowState(active) : NULL; 436} 437 438WindowState* GetWindowState(aura::Window* window) { 439 if (!window) 440 return NULL; 441 WindowState* settings = window->GetProperty(kWindowStateKey); 442 if(!settings) { 443 settings = new WindowState(window); 444 window->SetProperty(kWindowStateKey, settings); 445 } 446 return settings; 447} 448 449const WindowState* GetWindowState(const aura::Window* window) { 450 return GetWindowState(const_cast<aura::Window*>(window)); 451} 452 453} // namespace wm 454} // namespace ash 455