| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/wm/window_state.h" | 5 #include "ash/wm/window_state.h" |
| 6 | 6 |
| 7 #include "ash/ash_switches.h" | 7 #include "ash/ash_switches.h" |
| 8 #include "ash/root_window_controller.h" | 8 #include "ash/root_window_controller.h" |
| 9 #include "ash/screen_util.h" | 9 #include "ash/screen_util.h" |
| 10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
| 11 #include "ash/wm/default_state.h" | 11 #include "ash/wm/default_state.h" |
| 12 #include "ash/wm/window_properties.h" | 12 #include "ash/wm/window_properties.h" |
| 13 #include "ash/wm/window_state_delegate.h" | 13 #include "ash/wm/window_state_delegate.h" |
| 14 #include "ash/wm/window_state_observer.h" | 14 #include "ash/wm/window_state_observer.h" |
| 15 #include "ash/wm/window_util.h" | 15 #include "ash/wm/window_util.h" |
| 16 #include "ash/wm/wm_types.h" | 16 #include "ash/wm/wm_types.h" |
| 17 #include "base/auto_reset.h" | 17 #include "base/auto_reset.h" |
| 18 #include "base/command_line.h" | 18 #include "base/command_line.h" |
| 19 #include "ui/aura/client/aura_constants.h" | 19 #include "ui/aura/client/aura_constants.h" |
| 20 #include "ui/aura/layout_manager.h" |
| 20 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
| 21 #include "ui/aura/window_delegate.h" | 22 #include "ui/aura/window_delegate.h" |
| 23 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 22 #include "ui/gfx/display.h" | 24 #include "ui/gfx/display.h" |
| 23 #include "ui/views/corewm/window_util.h" | 25 #include "ui/views/corewm/window_util.h" |
| 24 | 26 |
| 25 namespace ash { | 27 namespace ash { |
| 26 namespace wm { | 28 namespace wm { |
| 27 | 29 |
| 30 namespace { |
| 31 |
| 32 // A tentative class to set the bounds on the window. |
| 33 // TODO(oshima): Once all logic are cleaned up, move this to the real layout |
| 34 // manager with proper friendship. |
| 35 class BoundsSetter : public aura::LayoutManager { |
| 36 public: |
| 37 BoundsSetter() {} |
| 38 virtual ~BoundsSetter() {} |
| 39 |
| 40 // aura::LayoutManager overrides: |
| 41 virtual void OnWindowResized() OVERRIDE {} |
| 42 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {} |
| 43 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {} |
| 44 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {} |
| 45 virtual void OnChildWindowVisibilityChanged( |
| 46 aura::Window* child, bool visible) OVERRIDE {} |
| 47 virtual void SetChildBounds( |
| 48 aura::Window* child, const gfx::Rect& requested_bounds) OVERRIDE {} |
| 49 |
| 50 void SetBounds(aura::Window* window, const gfx::Rect& bounds) { |
| 51 SetChildBoundsDirect(window, bounds); |
| 52 } |
| 53 |
| 54 private: |
| 55 DISALLOW_COPY_AND_ASSIGN(BoundsSetter); |
| 56 }; |
| 57 |
| 58 WMEvent WMEventFromShowState(ui::WindowShowState requested_show_state) { |
| 59 switch (requested_show_state) { |
| 60 case ui::SHOW_STATE_DEFAULT: |
| 61 case ui::SHOW_STATE_NORMAL: |
| 62 return NORMAL; |
| 63 case ui::SHOW_STATE_MINIMIZED: |
| 64 return MINIMIZE; |
| 65 case ui::SHOW_STATE_MAXIMIZED: |
| 66 return MAXIMIZE; |
| 67 case ui::SHOW_STATE_FULLSCREEN: |
| 68 return FULLSCREEN; |
| 69 case ui::SHOW_STATE_INACTIVE: |
| 70 case ui::SHOW_STATE_DETACHED: |
| 71 case ui::SHOW_STATE_END: |
| 72 NOTREACHED() << "No WMEvent defined for the show type:" |
| 73 << requested_show_state; |
| 74 } |
| 75 return NORMAL; |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 28 WindowState::WindowState(aura::Window* window) | 80 WindowState::WindowState(aura::Window* window) |
| 29 : window_(window), | 81 : window_(window), |
| 30 window_position_managed_(false), | 82 window_position_managed_(false), |
| 31 bounds_changed_by_user_(false), | 83 bounds_changed_by_user_(false), |
| 32 panel_attached_(true), | 84 panel_attached_(true), |
| 33 continue_drag_after_reparent_(false), | 85 continue_drag_after_reparent_(false), |
| 34 ignored_by_shelf_(false), | 86 ignored_by_shelf_(false), |
| 35 can_consume_system_keys_(false), | 87 can_consume_system_keys_(false), |
| 36 top_row_keys_are_function_keys_(false), | 88 top_row_keys_are_function_keys_(false), |
| 37 unminimize_to_restore_bounds_(false), | 89 unminimize_to_restore_bounds_(false), |
| 38 hide_shelf_when_fullscreen_(true), | 90 hide_shelf_when_fullscreen_(true), |
| 39 animate_to_fullscreen_(true), | 91 animate_to_fullscreen_(true), |
| 40 minimum_visibility_(false), | 92 minimum_visibility_(false), |
| 41 in_set_window_show_type_(false), | 93 ignore_property_change_(false), |
| 42 window_show_type_(ToWindowShowType(GetShowState())), | 94 window_show_type_(ToWindowShowType(GetShowState())), |
| 43 current_state_(new DefaultState) { | 95 current_state_(new DefaultState) { |
| 44 window_->AddObserver(this); | 96 window_->AddObserver(this); |
| 45 | |
| 46 #if defined(OS_CHROMEOS) | 97 #if defined(OS_CHROMEOS) |
| 47 // NOTE(pkotwicz): Animating to immersive fullscreen does not look good. When | 98 // NOTE(pkotwicz): Animating to immersive fullscreen does not look good. When |
| 48 // switches::UseImmersiveFullscreenForAllWindows() returns true, most windows | 99 // switches::UseImmersiveFullscreenForAllWindows() returns true, most windows |
| 49 // can be put into immersive fullscreen. It is not worth the added complexity | 100 // can be put into immersive fullscreen. It is not worth the added complexity |
| 50 // to only animate to fullscreen if the window is put into immersive | 101 // to only animate to fullscreen if the window is put into immersive |
| 51 // fullscreen. | 102 // fullscreen. |
| 52 animate_to_fullscreen_ = !switches::UseImmersiveFullscreenForAllWindows(); | 103 animate_to_fullscreen_ = !switches::UseImmersiveFullscreenForAllWindows(); |
| 53 #endif | 104 #endif |
| 54 } | 105 } |
| 55 | 106 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 bool WindowState::IsDocked() const { | 155 bool WindowState::IsDocked() const { |
| 105 return window_->parent() && | 156 return window_->parent() && |
| 106 window_->parent()->id() == internal::kShellWindowId_DockedContainer; | 157 window_->parent()->id() == internal::kShellWindowId_DockedContainer; |
| 107 } | 158 } |
| 108 | 159 |
| 109 bool WindowState::IsSnapped() const { | 160 bool WindowState::IsSnapped() const { |
| 110 return window_show_type_ == SHOW_TYPE_LEFT_SNAPPED || | 161 return window_show_type_ == SHOW_TYPE_LEFT_SNAPPED || |
| 111 window_show_type_ == SHOW_TYPE_RIGHT_SNAPPED; | 162 window_show_type_ == SHOW_TYPE_RIGHT_SNAPPED; |
| 112 } | 163 } |
| 113 | 164 |
| 165 bool WindowState::IsPanel() const { |
| 166 return window_->parent() && |
| 167 window_->parent()->id() == internal::kShellWindowId_PanelContainer; |
| 168 } |
| 169 |
| 114 bool WindowState::CanMaximize() const { | 170 bool WindowState::CanMaximize() const { |
| 115 return window_->GetProperty(aura::client::kCanMaximizeKey); | 171 return window_->GetProperty(aura::client::kCanMaximizeKey); |
| 116 } | 172 } |
| 117 | 173 |
| 118 bool WindowState::CanMinimize() const { | 174 bool WindowState::CanMinimize() const { |
| 119 internal::RootWindowController* controller = | 175 internal::RootWindowController* controller = |
| 120 internal::RootWindowController::ForWindow(window_); | 176 internal::RootWindowController::ForWindow(window_); |
| 121 if (!controller) | 177 if (!controller) |
| 122 return false; | 178 return false; |
| 123 aura::Window* lockscreen = controller->GetContainer( | 179 aura::Window* lockscreen = controller->GetContainer( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 | 234 |
| 179 void WindowState::Deactivate() { | 235 void WindowState::Deactivate() { |
| 180 DeactivateWindow(window_); | 236 DeactivateWindow(window_); |
| 181 } | 237 } |
| 182 | 238 |
| 183 void WindowState::Restore() { | 239 void WindowState::Restore() { |
| 184 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL); | 240 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_NORMAL); |
| 185 } | 241 } |
| 186 | 242 |
| 187 void WindowState::ToggleFullscreen() { | 243 void WindowState::ToggleFullscreen() { |
| 188 // Window which cannot be maximized should not be fullscreened. | 244 current_state_->OnWMEvent(this, TOGGLE_FULLSCREEN); |
| 189 // It can, however, be restored if it was fullscreened. | |
| 190 bool is_fullscreen = IsFullscreen(); | |
| 191 if (!is_fullscreen && !CanMaximize()) | |
| 192 return; | |
| 193 if (delegate_ && delegate_->ToggleFullscreen(this)) | |
| 194 return; | |
| 195 if (is_fullscreen) { | |
| 196 Restore(); | |
| 197 } else { | |
| 198 window_->SetProperty(aura::client::kShowStateKey, | |
| 199 ui::SHOW_STATE_FULLSCREEN); | |
| 200 } | |
| 201 } | 245 } |
| 202 | 246 |
| 203 void WindowState::OnWMEvent(WMEvent event) { | 247 void WindowState::OnWMEvent(WMEvent event) { |
| 204 current_state_->OnWMEvent(this, event); | 248 current_state_->OnWMEvent(this, event); |
| 205 } | 249 } |
| 206 | 250 |
| 207 void WindowState::SetBoundsInScreen( | 251 void WindowState::SetBoundsInScreen( |
| 208 const gfx::Rect& bounds_in_screen) { | 252 const gfx::Rect& bounds_in_screen) { |
| 209 gfx::Rect bounds_in_parent = | 253 gfx::Rect bounds_in_parent = |
| 210 ScreenUtil::ConvertRectFromScreen(window_->parent(), | 254 ScreenUtil::ConvertRectFromScreen(window_->parent(), |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 void WindowState::DeleteDragDetails() { | 309 void WindowState::DeleteDragDetails() { |
| 266 drag_details_.reset(); | 310 drag_details_.reset(); |
| 267 } | 311 } |
| 268 | 312 |
| 269 void WindowState::SetAndClearRestoreBounds() { | 313 void WindowState::SetAndClearRestoreBounds() { |
| 270 DCHECK(HasRestoreBounds()); | 314 DCHECK(HasRestoreBounds()); |
| 271 SetBoundsInScreen(GetRestoreBoundsInScreen()); | 315 SetBoundsInScreen(GetRestoreBoundsInScreen()); |
| 272 ClearRestoreBounds(); | 316 ClearRestoreBounds(); |
| 273 } | 317 } |
| 274 | 318 |
| 319 void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) { |
| 320 if (is_dragged() || !IsSnapped()) |
| 321 return; |
| 322 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent( |
| 323 window_); |
| 324 if (window_show_type() == wm::SHOW_TYPE_LEFT_SNAPPED) |
| 325 bounds->set_x(maximized_bounds.x()); |
| 326 else if (window_show_type() == wm::SHOW_TYPE_RIGHT_SNAPPED) |
| 327 bounds->set_x(maximized_bounds.right() - bounds->width()); |
| 328 bounds->set_y(maximized_bounds.y()); |
| 329 // TODO(varkha): Set width to 50% here for snapped windows. |
| 330 bounds->set_height(maximized_bounds.height()); |
| 331 } |
| 332 |
| 275 void WindowState::OnWindowPropertyChanged(aura::Window* window, | 333 void WindowState::OnWindowPropertyChanged(aura::Window* window, |
| 276 const void* key, | 334 const void* key, |
| 277 intptr_t old) { | 335 intptr_t old) { |
| 278 DCHECK_EQ(window, window_); | 336 DCHECK_EQ(window, window_); |
| 279 if (key == aura::client::kShowStateKey) | 337 if (key == aura::client::kShowStateKey && !ignore_property_change_) |
| 280 SetWindowShowType(ToWindowShowType(GetShowState())); | 338 OnWMEvent(WMEventFromShowState(GetShowState())); |
| 281 } | 339 } |
| 282 | 340 |
| 283 void WindowState::SnapWindow(WindowShowType left_or_right, | 341 void WindowState::SnapWindow(WindowShowType left_or_right, |
| 284 const gfx::Rect& bounds) { | 342 const gfx::Rect& bounds) { |
| 285 if (window_show_type_ == left_or_right) { | 343 if (window_show_type_ == left_or_right) { |
| 286 window_->SetBounds(bounds); | 344 window_->SetBounds(bounds); |
| 287 return; | 345 return; |
| 288 } | 346 } |
| 289 | 347 |
| 290 // Compute the bounds that the window will restore to. If the window does not | 348 // Compute the bounds that the window will restore to. If the window does not |
| 291 // already have restore bounds, it will be restored (when un-snapped) to the | 349 // already have restore bounds, it will be restored (when un-snapped) to the |
| 292 // last bounds that it had before getting snapped. | 350 // last bounds that it had before getting snapped. |
| 293 gfx::Rect restore_bounds_in_screen(HasRestoreBounds() ? | 351 gfx::Rect restore_bounds_in_screen(HasRestoreBounds() ? |
| 294 GetRestoreBoundsInScreen() : window_->GetBoundsInScreen()); | 352 GetRestoreBoundsInScreen() : window_->GetBoundsInScreen()); |
| 295 // Set the window's restore bounds so that WorkspaceLayoutManager knows | 353 // Set the window's restore bounds so that WorkspaceLayoutManager knows |
| 296 // which width to use when the snapped window is moved to the edge. | 354 // which width to use when the snapped window is moved to the edge. |
| 297 SetRestoreBoundsInParent(bounds); | 355 SetRestoreBoundsInParent(bounds); |
| 298 | 356 |
| 299 DCHECK(left_or_right == SHOW_TYPE_LEFT_SNAPPED || | 357 DCHECK(left_or_right == SHOW_TYPE_LEFT_SNAPPED || |
| 300 left_or_right == SHOW_TYPE_RIGHT_SNAPPED); | 358 left_or_right == SHOW_TYPE_RIGHT_SNAPPED); |
| 301 SetWindowShowType(left_or_right); | 359 OnWMEvent(left_or_right == SHOW_TYPE_LEFT_SNAPPED ? |
| 360 SNAP_LEFT : SNAP_RIGHT); |
| 361 |
| 302 // TODO(varkha): Ideally the bounds should be changed in a LayoutManager upon | 362 // TODO(varkha): Ideally the bounds should be changed in a LayoutManager upon |
| 303 // observing the WindowShowType change. | 363 // observing the WindowShowType change. |
| 304 // If the window is a child of kShellWindowId_DockedContainer such as during | 364 // If the window is a child of kShellWindowId_DockedContainer such as during |
| 305 // a drag, the window's bounds are not set in | 365 // a drag, the window's bounds are not set in |
| 306 // WorkspaceLayoutManager::OnWindowShowTypeChanged(). Set them here. Skip | 366 // WorkspaceLayoutManager::OnWindowShowTypeChanged(). Set them here. Skip |
| 307 // setting the bounds otherwise to avoid stopping the slide animation which | 367 // setting the bounds otherwise to avoid stopping the slide animation which |
| 308 // was started as a result of OnWindowShowTypeChanged(). | 368 // was started as a result of OnWindowShowTypeChanged(). |
| 309 if (IsDocked()) | 369 if (IsDocked()) |
| 310 window_->SetBounds(bounds); | 370 window_->SetBounds(bounds); |
| 311 SetRestoreBoundsInScreen(restore_bounds_in_screen); | 371 SetRestoreBoundsInScreen(restore_bounds_in_screen); |
| 312 } | 372 } |
| 313 | 373 |
| 314 void WindowState::SetWindowShowType(WindowShowType new_window_show_type) { | 374 void WindowState::UpdateWindowShowType(WindowShowType new_window_show_type) { |
| 315 if (in_set_window_show_type_) | |
| 316 return; | |
| 317 base::AutoReset<bool> resetter(&in_set_window_show_type_, true); | |
| 318 | |
| 319 ui::WindowShowState new_window_state = | 375 ui::WindowShowState new_window_state = |
| 320 ToWindowShowState(new_window_show_type); | 376 ToWindowShowState(new_window_show_type); |
| 377 base::AutoReset<bool> resetter(&ignore_property_change_, true); |
| 321 if (new_window_state != GetShowState()) | 378 if (new_window_state != GetShowState()) |
| 322 window_->SetProperty(aura::client::kShowStateKey, new_window_state); | 379 window_->SetProperty(aura::client::kShowStateKey, new_window_state); |
| 323 WindowShowType old_window_show_type = window_show_type_; | |
| 324 window_show_type_ = new_window_show_type; | 380 window_show_type_ = new_window_show_type; |
| 325 if (old_window_show_type != window_show_type_) { | 381 } |
| 326 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, | 382 |
| 327 OnWindowShowTypeChanged(this, old_window_show_type)); | 383 void WindowState::NotifyPreShowTypeChange(WindowShowType old_window_show_type) { |
| 328 } | 384 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, |
| 385 OnPreWindowShowTypeChange(this, old_window_show_type)); |
| 386 } |
| 387 |
| 388 void WindowState::NotifyPostShowTypeChange( |
| 389 WindowShowType old_window_show_type) { |
| 390 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_, |
| 391 OnPostWindowShowTypeChange(this, old_window_show_type)); |
| 392 } |
| 393 |
| 394 void WindowState::SetBoundsDirect(const gfx::Rect& bounds) { |
| 395 BoundsSetter().SetBounds(window_, bounds); |
| 396 } |
| 397 |
| 398 void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) { |
| 399 const int kBoundsChangeSlideDurationMs = 120; |
| 400 |
| 401 ui::Layer* layer = window_->layer(); |
| 402 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator()); |
| 403 slide_settings.SetPreemptionStrategy( |
| 404 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET); |
| 405 slide_settings.SetTransitionDuration( |
| 406 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs)); |
| 407 SetBoundsDirect(bounds); |
| 329 } | 408 } |
| 330 | 409 |
| 331 WindowState* GetActiveWindowState() { | 410 WindowState* GetActiveWindowState() { |
| 332 aura::Window* active = GetActiveWindow(); | 411 aura::Window* active = GetActiveWindow(); |
| 333 return active ? GetWindowState(active) : NULL; | 412 return active ? GetWindowState(active) : NULL; |
| 334 } | 413 } |
| 335 | 414 |
| 336 WindowState* GetWindowState(aura::Window* window) { | 415 WindowState* GetWindowState(aura::Window* window) { |
| 337 if (!window) | 416 if (!window) |
| 338 return NULL; | 417 return NULL; |
| 339 WindowState* settings = window->GetProperty(internal::kWindowStateKey); | 418 WindowState* settings = window->GetProperty(internal::kWindowStateKey); |
| 340 if(!settings) { | 419 if(!settings) { |
| 341 settings = new WindowState(window); | 420 settings = new WindowState(window); |
| 342 window->SetProperty(internal::kWindowStateKey, settings); | 421 window->SetProperty(internal::kWindowStateKey, settings); |
| 343 } | 422 } |
| 344 return settings; | 423 return settings; |
| 345 } | 424 } |
| 346 | 425 |
| 347 const WindowState* GetWindowState(const aura::Window* window) { | 426 const WindowState* GetWindowState(const aura::Window* window) { |
| 348 return GetWindowState(const_cast<aura::Window*>(window)); | 427 return GetWindowState(const_cast<aura::Window*>(window)); |
| 349 } | 428 } |
| 350 | 429 |
| 351 } // namespace wm | 430 } // namespace wm |
| 352 } // namespace ash | 431 } // namespace ash |
| OLD | NEW |