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
3 // found in the LICENSE file.
5 #include "ui/wm/core/shadow_controller.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/linked_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "ui/aura/client/aura_constants.h"
14 #include "ui/aura/env.h"
15 #include "ui/aura/env_observer.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_observer.h"
18 #include "ui/base/ui_base_types.h"
19 #include "ui/compositor/layer.h"
20 #include "ui/wm/core/shadow.h"
21 #include "ui/wm/core/shadow_types.h"
22 #include "ui/wm/core/window_util.h"
23 #include "ui/wm/public/activation_client.h"
31 ShadowType
GetShadowTypeFromWindow(aura::Window
* window
) {
32 switch (window
->type()) {
33 case ui::wm::WINDOW_TYPE_NORMAL
:
34 case ui::wm::WINDOW_TYPE_PANEL
:
35 case ui::wm::WINDOW_TYPE_MENU
:
36 case ui::wm::WINDOW_TYPE_TOOLTIP
:
37 return SHADOW_TYPE_RECTANGULAR
;
41 return SHADOW_TYPE_NONE
;
44 bool ShouldUseSmallShadowForWindow(aura::Window
* window
) {
45 switch (window
->type()) {
46 case ui::wm::WINDOW_TYPE_MENU
:
47 case ui::wm::WINDOW_TYPE_TOOLTIP
:
55 Shadow::Style
GetShadowStyleForWindow(aura::Window
* window
) {
56 return ShouldUseSmallShadowForWindow(window
) ? Shadow::STYLE_SMALL
:
57 (IsActiveWindow(window
) ? Shadow::STYLE_ACTIVE
: Shadow::STYLE_INACTIVE
);
60 // Returns the shadow style to be applied to |losing_active| when it is losing
61 // active to |gaining_active|. |gaining_active| may be of a type that hides when
62 // inactive, and as such we do not want to render |losing_active| as inactive.
63 Shadow::Style
GetShadowStyleForWindowLosingActive(
64 aura::Window
* losing_active
,
65 aura::Window
* gaining_active
) {
66 if (gaining_active
&& aura::client::GetHideOnDeactivate(gaining_active
)) {
67 aura::Window::Windows::const_iterator it
=
68 std::find(GetTransientChildren(losing_active
).begin(),
69 GetTransientChildren(losing_active
).end(),
71 if (it
!= GetTransientChildren(losing_active
).end())
72 return Shadow::STYLE_ACTIVE
;
74 return Shadow::STYLE_INACTIVE
;
79 // ShadowController::Impl ------------------------------------------------------
81 // Real implementation of the ShadowController. ShadowController observes
82 // ActivationChangeObserver, which are per ActivationClient, where as there is
83 // only a single Impl (as it observes all window creation by way of an
85 class ShadowController::Impl
:
86 public aura::EnvObserver
,
87 public aura::WindowObserver
,
88 public base::RefCounted
<Impl
> {
90 // Returns the singleton instance, destroyed when there are no more refs.
91 static Impl
* GetInstance();
93 // aura::EnvObserver override:
94 void OnWindowInitialized(aura::Window
* window
) override
;
96 // aura::WindowObserver overrides:
97 void OnWindowPropertyChanged(aura::Window
* window
,
99 intptr_t old
) override
;
100 void OnWindowBoundsChanged(aura::Window
* window
,
101 const gfx::Rect
& old_bounds
,
102 const gfx::Rect
& new_bounds
) override
;
103 void OnWindowDestroyed(aura::Window
* window
) override
;
106 friend class base::RefCounted
<Impl
>;
107 friend class ShadowController
;
108 friend class ShadowController::TestApi
;
110 typedef std::map
<aura::Window
*, linked_ptr
<Shadow
> > WindowShadowMap
;
115 // Forwarded from ShadowController.
116 void OnWindowActivated(aura::Window
* gained_active
,
117 aura::Window
* lost_active
);
119 // Checks if |window| is visible and contains a property requesting a shadow.
120 bool ShouldShowShadowForWindow(aura::Window
* window
) const;
122 // Returns |window|'s shadow from |window_shadows_|, or NULL if no shadow
124 Shadow
* GetShadowForWindow(aura::Window
* window
);
126 // Updates the shadow styles for windows when activation changes.
127 void HandleWindowActivationChange(aura::Window
* gaining_active
,
128 aura::Window
* losing_active
);
130 // Shows or hides |window|'s shadow as needed (creating the shadow if
132 void HandlePossibleShadowVisibilityChange(aura::Window
* window
);
134 // Creates a new shadow for |window| and stores it in |window_shadows_|. The
135 // shadow's bounds are initialized and it is added to the window's layer.
136 void CreateShadowForWindow(aura::Window
* window
);
138 WindowShadowMap window_shadows_
;
140 ScopedObserver
<aura::Window
, aura::WindowObserver
> observer_manager_
;
142 static Impl
* instance_
;
144 DISALLOW_COPY_AND_ASSIGN(Impl
);
148 ShadowController::Impl
* ShadowController::Impl::instance_
= NULL
;
151 ShadowController::Impl
* ShadowController::Impl::GetInstance() {
153 instance_
= new Impl();
157 void ShadowController::Impl::OnWindowInitialized(aura::Window
* window
) {
158 observer_manager_
.Add(window
);
159 SetShadowType(window
, GetShadowTypeFromWindow(window
));
160 HandlePossibleShadowVisibilityChange(window
);
163 void ShadowController::Impl::OnWindowPropertyChanged(aura::Window
* window
,
166 if (key
== kShadowTypeKey
|| key
== aura::client::kShowStateKey
) {
167 HandlePossibleShadowVisibilityChange(window
);
172 void ShadowController::Impl::OnWindowBoundsChanged(
173 aura::Window
* window
,
174 const gfx::Rect
& old_bounds
,
175 const gfx::Rect
& new_bounds
) {
176 Shadow
* shadow
= GetShadowForWindow(window
);
178 shadow
->SetContentBounds(gfx::Rect(new_bounds
.size()));
181 void ShadowController::Impl::OnWindowDestroyed(aura::Window
* window
) {
182 window_shadows_
.erase(window
);
183 observer_manager_
.Remove(window
);
186 void ShadowController::Impl::OnWindowActivated(aura::Window
* gained_active
,
187 aura::Window
* lost_active
) {
189 Shadow
* shadow
= GetShadowForWindow(gained_active
);
190 if (shadow
&& !ShouldUseSmallShadowForWindow(gained_active
))
191 shadow
->SetStyle(Shadow::STYLE_ACTIVE
);
194 Shadow
* shadow
= GetShadowForWindow(lost_active
);
195 if (shadow
&& !ShouldUseSmallShadowForWindow(lost_active
)) {
196 shadow
->SetStyle(GetShadowStyleForWindowLosingActive(lost_active
,
202 bool ShadowController::Impl::ShouldShowShadowForWindow(
203 aura::Window
* window
) const {
204 ui::WindowShowState show_state
=
205 window
->GetProperty(aura::client::kShowStateKey
);
206 if (show_state
== ui::SHOW_STATE_FULLSCREEN
||
207 show_state
== ui::SHOW_STATE_MAXIMIZED
) {
208 return SHADOW_TYPE_NONE
;
211 const ShadowType type
= GetShadowType(window
);
213 case SHADOW_TYPE_NONE
:
215 case SHADOW_TYPE_RECTANGULAR
:
218 NOTREACHED() << "Unknown shadow type " << type
;
223 Shadow
* ShadowController::Impl::GetShadowForWindow(aura::Window
* window
) {
224 WindowShadowMap::const_iterator it
= window_shadows_
.find(window
);
225 return it
!= window_shadows_
.end() ? it
->second
.get() : NULL
;
228 void ShadowController::Impl::HandlePossibleShadowVisibilityChange(
229 aura::Window
* window
) {
230 const bool should_show
= ShouldShowShadowForWindow(window
);
231 Shadow
* shadow
= GetShadowForWindow(window
);
233 shadow
->SetStyle(GetShadowStyleForWindow(window
));
234 shadow
->layer()->SetVisible(should_show
);
235 } else if (should_show
&& !shadow
) {
236 CreateShadowForWindow(window
);
240 void ShadowController::Impl::CreateShadowForWindow(aura::Window
* window
) {
241 linked_ptr
<Shadow
> shadow(new Shadow());
242 window_shadows_
.insert(make_pair(window
, shadow
));
243 shadow
->Init(GetShadowStyleForWindow(window
));
244 shadow
->SetContentBounds(gfx::Rect(window
->bounds().size()));
245 shadow
->layer()->SetVisible(ShouldShowShadowForWindow(window
));
246 window
->layer()->Add(shadow
->layer());
249 ShadowController::Impl::Impl()
250 : observer_manager_(this) {
251 aura::Env::GetInstance()->AddObserver(this);
254 ShadowController::Impl::~Impl() {
255 DCHECK_EQ(instance_
, this);
256 aura::Env::GetInstance()->RemoveObserver(this);
260 // ShadowController ------------------------------------------------------------
262 ShadowController::ShadowController(
263 aura::client::ActivationClient
* activation_client
)
264 : activation_client_(activation_client
),
265 impl_(Impl::GetInstance()) {
266 // Watch for window activation changes.
267 activation_client_
->AddObserver(this);
270 ShadowController::~ShadowController() {
271 activation_client_
->RemoveObserver(this);
274 void ShadowController::OnWindowActivated(aura::Window
* gained_active
,
275 aura::Window
* lost_active
) {
276 impl_
->OnWindowActivated(gained_active
, lost_active
);
279 // ShadowController::TestApi ---------------------------------------------------
281 Shadow
* ShadowController::TestApi::GetShadowForWindow(aura::Window
* window
) {
282 return controller_
->impl_
->GetShadowForWindow(window
);