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