OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/aura_shell/shadow_controller.h" | 5 #include "ui/aura_shell/shadow_controller.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "ui/aura/client/aura_constants.h" | 10 #include "ui/aura/client/aura_constants.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 ShadowController::~ShadowController() { | 25 ShadowController::~ShadowController() { |
26 for (WindowShadowMap::const_iterator it = window_shadows_.begin(); | 26 for (WindowShadowMap::const_iterator it = window_shadows_.begin(); |
27 it != window_shadows_.end(); ++it) { | 27 it != window_shadows_.end(); ++it) { |
28 it->first->RemoveObserver(this); | 28 it->first->RemoveObserver(this); |
29 } | 29 } |
30 aura::Desktop::GetInstance()->RemoveObserver(this); | 30 aura::Desktop::GetInstance()->RemoveObserver(this); |
31 } | 31 } |
32 | 32 |
33 void ShadowController::OnWindowInitialized(aura::Window* window) { | 33 void ShadowController::OnWindowInitialized(aura::Window* window) { |
34 window->AddObserver(this); | 34 window->AddObserver(this); |
35 } | 35 HandlePossibleShadowVisibilityChange(window); |
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 } | 36 } |
54 | 37 |
55 void ShadowController::OnWindowPropertyChanged(aura::Window* window, | 38 void ShadowController::OnWindowPropertyChanged(aura::Window* window, |
56 const char* name, | 39 const char* name, |
57 void* old) { | 40 void* old) { |
58 if (name == aura::kShadowTypeKey) | 41 if (name == aura::kShadowTypeKey) |
59 HandlePossibleShadowVisibilityChange(window); | 42 HandlePossibleShadowVisibilityChange(window); |
60 } | 43 } |
61 | 44 |
62 void ShadowController::OnWindowVisibilityChanged(aura::Window* window, | |
63 bool visible) { | |
64 HandlePossibleShadowVisibilityChange(window); | |
65 } | |
66 | |
67 void ShadowController::OnWindowBoundsChanged(aura::Window* window, | 45 void ShadowController::OnWindowBoundsChanged(aura::Window* window, |
68 const gfx::Rect& bounds) { | 46 const gfx::Rect& bounds) { |
69 Shadow* shadow = GetShadowForWindow(window); | 47 Shadow* shadow = GetShadowForWindow(window); |
70 if (shadow) | 48 if (shadow) |
71 shadow->SetContentBounds(bounds); | 49 shadow->SetContentBounds(gfx::Rect(bounds.size())); |
72 } | |
73 | |
74 void ShadowController::OnWindowStackingChanged(aura::Window* window) { | |
75 Shadow* shadow = GetShadowForWindow(window); | |
76 if (shadow) | |
77 StackShadowBelowWindow(shadow, window); | |
78 } | 50 } |
79 | 51 |
80 void ShadowController::OnWindowDestroyed(aura::Window* window) { | 52 void ShadowController::OnWindowDestroyed(aura::Window* window) { |
81 window_shadows_.erase(window); | 53 window_shadows_.erase(window); |
82 } | 54 } |
83 | 55 |
84 bool ShadowController::ShouldShowShadowForWindow(aura::Window* window) const { | 56 bool ShadowController::ShouldShowShadowForWindow(aura::Window* window) const { |
85 const aura::ShadowType type = static_cast<aura::ShadowType>( | 57 const aura::ShadowType type = static_cast<aura::ShadowType>( |
86 window->GetIntProperty(aura::kShadowTypeKey)); | 58 window->GetIntProperty(aura::kShadowTypeKey)); |
87 bool requested = false; | |
88 switch (type) { | 59 switch (type) { |
89 case aura::SHADOW_TYPE_NONE: | 60 case aura::SHADOW_TYPE_NONE: |
90 break; | 61 return false; |
91 case aura::SHADOW_TYPE_RECTANGULAR: | 62 case aura::SHADOW_TYPE_RECTANGULAR: |
92 requested = true; | 63 return true; |
93 break; | |
94 default: | 64 default: |
95 NOTREACHED() << "Unknown shadow type " << type; | 65 NOTREACHED() << "Unknown shadow type " << type; |
| 66 return false; |
96 } | 67 } |
97 | |
98 return requested && window->layer()->visible(); | |
99 } | 68 } |
100 | 69 |
101 Shadow* ShadowController::GetShadowForWindow(aura::Window* window) { | 70 Shadow* ShadowController::GetShadowForWindow(aura::Window* window) { |
102 WindowShadowMap::const_iterator it = window_shadows_.find(window); | 71 WindowShadowMap::const_iterator it = window_shadows_.find(window); |
103 return it != window_shadows_.end() ? it->second.get() : NULL; | 72 return it != window_shadows_.end() ? it->second.get() : NULL; |
104 } | 73 } |
105 | 74 |
106 void ShadowController::HandlePossibleShadowVisibilityChange( | 75 void ShadowController::HandlePossibleShadowVisibilityChange( |
107 aura::Window* window) { | 76 aura::Window* window) { |
108 const bool should_show = ShouldShowShadowForWindow(window); | 77 const bool should_show = ShouldShowShadowForWindow(window); |
109 Shadow* shadow = GetShadowForWindow(window); | 78 Shadow* shadow = GetShadowForWindow(window); |
110 if (shadow) | 79 if (shadow) |
111 shadow->layer()->SetVisible(should_show); | 80 shadow->layer()->SetVisible(should_show); |
112 else if (should_show && !shadow) | 81 else if (should_show && !shadow) |
113 CreateShadowForWindow(window); | 82 CreateShadowForWindow(window); |
114 } | 83 } |
115 | 84 |
116 void ShadowController::CreateShadowForWindow(aura::Window* window) { | 85 void ShadowController::CreateShadowForWindow(aura::Window* window) { |
117 linked_ptr<Shadow> shadow(new Shadow()); | 86 linked_ptr<Shadow> shadow(new Shadow()); |
118 window_shadows_.insert(make_pair(window, shadow)); | 87 window_shadows_.insert(make_pair(window, shadow)); |
119 | 88 |
120 shadow->Init(); | 89 shadow->Init(); |
121 shadow->SetContentBounds(window->bounds()); | 90 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); |
122 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); | 91 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); |
123 | 92 window->layer()->Add(shadow->layer()); |
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 StackBelow() 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->StackAbove(shadow->layer(), window->layer()); | |
139 parent_layer->StackAbove(window->layer(), shadow->layer()); | |
140 } | 93 } |
141 | 94 |
142 } // namespace internal | 95 } // namespace internal |
143 } // namespace aura_shell | 96 } // namespace aura_shell |
OLD | NEW |