// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "ash/wm/compact_layout_manager.h" #include #include "ash/shell.h" #include "ash/shell_delegate.h" #include "ash/shell_window_ids.h" #include "ash/wm/window_util.h" #include "ui/aura/client/aura_constants.h" #include "ui/aura/window.h" #include "ui/gfx/compositor/layer.h" #include "ui/gfx/compositor/layer_animation_sequence.h" #include "ui/gfx/compositor/scoped_layer_animation_settings.h" #include "ui/gfx/screen.h" #include "ui/views/widget/widget.h" namespace ash { namespace internal { namespace { typedef std::vector WindowList; typedef std::vector::const_iterator WindowListConstIter; // Convenience method to get the layer of this container. ui::Layer* GetDefaultContainerLayer() { return Shell::GetInstance()->GetContainer( internal::kShellWindowId_DefaultContainer)->layer(); } // Whether it is a window that should be animated on entrance. bool ShouldAnimateOnEntrance(aura::Window* window) { return window && window->type() == aura::client::WINDOW_TYPE_NORMAL && wm::IsWindowMaximized(window); } // Adjust layer bounds to grow or shrink in |delta_width|. void AdjustContainerLayerWidth(int delta_width) { gfx::Rect bounds(GetDefaultContainerLayer()->bounds()); bounds.set_width(bounds.width() + delta_width); GetDefaultContainerLayer()->SetBounds(bounds); } } // namespace ///////////////////////////////////////////////////////////////////////////// // CompactLayoutManager, public: CompactLayoutManager::CompactLayoutManager() : status_area_widget_(NULL), current_window_(NULL) { } CompactLayoutManager::~CompactLayoutManager() { } ///////////////////////////////////////////////////////////////////////////// // CompactLayoutManager, LayoutManager overrides: void CompactLayoutManager::OnWindowAddedToLayout(aura::Window* child) { // Windows added to this container does not need extra animation. if (child->type() == aura::client::WINDOW_TYPE_NORMAL) child->SetProperty(aura::client::kAnimationsDisabledKey, true); BaseLayoutManager::OnWindowAddedToLayout(child); UpdateStatusAreaVisibility(); if (windows().size() > 1 && child->type() == aura::client::WINDOW_TYPE_NORMAL) { // The first window is already contained in the current layer, // add subsequent windows to layer bounds calculation. AdjustContainerLayerWidth(child->bounds().width()); } } void CompactLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) { BaseLayoutManager::OnWillRemoveWindowFromLayout(child); UpdateStatusAreaVisibility(); if (windows().size() > 1 && ShouldAnimateOnEntrance(child)) AdjustContainerLayerWidth(-child->bounds().width()); if (child == current_window_) { LayoutWindows(current_window_); SwitchToReplacementWindow(); } // Allow window to be animated by others. if (child->type() == aura::client::WINDOW_TYPE_NORMAL) child->SetProperty(aura::client::kAnimationsDisabledKey, false); } void CompactLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child, bool visible) { BaseLayoutManager::OnChildWindowVisibilityChanged(child, visible); UpdateStatusAreaVisibility(); if (ShouldAnimateOnEntrance(child)) { LayoutWindows(visible ? NULL : child); if (visible) { current_window_ = child; AnimateSlideTo(child->bounds().x()); } else if (child == current_window_) { SwitchToReplacementWindow(); } } } void CompactLayoutManager::SetChildBounds(aura::Window* child, const gfx::Rect& requested_bounds) { gfx::Rect child_bounds(requested_bounds); // Avoid a janky resize on startup by ensuring the initial bounds fill the // screen. if (wm::IsWindowMaximized(child)) child_bounds = gfx::Screen::GetMonitorWorkAreaNearestWindow(child); else if (wm::IsWindowFullscreen(child)) child_bounds = gfx::Screen::GetMonitorAreaNearestWindow(child); else if (current_window_) { // All other windows should be offset by the current viewport. int offset_x = current_window_->bounds().x(); child_bounds.Offset(offset_x, 0); } SetChildBoundsDirect(child, child_bounds); } ///////////////////////////////////////////////////////////////////////////// // CompactLayoutManager, aura::WindowObserver overrides: void CompactLayoutManager::OnWindowPropertyChanged(aura::Window* window, const void* key, intptr_t old) { BaseLayoutManager::OnWindowPropertyChanged(window, key, old); if (key == aura::client::kShowStateKey) UpdateStatusAreaVisibility(); } void CompactLayoutManager::OnWindowStackingChanged(aura::Window* window) { if (!current_window_ || ShouldAnimateOnEntrance(window)) { if (current_window_ != window) { LayoutWindows(current_window_); current_window_ = window; } else { // Same window as |current_window_|, and already animating. if (GetDefaultContainerLayer()->GetAnimator()->is_animating()) return; } // Animate to |window| when there is a stacking change. AnimateSlideTo(window->bounds().x()); } } ///////////////////////////////////////////////////////////////////////////// // CompactLayoutManager, AnimationDelegate overrides: void CompactLayoutManager::OnImplicitAnimationsCompleted() { if (!GetDefaultContainerLayer()->GetAnimator()->is_animating()) HideWindows(); } ////////////////////////////////////////////////////////////////////////////// // CompactLayoutManager, private: void CompactLayoutManager::UpdateStatusAreaVisibility() { if (!status_area_widget_) return; // Full screen windows should hide the status area widget. bool has_fullscreen = wm::HasFullscreenWindow(windows()); bool widget_visible = status_area_widget_->IsVisible(); if (has_fullscreen && widget_visible) status_area_widget_->Hide(); else if (!has_fullscreen && !widget_visible) status_area_widget_->Show(); } void CompactLayoutManager::AnimateSlideTo(int offset_x) { GetDefaultContainerLayer()->GetAnimator()->RemoveObserver(this); ui::ScopedLayerAnimationSettings settings( GetDefaultContainerLayer()->GetAnimator()); settings.AddObserver(this); ui::Transform transform; transform.ConcatTranslate(-offset_x, 0); GetDefaultContainerLayer()->SetTransform(transform); // Will be animated! } void CompactLayoutManager::LayoutWindows(aura::Window* skip) { ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate(); const WindowList& windows_list = shell_delegate->GetCycleWindowList( ShellDelegate::SOURCE_KEYBOARD, ShellDelegate::ORDER_LINEAR); int new_x = 0; for (WindowListConstIter const_it = windows_list.begin(); const_it != windows_list.end(); ++const_it) { if (*const_it != skip) { gfx::Rect new_bounds((*const_it)->bounds()); new_bounds.set_x(new_x); SetChildBoundsDirect(*const_it, new_bounds); (*const_it)->layer()->SetVisible(true); new_x += (*const_it)->bounds().width(); } } } void CompactLayoutManager::HideWindows() { // If we do not know which one is the current window, or if the current // window is not visible, do not attempt to hide the windows. if (current_window_ == NULL) return; // Current window should be visible, if not it is an error and we shouldn't // proceed. if (!current_window_->layer()->visible()) NOTREACHED() << "Current window is invisible"; ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate(); const WindowList& windows_list = shell_delegate->GetCycleWindowList( ShellDelegate::SOURCE_KEYBOARD, ShellDelegate::ORDER_LINEAR); for (WindowListConstIter const_it = windows_list.begin(); const_it != windows_list.end(); ++const_it) { if (*const_it != current_window_) (*const_it)->layer()->SetVisible(false); } } aura::Window* CompactLayoutManager::FindReplacementWindow( aura::Window* window) { ShellDelegate* shell_delegate = ash::Shell::GetInstance()->delegate(); const WindowList& windows_list = shell_delegate->GetCycleWindowList( ShellDelegate::SOURCE_KEYBOARD, ShellDelegate::ORDER_LINEAR); WindowListConstIter const_it = std::find(windows_list.begin(), windows_list.end(), window); if (windows_list.size() > 1 && const_it != windows_list.end()) { do { ++const_it; if (const_it == windows_list.end()) const_it = windows_list.begin(); } while (*const_it != window && !(*const_it)->IsVisible()); if (*const_it != window) return *const_it; } return NULL; } void CompactLayoutManager::SwitchToReplacementWindow() { current_window_ = FindReplacementWindow(current_window_); if (current_window_) { wm::ActivateWindow(current_window_); AnimateSlideTo(current_window_->bounds().x()); } } } // namespace internal } // namespace ash