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 "ui/wm/core/shadow_controller.h" | 5 #include "ui/wm/core/shadow_controller.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 | 25 |
26 using std::make_pair; | 26 using std::make_pair; |
27 | 27 |
28 DECLARE_WINDOW_PROPERTY_TYPE(wm::Shadow*); | 28 DECLARE_WINDOW_PROPERTY_TYPE(wm::Shadow*); |
29 DEFINE_OWNED_WINDOW_PROPERTY_KEY(wm::Shadow, kShadowLayerKey, nullptr); | 29 DEFINE_OWNED_WINDOW_PROPERTY_KEY(wm::Shadow, kShadowLayerKey, nullptr); |
30 | 30 |
31 namespace wm { | 31 namespace wm { |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 ShadowType GetShadowTypeFromWindow(aura::Window* window) { | 35 constexpr ShadowElevation kActiveNormalShadowElevation = ShadowElevation::LARGE; |
| 36 constexpr ShadowElevation kInactiveNormalShadowElevation = |
| 37 ShadowElevation::MEDIUM; |
| 38 |
| 39 ShadowElevation GetDefaultShadowElevationForWindow(aura::Window* window) { |
36 switch (window->type()) { | 40 switch (window->type()) { |
37 case ui::wm::WINDOW_TYPE_NORMAL: | 41 case ui::wm::WINDOW_TYPE_NORMAL: |
38 case ui::wm::WINDOW_TYPE_PANEL: | 42 case ui::wm::WINDOW_TYPE_PANEL: |
| 43 return kInactiveNormalShadowElevation; |
| 44 |
39 case ui::wm::WINDOW_TYPE_MENU: | 45 case ui::wm::WINDOW_TYPE_MENU: |
40 case ui::wm::WINDOW_TYPE_TOOLTIP: | 46 case ui::wm::WINDOW_TYPE_TOOLTIP: |
41 return SHADOW_TYPE_RECTANGULAR; | 47 return ShadowElevation::SMALL; |
| 48 |
42 default: | 49 default: |
43 break; | 50 break; |
44 } | 51 } |
45 return SHADOW_TYPE_NONE; | 52 return ShadowElevation::NONE; |
46 } | 53 } |
47 | 54 |
48 Shadow::Style GetShadowStyleForWindow(aura::Window* window) { | 55 ShadowElevation GetShadowElevationForActiveState(aura::Window* window) { |
49 switch (window->type()) { | 56 if (IsActiveWindow(window)) |
50 case ui::wm::WINDOW_TYPE_MENU: | 57 return kActiveNormalShadowElevation; |
51 case ui::wm::WINDOW_TYPE_TOOLTIP: | |
52 return Shadow::STYLE_SMALL; | |
53 | 58 |
54 // System tray bubbles render like active windows. TODO(estade): this | 59 return GetShadowElevation(window); |
55 // mechanism will need to be revisited for applying WM shadows to other | |
56 // types of bubbles which don't want to render such large shadows. | |
57 case ui::wm::WINDOW_TYPE_POPUP: | |
58 return Shadow::STYLE_ACTIVE; | |
59 | |
60 default: | |
61 return IsActiveWindow(window) ? Shadow::STYLE_ACTIVE | |
62 : Shadow::STYLE_INACTIVE; | |
63 } | |
64 } | 60 } |
65 | 61 |
66 // Returns the shadow style to be applied to |losing_active| when it is losing | 62 // Returns the shadow style to be applied to |losing_active| when it is losing |
67 // active to |gaining_active|. |gaining_active| may be of a type that hides when | 63 // active to |gaining_active|. |gaining_active| may be of a type that hides when |
68 // inactive, and as such we do not want to render |losing_active| as inactive. | 64 // inactive, and as such we do not want to render |losing_active| as inactive. |
69 Shadow::Style GetShadowStyleForWindowLosingActive( | 65 ShadowElevation GetShadowElevationForWindowLosingActive( |
70 aura::Window* losing_active, | 66 aura::Window* losing_active, |
71 aura::Window* gaining_active) { | 67 aura::Window* gaining_active) { |
72 if (gaining_active && aura::client::GetHideOnDeactivate(gaining_active)) { | 68 if (gaining_active && aura::client::GetHideOnDeactivate(gaining_active)) { |
73 aura::Window::Windows::const_iterator it = | 69 aura::Window::Windows::const_iterator it = |
74 std::find(GetTransientChildren(losing_active).begin(), | 70 std::find(GetTransientChildren(losing_active).begin(), |
75 GetTransientChildren(losing_active).end(), | 71 GetTransientChildren(losing_active).end(), |
76 gaining_active); | 72 gaining_active); |
77 if (it != GetTransientChildren(losing_active).end()) | 73 if (it != GetTransientChildren(losing_active).end()) |
78 return Shadow::STYLE_ACTIVE; | 74 return kActiveNormalShadowElevation; |
79 } | 75 } |
80 return Shadow::STYLE_INACTIVE; | 76 return kInactiveNormalShadowElevation; |
81 } | 77 } |
82 | 78 |
83 } // namespace | 79 } // namespace |
84 | 80 |
85 // ShadowController::Impl ------------------------------------------------------ | 81 // ShadowController::Impl ------------------------------------------------------ |
86 | 82 |
87 // Real implementation of the ShadowController. ShadowController observes | 83 // Real implementation of the ShadowController. ShadowController observes |
88 // ActivationChangeObserver, which are per ActivationClient, where as there is | 84 // ActivationChangeObserver, which are per ActivationClient, where as there is |
89 // only a single Impl (as it observes all window creation by way of an | 85 // only a single Impl (as it observes all window creation by way of an |
90 // EnvObserver). | 86 // EnvObserver). |
(...skipping 25 matching lines...) Expand all Loading... |
116 ~Impl() override; | 112 ~Impl() override; |
117 | 113 |
118 // Forwarded from ShadowController. | 114 // Forwarded from ShadowController. |
119 void OnWindowActivated(ActivationReason reason, | 115 void OnWindowActivated(ActivationReason reason, |
120 aura::Window* gained_active, | 116 aura::Window* gained_active, |
121 aura::Window* lost_active); | 117 aura::Window* lost_active); |
122 | 118 |
123 // Checks if |window| is visible and contains a property requesting a shadow. | 119 // Checks if |window| is visible and contains a property requesting a shadow. |
124 bool ShouldShowShadowForWindow(aura::Window* window) const; | 120 bool ShouldShowShadowForWindow(aura::Window* window) const; |
125 | 121 |
126 // Updates the shadow styles for windows when activation changes. | 122 // Updates the shadow for windows when activation changes. |
127 void HandleWindowActivationChange(aura::Window* gaining_active, | 123 void HandleWindowActivationChange(aura::Window* gaining_active, |
128 aura::Window* losing_active); | 124 aura::Window* losing_active); |
129 | 125 |
130 // Shows or hides |window|'s shadow as needed (creating the shadow if | 126 // Shows or hides |window|'s shadow as needed (creating the shadow if |
131 // necessary). | 127 // necessary). |
132 void HandlePossibleShadowVisibilityChange(aura::Window* window); | 128 void HandlePossibleShadowVisibilityChange(aura::Window* window); |
133 | 129 |
134 // Creates a new shadow for |window| and stores it with the |kShadowLayerKey| | 130 // Creates a new shadow for |window| and stores it with the |kShadowLayerKey| |
135 // key. | 131 // key. |
136 // The shadow's bounds are initialized and it is added to the window's layer. | 132 // The shadow's bounds are initialized and it is added to the window's layer. |
(...skipping 11 matching lines...) Expand all Loading... |
148 | 144 |
149 // static | 145 // static |
150 ShadowController::Impl* ShadowController::Impl::GetInstance() { | 146 ShadowController::Impl* ShadowController::Impl::GetInstance() { |
151 if (!instance_) | 147 if (!instance_) |
152 instance_ = new Impl(); | 148 instance_ = new Impl(); |
153 return instance_; | 149 return instance_; |
154 } | 150 } |
155 | 151 |
156 void ShadowController::Impl::OnWindowInitialized(aura::Window* window) { | 152 void ShadowController::Impl::OnWindowInitialized(aura::Window* window) { |
157 observer_manager_.Add(window); | 153 observer_manager_.Add(window); |
158 SetShadowType(window, GetShadowTypeFromWindow(window)); | 154 SetShadowElevation(window, GetDefaultShadowElevationForWindow(window)); |
159 HandlePossibleShadowVisibilityChange(window); | 155 HandlePossibleShadowVisibilityChange(window); |
160 } | 156 } |
161 | 157 |
162 void ShadowController::Impl::OnWindowPropertyChanged(aura::Window* window, | 158 void ShadowController::Impl::OnWindowPropertyChanged(aura::Window* window, |
163 const void* key, | 159 const void* key, |
164 intptr_t old) { | 160 intptr_t old) { |
165 if (key == kShadowTypeKey) { | 161 if (key == kShadowElevationKey) { |
166 if (window->GetProperty(kShadowTypeKey) == static_cast<ShadowType>(old)) | 162 if (window->GetProperty(kShadowElevationKey) == |
| 163 static_cast<ShadowElevation>(old)) |
167 return; | 164 return; |
168 } else if (key == aura::client::kShowStateKey) { | 165 } else if (key == aura::client::kShowStateKey) { |
169 if (window->GetProperty(aura::client::kShowStateKey) == | 166 if (window->GetProperty(aura::client::kShowStateKey) == |
170 static_cast<ui::WindowShowState>(old)) { | 167 static_cast<ui::WindowShowState>(old)) { |
171 return; | 168 return; |
172 } | 169 } |
173 } else { | 170 } else { |
174 return; | 171 return; |
175 } | 172 } |
176 HandlePossibleShadowVisibilityChange(window); | 173 HandlePossibleShadowVisibilityChange(window); |
(...skipping 12 matching lines...) Expand all Loading... |
189 window->ClearProperty(kShadowLayerKey); | 186 window->ClearProperty(kShadowLayerKey); |
190 observer_manager_.Remove(window); | 187 observer_manager_.Remove(window); |
191 } | 188 } |
192 | 189 |
193 void ShadowController::Impl::OnWindowActivated(ActivationReason reason, | 190 void ShadowController::Impl::OnWindowActivated(ActivationReason reason, |
194 aura::Window* gained_active, | 191 aura::Window* gained_active, |
195 aura::Window* lost_active) { | 192 aura::Window* lost_active) { |
196 if (gained_active) { | 193 if (gained_active) { |
197 Shadow* shadow = GetShadowForWindow(gained_active); | 194 Shadow* shadow = GetShadowForWindow(gained_active); |
198 if (shadow) | 195 if (shadow) |
199 shadow->SetStyle(GetShadowStyleForWindow(gained_active)); | 196 shadow->SetElevation(GetShadowElevationForActiveState(gained_active)); |
200 } | 197 } |
201 if (lost_active) { | 198 if (lost_active) { |
202 Shadow* shadow = GetShadowForWindow(lost_active); | 199 Shadow* shadow = GetShadowForWindow(lost_active); |
203 if (shadow && | 200 if (shadow && |
204 GetShadowStyleForWindow(lost_active) == Shadow::STYLE_INACTIVE) { | 201 GetShadowElevation(lost_active) == kInactiveNormalShadowElevation) { |
205 shadow->SetStyle(GetShadowStyleForWindowLosingActive(lost_active, | 202 shadow->SetElevation( |
206 gained_active)); | 203 GetShadowElevationForWindowLosingActive(lost_active, gained_active)); |
207 } | 204 } |
208 } | 205 } |
209 } | 206 } |
210 | 207 |
211 bool ShadowController::Impl::ShouldShowShadowForWindow( | 208 bool ShadowController::Impl::ShouldShowShadowForWindow( |
212 aura::Window* window) const { | 209 aura::Window* window) const { |
213 ui::WindowShowState show_state = | 210 ui::WindowShowState show_state = |
214 window->GetProperty(aura::client::kShowStateKey); | 211 window->GetProperty(aura::client::kShowStateKey); |
215 if (show_state == ui::SHOW_STATE_FULLSCREEN || | 212 if (show_state == ui::SHOW_STATE_FULLSCREEN || |
216 show_state == ui::SHOW_STATE_MAXIMIZED) { | 213 show_state == ui::SHOW_STATE_MAXIMIZED) { |
217 return SHADOW_TYPE_NONE; | 214 return false; |
218 } | 215 } |
219 | 216 |
220 const ShadowType type = GetShadowType(window); | 217 return static_cast<int>(GetShadowElevation(window)) > 0; |
221 switch (type) { | |
222 case SHADOW_TYPE_NONE: | |
223 return false; | |
224 case SHADOW_TYPE_RECTANGULAR: | |
225 return true; | |
226 default: | |
227 NOTREACHED() << "Unknown shadow type " << type; | |
228 return false; | |
229 } | |
230 } | 218 } |
231 | 219 |
232 void ShadowController::Impl::HandlePossibleShadowVisibilityChange( | 220 void ShadowController::Impl::HandlePossibleShadowVisibilityChange( |
233 aura::Window* window) { | 221 aura::Window* window) { |
234 const bool should_show = ShouldShowShadowForWindow(window); | 222 const bool should_show = ShouldShowShadowForWindow(window); |
235 Shadow* shadow = GetShadowForWindow(window); | 223 Shadow* shadow = GetShadowForWindow(window); |
236 if (shadow) { | 224 if (shadow) { |
237 shadow->SetStyle(GetShadowStyleForWindow(window)); | 225 shadow->SetElevation(GetShadowElevationForActiveState(window)); |
238 shadow->layer()->SetVisible(should_show); | 226 shadow->layer()->SetVisible(should_show); |
239 } else if (should_show && !shadow) { | 227 } else if (should_show && !shadow) { |
240 CreateShadowForWindow(window); | 228 CreateShadowForWindow(window); |
241 } | 229 } |
242 } | 230 } |
243 | 231 |
244 void ShadowController::Impl::CreateShadowForWindow(aura::Window* window) { | 232 void ShadowController::Impl::CreateShadowForWindow(aura::Window* window) { |
245 Shadow* shadow = new Shadow(); | 233 Shadow* shadow = new Shadow(); |
246 window->SetProperty(kShadowLayerKey, shadow); | 234 window->SetProperty(kShadowLayerKey, shadow); |
247 shadow->Init(GetShadowStyleForWindow(window)); | 235 shadow->Init(GetShadowElevationForActiveState(window)); |
248 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); | 236 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); |
249 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); | 237 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); |
250 window->layer()->Add(shadow->layer()); | 238 window->layer()->Add(shadow->layer()); |
251 } | 239 } |
252 | 240 |
253 ShadowController::Impl::Impl() | 241 ShadowController::Impl::Impl() |
254 : observer_manager_(this) { | 242 : observer_manager_(this) { |
255 aura::Env::GetInstance()->AddObserver(this); | 243 aura::Env::GetInstance()->AddObserver(this); |
256 } | 244 } |
257 | 245 |
(...skipping 21 matching lines...) Expand all Loading... |
279 activation_client_->RemoveObserver(this); | 267 activation_client_->RemoveObserver(this); |
280 } | 268 } |
281 | 269 |
282 void ShadowController::OnWindowActivated(ActivationReason reason, | 270 void ShadowController::OnWindowActivated(ActivationReason reason, |
283 aura::Window* gained_active, | 271 aura::Window* gained_active, |
284 aura::Window* lost_active) { | 272 aura::Window* lost_active) { |
285 impl_->OnWindowActivated(reason, gained_active, lost_active); | 273 impl_->OnWindowActivated(reason, gained_active, lost_active); |
286 } | 274 } |
287 | 275 |
288 } // namespace wm | 276 } // namespace wm |
OLD | NEW |