OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "athena/wm/window_list_provider_impl.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "athena/athena_export.h" | |
10 #include "athena/wm/public/window_list_provider_observer.h" | |
11 #include "ui/aura/window.h" | |
12 #include "ui/aura/window_property.h" | |
13 #include "ui/wm/core/transient_window_manager.h" | |
14 #include "ui/wm/core/window_util.h" | |
15 | |
16 namespace athena { | |
17 namespace { | |
18 | |
19 // Used to keep track of which window should be managed. This is necessary | |
20 // as the necessary informatino used in IsValidWindow (transient parent | |
21 // for example) may not available during destruction. | |
22 DEFINE_WINDOW_PROPERTY_KEY(bool, kManagedKey, false); | |
23 | |
24 } // namespace | |
25 | |
26 WindowListProviderImpl::WindowListProviderImpl(aura::Window* container) | |
27 : container_(container) { | |
28 CHECK(container_); | |
29 container_->AddObserver(this); | |
30 RecreateWindowList(); | |
31 for (auto* window : window_list_) | |
32 window->AddObserver(this); | |
33 } | |
34 | |
35 WindowListProviderImpl::~WindowListProviderImpl() { | |
36 // Remove all remaining window observers. | |
37 for (auto* window : window_list_) { | |
38 CHECK(window->GetProperty(kManagedKey)); | |
39 window->RemoveObserver(this); | |
40 } | |
41 container_->RemoveObserver(this); | |
42 } | |
43 | |
44 bool WindowListProviderImpl::IsValidWindow(aura::Window* window) const { | |
45 if (wm::GetTransientParent(window)) | |
46 return false; | |
47 | |
48 // TODO(oshima): crbug.com/413912. | |
49 return window->type() == ui::wm::WINDOW_TYPE_NORMAL || | |
50 window->type() == ui::wm::WINDOW_TYPE_PANEL; | |
51 } | |
52 | |
53 void WindowListProviderImpl::AddObserver(WindowListProviderObserver* observer) { | |
54 observers_.AddObserver(observer); | |
55 } | |
56 | |
57 void WindowListProviderImpl::RemoveObserver( | |
58 WindowListProviderObserver* observer) { | |
59 observers_.RemoveObserver(observer); | |
60 } | |
61 | |
62 const aura::Window::Windows& WindowListProviderImpl::GetWindowList() const { | |
63 return window_list_; | |
64 } | |
65 | |
66 bool WindowListProviderImpl::IsWindowInList(aura::Window* window) const { | |
67 return std::find(window_list_.begin(), window_list_.end(), window) != | |
68 window_list_.end(); | |
69 } | |
70 | |
71 void WindowListProviderImpl::StackWindowFrontOf( | |
72 aura::Window* window, | |
73 aura::Window* reference_window) { | |
74 DCHECK_NE(window, reference_window); | |
75 DCHECK(IsWindowInList(window)); | |
76 DCHECK(IsWindowInList(reference_window)); | |
77 container_->StackChildAbove(window, reference_window); | |
78 } | |
79 | |
80 void WindowListProviderImpl::StackWindowBehindTo( | |
81 aura::Window* window, | |
82 aura::Window* reference_window) { | |
83 DCHECK_NE(window, reference_window); | |
84 DCHECK(IsWindowInList(window)); | |
85 DCHECK(IsWindowInList(reference_window)); | |
86 container_->StackChildBelow(window, reference_window); | |
87 } | |
88 | |
89 void WindowListProviderImpl::RecreateWindowList() { | |
90 window_list_.clear(); | |
91 for (auto* window : container_->children()) { | |
92 if (window->GetProperty(kManagedKey)) | |
93 window_list_.push_back(window); | |
94 } | |
95 } | |
96 | |
97 void WindowListProviderImpl::OnWindowAdded(aura::Window* window) { | |
98 if (!IsValidWindow(window) || window->parent() != container_) | |
99 return; | |
100 | |
101 window->SetProperty(kManagedKey, true); | |
102 RecreateWindowList(); | |
103 DCHECK(IsWindowInList(window)); | |
104 window->AddObserver(this); | |
105 FOR_EACH_OBSERVER( | |
106 WindowListProviderObserver, observers_, OnWindowAddedToList(window)); | |
107 } | |
108 | |
109 void WindowListProviderImpl::OnWillRemoveWindow(aura::Window* window) { | |
110 if (!window->GetProperty(kManagedKey)) | |
111 return; | |
112 DCHECK(IsWindowInList(window)); | |
113 aura::Window::Windows::iterator find = std::find(window_list_.begin(), | |
114 window_list_.end(), | |
115 window); | |
116 CHECK(find != window_list_.end()); | |
117 int index = find - window_list_.begin(); | |
118 window_list_.erase(find); | |
119 window->ClearProperty(kManagedKey); | |
120 window->RemoveObserver(this); | |
121 FOR_EACH_OBSERVER(WindowListProviderObserver, | |
122 observers_, | |
123 OnWindowRemovedFromList(window, index)); | |
124 } | |
125 | |
126 void WindowListProviderImpl::OnWindowStackingChanged(aura::Window* window) { | |
127 if (window == container_) | |
128 return; | |
129 DCHECK(IsWindowInList(window)); | |
130 RecreateWindowList(); | |
131 // Inform our listeners that the stacking has been changed. | |
132 FOR_EACH_OBSERVER( | |
133 WindowListProviderObserver, observers_, OnWindowStackingChangedInList()); | |
134 } | |
135 | |
136 } // namespace athena | |
OLD | NEW |