Move render_view_context_menu.* and related files out of tab_contents.
[chromium-blink-merge.git] / ash / wm / window_state.cc
bloba2e8aef7c059013e84a7b2f0f5e0a3bf80ddd77e
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.
5 #include "ash/wm/window_state.h"
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_types.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/scoped_layer_animation_settings.h"
25 #include "ui/gfx/display.h"
26 #include "ui/views/corewm/window_util.h"
28 namespace ash {
29 namespace wm {
31 namespace {
33 // A tentative class to set the bounds on the window.
34 // TODO(oshima): Once all logic is cleaned up, move this to the real layout
35 // manager with proper friendship.
36 class BoundsSetter : public aura::LayoutManager {
37 public:
38 BoundsSetter() {}
39 virtual ~BoundsSetter() {}
41 // aura::LayoutManager overrides:
42 virtual void OnWindowResized() OVERRIDE {}
43 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {}
44 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
45 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
46 virtual void OnChildWindowVisibilityChanged(
47 aura::Window* child, bool visible) OVERRIDE {}
48 virtual void SetChildBounds(
49 aura::Window* child, const gfx::Rect& requested_bounds) OVERRIDE {}
51 void SetBounds(aura::Window* window, const gfx::Rect& bounds) {
52 SetChildBoundsDirect(window, bounds);
55 private:
56 DISALLOW_COPY_AND_ASSIGN(BoundsSetter);
59 WMEvent WMEventFromShowState(ui::WindowShowState requested_show_state) {
60 switch (requested_show_state) {
61 case ui::SHOW_STATE_DEFAULT:
62 case ui::SHOW_STATE_NORMAL:
63 return NORMAL;
64 case ui::SHOW_STATE_MINIMIZED:
65 return MINIMIZE;
66 case ui::SHOW_STATE_MAXIMIZED:
67 return MAXIMIZE;
68 case ui::SHOW_STATE_FULLSCREEN:
69 return FULLSCREEN;
70 case ui::SHOW_STATE_INACTIVE:
71 return SHOW_INACTIVE;
72 case ui::SHOW_STATE_DETACHED:
73 case ui::SHOW_STATE_END:
74 NOTREACHED() << "No WMEvent defined for the show type:"
75 << requested_show_state;
77 return NORMAL;
80 } // namespace
82 WindowState::WindowState(aura::Window* window)
83 : window_(window),
84 window_position_managed_(false),
85 bounds_changed_by_user_(false),
86 panel_attached_(true),
87 ignored_by_shelf_(false),
88 can_consume_system_keys_(false),
89 top_row_keys_are_function_keys_(false),
90 unminimize_to_restore_bounds_(false),
91 hide_shelf_when_fullscreen_(true),
92 animate_to_fullscreen_(true),
93 minimum_visibility_(false),
94 ignore_property_change_(false),
95 window_show_type_(ToWindowShowType(GetShowState())),
96 current_state_(new DefaultState) {
97 window_->AddObserver(this);
98 #if defined(OS_CHROMEOS)
99 // NOTE(pkotwicz): Animating to immersive fullscreen does not look good. When
100 // switches::UseImmersiveFullscreenForAllWindows() returns true, most windows
101 // can be put into immersive fullscreen. It is not worth the added complexity
102 // to only animate to fullscreen if the window is put into immersive
103 // fullscreen.
104 animate_to_fullscreen_ = !switches::UseImmersiveFullscreenForAllWindows();
105 #endif
108 WindowState::~WindowState() {
111 bool WindowState::HasDelegate() const {
112 return delegate_;
115 void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) {
116 DCHECK(!delegate_.get());
117 delegate_ = delegate.Pass();
120 ui::WindowShowState WindowState::GetShowState() const {
121 return window_->GetProperty(aura::client::kShowStateKey);
124 bool WindowState::IsMinimized() const {
125 return GetShowState() == ui::SHOW_STATE_MINIMIZED;
128 bool WindowState::IsMaximized() const {
129 return GetShowState() == ui::SHOW_STATE_MAXIMIZED;
132 bool WindowState::IsFullscreen() const {
133 return GetShowState() == ui::SHOW_STATE_FULLSCREEN;
136 bool WindowState::IsMaximizedOrFullscreen() const {
137 ui::WindowShowState show_state(GetShowState());
138 return show_state == ui::SHOW_STATE_FULLSCREEN ||
139 show_state == ui::SHOW_STATE_MAXIMIZED;
142 bool WindowState::IsSnapped() const {
143 return window_show_type_ == SHOW_TYPE_LEFT_SNAPPED ||
144 window_show_type_ == SHOW_TYPE_RIGHT_SNAPPED;
147 bool WindowState::IsNormalShowType() const {
148 return window_show_type_ == SHOW_TYPE_NORMAL ||
149 window_show_type_ == SHOW_TYPE_DEFAULT;
152 bool WindowState::IsNormalOrSnapped() const {
153 return IsNormalShowType() || IsSnapped();
156 bool WindowState::IsActive() const {
157 return IsActiveWindow(window_);
160 bool WindowState::IsDocked() const {
161 return window_->parent() &&
162 window_->parent()->id() == internal::kShellWindowId_DockedContainer;
165 bool WindowState::CanMaximize() const {
166 return window_->GetProperty(aura::client::kCanMaximizeKey);
169 bool WindowState::CanMinimize() const {
170 internal::RootWindowController* controller =
171 internal::RootWindowController::ForWindow(window_);
172 if (!controller)
173 return false;
174 aura::Window* lockscreen = controller->GetContainer(
175 internal::kShellWindowId_LockScreenContainersContainer);
176 if (lockscreen->Contains(window_))
177 return false;
179 return true;
182 bool WindowState::CanResize() const {
183 return window_->GetProperty(aura::client::kCanResizeKey);
186 bool WindowState::CanActivate() const {
187 return views::corewm::CanActivateWindow(window_);
190 bool WindowState::CanSnap() const {
191 if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL ||
192 views::corewm::GetTransientParent(window_))
193 return false;
194 // If a window has a maximum size defined, snapping may make it too big.
195 return window_->delegate() ? window_->delegate()->GetMaximumSize().IsEmpty() :
196 true;
199 bool WindowState::HasRestoreBounds() const {
200 return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL;
203 void WindowState::Maximize() {
204 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
207 void WindowState::SnapLeftWithDefaultWidth() {
208 SnapWindowWithDefaultWidth(SHOW_TYPE_LEFT_SNAPPED);
211 void WindowState::SnapRightWithDefaultWidth() {
212 SnapWindowWithDefaultWidth(SHOW_TYPE_RIGHT_SNAPPED);
215 void WindowState::RequestBounds(const gfx::Rect& requested_bounds) {
216 current_state_->RequestBounds(this, requested_bounds);
219 void WindowState::Minimize() {
220 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
223 void WindowState::Unminimize() {
224 window_->SetProperty(
225 aura::client::kShowStateKey,
226 window_->GetProperty(aura::client::kRestoreShowStateKey));
227 window_->ClearProperty(aura::client::kRestoreShowStateKey);
230 void WindowState::Activate() {
231 ActivateWindow(window_);
234 void WindowState::Deactivate() {
235 DeactivateWindow(window_);
238 void WindowState::Restore() {
239 if (!IsNormalShowType())
240 OnWMEvent(NORMAL);
243 void WindowState::ToggleFullscreen() {
244 OnWMEvent(TOGGLE_FULLSCREEN);
247 void WindowState::OnWMEvent(WMEvent event) {
248 current_state_->OnWMEvent(this, event);
251 void WindowState::SetBoundsInScreen(
252 const gfx::Rect& bounds_in_screen) {
253 gfx::Rect bounds_in_parent =
254 ScreenUtil::ConvertRectFromScreen(window_->parent(),
255 bounds_in_screen);
256 window_->SetBounds(bounds_in_parent);
259 void WindowState::SaveCurrentBoundsForRestore() {
260 gfx::Rect bounds_in_screen =
261 ScreenUtil::ConvertRectToScreen(window_->parent(),
262 window_->bounds());
263 SetRestoreBoundsInScreen(bounds_in_screen);
266 gfx::Rect WindowState::GetRestoreBoundsInScreen() const {
267 return *window_->GetProperty(aura::client::kRestoreBoundsKey);
270 gfx::Rect WindowState::GetRestoreBoundsInParent() const {
271 return ScreenUtil::ConvertRectFromScreen(window_->parent(),
272 GetRestoreBoundsInScreen());
275 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) {
276 window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
279 void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) {
280 SetRestoreBoundsInScreen(
281 ScreenUtil::ConvertRectToScreen(window_->parent(), bounds));
284 void WindowState::ClearRestoreBounds() {
285 window_->ClearProperty(aura::client::kRestoreBoundsKey);
288 void WindowState::SetPreAutoManageWindowBounds(
289 const gfx::Rect& bounds) {
290 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds));
293 void WindowState::AddObserver(WindowStateObserver* observer) {
294 observer_list_.AddObserver(observer);
297 void WindowState::RemoveObserver(WindowStateObserver* observer) {
298 observer_list_.RemoveObserver(observer);
301 void WindowState::CreateDragDetails(aura::Window* window,
302 const gfx::Point& point_in_parent,
303 int window_component,
304 aura::client::WindowMoveSource source) {
305 drag_details_.reset(
306 new DragDetails(window, point_in_parent, window_component, source));
309 void WindowState::DeleteDragDetails() {
310 drag_details_.reset();
313 void WindowState::SetAndClearRestoreBounds() {
314 DCHECK(HasRestoreBounds());
315 SetBoundsInScreen(GetRestoreBoundsInScreen());
316 ClearRestoreBounds();
319 void WindowState::OnWindowPropertyChanged(aura::Window* window,
320 const void* key,
321 intptr_t old) {
322 DCHECK_EQ(window, window_);
323 if (key == aura::client::kShowStateKey && !ignore_property_change_)
324 OnWMEvent(WMEventFromShowState(GetShowState()));
327 void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) {
328 if (is_dragged() || !IsSnapped())
329 return;
330 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent(
331 window_);
332 if (window_show_type() == SHOW_TYPE_LEFT_SNAPPED)
333 bounds->set_x(maximized_bounds.x());
334 else if (window_show_type() == SHOW_TYPE_RIGHT_SNAPPED)
335 bounds->set_x(maximized_bounds.right() - bounds->width());
336 bounds->set_y(maximized_bounds.y());
337 bounds->set_height(maximized_bounds.height());
340 void WindowState::SnapWindowWithDefaultWidth(WindowShowType left_or_right) {
341 DCHECK(left_or_right == SHOW_TYPE_LEFT_SNAPPED ||
342 left_or_right == SHOW_TYPE_RIGHT_SNAPPED);
343 gfx::Rect bounds_in_parent(left_or_right == SHOW_TYPE_LEFT_SNAPPED ?
344 GetDefaultLeftSnappedWindowBoundsInParent(window()) :
345 GetDefaultRightSnappedWindowBoundsInParent(window()));
347 if (window_show_type_ == left_or_right) {
348 window_->SetBounds(bounds_in_parent);
349 return;
352 // Compute the bounds that the window will restore to. If the window does not
353 // already have restore bounds, it will be restored (when un-snapped) to the
354 // last bounds that it had before getting snapped.
355 gfx::Rect restore_bounds_in_screen(HasRestoreBounds() ?
356 GetRestoreBoundsInScreen() : window_->GetBoundsInScreen());
357 // Set the window's restore bounds so that WorkspaceLayoutManager knows
358 // which width to use when the snapped window is moved to the edge.
359 SetRestoreBoundsInParent(bounds_in_parent);
361 OnWMEvent(left_or_right == SHOW_TYPE_LEFT_SNAPPED ?
362 SNAP_LEFT : SNAP_RIGHT);
364 // TODO(varkha): Ideally the bounds should be changed in a LayoutManager upon
365 // observing the WindowShowType change.
366 // If the window is a child of kShellWindowId_DockedContainer such as during
367 // a drag, the window's bounds are not set in
368 // WorkspaceLayoutManager::OnWindowShowTypeChanged(). Set them here. Skip
369 // setting the bounds otherwise to avoid stopping the slide animation which
370 // was started as a result of OnWindowShowTypeChanged().
371 if (IsDocked())
372 window_->SetBounds(bounds_in_parent);
373 SetRestoreBoundsInScreen(restore_bounds_in_screen);
376 void WindowState::UpdateWindowShowType(WindowShowType new_window_show_type) {
377 ui::WindowShowState new_window_state =
378 ToWindowShowState(new_window_show_type);
379 if (new_window_state != GetShowState()) {
380 base::AutoReset<bool> resetter(&ignore_property_change_, true);
381 window_->SetProperty(aura::client::kShowStateKey, new_window_state);
383 window_show_type_ = new_window_show_type;
386 void WindowState::NotifyPreShowTypeChange(WindowShowType old_window_show_type) {
387 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
388 OnPreWindowShowTypeChange(this, old_window_show_type));
391 void WindowState::NotifyPostShowTypeChange(
392 WindowShowType old_window_show_type) {
393 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
394 OnPostWindowShowTypeChange(this, old_window_show_type));
397 void WindowState::SetBoundsDirect(const gfx::Rect& bounds) {
398 BoundsSetter().SetBounds(window_, bounds);
401 void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) {
402 gfx::Rect work_area_in_parent =
403 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_);
404 gfx::Rect child_bounds(bounds);
405 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds);
406 SetBoundsDirect(child_bounds);
409 void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) {
410 const int kBoundsChangeSlideDurationMs = 120;
412 ui::Layer* layer = window_->layer();
413 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator());
414 slide_settings.SetPreemptionStrategy(
415 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
416 slide_settings.SetTransitionDuration(
417 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs));
418 SetBoundsDirect(bounds);
421 void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) {
422 // Some test results in invoking CrossFadeToBounds when window is not visible.
423 // No animation is necessary in that case, thus just change the bounds and
424 // quit.
425 if (!window_->TargetVisibility()) {
426 SetBoundsConstrained(new_bounds);
427 return;
430 const gfx::Rect old_bounds = window_->bounds();
432 // Create fresh layers for the window and all its children to paint into.
433 // Takes ownership of the old layer and all its children, which will be
434 // cleaned up after the animation completes.
435 // Specify |set_bounds| to true here to keep the old bounds in the child
436 // windows of |window|.
437 ui::Layer* old_layer = views::corewm::RecreateWindowLayers(window_, true);
438 ui::Layer* new_layer = window_->layer();
440 // Resize the window to the new size, which will force a layout and paint.
441 SetBoundsDirect(new_bounds);
443 // Ensure the higher-resolution layer is on top.
444 bool old_on_top = (old_bounds.width() > new_bounds.width());
445 if (old_on_top)
446 old_layer->parent()->StackBelow(new_layer, old_layer);
447 else
448 old_layer->parent()->StackAbove(new_layer, old_layer);
450 CrossFadeAnimation(window_, old_layer, gfx::Tween::EASE_OUT);
453 WindowState* GetActiveWindowState() {
454 aura::Window* active = GetActiveWindow();
455 return active ? GetWindowState(active) : NULL;
458 WindowState* GetWindowState(aura::Window* window) {
459 if (!window)
460 return NULL;
461 WindowState* settings = window->GetProperty(internal::kWindowStateKey);
462 if(!settings) {
463 settings = new WindowState(window);
464 window->SetProperty(internal::kWindowStateKey, settings);
466 return settings;
469 const WindowState* GetWindowState(const aura::Window* window) {
470 return GetWindowState(const_cast<aura::Window*>(window));
473 } // namespace wm
474 } // namespace ash