window_state.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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 // WindowState is registered as an owned property of |window_|, and window 103 // unregisters all of its observers in its d'tor before destroying its 104 // properties. As a result, window_->RemoveObserver() doesn't need to (and 105 // shouldn't) be called here. 106} 107 108bool WindowState::HasDelegate() const { 109 return delegate_; 110} 111 112void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) { 113 DCHECK(!delegate_.get()); 114 delegate_ = delegate.Pass(); 115} 116 117WindowStateType WindowState::GetStateType() const { 118 return current_state_->GetType(); 119} 120 121bool WindowState::IsMinimized() const { 122 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED; 123} 124 125bool WindowState::IsMaximized() const { 126 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 127} 128 129bool WindowState::IsFullscreen() const { 130 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN; 131} 132 133bool WindowState::IsMaximizedOrFullscreen() const { 134 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN || 135 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED; 136} 137 138bool WindowState::IsSnapped() const { 139 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED || 140 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED; 141} 142 143bool WindowState::IsNormalStateType() const { 144 return GetStateType() == WINDOW_STATE_TYPE_NORMAL || 145 GetStateType() == WINDOW_STATE_TYPE_DEFAULT; 146} 147 148bool WindowState::IsNormalOrSnapped() const { 149 return IsNormalStateType() || IsSnapped(); 150} 151 152bool WindowState::IsActive() const { 153 return IsActiveWindow(window_); 154} 155 156bool WindowState::IsDocked() const { 157 return window_->parent() && 158 window_->parent()->id() == kShellWindowId_DockedContainer; 159} 160 161bool WindowState::CanMaximize() const { 162 return window_->GetProperty(aura::client::kCanMaximizeKey); 163} 164 165bool WindowState::CanMinimize() const { 166 RootWindowController* controller = RootWindowController::ForWindow(window_); 167 if (!controller) 168 return false; 169 aura::Window* lockscreen = 170 controller->GetContainer(kShellWindowId_LockScreenContainersContainer); 171 if (lockscreen->Contains(window_)) 172 return false; 173 174 return true; 175} 176 177bool WindowState::CanResize() const { 178 return window_->GetProperty(aura::client::kCanResizeKey); 179} 180 181bool WindowState::CanActivate() const { 182 return ::wm::CanActivateWindow(window_); 183} 184 185bool WindowState::CanSnap() const { 186 if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL || 187 ::wm::GetTransientParent(window_)) 188 return false; 189 // If a window has a maximum size defined, snapping may make it too big. 190 // TODO(oshima): We probably should snap if possible. 191 return window_->delegate() ? window_->delegate()->GetMaximumSize().IsEmpty() : 192 true; 193} 194 195bool WindowState::HasRestoreBounds() const { 196 return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL; 197} 198 199void WindowState::Maximize() { 200 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED); 201} 202 203void WindowState::Minimize() { 204 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED); 205} 206 207void WindowState::Unminimize() { 208 window_->SetProperty( 209 aura::client::kShowStateKey, 210 window_->GetProperty(aura::client::kRestoreShowStateKey)); 211 window_->ClearProperty(aura::client::kRestoreShowStateKey); 212} 213 214void WindowState::Activate() { 215 ActivateWindow(window_); 216} 217 218void WindowState::Deactivate() { 219 DeactivateWindow(window_); 220} 221 222void WindowState::Restore() { 223 if (!IsNormalStateType()) { 224 const WMEvent event(WM_EVENT_NORMAL); 225 OnWMEvent(&event); 226 } 227} 228 229void WindowState::OnWMEvent(const WMEvent* event) { 230 current_state_->OnWMEvent(this, event); 231} 232 233void WindowState::SaveCurrentBoundsForRestore() { 234 gfx::Rect bounds_in_screen = 235 ScreenUtil::ConvertRectToScreen(window_->parent(), 236 window_->bounds()); 237 SetRestoreBoundsInScreen(bounds_in_screen); 238} 239 240gfx::Rect WindowState::GetRestoreBoundsInScreen() const { 241 return *window_->GetProperty(aura::client::kRestoreBoundsKey); 242} 243 244gfx::Rect WindowState::GetRestoreBoundsInParent() const { 245 return ScreenUtil::ConvertRectFromScreen(window_->parent(), 246 GetRestoreBoundsInScreen()); 247} 248 249void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) { 250 window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds)); 251} 252 253void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) { 254 SetRestoreBoundsInScreen( 255 ScreenUtil::ConvertRectToScreen(window_->parent(), bounds)); 256} 257 258void WindowState::ClearRestoreBounds() { 259 window_->ClearProperty(aura::client::kRestoreBoundsKey); 260} 261 262scoped_ptr<WindowState::State> WindowState::SetStateObject( 263 scoped_ptr<WindowState::State> new_state) { 264 current_state_->DetachState(this); 265 scoped_ptr<WindowState::State> old_object = current_state_.Pass(); 266 current_state_ = new_state.Pass(); 267 current_state_->AttachState(this, old_object.get()); 268 return old_object.Pass(); 269} 270 271void WindowState::SetPreAutoManageWindowBounds( 272 const gfx::Rect& bounds) { 273 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds)); 274} 275 276void WindowState::AddObserver(WindowStateObserver* observer) { 277 observer_list_.AddObserver(observer); 278} 279 280void WindowState::RemoveObserver(WindowStateObserver* observer) { 281 observer_list_.RemoveObserver(observer); 282} 283 284void WindowState::set_bounds_changed_by_user(bool bounds_changed_by_user) { 285 bounds_changed_by_user_ = bounds_changed_by_user; 286 if (bounds_changed_by_user) 287 pre_auto_manage_window_bounds_.reset(); 288} 289 290void WindowState::CreateDragDetails(aura::Window* window, 291 const gfx::Point& point_in_parent, 292 int window_component, 293 aura::client::WindowMoveSource source) { 294 drag_details_.reset( 295 new DragDetails(window, point_in_parent, window_component, source)); 296} 297 298void WindowState::DeleteDragDetails() { 299 drag_details_.reset(); 300} 301 302void WindowState::SetAndClearRestoreBounds() { 303 DCHECK(HasRestoreBounds()); 304 SetBoundsInScreen(GetRestoreBoundsInScreen()); 305 ClearRestoreBounds(); 306} 307 308void WindowState::OnWindowPropertyChanged(aura::Window* window, 309 const void* key, 310 intptr_t old) { 311 DCHECK_EQ(window, window_); 312 if (key == aura::client::kShowStateKey && !ignore_property_change_) { 313 WMEvent event(WMEventTypeFromShowState(GetShowState())); 314 OnWMEvent(&event); 315 } 316} 317 318void WindowState::SetBoundsInScreen( 319 const gfx::Rect& bounds_in_screen) { 320 gfx::Rect bounds_in_parent = 321 ScreenUtil::ConvertRectFromScreen(window_->parent(), 322 bounds_in_screen); 323 window_->SetBounds(bounds_in_parent); 324} 325 326ui::WindowShowState WindowState::GetShowState() const { 327 return window_->GetProperty(aura::client::kShowStateKey); 328} 329 330void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) { 331 if (is_dragged() || !IsSnapped()) 332 return; 333 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent( 334 window_); 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()); 341} 342 343void 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); 349 } 350} 351 352void WindowState::NotifyPreStateTypeChange( 353 WindowStateType old_window_state_type) { 354 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 355 OnPreWindowStateTypeChange(this, old_window_state_type)); 356} 357 358void WindowState::NotifyPostStateTypeChange( 359 WindowStateType old_window_state_type) { 360 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 361 OnPostWindowStateTypeChange(this, old_window_state_type)); 362} 363 364void 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 367 // modes 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()); 373 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())); 378 } 379 BoundsSetter().SetBounds(window_, actual_new_bounds); 380} 381 382void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) { 383 gfx::Rect work_area_in_parent = 384 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_); 385 gfx::Rect child_bounds(bounds); 386 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds); 387 SetBoundsDirect(child_bounds); 388} 389 390void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) { 391 const int kBoundsChangeSlideDurationMs = 120; 392 393 ui::Layer* layer = window_->layer(); 394 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator()); 395 slide_settings.SetPreemptionStrategy( 396 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 397 slide_settings.SetTransitionDuration( 398 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs)); 399 SetBoundsDirect(bounds); 400} 401 402void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) { 403 // Some test results in invoking CrossFadeToBounds when window is not visible. 404 // No animation is necessary in that case, thus just change the bounds and 405 // quit. 406 if (!window_->TargetVisibility()) { 407 SetBoundsConstrained(new_bounds); 408 return; 409 } 410 411 const gfx::Rect old_bounds = window_->bounds(); 412 413 // Create fresh layers for the window and all its children to paint into. 414 // Takes ownership of the old layer and all its children, which will be 415 // cleaned up after the animation completes. 416 // Specify |set_bounds| to true here to keep the old bounds in the child 417 // windows of |window|. 418 scoped_ptr<ui::LayerTreeOwner> old_layer_owner = 419 ::wm::RecreateLayers(window_); 420 ui::Layer* old_layer = old_layer_owner->root(); 421 DCHECK(old_layer); 422 ui::Layer* new_layer = window_->layer(); 423 424 // Resize the window to the new size, which will force a layout and paint. 425 SetBoundsDirect(new_bounds); 426 427 // Ensure the higher-resolution layer is on top. 428 bool old_on_top = (old_bounds.width() > new_bounds.width()); 429 if (old_on_top) 430 old_layer->parent()->StackBelow(new_layer, old_layer); 431 else 432 old_layer->parent()->StackAbove(new_layer, old_layer); 433 434 CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT); 435} 436 437WindowState* GetActiveWindowState() { 438 aura::Window* active = GetActiveWindow(); 439 return active ? GetWindowState(active) : NULL; 440} 441 442WindowState* GetWindowState(aura::Window* window) { 443 if (!window) 444 return NULL; 445 WindowState* settings = window->GetProperty(kWindowStateKey); 446 if(!settings) { 447 settings = new WindowState(window); 448 window->SetProperty(kWindowStateKey, settings); 449 } 450 return settings; 451} 452 453const WindowState* GetWindowState(const aura::Window* window) { 454 return GetWindowState(const_cast<aura::Window*>(window)); 455} 456 457} // namespace wm 458} // namespace ash 459