This source file includes following definitions.
- visible_background_change_type_
- SetBackgroundBounds
- SetBackgroundType
- OnNativeWidgetVisibilityChanged
- OnNativeWidgetPaint
- UpdateBackground
- InitWidget
- UpdateBackground
- IsPopupOrTransient
- IsUsedByLayout
- UndockWindow
- GetWindowWidthCloseTo
- GetWindowHeightCloseTo
- ShelfWindowObserver
- ShelfWindowObserver
- OnWindowBoundsChanged
- shelf_bounds_in_screen
- background_widget_
- Shutdown
- AddObserver
- RemoveObserver
- StartDragging
- DockDraggedWindow
- UndockDraggedWindow
- FinishDragging
- SetShelf
- GetAlignmentOfWindow
- CalculateAlignment
- CanDockWindow
- OnShelfBoundsChanged
- OnWindowResized
- OnWindowAddedToLayout
- OnWindowRemovedFromLayout
- OnChildWindowVisibilityChanged
- SetChildBounds
- OnDisplayWorkAreaInsetsChanged
- OnFullscreenStateChanged
- OnShelfAlignmentChanged
- OnBackgroundUpdated
- OnPreWindowStateTypeChange
- OnWindowBoundsChanged
- OnWindowVisibilityChanging
- OnWindowDestroying
- OnWindowActivated
- MaybeMinimizeChildrenExcept
- MinimizeDockedWindow
- RestoreDockedWindow
- RecordUmaAction
- UpdateDockedWidth
- OnDraggedWindowDocked
- OnDraggedWindowUndocked
- IsAnyWindowDocked
- GetEdgeNearestWindow
- Relayout
- CalculateWindowHeightsAndRemainingRoom
- CalculateIdealWidth
- FanOutChildren
- UpdateDockBounds
- UpdateStacking
- OnKeyboardBoundsChanging
#include "ash/wm/dock/docked_window_layout_manager.h"
#include "ash/ash_switches.h"
#include "ash/screen_util.h"
#include "ash/shelf/shelf.h"
#include "ash/shelf/shelf_constants.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_types.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/coordinate_conversion.h"
#include "ash/wm/window_animations.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "ash/wm/workspace_controller.h"
#include "base/auto_reset.h"
#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "grit/ash_resources.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/rect.h"
#include "ui/views/background.h"
#include "ui/wm/core/window_util.h"
#include "ui/wm/public/activation_client.h"
namespace ash {
const int DockedWindowLayoutManager::kMaxDockWidth = 360;
const int DockedWindowLayoutManager::kMinDockWidth = 200;
const int DockedWindowLayoutManager::kMinDockGap = 2;
const int DockedWindowLayoutManager::kIdealWidth = 250;
const int kMinimumHeight = 250;
const int kSlideDurationMs = 120;
const int kFadeDurationMs = 60;
const int kMinimizeDurationMs = 720;
class DockedBackgroundWidget : public views::Widget,
public BackgroundAnimatorDelegate {
public:
explicit DockedBackgroundWidget(aura::Window* container)
: alignment_(DOCKED_ALIGNMENT_NONE),
background_animator_(this, 0, kShelfBackgroundAlpha),
alpha_(0),
opaque_background_(ui::LAYER_SOLID_COLOR),
visible_background_type_(SHELF_BACKGROUND_DEFAULT),
visible_background_change_type_(BACKGROUND_CHANGE_IMMEDIATE) {
InitWidget(container);
}
void SetBackgroundBounds(const gfx::Rect bounds, DockedAlignment alignment) {
SetBounds(bounds);
opaque_background_.SetBounds(gfx::Rect(bounds.size()));
alignment_ = alignment;
}
void SetBackgroundType(ShelfBackgroundType background_type,
BackgroundAnimatorChangeType change_type) {
visible_background_type_ = background_type;
visible_background_change_type_ = change_type;
if (IsVisible())
UpdateBackground();
}
virtual void OnNativeWidgetVisibilityChanged(bool visible) OVERRIDE {
views::Widget::OnNativeWidgetVisibilityChanged(visible);
UpdateBackground();
}
virtual void OnNativeWidgetPaint(gfx::Canvas* canvas) OVERRIDE {
const gfx::ImageSkia& shelf_background(
alignment_ == DOCKED_ALIGNMENT_LEFT ?
shelf_background_left_ : shelf_background_right_);
gfx::Rect rect = gfx::Rect(GetWindowBoundsInScreen().size());
SkPaint paint;
paint.setAlpha(alpha_);
canvas->DrawImageInt(shelf_background,
0,
0,
shelf_background.width(),
shelf_background.height(),
alignment_ == DOCKED_ALIGNMENT_LEFT
? rect.width() - shelf_background.width()
: 0,
0,
shelf_background.width(),
rect.height(),
false,
paint);
canvas->DrawImageInt(
shelf_background,
alignment_ == DOCKED_ALIGNMENT_LEFT ? 0 : shelf_background.width() - 1,
0,
1,
shelf_background.height(),
alignment_ == DOCKED_ALIGNMENT_LEFT ? 0 : shelf_background.width(),
0,
rect.width() - shelf_background.width(),
rect.height(),
false,
paint);
}
virtual void UpdateBackground(int alpha) OVERRIDE {
alpha_ = alpha;
SchedulePaintInRect(gfx::Rect(GetWindowBoundsInScreen().size()));
}
private:
void InitWidget(aura::Window* parent) {
views::Widget::InitParams params;
params.type = views::Widget::InitParams::TYPE_POPUP;
params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
params.can_activate = false;
params.keep_on_top = false;
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.parent = parent;
params.accept_events = false;
set_focus_on_creation(false);
Init(params);
SetVisibilityChangedAnimationsEnabled(false);
GetNativeWindow()->SetProperty(kStayInSameRootWindowKey, true);
opaque_background_.SetColor(SK_ColorBLACK);
opaque_background_.SetBounds(gfx::Rect(GetWindowBoundsInScreen().size()));
opaque_background_.SetOpacity(0.0f);
GetNativeWindow()->layer()->Add(&opaque_background_);
Hide();
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
gfx::ImageSkia shelf_background =
*rb.GetImageSkiaNamed(IDR_AURA_LAUNCHER_BACKGROUND);
shelf_background_left_ = gfx::ImageSkiaOperations::CreateRotatedImage(
shelf_background, SkBitmapOperations::ROTATION_90_CW);
shelf_background_right_ = gfx::ImageSkiaOperations::CreateRotatedImage(
shelf_background, SkBitmapOperations::ROTATION_270_CW);
}
void UpdateBackground() {
ShelfBackgroundType background_type = IsVisible() ?
visible_background_type_ : SHELF_BACKGROUND_DEFAULT;
BackgroundAnimatorChangeType change_type = IsVisible() ?
visible_background_change_type_ : BACKGROUND_CHANGE_IMMEDIATE;
float target_opacity =
(background_type == SHELF_BACKGROUND_MAXIMIZED) ? 1.0f : 0.0f;
scoped_ptr<ui::ScopedLayerAnimationSettings> opaque_background_animation;
if (change_type != BACKGROUND_CHANGE_IMMEDIATE) {
opaque_background_animation.reset(new ui::ScopedLayerAnimationSettings(
opaque_background_.GetAnimator()));
opaque_background_animation->SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kTimeToSwitchBackgroundMs));
}
opaque_background_.SetOpacity(target_opacity);
background_animator_.SetPaintsBackground(
background_type != SHELF_BACKGROUND_DEFAULT,
change_type);
SchedulePaintInRect(gfx::Rect(GetWindowBoundsInScreen().size()));
}
DockedAlignment alignment_;
BackgroundAnimator background_animator_;
int alpha_;
ui::Layer opaque_background_;
gfx::ImageSkia shelf_background_left_;
gfx::ImageSkia shelf_background_right_;
ShelfBackgroundType visible_background_type_;
BackgroundAnimatorChangeType visible_background_change_type_;
DISALLOW_COPY_AND_ASSIGN(DockedBackgroundWidget);
};
namespace {
bool IsPopupOrTransient(const aura::Window* window) {
return (window->type() == ui::wm::WINDOW_TYPE_POPUP ||
::wm::GetTransientParent(window));
}
bool IsUsedByLayout(const aura::Window* window) {
return (window->IsVisible() &&
!wm::GetWindowState(window)->IsMinimized() &&
!IsPopupOrTransient(window));
}
void UndockWindow(aura::Window* window) {
gfx::Rect previous_bounds = window->bounds();
aura::Window* old_parent = window->parent();
aura::client::ParentWindowWithContext(window, window, gfx::Rect());
if (window->parent() != old_parent)
wm::ReparentTransientChildrenOfChild(window, old_parent, window->parent());
window->layer()->SetBounds(previous_bounds);
}
int GetWindowWidthCloseTo(const aura::Window* window, int target_width) {
if (!wm::GetWindowState(window)->CanResize()) {
DCHECK_LE(window->bounds().width(),
DockedWindowLayoutManager::kMaxDockWidth);
return window->bounds().width();
}
int width = std::max(DockedWindowLayoutManager::kMinDockWidth,
std::min(target_width,
DockedWindowLayoutManager::kMaxDockWidth));
if (window->delegate()) {
if (window->delegate()->GetMinimumSize().width() != 0)
width = std::max(width, window->delegate()->GetMinimumSize().width());
if (window->delegate()->GetMaximumSize().width() != 0)
width = std::min(width, window->delegate()->GetMaximumSize().width());
}
DCHECK_LE(width, DockedWindowLayoutManager::kMaxDockWidth);
return width;
}
int GetWindowHeightCloseTo(const aura::Window* window, int target_height) {
if (!wm::GetWindowState(window)->CanResize())
return window->bounds().height();
int minimum_height = kMinimumHeight;
int maximum_height = 0;
const aura::WindowDelegate* delegate(window->delegate());
if (delegate) {
if (delegate->GetMinimumSize().height() != 0) {
minimum_height = std::max(kMinimumHeight,
delegate->GetMinimumSize().height());
}
if (delegate->GetMaximumSize().height() != 0)
maximum_height = delegate->GetMaximumSize().height();
}
if (minimum_height)
target_height = std::max(target_height, minimum_height);
if (maximum_height)
target_height = std::min(target_height, maximum_height);
return target_height;
}
struct CompareMinimumHeight {
bool operator()(WindowWithHeight win1, WindowWithHeight win2) {
return GetWindowHeightCloseTo(win1.window(), 0) <
GetWindowHeightCloseTo(win2.window(), 0);
}
};
struct CompareWindowPos {
CompareWindowPos(aura::Window* dragged_window,
aura::Window* docked_container,
float delta)
: dragged_window_(dragged_window),
docked_container_(docked_container),
delta_(delta / 2) {}
bool operator()(WindowWithHeight window_with_height1,
WindowWithHeight window_with_height2) {
aura::Window* win1(window_with_height1.window());
aura::Window* win2(window_with_height2.window());
gfx::Rect win1_bounds = ScreenUtil::ConvertRectToScreen(
docked_container_, win1->GetTargetBounds());
gfx::Rect win2_bounds = ScreenUtil::ConvertRectToScreen(
docked_container_, win2->GetTargetBounds());
win1_bounds.set_height(window_with_height1.height_);
win2_bounds.set_height(window_with_height2.height_);
if (win1 == dragged_window_)
return compare_two_windows(win1_bounds, win2_bounds);
if (win2 == dragged_window_)
return !compare_two_windows(win2_bounds, win1_bounds);
return win1_bounds.CenterPoint().y() < win2_bounds.CenterPoint().y();
}
bool compare_bounds(const gfx::Rect dragged, const gfx::Rect other) {
if (dragged.CenterPoint().y() < other.CenterPoint().y())
return dragged.CenterPoint().y() < other.y() - delta_;
return dragged.CenterPoint().y() < other.bottom() + delta_;
}
bool compare_two_windows(const gfx::Rect bounds1, const gfx::Rect bounds2) {
bool result1 = compare_bounds(bounds1, bounds2);
bool result2 = compare_bounds(bounds2, bounds1);
if (result1 != result2)
return result1;
return bounds1.CenterPoint().y() < bounds2.CenterPoint().y();
}
private:
aura::Window* dragged_window_;
aura::Window* docked_container_;
float delta_;
};
}
class DockedWindowLayoutManager::ShelfWindowObserver : public WindowObserver {
public:
explicit ShelfWindowObserver(
DockedWindowLayoutManager* docked_layout_manager)
: docked_layout_manager_(docked_layout_manager) {
DCHECK(docked_layout_manager_->shelf()->shelf_widget());
docked_layout_manager_->shelf()->shelf_widget()->GetNativeView()
->AddObserver(this);
}
virtual ~ShelfWindowObserver() {
if (docked_layout_manager_->shelf() &&
docked_layout_manager_->shelf()->shelf_widget())
docked_layout_manager_->shelf()->shelf_widget()->GetNativeView()
->RemoveObserver(this);
}
virtual void OnWindowBoundsChanged(aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) OVERRIDE {
shelf_bounds_in_screen_ = ScreenUtil::ConvertRectToScreen(
window->parent(), new_bounds);
docked_layout_manager_->OnShelfBoundsChanged();
}
const gfx::Rect& shelf_bounds_in_screen() const {
return shelf_bounds_in_screen_;
}
private:
DockedWindowLayoutManager* docked_layout_manager_;
gfx::Rect shelf_bounds_in_screen_;
DISALLOW_COPY_AND_ASSIGN(ShelfWindowObserver);
};
DockedWindowLayoutManager::DockedWindowLayoutManager(
aura::Window* dock_container, WorkspaceController* workspace_controller)
: dock_container_(dock_container),
in_layout_(false),
dragged_window_(NULL),
is_dragged_window_docked_(false),
is_dragged_from_dock_(false),
shelf_(NULL),
workspace_controller_(workspace_controller),
in_fullscreen_(workspace_controller_->GetWindowState() ==
WORKSPACE_WINDOW_STATE_FULL_SCREEN),
docked_width_(0),
alignment_(DOCKED_ALIGNMENT_NONE),
last_active_window_(NULL),
last_action_time_(base::Time::Now()),
background_widget_(new DockedBackgroundWidget(dock_container_)) {
DCHECK(dock_container);
aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
AddObserver(this);
Shell::GetInstance()->AddShellObserver(this);
}
DockedWindowLayoutManager::~DockedWindowLayoutManager() {
Shutdown();
}
void DockedWindowLayoutManager::Shutdown() {
if (shelf_ && shelf_->shelf_widget()) {
ShelfLayoutManager* shelf_layout_manager = ShelfLayoutManager::ForShelf(
shelf_->shelf_widget()->GetNativeWindow());
shelf_layout_manager->RemoveObserver(this);
shelf_observer_.reset();
}
shelf_ = NULL;
for (size_t i = 0; i < dock_container_->children().size(); ++i) {
aura::Window* child = dock_container_->children()[i];
child->RemoveObserver(this);
wm::GetWindowState(child)->RemoveObserver(this);
}
aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())->
RemoveObserver(this);
Shell::GetInstance()->RemoveShellObserver(this);
}
void DockedWindowLayoutManager::AddObserver(
DockedWindowLayoutManagerObserver* observer) {
observer_list_.AddObserver(observer);
}
void DockedWindowLayoutManager::RemoveObserver(
DockedWindowLayoutManagerObserver* observer) {
observer_list_.RemoveObserver(observer);
}
void DockedWindowLayoutManager::StartDragging(aura::Window* window) {
DCHECK(!dragged_window_);
dragged_window_ = window;
DCHECK(!IsPopupOrTransient(window));
wm::WindowState* dragged_state = wm::GetWindowState(dragged_window_);
if (dragged_window_->parent() != dock_container_) {
dragged_window_->AddObserver(this);
dragged_state->AddObserver(this);
} else if (!IsAnyWindowDocked() &&
dragged_state->drag_details() &&
!(dragged_state->drag_details()->bounds_change &
WindowResizer::kBoundsChange_Resizes)) {
alignment_ = DOCKED_ALIGNMENT_NONE;
}
is_dragged_from_dock_ = window->parent() == dock_container_;
DCHECK(!is_dragged_window_docked_);
if (dragged_window_->bounds().width() == docked_width_ &&
(dragged_state->drag_details()->bounds_change &
WindowResizer::kBoundsChange_Resizes) &&
(dragged_state->drag_details()->size_change_direction &
WindowResizer::kBoundsChangeDirection_Horizontal)) {
for (size_t i = 0; i < dock_container_->children().size(); ++i) {
aura::Window* window1(dock_container_->children()[i]);
if (IsUsedByLayout(window1) &&
window1 != dragged_window_ &&
window1->bounds().width() == docked_width_) {
wm::GetWindowState(window1)->set_bounds_changed_by_user(false);
}
}
}
}
void DockedWindowLayoutManager::DockDraggedWindow(aura::Window* window) {
DCHECK(!IsPopupOrTransient(window));
OnDraggedWindowDocked(window);
Relayout();
}
void DockedWindowLayoutManager::UndockDraggedWindow() {
DCHECK(!IsPopupOrTransient(dragged_window_));
OnDraggedWindowUndocked();
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
is_dragged_from_dock_ = false;
}
void DockedWindowLayoutManager::FinishDragging(DockedAction action,
DockedActionSource source) {
DCHECK(dragged_window_);
DCHECK(!IsPopupOrTransient(dragged_window_));
if (is_dragged_window_docked_)
OnDraggedWindowUndocked();
DCHECK (!is_dragged_window_docked_);
if (dragged_window_->parent() != dock_container_) {
dragged_window_->RemoveObserver(this);
wm::GetWindowState(dragged_window_)->RemoveObserver(this);
if (last_active_window_ == dragged_window_)
last_active_window_ = NULL;
} else {
if (alignment_ == DOCKED_ALIGNMENT_NONE)
alignment_ = GetEdgeNearestWindow(dragged_window_);
MaybeMinimizeChildrenExcept(dragged_window_);
}
dragged_window_ = NULL;
dragged_bounds_ = gfx::Rect();
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
RecordUmaAction(action, source);
}
void DockedWindowLayoutManager::SetShelf(Shelf* shelf) {
DCHECK(!shelf_);
shelf_ = shelf;
if (shelf_->shelf_widget()) {
ShelfLayoutManager* shelf_layout_manager = ShelfLayoutManager::ForShelf(
shelf_->shelf_widget()->GetNativeWindow());
shelf_layout_manager->AddObserver(this);
shelf_observer_.reset(new ShelfWindowObserver(this));
}
}
DockedAlignment DockedWindowLayoutManager::GetAlignmentOfWindow(
const aura::Window* window) const {
const gfx::Rect& bounds(window->GetBoundsInScreen());
if (docked_bounds_.Intersects(bounds) &&
alignment_ != DOCKED_ALIGNMENT_NONE) {
return alignment_;
}
const gfx::Rect container_bounds = dock_container_->GetBoundsInScreen();
if (bounds.x() <= container_bounds.x() &&
bounds.right() > container_bounds.x()) {
return DOCKED_ALIGNMENT_LEFT;
} else if (bounds.x() < container_bounds.right() &&
bounds.right() >= container_bounds.right()) {
return DOCKED_ALIGNMENT_RIGHT;
}
return DOCKED_ALIGNMENT_NONE;
}
DockedAlignment DockedWindowLayoutManager::CalculateAlignment() const {
for (size_t i = 0; i < dock_container_->children().size(); ++i) {
aura::Window* window(dock_container_->children()[i]);
if (window != dragged_window_ && !IsPopupOrTransient(window))
return alignment_;
}
return DOCKED_ALIGNMENT_NONE;
}
bool DockedWindowLayoutManager::CanDockWindow(aura::Window* window,
SnapType edge) {
if (!switches::UseDockedWindows())
return false;
wm::WindowState* window_state = wm::GetWindowState(window);
bool should_attach_to_shelf = window_state->drag_details() &&
window_state->drag_details()->should_attach_to_shelf;
if (IsPopupOrTransient(window) || should_attach_to_shelf)
return false;
if (window->bounds().width() > kMaxDockWidth &&
(!window_state->CanResize() ||
(window->delegate() &&
window->delegate()->GetMinimumSize().width() != 0 &&
window->delegate()->GetMinimumSize().width() > kMaxDockWidth))) {
return false;
}
const gfx::Rect work_area =
Shell::GetScreen()->GetDisplayNearestWindow(dock_container_).work_area();
if (GetWindowHeightCloseTo(window, work_area.height()) > work_area.height())
return false;
const DockedAlignment alignment = CalculateAlignment();
if ((edge == SNAP_LEFT && alignment == DOCKED_ALIGNMENT_RIGHT) ||
(edge == SNAP_RIGHT && alignment == DOCKED_ALIGNMENT_LEFT)) {
return false;
}
ShelfAlignment shelf_alignment = SHELF_ALIGNMENT_BOTTOM;
if (shelf_)
shelf_alignment = shelf_->alignment();
if ((edge == SNAP_LEFT && shelf_alignment == SHELF_ALIGNMENT_LEFT) ||
(edge == SNAP_RIGHT && shelf_alignment == SHELF_ALIGNMENT_RIGHT)) {
return false;
}
return true;
}
void DockedWindowLayoutManager::OnShelfBoundsChanged() {
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::DISPLAY_INSETS_CHANGED);
}
void DockedWindowLayoutManager::OnWindowResized() {
MaybeMinimizeChildrenExcept(dragged_window_);
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::DISPLAY_RESIZED);
}
void DockedWindowLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
if (IsPopupOrTransient(child))
return;
if (child == dragged_window_)
return;
if (alignment_ == DOCKED_ALIGNMENT_NONE)
alignment_ = GetEdgeNearestWindow(child);
MaybeMinimizeChildrenExcept(child);
child->AddObserver(this);
wm::GetWindowState(child)->AddObserver(this);
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
}
void DockedWindowLayoutManager::OnWindowRemovedFromLayout(aura::Window* child) {
if (IsPopupOrTransient(child))
return;
if (child == dragged_window_)
return;
if (!IsAnyWindowDocked()) {
alignment_ = DOCKED_ALIGNMENT_NONE;
UpdateDockedWidth(0);
}
if (last_active_window_ == child)
last_active_window_ = NULL;
child->RemoveObserver(this);
wm::GetWindowState(child)->RemoveObserver(this);
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
}
void DockedWindowLayoutManager::OnChildWindowVisibilityChanged(
aura::Window* child,
bool visible) {
if (IsPopupOrTransient(child))
return;
if (visible)
wm::GetWindowState(child)->Restore();
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
}
void DockedWindowLayoutManager::SetChildBounds(
aura::Window* child,
const gfx::Rect& requested_bounds) {
SetChildBoundsDirect(child, requested_bounds);
if (IsPopupOrTransient(child))
return;
ShelfLayoutManager* shelf_layout =
ShelfLayoutManager::ForShelf(dock_container_);
if (shelf_layout)
shelf_layout->UpdateVisibilityState();
}
void DockedWindowLayoutManager::OnDisplayWorkAreaInsetsChanged() {
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::DISPLAY_INSETS_CHANGED);
MaybeMinimizeChildrenExcept(dragged_window_);
}
void DockedWindowLayoutManager::OnFullscreenStateChanged(
bool is_fullscreen, aura::Window* root_window) {
if (dock_container_->GetRootWindow() != root_window)
return;
in_fullscreen_ = workspace_controller_->GetWindowState() ==
WORKSPACE_WINDOW_STATE_FULL_SCREEN;
{
base::AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
aura::Window::Windows children(dock_container_->children());
for (aura::Window::Windows::const_iterator iter = children.begin();
iter != children.end(); ++iter) {
aura::Window* window(*iter);
if (IsPopupOrTransient(window))
continue;
wm::WindowState* window_state = wm::GetWindowState(window);
if (in_fullscreen_) {
if (window->IsVisible())
MinimizeDockedWindow(window_state);
} else {
if (!window_state->IsMinimized())
RestoreDockedWindow(window_state);
}
}
}
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::CHILD_CHANGED);
}
void DockedWindowLayoutManager::OnShelfAlignmentChanged(
aura::Window* root_window) {
if (dock_container_->GetRootWindow() != root_window)
return;
if (!shelf_ || !shelf_->shelf_widget())
return;
if (alignment_ == DOCKED_ALIGNMENT_NONE)
return;
ShelfAlignment shelf_alignment = shelf_->shelf_widget()->GetAlignment();
if (alignment_ == DOCKED_ALIGNMENT_LEFT &&
shelf_alignment == SHELF_ALIGNMENT_LEFT) {
alignment_ = DOCKED_ALIGNMENT_RIGHT;
} else if (alignment_ == DOCKED_ALIGNMENT_RIGHT &&
shelf_alignment == SHELF_ALIGNMENT_RIGHT) {
alignment_ = DOCKED_ALIGNMENT_LEFT;
}
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::SHELF_ALIGNMENT_CHANGED);
}
void DockedWindowLayoutManager::OnBackgroundUpdated(
ShelfBackgroundType background_type,
BackgroundAnimatorChangeType change_type) {
background_widget_->SetBackgroundType(background_type, change_type);
}
void DockedWindowLayoutManager::OnPreWindowStateTypeChange(
wm::WindowState* window_state,
wm::WindowStateType old_type) {
aura::Window* window = window_state->window();
if (IsPopupOrTransient(window))
return;
if (in_fullscreen_)
return;
if (window_state->IsMinimized()) {
MinimizeDockedWindow(window_state);
} else if (window_state->IsMaximizedOrFullscreen() ||
window_state->IsSnapped()) {
if (window != dragged_window_) {
UndockWindow(window);
RecordUmaAction(DOCKED_ACTION_MAXIMIZE, DOCKED_ACTION_SOURCE_UNKNOWN);
}
} else if (old_type == wm::WINDOW_STATE_TYPE_MINIMIZED) {
RestoreDockedWindow(window_state);
}
}
void DockedWindowLayoutManager::OnWindowBoundsChanged(
aura::Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) {
if (window == dragged_window_ && is_dragged_window_docked_)
Relayout();
}
void DockedWindowLayoutManager::OnWindowVisibilityChanging(
aura::Window* window, bool visible) {
if (IsPopupOrTransient(window))
return;
int animation_type = ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_DEFAULT;
if (visible) {
animation_type = ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_DROP;
::wm::SetWindowVisibilityAnimationDuration(
window, base::TimeDelta::FromMilliseconds(kFadeDurationMs));
} else if (wm::GetWindowState(window)->IsMinimized()) {
animation_type = WINDOW_VISIBILITY_ANIMATION_TYPE_MINIMIZE;
}
::wm::SetWindowVisibilityAnimationType(window, animation_type);
}
void DockedWindowLayoutManager::OnWindowDestroying(aura::Window* window) {
if (dragged_window_ == window) {
FinishDragging(DOCKED_ACTION_NONE, DOCKED_ACTION_SOURCE_UNKNOWN);
DCHECK(!dragged_window_);
DCHECK(!is_dragged_window_docked_);
}
if (window == last_active_window_)
last_active_window_ = NULL;
RecordUmaAction(DOCKED_ACTION_CLOSE, DOCKED_ACTION_SOURCE_UNKNOWN);
}
void DockedWindowLayoutManager::OnWindowActivated(aura::Window* gained_active,
aura::Window* lost_active) {
if (gained_active && IsPopupOrTransient(gained_active))
return;
aura::Window* ancestor = NULL;
for (aura::Window* parent = gained_active;
parent; parent = parent->parent()) {
if (parent->parent() == dock_container_) {
ancestor = parent;
break;
}
}
if (ancestor)
UpdateStacking(ancestor);
}
void DockedWindowLayoutManager::MaybeMinimizeChildrenExcept(
aura::Window* child) {
const gfx::Rect work_area =
Shell::GetScreen()->GetDisplayNearestWindow(dock_container_).work_area();
int available_room = work_area.height();
bool gap_needed = !!child;
if (child)
available_room -= GetWindowHeightCloseTo(child, 0);
aura::Window::Windows children(dock_container_->children());
aura::Window::Windows::const_reverse_iterator iter = children.rbegin();
while (iter != children.rend()) {
aura::Window* window(*iter++);
if (window == child || !IsUsedByLayout(window))
continue;
int room_needed = GetWindowHeightCloseTo(window, 0) +
(gap_needed ? kMinDockGap : 0);
gap_needed = true;
if (available_room > room_needed) {
available_room -= room_needed;
} else {
ui::ScopedLayerAnimationSettings settings(window->layer()->GetAnimator());
settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kMinimizeDurationMs));
settings.LockTransitionDuration();
wm::GetWindowState(window)->Minimize();
}
}
}
void DockedWindowLayoutManager::MinimizeDockedWindow(
wm::WindowState* window_state) {
DCHECK(!IsPopupOrTransient(window_state->window()));
window_state->window()->Hide();
if (window_state->IsActive())
window_state->Deactivate();
RecordUmaAction(DOCKED_ACTION_MINIMIZE, DOCKED_ACTION_SOURCE_UNKNOWN);
}
void DockedWindowLayoutManager::RestoreDockedWindow(
wm::WindowState* window_state) {
aura::Window* window = window_state->window();
DCHECK(!IsPopupOrTransient(window));
gfx::Display display = Shell::GetScreen()->GetDisplayNearestWindow(
dock_container_);
const gfx::Rect work_area = display.work_area();
if (!CanDockWindow(window, SNAP_NONE)) {
UndockWindow(window);
RecordUmaAction(DOCKED_ACTION_EVICT, DOCKED_ACTION_SOURCE_UNKNOWN);
return;
}
gfx::Rect bounds(window->bounds());
bounds.set_y(work_area.bottom());
window->SetBounds(bounds);
window->Show();
MaybeMinimizeChildrenExcept(window);
RecordUmaAction(DOCKED_ACTION_RESTORE, DOCKED_ACTION_SOURCE_UNKNOWN);
}
void DockedWindowLayoutManager::RecordUmaAction(DockedAction action,
DockedActionSource source) {
if (action == DOCKED_ACTION_NONE)
return;
UMA_HISTOGRAM_ENUMERATION("Ash.Dock.Action", action, DOCKED_ACTION_COUNT);
UMA_HISTOGRAM_ENUMERATION("Ash.Dock.ActionSource", source,
DOCKED_ACTION_SOURCE_COUNT);
base::Time time_now = base::Time::Now();
base::TimeDelta time_between_use = time_now - last_action_time_;
UMA_HISTOGRAM_CUSTOM_COUNTS("Ash.Dock.TimeBetweenUse",
time_between_use.InSeconds(),
1,
base::TimeDelta::FromHours(10).InSeconds(),
100);
last_action_time_ = time_now;
int docked_all_count = 0;
int docked_visible_count = 0;
int docked_panels_count = 0;
int large_windows_count = 0;
for (size_t i = 0; i < dock_container_->children().size(); ++i) {
const aura::Window* window(dock_container_->children()[i]);
if (IsPopupOrTransient(window))
continue;
docked_all_count++;
if (!IsUsedByLayout(window))
continue;
docked_visible_count++;
if (window->type() == ui::wm::WINDOW_TYPE_PANEL)
docked_panels_count++;
const wm::WindowState* window_state = wm::GetWindowState(window);
if (window_state->HasRestoreBounds()) {
const gfx::Rect restore_bounds = window_state->GetRestoreBoundsInScreen();
if (restore_bounds.width() > kMaxDockWidth)
large_windows_count++;
}
}
UMA_HISTOGRAM_COUNTS_100("Ash.Dock.ItemsAll", docked_all_count);
UMA_HISTOGRAM_COUNTS_100("Ash.Dock.ItemsLarge", large_windows_count);
UMA_HISTOGRAM_COUNTS_100("Ash.Dock.ItemsPanels", docked_panels_count);
UMA_HISTOGRAM_COUNTS_100("Ash.Dock.ItemsVisible", docked_visible_count);
}
void DockedWindowLayoutManager::UpdateDockedWidth(int width) {
if (docked_width_ == width)
return;
docked_width_ = width;
UMA_HISTOGRAM_COUNTS_10000("Ash.Dock.Width", docked_width_);
}
void DockedWindowLayoutManager::OnDraggedWindowDocked(aura::Window* window) {
DCHECK(!is_dragged_window_docked_);
is_dragged_window_docked_ = true;
}
void DockedWindowLayoutManager::OnDraggedWindowUndocked() {
DCHECK (is_dragged_window_docked_);
is_dragged_window_docked_ = false;
}
bool DockedWindowLayoutManager::IsAnyWindowDocked() {
return CalculateAlignment() != DOCKED_ALIGNMENT_NONE;
}
DockedAlignment DockedWindowLayoutManager::GetEdgeNearestWindow(
const aura::Window* window) const {
const gfx::Rect& bounds(window->GetBoundsInScreen());
const gfx::Rect container_bounds = dock_container_->GetBoundsInScreen();
return (abs(bounds.x() - container_bounds.x()) <
abs(bounds.right() - container_bounds.right())) ?
DOCKED_ALIGNMENT_LEFT : DOCKED_ALIGNMENT_RIGHT;
}
void DockedWindowLayoutManager::Relayout() {
if (in_layout_)
return;
if (alignment_ == DOCKED_ALIGNMENT_NONE && !is_dragged_window_docked_)
return;
base::AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
gfx::Rect dock_bounds = dock_container_->GetBoundsInScreen();
aura::Window* active_window = NULL;
std::vector<WindowWithHeight> visible_windows;
for (size_t i = 0; i < dock_container_->children().size(); ++i) {
aura::Window* window(dock_container_->children()[i]);
if (!IsUsedByLayout(window) || window == dragged_window_)
continue;
if (in_fullscreen_) {
window->Hide();
continue;
}
if (window->HasFocus() ||
window->Contains(
aura::client::GetFocusClient(window)->GetFocusedWindow())) {
DCHECK(!active_window);
active_window = window;
}
visible_windows.push_back(WindowWithHeight(window));
}
if (is_dragged_window_docked_) {
visible_windows.push_back(WindowWithHeight(dragged_window_));
DCHECK(!active_window);
active_window = dragged_window_;
}
gfx::Rect work_area =
Shell::GetScreen()->GetDisplayNearestWindow(dock_container_).work_area();
if (shelf_observer_)
work_area.Subtract(shelf_observer_->shelf_bounds_in_screen());
int available_room = CalculateWindowHeightsAndRemainingRoom(work_area,
&visible_windows);
FanOutChildren(work_area,
CalculateIdealWidth(visible_windows),
available_room,
&visible_windows);
is_dragged_from_dock_ = true;
UpdateStacking(active_window);
}
int DockedWindowLayoutManager::CalculateWindowHeightsAndRemainingRoom(
const gfx::Rect work_area,
std::vector<WindowWithHeight>* visible_windows) {
int available_room = work_area.height();
int remaining_windows = visible_windows->size();
int gap_height = remaining_windows > 1 ? kMinDockGap : 0;
std::sort(visible_windows->begin(), visible_windows->end(),
CompareMinimumHeight());
for (std::vector<WindowWithHeight>::reverse_iterator iter =
visible_windows->rbegin();
iter != visible_windows->rend(); ++iter) {
iter->height_ = GetWindowHeightCloseTo(
iter->window(),
(available_room + gap_height) / remaining_windows - gap_height);
available_room -= (iter->height_ + gap_height);
remaining_windows--;
}
return available_room + gap_height;
}
int DockedWindowLayoutManager::CalculateIdealWidth(
const std::vector<WindowWithHeight>& visible_windows) {
int smallest_max_width = kMaxDockWidth;
int largest_min_width = kMinDockWidth;
for (std::vector<WindowWithHeight>::const_iterator iter =
visible_windows.begin();
iter != visible_windows.end(); ++iter) {
const aura::Window* window = iter->window();
int min_window_width = window->bounds().width();
int max_window_width = min_window_width;
if (!wm::GetWindowState(window)->bounds_changed_by_user()) {
min_window_width = GetWindowWidthCloseTo(window, kMinDockWidth);
max_window_width = GetWindowWidthCloseTo(window, kMaxDockWidth);
}
largest_min_width = std::max(largest_min_width, min_window_width);
smallest_max_width = std::min(smallest_max_width, max_window_width);
}
int ideal_width = std::max(largest_min_width,
std::min(smallest_max_width, kIdealWidth));
ideal_width = std::max(std::min(ideal_width, kMaxDockWidth), kMinDockWidth);
return ideal_width;
}
void DockedWindowLayoutManager::FanOutChildren(
const gfx::Rect& work_area,
int ideal_docked_width,
int available_room,
std::vector<WindowWithHeight>* visible_windows) {
gfx::Rect dock_bounds = dock_container_->GetBoundsInScreen();
const int num_windows = visible_windows->size();
const float delta = static_cast<float>(available_room) /
((available_room > 0 || num_windows <= 1) ?
num_windows + 1 : num_windows - 1);
float y_pos = work_area.y() + ((delta > 0) ? delta : 0);
int new_width = ideal_docked_width;
if (visible_windows->empty() ||
(visible_windows->size() == 1 &&
(*visible_windows)[0].window() == dragged_window_)) {
new_width = 0;
}
UpdateDockedWidth(new_width);
std::sort(visible_windows->begin(), visible_windows->end(),
CompareWindowPos(is_dragged_from_dock_ ? dragged_window_ : NULL,
dock_container_,
delta));
for (std::vector<WindowWithHeight>::iterator iter = visible_windows->begin();
iter != visible_windows->end(); ++iter) {
aura::Window* window = iter->window();
gfx::Rect bounds = ScreenUtil::ConvertRectToScreen(
dock_container_, window->GetTargetBounds());
bounds.set_width(GetWindowWidthCloseTo(
window,
wm::GetWindowState(window)->bounds_changed_by_user() ?
bounds.width() : ideal_docked_width));
DCHECK_LE(bounds.width(), ideal_docked_width);
DockedAlignment alignment = alignment_;
if (alignment == DOCKED_ALIGNMENT_NONE && window == dragged_window_)
alignment = GetEdgeNearestWindow(window);
bounds.set_height(iter->height_);
bounds.set_y(std::max(work_area.y(),
std::min(work_area.bottom() - bounds.height(),
static_cast<int>(y_pos + 0.5))));
y_pos += bounds.height() + delta + kMinDockGap;
switch (alignment) {
case DOCKED_ALIGNMENT_LEFT:
bounds.set_x(dock_bounds.x() +
(ideal_docked_width - bounds.width()) / 2);
break;
case DOCKED_ALIGNMENT_RIGHT:
bounds.set_x(dock_bounds.right() -
(ideal_docked_width + bounds.width()) / 2);
break;
case DOCKED_ALIGNMENT_NONE:
break;
}
if (window == dragged_window_) {
dragged_bounds_ = bounds;
continue;
}
DCHECK_NE(alignment_, DOCKED_ALIGNMENT_NONE);
bounds = ScreenUtil::ConvertRectFromScreen(dock_container_, bounds);
if (bounds != window->GetTargetBounds()) {
ui::Layer* layer = window->layer();
ui::ScopedLayerAnimationSettings slide_settings(layer->GetAnimator());
slide_settings.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET);
slide_settings.SetTransitionDuration(
base::TimeDelta::FromMilliseconds(kSlideDurationMs));
SetChildBoundsDirect(window, bounds);
}
}
}
void DockedWindowLayoutManager::UpdateDockBounds(
DockedWindowLayoutManagerObserver::Reason reason) {
int dock_inset = docked_width_ + (docked_width_ > 0 ? kMinDockGap : 0);
const gfx::Rect work_area =
Shell::GetScreen()->GetDisplayNearestWindow(dock_container_).work_area();
gfx::Rect bounds = gfx::Rect(
alignment_ == DOCKED_ALIGNMENT_RIGHT && dock_inset > 0 ?
dock_container_->bounds().right() - dock_inset:
dock_container_->bounds().x(),
dock_container_->bounds().y(),
dock_inset,
work_area.height());
docked_bounds_ = bounds +
dock_container_->GetBoundsInScreen().OffsetFromOrigin();
FOR_EACH_OBSERVER(
DockedWindowLayoutManagerObserver,
observer_list_,
OnDockBoundsChanging(bounds, reason));
gfx::Rect background_bounds(docked_bounds_);
if (shelf_observer_)
background_bounds.Subtract(shelf_observer_->shelf_bounds_in_screen());
background_widget_->SetBackgroundBounds(background_bounds, alignment_);
if (docked_width_ > 0)
background_widget_->Show();
else
background_widget_->Hide();
}
void DockedWindowLayoutManager::UpdateStacking(aura::Window* active_window) {
if (!active_window) {
if (!last_active_window_)
return;
active_window = last_active_window_;
}
std::map<int, aura::Window*> window_ordering;
for (aura::Window::Windows::const_iterator it =
dock_container_->children().begin();
it != dock_container_->children().end(); ++it) {
if (!IsUsedByLayout(*it) ||
((*it) == dragged_window_ && !is_dragged_window_docked_)) {
continue;
}
gfx::Rect bounds = (*it)->bounds();
window_ordering.insert(std::make_pair(bounds.y() + bounds.height() / 2,
*it));
}
int active_center_y = active_window->bounds().CenterPoint().y();
aura::Window* previous_window = NULL;
for (std::map<int, aura::Window*>::const_iterator it =
window_ordering.begin();
it != window_ordering.end() && it->first < active_center_y; ++it) {
if (previous_window)
dock_container_->StackChildAbove(it->second, previous_window);
previous_window = it->second;
}
for (std::map<int, aura::Window*>::const_reverse_iterator it =
window_ordering.rbegin();
it != window_ordering.rend() && it->first > active_center_y; ++it) {
if (previous_window)
dock_container_->StackChildAbove(it->second, previous_window);
previous_window = it->second;
}
if (previous_window && active_window->parent() == dock_container_)
dock_container_->StackChildAbove(active_window, previous_window);
if (active_window != dragged_window_)
last_active_window_ = active_window;
}
void DockedWindowLayoutManager::OnKeyboardBoundsChanging(
const gfx::Rect& keyboard_bounds) {
Relayout();
UpdateDockBounds(DockedWindowLayoutManagerObserver::KEYBOARD_BOUNDS_CHANGING);
}
}