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/command_line.h" | |
10 #include "base/logging.h" | |
11 #include "ui/aura/root_window.h" | |
12 #include "ui/aura/window.h" | |
13 #include "ui/aura_shell/aura_shell_switches.h" | |
14 #include "ui/aura_shell/shadow.h" | |
15 #include "ui/aura_shell/shadow_types.h" | |
16 #include "ui/aura_shell/window_properties.h" | |
17 | |
18 using std::make_pair; | |
19 | |
20 namespace aura_shell { | |
21 namespace internal { | |
22 | |
23 namespace { | |
24 | |
25 ShadowType GetShadowTypeFromWindowType(aura::Window* window) { | |
26 switch (window->type()) { | |
27 case aura::client::WINDOW_TYPE_NORMAL: | |
28 return CommandLine::ForCurrentProcess()->HasSwitch( | |
29 switches::kAuraTranslucentFrames) ? | |
30 SHADOW_TYPE_NONE : SHADOW_TYPE_RECTANGULAR; | |
31 case aura::client::WINDOW_TYPE_MENU: | |
32 case aura::client::WINDOW_TYPE_TOOLTIP: | |
33 return SHADOW_TYPE_RECTANGULAR; | |
34 default: | |
35 break; | |
36 } | |
37 return SHADOW_TYPE_NONE; | |
38 } | |
39 | |
40 } // namespace | |
41 | |
42 ShadowController::ShadowController() { | |
43 aura::RootWindow::GetInstance()->AddRootWindowObserver(this); | |
44 } | |
45 | |
46 ShadowController::~ShadowController() { | |
47 for (WindowShadowMap::const_iterator it = window_shadows_.begin(); | |
48 it != window_shadows_.end(); ++it) { | |
49 it->first->RemoveObserver(this); | |
50 } | |
51 aura::RootWindow::GetInstance()->RemoveRootWindowObserver(this); | |
52 } | |
53 | |
54 void ShadowController::OnWindowInitialized(aura::Window* window) { | |
55 window->AddObserver(this); | |
56 SetShadowType(window, GetShadowTypeFromWindowType(window)); | |
57 HandlePossibleShadowVisibilityChange(window); | |
58 } | |
59 | |
60 void ShadowController::OnWindowPropertyChanged(aura::Window* window, | |
61 const char* name, | |
62 void* old) { | |
63 if (name == kShadowTypeKey) | |
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(gfx::Rect(bounds.size())); | |
72 } | |
73 | |
74 void ShadowController::OnWindowDestroyed(aura::Window* window) { | |
75 window_shadows_.erase(window); | |
76 } | |
77 | |
78 bool ShadowController::ShouldShowShadowForWindow(aura::Window* window) const { | |
79 const ShadowType type = GetShadowType(window); | |
80 switch (type) { | |
81 case SHADOW_TYPE_NONE: | |
82 return false; | |
83 case SHADOW_TYPE_RECTANGULAR: | |
84 return true; | |
85 default: | |
86 NOTREACHED() << "Unknown shadow type " << type; | |
87 return false; | |
88 } | |
89 } | |
90 | |
91 Shadow* ShadowController::GetShadowForWindow(aura::Window* window) { | |
92 WindowShadowMap::const_iterator it = window_shadows_.find(window); | |
93 return it != window_shadows_.end() ? it->second.get() : NULL; | |
94 } | |
95 | |
96 void ShadowController::HandlePossibleShadowVisibilityChange( | |
97 aura::Window* window) { | |
98 const bool should_show = ShouldShowShadowForWindow(window); | |
99 Shadow* shadow = GetShadowForWindow(window); | |
100 if (shadow) | |
101 shadow->layer()->SetVisible(should_show); | |
102 else if (should_show && !shadow) | |
103 CreateShadowForWindow(window); | |
104 } | |
105 | |
106 void ShadowController::CreateShadowForWindow(aura::Window* window) { | |
107 linked_ptr<Shadow> shadow(new Shadow()); | |
108 window_shadows_.insert(make_pair(window, shadow)); | |
109 | |
110 shadow->Init(); | |
111 shadow->SetContentBounds(gfx::Rect(window->bounds().size())); | |
112 shadow->layer()->SetVisible(ShouldShowShadowForWindow(window)); | |
113 window->layer()->Add(shadow->layer()); | |
114 } | |
115 | |
116 } // namespace internal | |
117 } // namespace aura_shell | |
OLD | NEW |