blob: c01d112d941eb1fc2da231944c890a3bbd6ab677 [file] [log] [blame]
[email protected]ae18b9112011-11-07 16:59:131// Copyright (c) 2011 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.
4
[email protected]4bb16502011-12-06 14:44:585#include "ui/aura_shell/shelf_layout_manager.h"
[email protected]ae18b9112011-11-07 16:59:136
[email protected]4bb16502011-12-06 14:44:587#include "base/auto_reset.h"
[email protected]ae18b9112011-11-07 16:59:138#include "ui/aura/desktop.h"
9#include "ui/aura/screen_aura.h"
10#include "ui/gfx/compositor/layer.h"
11#include "ui/gfx/compositor/layer_animator.h"
[email protected]c13be0d2011-11-22 02:09:5812#include "ui/views/widget/widget.h"
[email protected]ae18b9112011-11-07 16:59:1313
14namespace aura_shell {
15namespace internal {
16
17namespace {
18
19ui::Layer* GetLayer(views::Widget* widget) {
20 return widget->GetNativeView()->layer();
21}
22
23} // namespace
24
[email protected]4bb16502011-12-06 14:44:5825////////////////////////////////////////////////////////////////////////////////
26// ShelfLayoutManager, public:
27
28ShelfLayoutManager::ShelfLayoutManager(views::Widget* launcher,
29 views::Widget* status)
[email protected]ae18b9112011-11-07 16:59:1330 : animating_(false),
[email protected]4bb16502011-12-06 14:44:5831 in_layout_(false),
[email protected]ae18b9112011-11-07 16:59:1332 visible_(true),
33 max_height_(-1),
34 launcher_(launcher),
35 status_(status) {
36 gfx::Rect launcher_bounds = launcher->GetWindowScreenBounds();
37 gfx::Rect status_bounds = status->GetWindowScreenBounds();
38 max_height_ = std::max(launcher_bounds.height(), status_bounds.height());
39 GetLayer(launcher)->GetAnimator()->AddObserver(this);
40}
41
[email protected]4bb16502011-12-06 14:44:5842
43ShelfLayoutManager::~ShelfLayoutManager() {
[email protected]ef589af2011-12-03 01:07:1544 // Do not try to remove observer from layer as the Launcher is
45 // already deleted.
[email protected]ae18b9112011-11-07 16:59:1346}
47
[email protected]4bb16502011-12-06 14:44:5848void ShelfLayoutManager::LayoutShelf() {
49 AutoReset<bool> auto_reset_in_layout(&in_layout_, true);
[email protected]ae18b9112011-11-07 16:59:1350 StopAnimating();
51 TargetBounds target_bounds;
52 float target_opacity = visible_ ? 1.0f : 0.0f;
53 CalculateTargetBounds(visible_, &target_bounds);
54 GetLayer(launcher_)->SetOpacity(target_opacity);
55 GetLayer(status_)->SetOpacity(target_opacity);
56 launcher_->SetBounds(target_bounds.launcher_bounds);
57 status_->SetBounds(target_bounds.status_bounds);
58 aura::Desktop::GetInstance()->screen()->set_work_area_insets(
59 target_bounds.work_area_insets);
60}
61
[email protected]4bb16502011-12-06 14:44:5862void ShelfLayoutManager::SetVisible(bool visible) {
[email protected]ae18b9112011-11-07 16:59:1363 bool current_visibility = animating_ ? !visible_ : visible_;
64 if (visible == current_visibility)
65 return; // Nothing changed.
66
67 StopAnimating();
68
69 TargetBounds target_bounds;
70 float target_opacity = visible ? 1.0f : 0.0f;
71 CalculateTargetBounds(visible, &target_bounds);
72 AnimateWidgetTo(launcher_, target_bounds.launcher_bounds, target_opacity);
73 AnimateWidgetTo(status_, target_bounds.status_bounds, target_opacity);
74 animating_ = true;
75 // |visible_| is updated once the animation completes.
76}
77
[email protected]4bb16502011-12-06 14:44:5878////////////////////////////////////////////////////////////////////////////////
79// ShelfLayoutManager, aura::LayoutManager implementation:
80
81void ShelfLayoutManager::OnWindowResized() {
82 LayoutShelf();
83}
84
85void ShelfLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
86}
87
88void ShelfLayoutManager::OnWillRemoveWindowFromLayout(aura::Window* child) {
89}
90
91void ShelfLayoutManager::OnChildWindowVisibilityChanged(aura::Window* child,
92 bool visible) {
93}
94
95void ShelfLayoutManager::SetChildBounds(aura::Window* child,
96 const gfx::Rect& requested_bounds) {
97 SetChildBoundsDirect(child, requested_bounds);
98 if (!in_layout_)
99 LayoutShelf();
100}
101
102////////////////////////////////////////////////////////////////////////////////
103// ShelfLayoutManager, private:
104
105void ShelfLayoutManager::StopAnimating() {
[email protected]ae18b9112011-11-07 16:59:13106 if (animating_) {
107 animating_ = false;
108 visible_ = !visible_;
109 }
110 GetLayer(launcher_)->GetAnimator()->StopAnimating();
111}
112
[email protected]4bb16502011-12-06 14:44:58113void ShelfLayoutManager::CalculateTargetBounds(bool visible,
114 TargetBounds* target_bounds) {
[email protected]ae18b9112011-11-07 16:59:13115 const gfx::Rect& available_bounds(aura::Desktop::GetInstance()->bounds());
116 int y = available_bounds.bottom() - (visible ? max_height_ : 0);
117 gfx::Rect status_bounds(status_->GetWindowScreenBounds());
118 target_bounds->status_bounds = gfx::Rect(
119 available_bounds.right() - status_bounds.width(),
120 y + (max_height_ - status_bounds.height()) / 2,
121 status_bounds.width(), status_bounds.height());
122 gfx::Rect launcher_bounds(launcher_->GetWindowScreenBounds());
123 target_bounds->launcher_bounds = gfx::Rect(
124 available_bounds.x(), y + (max_height_ - launcher_bounds.height()) / 2,
125 available_bounds.width() - status_bounds.width(),
126 launcher_bounds.height());
127 if (visible)
128 target_bounds->work_area_insets = gfx::Insets(0, 0, max_height_, 0);
129}
130
[email protected]4bb16502011-12-06 14:44:58131void ShelfLayoutManager::AnimateWidgetTo(views::Widget* widget,
132 const gfx::Rect& target_bounds,
133 float target_opacity) {
[email protected]ae18b9112011-11-07 16:59:13134 ui::Layer* layer = GetLayer(widget);
135 ui::LayerAnimator::ScopedSettings animation_setter(layer->GetAnimator());
136 widget->SetBounds(target_bounds);
137 layer->SetOpacity(target_opacity);
138}
139
[email protected]4bb16502011-12-06 14:44:58140void ShelfLayoutManager::OnLayerAnimationEnded(
[email protected]ae18b9112011-11-07 16:59:13141 const ui::LayerAnimationSequence* sequence) {
142 if (!animating_)
143 return;
144 animating_ = false;
145 visible_ = !visible_;
146 TargetBounds target_bounds;
147 CalculateTargetBounds(visible_, &target_bounds);
148 aura::Desktop::GetInstance()->screen()->set_work_area_insets(
149 target_bounds.work_area_insets);
150}
151
152} // internal
153} // aura_shell