OLD | NEW |
| (Empty) |
1 // Copyright 2015 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/common/wm/window_dimmer.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "ash/common/wm_shell.h" | |
10 #include "ash/common/wm_window.h" | |
11 #include "base/time/time.h" | |
12 #include "ui/aura/window.h" | |
13 #include "ui/compositor/layer.h" | |
14 #include "ui/wm/core/window_animations.h" | |
15 | |
16 namespace ash { | |
17 namespace { | |
18 | |
19 const int kDefaultDimAnimationDurationMs = 200; | |
20 | |
21 const float kDefaultDimOpacity = 0.5f; | |
22 | |
23 } // namespace | |
24 | |
25 WindowDimmer::WindowDimmer(WmWindow* parent) | |
26 : parent_(parent), | |
27 window_(WmShell::Get()->NewWindow(ui::wm::WINDOW_TYPE_NORMAL, | |
28 ui::LAYER_SOLID_COLOR)) { | |
29 window_->SetVisibilityChangesAnimated(); | |
30 window_->SetVisibilityAnimationType( | |
31 ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); | |
32 window_->SetVisibilityAnimationDuration( | |
33 base::TimeDelta::FromMilliseconds(kDefaultDimAnimationDurationMs)); | |
34 window_->aura_window()->AddObserver(this); | |
35 | |
36 SetDimOpacity(kDefaultDimOpacity); | |
37 | |
38 parent->AddChild(window_); | |
39 parent->aura_window()->AddObserver(this); | |
40 parent->StackChildAtTop(window_); | |
41 | |
42 window_->SetBounds(gfx::Rect(parent_->GetBounds().size())); | |
43 } | |
44 | |
45 WindowDimmer::~WindowDimmer() { | |
46 if (parent_) | |
47 parent_->aura_window()->RemoveObserver(this); | |
48 if (window_) { | |
49 window_->aura_window()->RemoveObserver(this); | |
50 window_->Destroy(); | |
51 } | |
52 } | |
53 | |
54 void WindowDimmer::SetDimOpacity(float target_opacity) { | |
55 DCHECK(window_); | |
56 window_->GetLayer()->SetColor( | |
57 SkColorSetA(SK_ColorBLACK, 255 * target_opacity)); | |
58 } | |
59 | |
60 void WindowDimmer::OnWindowBoundsChanged(aura::Window* window, | |
61 const gfx::Rect& old_bounds, | |
62 const gfx::Rect& new_bounds) { | |
63 if (WmWindow::Get(window) == parent_) | |
64 window_->SetBounds(gfx::Rect(new_bounds.size())); | |
65 } | |
66 | |
67 void WindowDimmer::OnWindowDestroying(aura::Window* window) { | |
68 if (WmWindow::Get(window) == parent_) { | |
69 parent_->aura_window()->RemoveObserver(this); | |
70 parent_ = nullptr; | |
71 } else { | |
72 DCHECK_EQ(window_, WmWindow::Get(window)); | |
73 window_->aura_window()->RemoveObserver(this); | |
74 window_ = nullptr; | |
75 } | |
76 } | |
77 | |
78 void WindowDimmer::OnWindowHierarchyChanging( | |
79 const HierarchyChangeParams& params) { | |
80 if (WmWindow::Get(params.receiver) == window_ && | |
81 params.target == params.receiver) { | |
82 // This may happen on a display change or some unexpected condition. Hide | |
83 // the window to ensure it isn't obscuring the wrong thing. | |
84 window_->Hide(); | |
85 } | |
86 } | |
87 | |
88 } // namespace ash | |
OLD | NEW |