OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/aura_shell/shadow_controller.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "ui/aura/client/aura_constants.h" |
| 11 #include "ui/aura/client/shadow_types.h" |
| 12 #include "ui/aura/desktop.h" |
| 13 #include "ui/aura/window.h" |
| 14 #include "ui/aura_shell/shadow.h" |
| 15 |
| 16 using std::make_pair; |
| 17 |
| 18 namespace aura_shell { |
| 19 namespace internal { |
| 20 |
| 21 ShadowController::ShadowController() { |
| 22 aura::Desktop::GetInstance()->AddObserver(this); |
| 23 } |
| 24 |
| 25 ShadowController::~ShadowController() { |
| 26 for (WindowShadowMap::const_iterator it = window_shadows_.begin(); |
| 27 it != window_shadows_.end(); ++it) { |
| 28 it->first->RemoveObserver(this); |
| 29 } |
| 30 aura::Desktop::GetInstance()->RemoveObserver(this); |
| 31 } |
| 32 |
| 33 void ShadowController::OnWindowInitialized(aura::Window* window) { |
| 34 window->AddObserver(this); |
| 35 } |
| 36 |
| 37 void ShadowController::OnWindowParentChanged(aura::Window* window, |
| 38 aura::Window* parent) { |
| 39 Shadow* shadow = GetShadowForWindow(window); |
| 40 |
| 41 if (parent) { |
| 42 if (shadow) { |
| 43 parent->layer()->Add(shadow->layer()); |
| 44 StackShadowBelowWindow(shadow, window); |
| 45 } else { |
| 46 if (ShouldShowShadowForWindow(window)) |
| 47 CreateShadowForWindow(window); |
| 48 } |
| 49 } else { |
| 50 if (shadow && shadow->layer()->parent()) |
| 51 shadow->layer()->parent()->Remove(shadow->layer()); |
| 52 } |
| 53 } |
| 54 |
| 55 void ShadowController::OnPropertyChanged(aura::Window* window, |
| 56 const char* name, |
| 57 void* old) { |
| 58 if (name == aura::kShadowTypeKey) |
| 59 HandlePossibleShadowVisibilityChange(window); |
| 60 } |
| 61 |
| 62 void ShadowController::OnWindowVisibilityChanged(aura::Window* window, |
| 63 bool visible) { |
| 64 HandlePossibleShadowVisibilityChange(window); |
| 65 } |
| 66 |
| 67 void ShadowController::OnWindowBoundsChanged(aura::Window* window, |
| 68 const gfx::Rect& bounds) { |
| 69 Shadow* shadow = GetShadowForWindow(window); |
| 70 if (shadow) |
| 71 shadow->SetContentBounds(bounds); |
| 72 } |
| 73 |
| 74 void ShadowController::OnWindowStackingChanged(aura::Window* window) { |
| 75 Shadow* shadow = GetShadowForWindow(window); |
| 76 if (shadow) |
| 77 StackShadowBelowWindow(shadow, window); |
| 78 } |
| 79 |
| 80 void ShadowController::OnWindowDestroyed(aura::Window* window) { |
| 81 window_shadows_.erase(window); |
| 82 } |
| 83 |
| 84 bool ShadowController::ShouldShowShadowForWindow(aura::Window* window) const { |
| 85 const aura::ShadowType type = static_cast<aura::ShadowType>( |
| 86 window->GetIntProperty(aura::kShadowTypeKey)); |
| 87 bool requested = false; |
| 88 switch (type) { |
| 89 case aura::SHADOW_TYPE_NONE: |
| 90 break; |
| 91 case aura::SHADOW_TYPE_RECTANGULAR: |
| 92 requested = true; |
| 93 break; |
| 94 default: |
| 95 NOTREACHED() << "Unknown shadow type " << type; |
| 96 } |
| 97 |
| 98 return requested && window->layer()->visible(); |
| 99 } |
| 100 |
| 101 Shadow* ShadowController::GetShadowForWindow(aura::Window* window) { |
| 102 WindowShadowMap::const_iterator it = window_shadows_.find(window); |
| 103 return it != window_shadows_.end() ? it->second.get() : NULL; |
| 104 } |
| 105 |
| 106 void ShadowController::HandlePossibleShadowVisibilityChange( |
| 107 aura::Window* window) { |
| 108 const bool should_show = ShouldShowShadowForWindow(window); |
| 109 Shadow* shadow = GetShadowForWindow(window); |
| 110 if (shadow) |
| 111 shadow->layer()->SetVisible(should_show); |
| 112 else if (should_show && !shadow) |
| 113 CreateShadowForWindow(window); |
| 114 } |
| 115 |
| 116 void ShadowController::CreateShadowForWindow(aura::Window* window) { |
| 117 linked_ptr<Shadow> shadow(new Shadow()); |
| 118 window_shadows_.insert(make_pair(window, shadow)); |
| 119 |
| 120 shadow->Init(); |
| 121 shadow->SetContentBounds(window->bounds()); |
| 122 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); |
| 123 |
| 124 if (window->parent()) { |
| 125 window->parent()->layer()->Add(shadow->layer()); |
| 126 StackShadowBelowWindow(shadow.get(), window); |
| 127 } |
| 128 } |
| 129 |
| 130 void ShadowController::StackShadowBelowWindow(Shadow* shadow, |
| 131 aura::Window* window) { |
| 132 ui::Layer* parent_layer = window->parent()->layer(); |
| 133 DCHECK_EQ(shadow->layer()->parent(), parent_layer); |
| 134 |
| 135 // TODO(derat): Add a MoveBelow() method and use that instead (although we |
| 136 // then run the risk of other layers getting stacked between a window and its |
| 137 // shadow). |
| 138 parent_layer->MoveAbove(shadow->layer(), window->layer()); |
| 139 parent_layer->MoveAbove(window->layer(), shadow->layer()); |
| 140 } |
| 141 |
| 142 } // namespace internal |
| 143 } // namespace aura_shell |
OLD | NEW |