Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / wm / core / shadow_controller.cc
blob1e5dd49b35d164715281ea60b2812a1244f09ae1
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"
7 #include <utility>
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"
25 using std::make_pair;
27 namespace wm {
29 namespace {
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;
38 default:
39 break;
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:
48 return true;
49 default:
50 break;
52 return false;
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(),
70 gaining_active);
71 if (it != GetTransientChildren(losing_active).end())
72 return Shadow::STYLE_ACTIVE;
74 return Shadow::STYLE_INACTIVE;
77 } // namespace
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
84 // EnvObserver).
85 class ShadowController::Impl :
86 public aura::EnvObserver,
87 public aura::WindowObserver,
88 public base::RefCounted<Impl> {
89 public:
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,
98 const void* key,
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;
105 private:
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;
112 Impl();
113 ~Impl() override;
115 // Forwarded from ShadowController.
116 void OnWindowActivated(ActivationReason reason,
117 aura::Window* gained_active,
118 aura::Window* lost_active);
120 // Checks if |window| is visible and contains a property requesting a shadow.
121 bool ShouldShowShadowForWindow(aura::Window* window) const;
123 // Returns |window|'s shadow from |window_shadows_|, or NULL if no shadow
124 // exists.
125 Shadow* GetShadowForWindow(aura::Window* window);
127 // Updates the shadow styles for windows when activation changes.
128 void HandleWindowActivationChange(aura::Window* gaining_active,
129 aura::Window* losing_active);
131 // Shows or hides |window|'s shadow as needed (creating the shadow if
132 // necessary).
133 void HandlePossibleShadowVisibilityChange(aura::Window* window);
135 // Creates a new shadow for |window| and stores it in |window_shadows_|. The
136 // shadow's bounds are initialized and it is added to the window's layer.
137 void CreateShadowForWindow(aura::Window* window);
139 WindowShadowMap window_shadows_;
141 ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_;
143 static Impl* instance_;
145 DISALLOW_COPY_AND_ASSIGN(Impl);
148 // static
149 ShadowController::Impl* ShadowController::Impl::instance_ = NULL;
151 // static
152 ShadowController::Impl* ShadowController::Impl::GetInstance() {
153 if (!instance_)
154 instance_ = new Impl();
155 return instance_;
158 void ShadowController::Impl::OnWindowInitialized(aura::Window* window) {
159 observer_manager_.Add(window);
160 SetShadowType(window, GetShadowTypeFromWindow(window));
161 HandlePossibleShadowVisibilityChange(window);
164 void ShadowController::Impl::OnWindowPropertyChanged(aura::Window* window,
165 const void* key,
166 intptr_t old) {
167 if (key == kShadowTypeKey || key == aura::client::kShowStateKey) {
168 HandlePossibleShadowVisibilityChange(window);
169 return;
173 void ShadowController::Impl::OnWindowBoundsChanged(
174 aura::Window* window,
175 const gfx::Rect& old_bounds,
176 const gfx::Rect& new_bounds) {
177 Shadow* shadow = GetShadowForWindow(window);
178 if (shadow)
179 shadow->SetContentBounds(gfx::Rect(new_bounds.size()));
182 void ShadowController::Impl::OnWindowDestroyed(aura::Window* window) {
183 window_shadows_.erase(window);
184 observer_manager_.Remove(window);
187 void ShadowController::Impl::OnWindowActivated(ActivationReason reason,
188 aura::Window* gained_active,
189 aura::Window* lost_active) {
190 if (gained_active) {
191 Shadow* shadow = GetShadowForWindow(gained_active);
192 if (shadow && !ShouldUseSmallShadowForWindow(gained_active))
193 shadow->SetStyle(Shadow::STYLE_ACTIVE);
195 if (lost_active) {
196 Shadow* shadow = GetShadowForWindow(lost_active);
197 if (shadow && !ShouldUseSmallShadowForWindow(lost_active)) {
198 shadow->SetStyle(GetShadowStyleForWindowLosingActive(lost_active,
199 gained_active));
204 bool ShadowController::Impl::ShouldShowShadowForWindow(
205 aura::Window* window) const {
206 ui::WindowShowState show_state =
207 window->GetProperty(aura::client::kShowStateKey);
208 if (show_state == ui::SHOW_STATE_FULLSCREEN ||
209 show_state == ui::SHOW_STATE_MAXIMIZED) {
210 return SHADOW_TYPE_NONE;
213 const ShadowType type = GetShadowType(window);
214 switch (type) {
215 case SHADOW_TYPE_NONE:
216 return false;
217 case SHADOW_TYPE_RECTANGULAR:
218 return true;
219 default:
220 NOTREACHED() << "Unknown shadow type " << type;
221 return false;
225 Shadow* ShadowController::Impl::GetShadowForWindow(aura::Window* window) {
226 WindowShadowMap::const_iterator it = window_shadows_.find(window);
227 return it != window_shadows_.end() ? it->second.get() : NULL;
230 void ShadowController::Impl::HandlePossibleShadowVisibilityChange(
231 aura::Window* window) {
232 const bool should_show = ShouldShowShadowForWindow(window);
233 Shadow* shadow = GetShadowForWindow(window);
234 if (shadow) {
235 shadow->SetStyle(GetShadowStyleForWindow(window));
236 shadow->layer()->SetVisible(should_show);
237 } else if (should_show && !shadow) {
238 CreateShadowForWindow(window);
242 void ShadowController::Impl::CreateShadowForWindow(aura::Window* window) {
243 linked_ptr<Shadow> shadow(new Shadow());
244 window_shadows_.insert(make_pair(window, shadow));
245 shadow->Init(GetShadowStyleForWindow(window));
246 shadow->SetContentBounds(gfx::Rect(window->bounds().size()));
247 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window));
248 window->layer()->Add(shadow->layer());
251 ShadowController::Impl::Impl()
252 : observer_manager_(this) {
253 aura::Env::GetInstance()->AddObserver(this);
256 ShadowController::Impl::~Impl() {
257 DCHECK_EQ(instance_, this);
258 aura::Env::GetInstance()->RemoveObserver(this);
259 instance_ = NULL;
262 // ShadowController ------------------------------------------------------------
264 ShadowController::ShadowController(
265 aura::client::ActivationClient* activation_client)
266 : activation_client_(activation_client),
267 impl_(Impl::GetInstance()) {
268 // Watch for window activation changes.
269 activation_client_->AddObserver(this);
272 ShadowController::~ShadowController() {
273 activation_client_->RemoveObserver(this);
276 void ShadowController::OnWindowActivated(ActivationReason reason,
277 aura::Window* gained_active,
278 aura::Window* lost_active) {
279 impl_->OnWindowActivated(reason, gained_active, lost_active);
282 // ShadowController::TestApi ---------------------------------------------------
284 Shadow* ShadowController::TestApi::GetShadowForWindow(aura::Window* window) {
285 return controller_->impl_->GetShadowForWindow(window);
288 } // namespace wm