Fix translation extraction failure.
[chromium-blink-merge.git] / ash / wm / window_state.cc
blobb314c95336579081d17ae255f13b692a2a027b39
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_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"
30 namespace ash {
31 namespace wm {
33 namespace {
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.
38 class BoundsSetter : public aura::LayoutManager {
39 public:
40 BoundsSetter() {}
41 ~BoundsSetter() override {}
43 // aura::LayoutManager overrides:
44 void OnWindowResized() override {}
45 void OnWindowAddedToLayout(aura::Window* child) override {}
46 void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
47 void OnWindowRemovedFromLayout(aura::Window* child) override {}
48 void OnChildWindowVisibilityChanged(aura::Window* child,
49 bool visible) override {}
50 void SetChildBounds(aura::Window* child,
51 const gfx::Rect& requested_bounds) override {}
53 void SetBounds(aura::Window* window, const gfx::Rect& bounds) {
54 SetChildBoundsDirect(window, bounds);
57 private:
58 DISALLOW_COPY_AND_ASSIGN(BoundsSetter);
61 WMEventType 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_DOCKED:
75 return WM_EVENT_DOCK;
76 case ui::SHOW_STATE_END:
77 NOTREACHED() << "No WMEvent defined for the show state:"
78 << requested_show_state;
80 return WM_EVENT_NORMAL;
83 } // namespace
85 WindowState::~WindowState() {
86 // WindowState is registered as an owned property of |window_|, and window
87 // unregisters all of its observers in its d'tor before destroying its
88 // properties. As a result, window_->RemoveObserver() doesn't need to (and
89 // shouldn't) be called here.
92 bool WindowState::HasDelegate() const {
93 return delegate_;
96 void WindowState::SetDelegate(scoped_ptr<WindowStateDelegate> delegate) {
97 DCHECK(!delegate_.get());
98 delegate_ = delegate.Pass();
101 WindowStateType WindowState::GetStateType() const {
102 return current_state_->GetType();
105 bool WindowState::IsMinimized() const {
106 return GetStateType() == WINDOW_STATE_TYPE_MINIMIZED ||
107 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED;
110 bool WindowState::IsMaximized() const {
111 return GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
114 bool WindowState::IsFullscreen() const {
115 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN;
118 bool WindowState::IsMaximizedOrFullscreen() const {
119 return GetStateType() == WINDOW_STATE_TYPE_FULLSCREEN ||
120 GetStateType() == WINDOW_STATE_TYPE_MAXIMIZED;
123 bool WindowState::IsSnapped() const {
124 return GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED ||
125 GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED;
128 bool WindowState::IsNormalStateType() const {
129 return GetStateType() == WINDOW_STATE_TYPE_NORMAL ||
130 GetStateType() == WINDOW_STATE_TYPE_DEFAULT;
133 bool WindowState::IsNormalOrSnapped() const {
134 return IsNormalStateType() || IsSnapped();
137 bool WindowState::IsActive() const {
138 return IsActiveWindow(window_);
141 bool WindowState::IsDocked() const {
142 return GetStateType() == WINDOW_STATE_TYPE_DOCKED ||
143 GetStateType() == WINDOW_STATE_TYPE_DOCKED_MINIMIZED;
146 bool WindowState::IsUserPositionable() const {
147 return (window()->type() == ui::wm::WINDOW_TYPE_NORMAL ||
148 window()->type() == ui::wm::WINDOW_TYPE_PANEL);
151 bool WindowState::CanMaximize() const {
152 // Window must have the kCanMaximizeKey and have no maximum width or height.
153 if (!window()->GetProperty(aura::client::kCanMaximizeKey))
154 return false;
156 if (!window()->delegate())
157 return true;
159 gfx::Size max_size = window_->delegate()->GetMaximumSize();
160 return !max_size.width() && !max_size.height();
163 bool WindowState::CanMinimize() const {
164 return window()->GetProperty(aura::client::kCanMinimizeKey);
167 bool WindowState::CanResize() const {
168 return window_->GetProperty(aura::client::kCanResizeKey);
171 bool WindowState::CanActivate() const {
172 return ::wm::CanActivateWindow(window_);
175 bool WindowState::CanSnap() const {
176 if (!CanResize() || window_->type() == ui::wm::WINDOW_TYPE_PANEL ||
177 ::wm::GetTransientParent(window_))
178 return false;
179 // If a window cannot be maximized, assume it cannot snap either.
180 // TODO(oshima): We should probably snap if the maximum size is greater than
181 // the snapped size.
182 return CanMaximize();
185 bool WindowState::HasRestoreBounds() const {
186 return window_->GetProperty(aura::client::kRestoreBoundsKey) != NULL;
189 void WindowState::Maximize() {
190 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MAXIMIZED);
193 void WindowState::Minimize() {
194 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_MINIMIZED);
197 void WindowState::Unminimize() {
198 window_->SetProperty(
199 aura::client::kShowStateKey,
200 window_->GetProperty(aura::client::kRestoreShowStateKey));
201 window_->ClearProperty(aura::client::kRestoreShowStateKey);
204 void WindowState::Activate() {
205 ActivateWindow(window_);
208 void WindowState::Deactivate() {
209 DeactivateWindow(window_);
212 void WindowState::Restore() {
213 if (!IsNormalStateType()) {
214 const WMEvent event(WM_EVENT_NORMAL);
215 OnWMEvent(&event);
219 void WindowState::DisableAlwaysOnTop(aura::Window* window_on_top) {
220 DCHECK(window_on_top);
221 if (GetAlwaysOnTop()) {
222 // |window_| is hidden first to avoid canceling fullscreen mode when it is
223 // no longer always on top and gets added to default container. This avoids
224 // sending redundant OnFullscreenStateChanged to the layout manager. The
225 // |window_| visibility is restored after it no longer obscures the
226 // |window_on_top|.
227 bool visible = window_->IsVisible();
228 if (visible)
229 window_->Hide();
230 window_->SetProperty(aura::client::kAlwaysOnTopKey, false);
231 // Technically it is possible that a |window_| could make itself
232 // always_on_top really quickly. This is probably not a realistic case but
233 // check if the two windows are in the same container just in case.
234 if (window_on_top->parent() == window_->parent())
235 window_->parent()->StackChildAbove(window_on_top, window_);
236 if (visible)
237 window_->Show();
238 cached_always_on_top_ = true;
242 void WindowState::RestoreAlwaysOnTop() {
243 if (delegate() && delegate()->RestoreAlwaysOnTop(this))
244 return;
245 if (cached_always_on_top_) {
246 cached_always_on_top_ = false;
247 window_->SetProperty(aura::client::kAlwaysOnTopKey, true);
251 void WindowState::OnWMEvent(const WMEvent* event) {
252 current_state_->OnWMEvent(this, event);
255 void WindowState::SaveCurrentBoundsForRestore() {
256 gfx::Rect bounds_in_screen =
257 ScreenUtil::ConvertRectToScreen(window_->parent(),
258 window_->bounds());
259 SetRestoreBoundsInScreen(bounds_in_screen);
262 gfx::Rect WindowState::GetRestoreBoundsInScreen() const {
263 return *window_->GetProperty(aura::client::kRestoreBoundsKey);
266 gfx::Rect WindowState::GetRestoreBoundsInParent() const {
267 return ScreenUtil::ConvertRectFromScreen(window_->parent(),
268 GetRestoreBoundsInScreen());
271 void WindowState::SetRestoreBoundsInScreen(const gfx::Rect& bounds) {
272 window_->SetProperty(aura::client::kRestoreBoundsKey, new gfx::Rect(bounds));
275 void WindowState::SetRestoreBoundsInParent(const gfx::Rect& bounds) {
276 SetRestoreBoundsInScreen(
277 ScreenUtil::ConvertRectToScreen(window_->parent(), bounds));
280 void WindowState::ClearRestoreBounds() {
281 window_->ClearProperty(aura::client::kRestoreBoundsKey);
284 scoped_ptr<WindowState::State> WindowState::SetStateObject(
285 scoped_ptr<WindowState::State> new_state) {
286 current_state_->DetachState(this);
287 scoped_ptr<WindowState::State> old_object = current_state_.Pass();
288 current_state_ = new_state.Pass();
289 current_state_->AttachState(this, old_object.get());
290 return old_object.Pass();
293 void WindowState::SetPreAutoManageWindowBounds(
294 const gfx::Rect& bounds) {
295 pre_auto_manage_window_bounds_.reset(new gfx::Rect(bounds));
298 void WindowState::AddObserver(WindowStateObserver* observer) {
299 observer_list_.AddObserver(observer);
302 void WindowState::RemoveObserver(WindowStateObserver* observer) {
303 observer_list_.RemoveObserver(observer);
306 void WindowState::set_bounds_changed_by_user(bool bounds_changed_by_user) {
307 bounds_changed_by_user_ = bounds_changed_by_user;
308 if (bounds_changed_by_user)
309 pre_auto_manage_window_bounds_.reset();
312 void WindowState::CreateDragDetails(aura::Window* window,
313 const gfx::Point& point_in_parent,
314 int window_component,
315 aura::client::WindowMoveSource source) {
316 drag_details_.reset(
317 new DragDetails(window, point_in_parent, window_component, source));
320 void WindowState::DeleteDragDetails() {
321 drag_details_.reset();
324 void WindowState::SetAndClearRestoreBounds() {
325 DCHECK(HasRestoreBounds());
326 SetBoundsInScreen(GetRestoreBoundsInScreen());
327 ClearRestoreBounds();
330 void WindowState::OnWindowPropertyChanged(aura::Window* window,
331 const void* key,
332 intptr_t old) {
333 DCHECK_EQ(window, window_);
334 if (key == aura::client::kShowStateKey && !ignore_property_change_) {
335 WMEvent event(WMEventTypeFromShowState(GetShowState()));
336 OnWMEvent(&event);
340 WindowState::WindowState(aura::Window* window)
341 : window_(window),
342 window_position_managed_(false),
343 bounds_changed_by_user_(false),
344 panel_attached_(true),
345 ignored_by_shelf_(false),
346 can_consume_system_keys_(false),
347 top_row_keys_are_function_keys_(false),
348 unminimize_to_restore_bounds_(false),
349 in_immersive_fullscreen_(false),
350 hide_shelf_when_fullscreen_(true),
351 minimum_visibility_(false),
352 can_be_dragged_(true),
353 cached_always_on_top_(false),
354 ignore_property_change_(false),
355 current_state_(new DefaultState(ToWindowStateType(GetShowState()))) {
356 window_->AddObserver(this);
359 bool WindowState::GetAlwaysOnTop() const {
360 return window_->GetProperty(aura::client::kAlwaysOnTopKey);
363 ui::WindowShowState WindowState::GetShowState() const {
364 return window_->GetProperty(aura::client::kShowStateKey);
367 void WindowState::SetBoundsInScreen(
368 const gfx::Rect& bounds_in_screen) {
369 gfx::Rect bounds_in_parent =
370 ScreenUtil::ConvertRectFromScreen(window_->parent(),
371 bounds_in_screen);
372 window_->SetBounds(bounds_in_parent);
375 void WindowState::AdjustSnappedBounds(gfx::Rect* bounds) {
376 if (is_dragged() || !IsSnapped())
377 return;
378 gfx::Rect maximized_bounds = ScreenUtil::GetMaximizedWindowBoundsInParent(
379 window_);
380 if (GetStateType() == WINDOW_STATE_TYPE_LEFT_SNAPPED)
381 bounds->set_x(maximized_bounds.x());
382 else if (GetStateType() == WINDOW_STATE_TYPE_RIGHT_SNAPPED)
383 bounds->set_x(maximized_bounds.right() - bounds->width());
384 bounds->set_y(maximized_bounds.y());
385 bounds->set_height(maximized_bounds.height());
388 void WindowState::UpdateWindowShowStateFromStateType() {
389 ui::WindowShowState new_window_state =
390 ToWindowShowState(current_state_->GetType());
391 if (new_window_state != GetShowState()) {
392 base::AutoReset<bool> resetter(&ignore_property_change_, true);
393 window_->SetProperty(aura::client::kShowStateKey, new_window_state);
397 void WindowState::NotifyPreStateTypeChange(
398 WindowStateType old_window_state_type) {
399 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
400 OnPreWindowStateTypeChange(this, old_window_state_type));
403 void WindowState::NotifyPostStateTypeChange(
404 WindowStateType old_window_state_type) {
405 FOR_EACH_OBSERVER(WindowStateObserver, observer_list_,
406 OnPostWindowStateTypeChange(this, old_window_state_type));
409 void WindowState::SetBoundsDirect(const gfx::Rect& bounds) {
410 gfx::Rect actual_new_bounds(bounds);
411 // Ensure we don't go smaller than our minimum bounds in "normal" window
412 // modes
413 if (window_->delegate() && !IsMaximized() && !IsFullscreen()) {
414 // Get the minimum usable size of the minimum size and the screen size.
415 gfx::Size min_size = window_->delegate()->GetMinimumSize();
416 min_size.SetToMin(gfx::Screen::GetScreenFor(
417 window_)->GetDisplayNearestWindow(window_).work_area().size());
419 actual_new_bounds.set_width(
420 std::max(min_size.width(), actual_new_bounds.width()));
421 actual_new_bounds.set_height(
422 std::max(min_size.height(), actual_new_bounds.height()));
424 BoundsSetter().SetBounds(window_, actual_new_bounds);
425 SnapWindowToPixelBoundary(window_);
428 void WindowState::SetBoundsConstrained(const gfx::Rect& bounds) {
429 gfx::Rect work_area_in_parent =
430 ScreenUtil::GetDisplayWorkAreaBoundsInParent(window_);
431 gfx::Rect child_bounds(bounds);
432 AdjustBoundsSmallerThan(work_area_in_parent.size(), &child_bounds);
433 SetBoundsDirect(child_bounds);
436 void WindowState::SetBoundsDirectAnimated(const gfx::Rect& bounds) {
437 const int kBoundsChangeSlideDurationMs = 120;
439 ui::Layer* layer = window_->layer();
440 ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator());
441 slide_settings.SetPreemptionStrategy(
442 ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
443 slide_settings.SetTransitionDuration(
444 base::TimeDelta::FromMilliseconds(kBoundsChangeSlideDurationMs));
445 SetBoundsDirect(bounds);
448 void WindowState::SetBoundsDirectCrossFade(const gfx::Rect& new_bounds) {
449 // Some test results in invoking CrossFadeToBounds when window is not visible.
450 // No animation is necessary in that case, thus just change the bounds and
451 // quit.
452 if (!window_->TargetVisibility()) {
453 SetBoundsConstrained(new_bounds);
454 return;
457 const gfx::Rect old_bounds = window_->bounds();
459 // Create fresh layers for the window and all its children to paint into.
460 // Takes ownership of the old layer and all its children, which will be
461 // cleaned up after the animation completes.
462 // Specify |set_bounds| to true here to keep the old bounds in the child
463 // windows of |window|.
464 scoped_ptr<ui::LayerTreeOwner> old_layer_owner =
465 ::wm::RecreateLayers(window_);
466 ui::Layer* old_layer = old_layer_owner->root();
467 DCHECK(old_layer);
468 ui::Layer* new_layer = window_->layer();
470 // Resize the window to the new size, which will force a layout and paint.
471 SetBoundsDirect(new_bounds);
473 // Ensure the higher-resolution layer is on top.
474 bool old_on_top = (old_bounds.width() > new_bounds.width());
475 if (old_on_top)
476 old_layer->parent()->StackBelow(new_layer, old_layer);
477 else
478 old_layer->parent()->StackAbove(new_layer, old_layer);
480 CrossFadeAnimation(window_, old_layer_owner.Pass(), gfx::Tween::EASE_OUT);
483 WindowState* GetActiveWindowState() {
484 aura::Window* active = GetActiveWindow();
485 return active ? GetWindowState(active) : NULL;
488 WindowState* GetWindowState(aura::Window* window) {
489 if (!window)
490 return NULL;
491 WindowState* settings = window->GetProperty(kWindowStateKey);
492 if(!settings) {
493 settings = new WindowState(window);
494 window->SetProperty(kWindowStateKey, settings);
496 return settings;
499 const WindowState* GetWindowState(const aura::Window* window) {
500 return GetWindowState(const_cast<aura::Window*>(window));
503 } // namespace wm
504 } // namespace ash