window_state.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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() == kShellWindowId_DockedContainer; 154} 155 156bool WindowState::CanMaximize() const { 157 return window_->GetProperty(aura::client::kCanMaximizeKey); 158} 159 160bool WindowState::CanMinimize() const { 161 RootWindowController* controller = RootWindowController::ForWindow(window_); 162 if (!controller) 163 return false; 164 aura::Window* lockscreen = 165 controller->GetContainer(kShellWindowId_LockScreenContainersContainer); 166 if (lockscreen->Contains(window_)) 167 return false; 168 169 return true; 170} 171 172bool WindowState::CanResize() const { 173 return window_->GetProperty(aura::client::kCanResizeKey); 174} 175 176bool WindowState::CanActivate() const { 177 return ::wm::CanActivateWindow(window_); 178} 179 180bool WindowState::CanSnap() const { 181 if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL || 182 ::wm::GetTransientParent(window_)) 183 return false; 184 // If a window has a maximum size defined, snapping may make it too big. 185 // TODO(oshima): We probably should snap if possible. 186 return window_->delegate() ? window_->delegate()->GetMaximumSize().IsEmpty() : 187 true; 188} 189 190bool WindowState::HasRestoreBounds() const { 191 return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL; 192} 193 194void WindowState::Maximize() { 195 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED); 196} 197 198void WindowState::Minimize() { 199 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED); 200} 201 202void WindowState::Unminimize() { 203 window_->SetProperty( 204 aura::client::kShowStateKey, 205 window_->GetProperty(aura::client::kRestoreShowStateKey)); 206 window_->ClearProperty(aura::client::kRestoreShowStateKey); 207} 208 209void WindowState::Activate() { 210 ActivateWindow(window_); 211} 212 213void WindowState::Deactivate() { 214 DeactivateWindow(window_); 215} 216 217void WindowState::Restore() { 218 if (!IsNormalStateType()) { 219 const WMEvent event(WM_EVENT_NORMAL); 220 OnWMEvent(&event); 221 } 222} 223 224void WindowState::OnWMEvent(const WMEvent* event) { 225 current_state_->OnWMEvent(this, event); 226} 227 228void WindowState::SaveCurrentBoundsForRestore() { 229 gfx::Rect bounds_in_screen = 230 ScreenUtil::ConvertRectToScreen(window_->parent(), 231 window_->bounds()); 232 SetRestoreBoundsInScreen(bounds_in_screen); 233} 234 235gfx::Rect WindowState::GetRestoreBoundsInScreen() const { 236 return *window_->GetProperty(aura::client::kRestoreBoundsKey); 237} 238 239gfx::Rect WindowState::GetRestoreBoundsInParent() const { 240 return ScreenUtil::ConvertRectFromScreen(window_->parent(), 241 GetRestoreBoundsInScreen()); 242} 243 244void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) { 245 window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds)); 246} 247 248void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) { 249 SetRestoreBoundsInScreen( 250 ScreenUtil::ConvertRectToScreen(window_->parent(), bounds)); 251} 252 253void WindowState::ClearRestoreBounds() { 254 window_->ClearProperty(aura::client::kRestoreBoundsKey); 255} 256 257scoped_ptr<WindowState::State> WindowState::SetStateObject( 258 scoped_ptr<WindowState::State> new_state) { 259 current_state_->DetachState(this); 260 scoped_ptr<WindowState::State> old_object = current_state_.Pass(); 261 current_state_ = new_state.Pass(); 262 current_state_->AttachState(this, old_object.get()); 263 return old_object.Pass(); 264} 265 266void WindowState::SetPreAutoManageWindowBounds( 267 const gfx::Rect& bounds) { 268 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds)); 269} 270 271void WindowState::AddObserver(WindowStateObserver* observer) { 272 observer_list_.AddObserver(observer); 273} 274 275void WindowState::RemoveObserver(WindowStateObserver* observer) { 276 observer_list_.RemoveObserver(observer); 277} 278 279void WindowState::CreateDragDetails(aura::Window* window, 280 const gfx::Point& point_in_parent, 281 int window_component, 282 aura::client::WindowMoveSource source) { 283 drag_details_.reset( 284 new DragDetails(window, point_in_parent, window_component, source)); 285} 286 287void WindowState::DeleteDragDetails() { 288 drag_details_.reset(); 289} 290 291void WindowState::SetAndClearRestoreBounds() { 292 DCHECK(HasRestoreBounds()); 293 SetBoundsInScreen(GetRestoreBoundsInScreen()); 294 ClearRestoreBounds(); 295} 296 297void WindowState::OnWindowPropertyChanged(aura::Window* window, 298 const void* key, 299 intptr_t old) { 300 DCHECK_EQ(window, window_); 301 if (key == aura::client::kShowStateKey && !ignore_property_change_) { 302 WMEvent event(WMEventTypeFromShowState(GetShowState())); 303 OnWMEvent(&event); 304 } 305} 306 307void WindowState::SetBoundsInScreen( 308 const gfx::Rect& bounds_in_screen) { 309 gfx::Rect bounds_in_parent = 310 ScreenUtil::ConvertRectFromScreen(window_->parent(), 311 bounds_in_screen); 312 window_->SetBounds(bounds_in_parent); 313} 314 315ui::WindowShowState WindowState::GetShowState() const { 316 return window_->GetProperty(aura::client::kShowStateKey); 317} 318 319void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) { 320 if (is_dragged() || !IsSnapped()) 321 return; 322 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent( 323 window_); 324 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED) 325 bounds->set_x(maximized_bounds.x()); 326 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED) 327 bounds->set_x(maximized_bounds.right() - bounds->width()); 328 bounds->set_y(maximized_bounds.y()); 329 bounds->set_height(maximized_bounds.height()); 330} 331 332void WindowState::UpdateWindowShowStateFromStateType() { 333 ui::WindowShowState new_window_state = 334 ToWindowShowState(current_state_->GetType()); 335 if (new_window_state != GetShowState()) { 336 base::AutoReset<bool> resetter(&ignore_property_change_, true); 337 window_->SetProperty(aura::client::kShowStateKey, new_window_state); 338 } 339} 340 341void WindowState::NotifyPreStateTypeChange( 342 WindowStateType old_window_state_type) { 343 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 344 OnPreWindowStateTypeChange(this, old_window_state_type)); 345} 346 347void WindowState::NotifyPostStateTypeChange( 348 WindowStateType old_window_state_type) { 349 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, 350 OnPostWindowStateTypeChange(this, old_window_state_type)); 351} 352 353void WindowState::SetBoundsDirect(const gfx::Rect& bounds) { 354 BoundsSetter().SetBounds(window_, bounds); 355} 356 357void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) { 358 gfx::Rect work_area_in_parent = 359 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_); 360 gfx::Rect child_bounds(bounds); 361 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds); 362 SetBoundsDirect(child_bounds); 363} 364 365void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) { 366 const int kBoundsChangeSlideDurationMs = 120; 367 368 ui::Layer* layer = window_->layer(); 369 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator()); 370 slide_settings.SetPreemptionStrategy( 371 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); 372 slide_settings.SetTransitionDuration( 373 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs)); 374 SetBoundsDirect(bounds); 375} 376 377void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) { 378 // Some test results in invoking CrossFadeToBounds when window is not visible. 379 // No animation is necessary in that case, thus just change the bounds and 380 // quit. 381 if (!window_->TargetVisibility()) { 382 SetBoundsConstrained(new_bounds); 383 return; 384 } 385 386 const gfx::Rect old_bounds = window_->bounds(); 387 388 // Create fresh layers for the window and all its children to paint into. 389 // Takes ownership of the old layer and all its children, which will be 390 // cleaned up after the animation completes. 391 // Specify |set_bounds| to true here to keep the old bounds in the child 392 // windows of |window|. 393 scoped_ptr<ui::LayerTreeOwner> old_layer_owner = 394 ::wm::RecreateLayers(window_); 395 ui::Layer* old_layer = old_layer_owner->root(); 396 DCHECK(old_layer); 397 ui::Layer* new_layer = window_->layer(); 398 399 // Resize the window to the new size, which will force a layout and paint. 400 SetBoundsDirect(new_bounds); 401 402 // Ensure the higher-resolution layer is on top. 403 bool old_on_top = (old_bounds.width() > new_bounds.width()); 404 if (old_on_top) 405 old_layer->parent()->StackBelow(new_layer, old_layer); 406 else 407 old_layer->parent()->StackAbove(new_layer, old_layer); 408 409 CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT); 410} 411 412WindowState* GetActiveWindowState() { 413 aura::Window* active = GetActiveWindow(); 414 return active ? GetWindowState(active) : NULL; 415} 416 417WindowState* GetWindowState(aura::Window* window) { 418 if (!window) 419 return NULL; 420 WindowState* settings = window->GetProperty(kWindowStateKey); 421 if(!settings) { 422 settings = new WindowState(window); 423 window->SetProperty(kWindowStateKey, settings); 424 } 425 return settings; 426} 427 428const WindowState* GetWindowState(const aura::Window* window) { 429 return GetWindowState(const_cast<aura::Window*>(window)); 430} 431 432} // namespace wm 433} // namespace ash 434