OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/wm/shelf_layout_manager.h" | 5 #include "ash/wm/shelf_layout_manager.h" |
6 | 6 |
7 #include "ash/launcher/launcher.h" | 7 #include "ash/launcher/launcher.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
9 #include "base/auto_reset.h" | 9 #include "base/auto_reset.h" |
10 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
11 #include "ui/aura/screen_aura.h" | 11 #include "ui/aura/screen_aura.h" |
12 #include "ui/gfx/compositor/layer.h" | 12 #include "ui/gfx/compositor/layer.h" |
13 #include "ui/gfx/compositor/layer_animation_observer.h" | |
14 #include "ui/gfx/compositor/layer_animator.h" | 13 #include "ui/gfx/compositor/layer_animator.h" |
15 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" | 14 #include "ui/gfx/compositor/scoped_layer_animation_settings.h" |
16 #include "ui/views/widget/widget.h" | 15 #include "ui/views/widget/widget.h" |
17 | 16 |
18 namespace ash { | 17 namespace ash { |
19 namespace internal { | 18 namespace internal { |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 ui::Layer* GetLayer(views::Widget* widget) { | 22 ui::Layer* GetLayer(views::Widget* widget) { |
24 return widget->GetNativeView()->layer(); | 23 return widget->GetNativeView()->layer(); |
25 } | 24 } |
26 | 25 |
27 } // namespace | 26 } // namespace |
28 | 27 |
29 //////////////////////////////////////////////////////////////////////////////// | 28 //////////////////////////////////////////////////////////////////////////////// |
30 // ShelfLayoutManager, public: | 29 // ShelfLayoutManager, public: |
31 | 30 |
32 ShelfLayoutManager::ShelfLayoutManager(views::Widget* launcher, | 31 ShelfLayoutManager::ShelfLayoutManager(views::Widget* launcher, |
33 views::Widget* status) | 32 views::Widget* status) |
34 : in_layout_(false), | 33 : animating_(false), |
| 34 in_layout_(false), |
35 visible_(true), | 35 visible_(true), |
36 max_height_(-1), | 36 max_height_(-1), |
37 launcher_(launcher), | 37 launcher_(launcher), |
38 status_(status) { | 38 status_(status) { |
39 gfx::Rect launcher_bounds = launcher->GetWindowScreenBounds(); | 39 gfx::Rect launcher_bounds = launcher->GetWindowScreenBounds(); |
40 gfx::Rect status_bounds = status->GetWindowScreenBounds(); | 40 gfx::Rect status_bounds = status->GetWindowScreenBounds(); |
41 max_height_ = std::max(launcher_bounds.height(), status_bounds.height()); | 41 max_height_ = std::max(launcher_bounds.height(), status_bounds.height()); |
| 42 GetLayer(launcher)->GetAnimator()->AddObserver(this); |
42 } | 43 } |
43 | 44 |
44 ShelfLayoutManager::~ShelfLayoutManager() { | 45 ShelfLayoutManager::~ShelfLayoutManager() { |
| 46 GetLayer(launcher_)->GetAnimator()->RemoveObserver(this); |
45 // Without a shelf we don't need special insets anymore. | 47 // Without a shelf we don't need special insets anymore. |
46 aura::RootWindow::GetInstance()-> | 48 aura::RootWindow::GetInstance()-> |
47 screen()->set_work_area_insets(gfx::Insets()); | 49 screen()->set_work_area_insets(gfx::Insets()); |
48 } | 50 } |
49 | 51 |
50 void ShelfLayoutManager::LayoutShelf() { | 52 void ShelfLayoutManager::LayoutShelf() { |
51 AutoReset<bool> auto_reset_in_layout(&in_layout_, true); | 53 AutoReset<bool> auto_reset_in_layout(&in_layout_, true); |
52 StopAnimating(); | 54 StopAnimating(); |
53 TargetBounds target_bounds; | 55 TargetBounds target_bounds; |
54 float target_opacity = visible_ ? 1.0f : 0.0f; | 56 float target_opacity = visible_ ? 1.0f : 0.0f; |
55 CalculateTargetBounds(visible_, &target_bounds); | 57 CalculateTargetBounds(visible_, &target_bounds); |
56 GetLayer(launcher_)->SetOpacity(target_opacity); | 58 GetLayer(launcher_)->SetOpacity(target_opacity); |
57 GetLayer(status_)->SetOpacity(target_opacity); | 59 GetLayer(status_)->SetOpacity(target_opacity); |
58 launcher_->SetBounds(target_bounds.launcher_bounds); | 60 launcher_->SetBounds(target_bounds.launcher_bounds); |
59 status_->SetBounds(target_bounds.status_bounds); | 61 status_->SetBounds(target_bounds.status_bounds); |
60 Shell::GetInstance()->launcher()->SetStatusWidth( | 62 Shell::GetInstance()->launcher()->SetStatusWidth( |
61 target_bounds.status_bounds.width()); | 63 target_bounds.status_bounds.width()); |
62 aura::RootWindow::GetInstance()->screen()->set_work_area_insets( | 64 aura::RootWindow::GetInstance()->screen()->set_work_area_insets( |
63 target_bounds.work_area_insets); | 65 target_bounds.work_area_insets); |
64 } | 66 } |
65 | 67 |
66 void ShelfLayoutManager::SetVisible(bool visible) { | 68 void ShelfLayoutManager::SetVisible(bool visible) { |
67 ui::Layer* launcher_layer = GetLayer(launcher_); | 69 bool current_visibility = animating_ ? !visible_ : visible_; |
68 ui::Layer* status_layer = GetLayer(status_); | |
69 | |
70 // TODO(vollick): once visibility is animatable, use GetTargetVisibility. | |
71 bool current_visibility = visible_ && | |
72 launcher_layer->GetTargetOpacity() > 0.0f && | |
73 status_layer->GetTargetOpacity() > 0.0f; | |
74 | |
75 if (visible == current_visibility) | 70 if (visible == current_visibility) |
76 return; // Nothing changed. | 71 return; // Nothing changed. |
77 | 72 |
78 StopAnimating(); | 73 StopAnimating(); |
79 | 74 |
80 visible_ = visible; | |
81 TargetBounds target_bounds; | 75 TargetBounds target_bounds; |
82 float target_opacity = visible ? 1.0f : 0.0f; | 76 float target_opacity = visible ? 1.0f : 0.0f; |
83 CalculateTargetBounds(visible, &target_bounds); | 77 CalculateTargetBounds(visible, &target_bounds); |
84 | 78 AnimateWidgetTo(launcher_, target_bounds.launcher_bounds, target_opacity); |
85 ui::ScopedLayerAnimationSettings launcher_animation_setter( | 79 AnimateWidgetTo(status_, target_bounds.status_bounds, target_opacity); |
86 launcher_layer->GetAnimator()); | 80 animating_ = true; |
87 ui::ScopedLayerAnimationSettings status_animation_setter( | 81 // |visible_| is updated once the animation completes. |
88 status_layer->GetAnimator()); | |
89 | |
90 launcher_animation_setter.AddObserver(this); | |
91 status_animation_setter.AddObserver(this); | |
92 | |
93 launcher_layer->SetBounds(target_bounds.launcher_bounds); | |
94 launcher_layer->SetOpacity(target_opacity); | |
95 status_layer->SetBounds(target_bounds.status_bounds); | |
96 status_layer->SetOpacity(target_opacity); | |
97 } | 82 } |
98 | 83 |
99 //////////////////////////////////////////////////////////////////////////////// | 84 //////////////////////////////////////////////////////////////////////////////// |
100 // ShelfLayoutManager, aura::LayoutManager implementation: | 85 // ShelfLayoutManager, aura::LayoutManager implementation: |
101 | 86 |
102 void ShelfLayoutManager::OnWindowResized() { | 87 void ShelfLayoutManager::OnWindowResized() { |
103 LayoutShelf(); | 88 LayoutShelf(); |
104 } | 89 } |
105 | 90 |
106 void ShelfLayoutManager::OnWindowAddedToLayout(aura::Window* child) { | 91 void ShelfLayoutManager::OnWindowAddedToLayout(aura::Window* child) { |
(...skipping 10 matching lines...) Expand all Loading... |
117 const gfx::Rect& requested_bounds) { | 102 const gfx::Rect& requested_bounds) { |
118 SetChildBoundsDirect(child, requested_bounds); | 103 SetChildBoundsDirect(child, requested_bounds); |
119 if (!in_layout_) | 104 if (!in_layout_) |
120 LayoutShelf(); | 105 LayoutShelf(); |
121 } | 106 } |
122 | 107 |
123 //////////////////////////////////////////////////////////////////////////////// | 108 //////////////////////////////////////////////////////////////////////////////// |
124 // ShelfLayoutManager, private: | 109 // ShelfLayoutManager, private: |
125 | 110 |
126 void ShelfLayoutManager::StopAnimating() { | 111 void ShelfLayoutManager::StopAnimating() { |
127 StopObservingImplicitAnimations(); | 112 if (animating_) { |
| 113 animating_ = false; |
| 114 visible_ = !visible_; |
| 115 } |
128 GetLayer(launcher_)->GetAnimator()->StopAnimating(); | 116 GetLayer(launcher_)->GetAnimator()->StopAnimating(); |
129 GetLayer(status_)->GetAnimator()->StopAnimating(); | 117 GetLayer(status_)->GetAnimator()->StopAnimating(); |
130 } | 118 } |
131 | 119 |
132 void ShelfLayoutManager::CalculateTargetBounds(bool visible, | 120 void ShelfLayoutManager::CalculateTargetBounds(bool visible, |
133 TargetBounds* target_bounds) { | 121 TargetBounds* target_bounds) { |
134 const gfx::Rect& available_bounds(aura::RootWindow::GetInstance()->bounds()); | 122 const gfx::Rect& available_bounds(aura::RootWindow::GetInstance()->bounds()); |
135 int y = available_bounds.bottom() - (visible ? max_height_ : 0); | 123 int y = available_bounds.bottom() - (visible ? max_height_ : 0); |
136 gfx::Rect status_bounds(status_->GetWindowScreenBounds()); | 124 gfx::Rect status_bounds(status_->GetWindowScreenBounds()); |
137 target_bounds->status_bounds = gfx::Rect( | 125 target_bounds->status_bounds = gfx::Rect( |
138 available_bounds.right() - status_bounds.width(), | 126 available_bounds.right() - status_bounds.width(), |
139 y + (max_height_ - status_bounds.height()) / 2, | 127 y + (max_height_ - status_bounds.height()) / 2, |
140 status_bounds.width(), status_bounds.height()); | 128 status_bounds.width(), status_bounds.height()); |
141 gfx::Rect launcher_bounds(launcher_->GetWindowScreenBounds()); | 129 gfx::Rect launcher_bounds(launcher_->GetWindowScreenBounds()); |
142 target_bounds->launcher_bounds = gfx::Rect( | 130 target_bounds->launcher_bounds = gfx::Rect( |
143 available_bounds.x(), y + (max_height_ - launcher_bounds.height()) / 2, | 131 available_bounds.x(), y + (max_height_ - launcher_bounds.height()) / 2, |
144 available_bounds.width(), | 132 available_bounds.width(), |
145 launcher_bounds.height()); | 133 launcher_bounds.height()); |
146 if (visible) | 134 if (visible) |
147 target_bounds->work_area_insets = gfx::Insets(0, 0, max_height_, 0); | 135 target_bounds->work_area_insets = gfx::Insets(0, 0, max_height_, 0); |
148 } | 136 } |
149 | 137 |
150 void ShelfLayoutManager::OnImplicitAnimationsCompleted() { | 138 void ShelfLayoutManager::AnimateWidgetTo(views::Widget* widget, |
| 139 const gfx::Rect& target_bounds, |
| 140 float target_opacity) { |
| 141 ui::Layer* layer = GetLayer(widget); |
| 142 ui::ScopedLayerAnimationSettings animation_setter(layer->GetAnimator()); |
| 143 // Don't go through the widget, otherwise we end up back in SetChildBounds and |
| 144 // cancel the animation/layout. |
| 145 layer->SetBounds(target_bounds); |
| 146 layer->SetOpacity(target_opacity); |
| 147 } |
| 148 |
| 149 void ShelfLayoutManager::OnLayerAnimationEnded( |
| 150 const ui::LayerAnimationSequence* sequence) { |
| 151 if (!animating_) |
| 152 return; |
| 153 animating_ = false; |
| 154 visible_ = !visible_; |
151 TargetBounds target_bounds; | 155 TargetBounds target_bounds; |
152 CalculateTargetBounds(visible_, &target_bounds); | 156 CalculateTargetBounds(visible_, &target_bounds); |
153 aura::RootWindow::GetInstance()->screen()->set_work_area_insets( | 157 aura::RootWindow::GetInstance()->screen()->set_work_area_insets( |
154 target_bounds.work_area_insets); | 158 target_bounds.work_area_insets); |
155 } | 159 } |
156 | 160 |
157 } // namespace internal | 161 } // namespace internal |
158 } // namespace ash | 162 } // namespace ash |
OLD | NEW |