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 "ash/wm/shadow_controller.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "ash/shell.h" | |
10 #include "ash/wm/shadow.h" | |
11 #include "ash/wm/shadow_types.h" | |
12 #include "ash/wm/window_properties.h" | |
13 #include "base/command_line.h" | |
14 #include "base/logging.h" | |
15 #include "ui/aura/client/activation_client.h" | |
16 #include "ui/aura/env.h" | |
17 #include "ui/aura/root_window.h" | |
18 #include "ui/aura/window.h" | |
19 #include "ui/compositor/layer.h" | |
20 | |
21 using std::make_pair; | |
22 | |
23 namespace ash { | |
24 namespace internal { | |
25 | |
26 namespace { | |
27 | |
28 ShadowType GetShadowTypeFromWindow(aura::Window* window) { | |
29 switch (window->type()) { | |
30 case aura::client::WINDOW_TYPE_NORMAL: | |
31 case aura::client::WINDOW_TYPE_PANEL: | |
32 case aura::client::WINDOW_TYPE_MENU: | |
33 case aura::client::WINDOW_TYPE_TOOLTIP: | |
34 return SHADOW_TYPE_RECTANGULAR; | |
35 default: | |
36 break; | |
37 } | |
38 return SHADOW_TYPE_NONE; | |
39 } | |
40 | |
41 bool ShouldUseSmallShadowForWindow(aura::Window* window) { | |
42 switch (window->type()) { | |
43 case aura::client::WINDOW_TYPE_MENU: | |
44 case aura::client::WINDOW_TYPE_TOOLTIP: | |
45 return true; | |
46 default: | |
47 break; | |
48 } | |
49 return false; | |
50 } | |
51 | |
52 // Returns the shadow style to be applied to |losing_active| when it is losing | |
53 // active to |gaining_active|. |gaining_active| may be of a type that hides when | |
54 // inactive, and as such we do not want to render |losing_active| as inactive. | |
55 Shadow::Style GetShadowStyleForWindowLosingActive( | |
56 aura::Window* losing_active, | |
57 aura::Window* gaining_active) { | |
58 if (gaining_active && aura::client::GetHideOnDeactivate(gaining_active)) { | |
59 aura::Window::Windows::const_iterator it = | |
60 std::find(losing_active->transient_children().begin(), | |
61 losing_active->transient_children().end(), | |
62 gaining_active); | |
63 if (it != losing_active->transient_children().end()) | |
64 return Shadow::STYLE_ACTIVE; | |
65 } | |
66 return Shadow::STYLE_INACTIVE; | |
67 } | |
68 | |
69 } // namespace | |
70 | |
71 ShadowController::ShadowController() | |
72 : ALLOW_THIS_IN_INITIALIZER_LIST(observer_manager_(this)) { | |
73 aura::Env::GetInstance()->AddObserver(this); | |
74 // Watch for window activation changes. | |
75 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())-> | |
76 AddObserver(this); | |
77 } | |
78 | |
79 ShadowController::~ShadowController() { | |
80 aura::client::GetActivationClient(Shell::GetPrimaryRootWindow())-> | |
81 RemoveObserver(this); | |
82 aura::Env::GetInstance()->RemoveObserver(this); | |
83 } | |
84 | |
85 void ShadowController::OnWindowInitialized(aura::Window* window) { | |
86 observer_manager_.Add(window); | |
87 SetShadowType(window, GetShadowTypeFromWindow(window)); | |
88 HandlePossibleShadowVisibilityChange(window); | |
89 } | |
90 | |
91 void ShadowController::OnWindowPropertyChanged(aura::Window* window, | |
92 const void* key, | |
93 intptr_t old) { | |
94 if (key == kShadowTypeKey) { | |
95 HandlePossibleShadowVisibilityChange(window); | |
96 return; | |
97 } | |
98 } | |
99 | |
100 void ShadowController::OnWindowBoundsChanged(aura::Window* window, | |
101 const gfx::Rect& old_bounds, | |
102 const gfx::Rect& new_bounds) { | |
103 Shadow* shadow = GetShadowForWindow(window); | |
104 if (shadow) | |
105 shadow->SetContentBounds(gfx::Rect(new_bounds.size())); | |
106 } | |
107 | |
108 void ShadowController::OnWindowDestroyed(aura::Window* window) { | |
109 window_shadows_.erase(window); | |
110 observer_manager_.Remove(window); | |
111 } | |
112 | |
113 void ShadowController::OnWindowActivated(aura::Window* gaining_active, | |
114 aura::Window* losing_active) { | |
115 if (gaining_active) { | |
116 Shadow* shadow = GetShadowForWindow(gaining_active); | |
117 if (shadow && !ShouldUseSmallShadowForWindow(gaining_active)) | |
118 shadow->SetStyle(Shadow::STYLE_ACTIVE); | |
119 } | |
120 if (losing_active) { | |
121 Shadow* shadow = GetShadowForWindow(losing_active); | |
122 if (shadow && !ShouldUseSmallShadowForWindow(losing_active)) { | |
123 shadow->SetStyle(GetShadowStyleForWindowLosingActive(losing_active, | |
124 gaining_active)); | |
125 } | |
126 } | |
127 } | |
128 | |
129 bool ShadowController::ShouldShowShadowForWindow(aura::Window* window) const { | |
130 const ShadowType type = GetShadowType(window); | |
131 switch (type) { | |
132 case SHADOW_TYPE_NONE: | |
133 return false; | |
134 case SHADOW_TYPE_RECTANGULAR: | |
135 return true; | |
136 default: | |
137 NOTREACHED() << "Unknown shadow type " << type; | |
138 return false; | |
139 } | |
140 } | |
141 | |
142 Shadow* ShadowController::GetShadowForWindow(aura::Window* window) { | |
143 WindowShadowMap::const_iterator it = window_shadows_.find(window); | |
144 return it != window_shadows_.end() ? it->second.get() : NULL; | |
145 } | |
146 | |
147 void ShadowController::HandlePossibleShadowVisibilityChange( | |
148 aura::Window* window) { | |
149 const bool should_show = ShouldShowShadowForWindow(window); | |
150 Shadow* shadow = GetShadowForWindow(window); | |
151 if (shadow) | |
152 shadow->layer()->SetVisible(should_show); | |
153 else if (should_show && !shadow) | |
154 CreateShadowForWindow(window); | |
155 } | |
156 | |
157 void ShadowController::CreateShadowForWindow(aura::Window* window) { | |
158 linked_ptr<Shadow> shadow(new Shadow()); | |
159 window_shadows_.insert(make_pair(window, shadow)); | |
160 | |
161 shadow->Init(ShouldUseSmallShadowForWindow(window) ? | |
162 Shadow::STYLE_SMALL : Shadow::STYLE_ACTIVE); | |
163 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); | |
164 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); | |
165 window->layer()->Add(shadow->layer()); | |
166 } | |
167 | |
168 } // namespace internal | |
169 } // namespace ash | |
OLD | NEW |