| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "ui/views/corewm/shadow_controller.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 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/activation_client.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_event_dispatcher.h" | |
| 18 #include "ui/aura/window_observer.h" | |
| 19 #include "ui/compositor/layer.h" | |
| 20 #include "ui/views/corewm/shadow.h" | |
| 21 #include "ui/views/corewm/shadow_types.h" | |
| 22 #include "ui/views/corewm/window_util.h" | |
| 23 | |
| 24 using std::make_pair; | |
| 25 | |
| 26 namespace views { | |
| 27 namespace corewm { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 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; | |
| 40 } | |
| 41 return SHADOW_TYPE_NONE; | |
| 42 } | |
| 43 | |
| 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; | |
| 51 } | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 // Returns the shadow style to be applied to |losing_active| when it is losing | |
| 56 // active to |gaining_active|. |gaining_active| may be of a type that hides when | |
| 57 // inactive, and as such we do not want to render |losing_active| as inactive. | |
| 58 Shadow::Style GetShadowStyleForWindowLosingActive( | |
| 59 aura::Window* losing_active, | |
| 60 aura::Window* gaining_active) { | |
| 61 if (gaining_active && aura::client::GetHideOnDeactivate(gaining_active)) { | |
| 62 aura::Window::Windows::const_iterator it = | |
| 63 std::find(GetTransientChildren(losing_active).begin(), | |
| 64 GetTransientChildren(losing_active).end(), | |
| 65 gaining_active); | |
| 66 if (it != GetTransientChildren(losing_active).end()) | |
| 67 return Shadow::STYLE_ACTIVE; | |
| 68 } | |
| 69 return Shadow::STYLE_INACTIVE; | |
| 70 } | |
| 71 | |
| 72 } // namespace | |
| 73 | |
| 74 // ShadowController::Impl ------------------------------------------------------ | |
| 75 | |
| 76 // Real implementation of the ShadowController. ShadowController observes | |
| 77 // ActivationChangeObserver, which are per ActivationClient, where as there is | |
| 78 // only a single Impl (as it observes all window creation by way of an | |
| 79 // EnvObserver). | |
| 80 class ShadowController::Impl : | |
| 81 public aura::EnvObserver, | |
| 82 public aura::WindowObserver, | |
| 83 public base::RefCounted<Impl> { | |
| 84 public: | |
| 85 // Returns the singleton instance, destroyed when there are no more refs. | |
| 86 static Impl* GetInstance(); | |
| 87 | |
| 88 // aura::EnvObserver override: | |
| 89 virtual void OnWindowInitialized(aura::Window* window) OVERRIDE; | |
| 90 | |
| 91 // aura::WindowObserver overrides: | |
| 92 virtual void OnWindowPropertyChanged( | |
| 93 aura::Window* window, const void* key, intptr_t old) OVERRIDE; | |
| 94 virtual void OnWindowBoundsChanged( | |
| 95 aura::Window* window, | |
| 96 const gfx::Rect& old_bounds, | |
| 97 const gfx::Rect& new_bounds) OVERRIDE; | |
| 98 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE; | |
| 99 | |
| 100 private: | |
| 101 friend class base::RefCounted<Impl>; | |
| 102 friend class ShadowController; | |
| 103 friend class ShadowController::TestApi; | |
| 104 | |
| 105 typedef std::map<aura::Window*, linked_ptr<Shadow> > WindowShadowMap; | |
| 106 | |
| 107 Impl(); | |
| 108 virtual ~Impl(); | |
| 109 | |
| 110 // Forwarded from ShadowController. | |
| 111 void OnWindowActivated(aura::Window* gained_active, | |
| 112 aura::Window* lost_active); | |
| 113 | |
| 114 // Checks if |window| is visible and contains a property requesting a shadow. | |
| 115 bool ShouldShowShadowForWindow(aura::Window* window) const; | |
| 116 | |
| 117 // Returns |window|'s shadow from |window_shadows_|, or NULL if no shadow | |
| 118 // exists. | |
| 119 Shadow* GetShadowForWindow(aura::Window* window); | |
| 120 | |
| 121 // Updates the shadow styles for windows when activation changes. | |
| 122 void HandleWindowActivationChange(aura::Window* gaining_active, | |
| 123 aura::Window* losing_active); | |
| 124 | |
| 125 // Shows or hides |window|'s shadow as needed (creating the shadow if | |
| 126 // necessary). | |
| 127 void HandlePossibleShadowVisibilityChange(aura::Window* window); | |
| 128 | |
| 129 // Creates a new shadow for |window| and stores it in |window_shadows_|. The | |
| 130 // shadow's bounds are initialized and it is added to the window's layer. | |
| 131 void CreateShadowForWindow(aura::Window* window); | |
| 132 | |
| 133 WindowShadowMap window_shadows_; | |
| 134 | |
| 135 ScopedObserver<aura::Window, aura::WindowObserver> observer_manager_; | |
| 136 | |
| 137 static Impl* instance_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(Impl); | |
| 140 }; | |
| 141 | |
| 142 // static | |
| 143 ShadowController::Impl* ShadowController::Impl::instance_ = NULL; | |
| 144 | |
| 145 // static | |
| 146 ShadowController::Impl* ShadowController::Impl::GetInstance() { | |
| 147 if (!instance_) | |
| 148 instance_ = new Impl(); | |
| 149 return instance_; | |
| 150 } | |
| 151 | |
| 152 void ShadowController::Impl::OnWindowInitialized(aura::Window* window) { | |
| 153 observer_manager_.Add(window); | |
| 154 SetShadowType(window, GetShadowTypeFromWindow(window)); | |
| 155 HandlePossibleShadowVisibilityChange(window); | |
| 156 } | |
| 157 | |
| 158 void ShadowController::Impl::OnWindowPropertyChanged(aura::Window* window, | |
| 159 const void* key, | |
| 160 intptr_t old) { | |
| 161 if (key == kShadowTypeKey) { | |
| 162 HandlePossibleShadowVisibilityChange(window); | |
| 163 return; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 void ShadowController::Impl::OnWindowBoundsChanged( | |
| 168 aura::Window* window, | |
| 169 const gfx::Rect& old_bounds, | |
| 170 const gfx::Rect& new_bounds) { | |
| 171 Shadow* shadow = GetShadowForWindow(window); | |
| 172 if (shadow) | |
| 173 shadow->SetContentBounds(gfx::Rect(new_bounds.size())); | |
| 174 } | |
| 175 | |
| 176 void ShadowController::Impl::OnWindowDestroyed(aura::Window* window) { | |
| 177 window_shadows_.erase(window); | |
| 178 observer_manager_.Remove(window); | |
| 179 } | |
| 180 | |
| 181 void ShadowController::Impl::OnWindowActivated(aura::Window* gained_active, | |
| 182 aura::Window* lost_active) { | |
| 183 if (gained_active) { | |
| 184 Shadow* shadow = GetShadowForWindow(gained_active); | |
| 185 if (shadow && !ShouldUseSmallShadowForWindow(gained_active)) | |
| 186 shadow->SetStyle(Shadow::STYLE_ACTIVE); | |
| 187 } | |
| 188 if (lost_active) { | |
| 189 Shadow* shadow = GetShadowForWindow(lost_active); | |
| 190 if (shadow && !ShouldUseSmallShadowForWindow(lost_active)) { | |
| 191 shadow->SetStyle(GetShadowStyleForWindowLosingActive(lost_active, | |
| 192 gained_active)); | |
| 193 } | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 bool ShadowController::Impl::ShouldShowShadowForWindow( | |
| 198 aura::Window* window) const { | |
| 199 const ShadowType type = GetShadowType(window); | |
| 200 switch (type) { | |
| 201 case SHADOW_TYPE_NONE: | |
| 202 return false; | |
| 203 case SHADOW_TYPE_RECTANGULAR: | |
| 204 return true; | |
| 205 default: | |
| 206 NOTREACHED() << "Unknown shadow type " << type; | |
| 207 return false; | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 Shadow* ShadowController::Impl::GetShadowForWindow(aura::Window* window) { | |
| 212 WindowShadowMap::const_iterator it = window_shadows_.find(window); | |
| 213 return it != window_shadows_.end() ? it->second.get() : NULL; | |
| 214 } | |
| 215 | |
| 216 void ShadowController::Impl::HandlePossibleShadowVisibilityChange( | |
| 217 aura::Window* window) { | |
| 218 const bool should_show = ShouldShowShadowForWindow(window); | |
| 219 Shadow* shadow = GetShadowForWindow(window); | |
| 220 if (shadow) | |
| 221 shadow->layer()->SetVisible(should_show); | |
| 222 else if (should_show && !shadow) | |
| 223 CreateShadowForWindow(window); | |
| 224 } | |
| 225 | |
| 226 void ShadowController::Impl::CreateShadowForWindow(aura::Window* window) { | |
| 227 linked_ptr<Shadow> shadow(new Shadow()); | |
| 228 window_shadows_.insert(make_pair(window, shadow)); | |
| 229 | |
| 230 shadow->Init(ShouldUseSmallShadowForWindow(window) ? | |
| 231 Shadow::STYLE_SMALL : Shadow::STYLE_ACTIVE); | |
| 232 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); | |
| 233 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); | |
| 234 window->layer()->Add(shadow->layer()); | |
| 235 } | |
| 236 | |
| 237 ShadowController::Impl::Impl() | |
| 238 : observer_manager_(this) { | |
| 239 aura::Env::GetInstance()->AddObserver(this); | |
| 240 } | |
| 241 | |
| 242 ShadowController::Impl::~Impl() { | |
| 243 DCHECK_EQ(instance_, this); | |
| 244 aura::Env::GetInstance()->RemoveObserver(this); | |
| 245 instance_ = NULL; | |
| 246 } | |
| 247 | |
| 248 // ShadowController ------------------------------------------------------------ | |
| 249 | |
| 250 ShadowController::ShadowController( | |
| 251 aura::client::ActivationClient* activation_client) | |
| 252 : activation_client_(activation_client), | |
| 253 impl_(Impl::GetInstance()) { | |
| 254 // Watch for window activation changes. | |
| 255 activation_client_->AddObserver(this); | |
| 256 } | |
| 257 | |
| 258 ShadowController::~ShadowController() { | |
| 259 activation_client_->RemoveObserver(this); | |
| 260 } | |
| 261 | |
| 262 void ShadowController::OnWindowActivated(aura::Window* gained_active, | |
| 263 aura::Window* lost_active) { | |
| 264 impl_->OnWindowActivated(gained_active, lost_active); | |
| 265 } | |
| 266 | |
| 267 // ShadowController::TestApi --------------------------------------------------- | |
| 268 | |
| 269 Shadow* ShadowController::TestApi::GetShadowForWindow(aura::Window* window) { | |
| 270 return controller_->impl_->GetShadowForWindow(window); | |
| 271 } | |
| 272 | |
| 273 } // namespace corewm | |
| 274 } // namespace views | |
| OLD | NEW |