OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "mash/wm/user_window_controller_impl.h" | 5 #include "ash/mus/user_window_controller_impl.h" |
6 | 6 |
7 #include <stdint.h> | |
msw
2016/06/02 20:28:35
q: why is this neeeded? (trying to fix your TODO b
sky
2016/06/02 21:31:51
Indeed. I had thought it might be necessary to fix
| |
8 | |
9 #include "ash/mus/property_util.h" | |
10 #include "ash/mus/root_window_controller.h" | |
7 #include "ash/public/interfaces/container.mojom.h" | 11 #include "ash/public/interfaces/container.mojom.h" |
8 #include "components/mus/public/cpp/property_type_converters.h" | 12 #include "components/mus/public/cpp/property_type_converters.h" |
9 #include "components/mus/public/cpp/window.h" | 13 #include "components/mus/public/cpp/window.h" |
10 #include "components/mus/public/cpp/window_property.h" | 14 #include "components/mus/public/cpp/window_property.h" |
11 #include "components/mus/public/cpp/window_tree_client.h" | 15 #include "components/mus/public/cpp/window_tree_client.h" |
12 #include "mash/wm/property_util.h" | |
13 #include "mash/wm/root_window_controller.h" | |
14 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
15 #include "ui/resources/grit/ui_resources.h" | 17 #include "ui/resources/grit/ui_resources.h" |
16 | 18 |
17 MUS_DECLARE_WINDOW_PROPERTY_TYPE(uint32_t); | 19 // TODO(sky): figure out why this generates a complie time error. |
msw
2016/06/02 20:28:35
Should this be fixed before landing? (will it brea
sky
2016/06/02 21:31:51
It compiled fine without this. But I ended up reve
| |
20 // MUS_DECLARE_WINDOW_PROPERTY_TYPE(uint32_t); | |
18 | 21 |
19 namespace mash { | 22 namespace ash { |
20 namespace wm { | 23 namespace mus { |
21 | 24 |
22 // Key used for storing identifier sent to clients for windows. | 25 // Key used for storing identifier sent to clients for windows. |
23 MUS_DEFINE_LOCAL_WINDOW_PROPERTY_KEY(uint32_t, kUserWindowIdKey, 0u); | 26 MUS_DEFINE_LOCAL_WINDOW_PROPERTY_KEY(uint32_t, kUserWindowIdKey, 0u); |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 // Returns |window|, or an ancestor thereof, parented to |container|, or null. | 30 // Returns |window|, or an ancestor thereof, parented to |container|, or null. |
28 mus::Window* GetTopLevelWindow(mus::Window* window, mus::Window* container) { | 31 ::mus::Window* GetTopLevelWindow(::mus::Window* window, |
32 ::mus::Window* container) { | |
29 while (window && window->parent() != container) | 33 while (window && window->parent() != container) |
30 window = window->parent(); | 34 window = window->parent(); |
31 return window; | 35 return window; |
32 } | 36 } |
33 | 37 |
34 // Get a UserWindow struct from a mus::Window. | 38 // Get a UserWindow struct from a mus::Window. |
35 ash::mojom::UserWindowPtr GetUserWindow(mus::Window* window) { | 39 mojom::UserWindowPtr GetUserWindow(::mus::Window* window) { |
36 ash::mojom::UserWindowPtr user_window(ash::mojom::UserWindow::New()); | 40 mojom::UserWindowPtr user_window(mojom::UserWindow::New()); |
37 DCHECK_NE(0u, window->GetLocalProperty(kUserWindowIdKey)); | 41 DCHECK_NE(0u, window->GetLocalProperty(kUserWindowIdKey)); |
38 user_window->window_id = window->GetLocalProperty(kUserWindowIdKey); | 42 user_window->window_id = window->GetLocalProperty(kUserWindowIdKey); |
39 user_window->window_title = mojo::String::From(GetWindowTitle(window)); | 43 user_window->window_title = mojo::String::From(GetWindowTitle(window)); |
40 user_window->window_app_icon = GetWindowAppIcon(window); | 44 user_window->window_app_icon = GetWindowAppIcon(window); |
41 user_window->window_app_id = mojo::String::From(GetAppID(window)); | 45 user_window->window_app_id = mojo::String::From(GetAppID(window)); |
42 user_window->ignored_by_shelf = GetWindowIgnoredByShelf(window); | 46 user_window->ignored_by_shelf = GetWindowIgnoredByShelf(window); |
43 mus::Window* focused = window->window_tree()->GetFocusedWindow(); | 47 ::mus::Window* focused = window->window_tree()->GetFocusedWindow(); |
44 focused = GetTopLevelWindow(focused, window->parent()); | 48 focused = GetTopLevelWindow(focused, window->parent()); |
45 user_window->window_has_focus = focused == window; | 49 user_window->window_has_focus = focused == window; |
46 return user_window; | 50 return user_window; |
47 } | 51 } |
48 | 52 |
49 } // namespace | 53 } // namespace |
50 | 54 |
51 // Observes property changes on user windows. UserWindowControllerImpl uses | 55 // Observes property changes on user windows. UserWindowControllerImpl uses |
52 // this separate observer to avoid observing duplicate tree change | 56 // this separate observer to avoid observing duplicate tree change |
53 // notifications. | 57 // notifications. |
54 class WindowPropertyObserver : public mus::WindowObserver { | 58 class WindowPropertyObserver : public ::mus::WindowObserver { |
55 public: | 59 public: |
56 explicit WindowPropertyObserver(UserWindowControllerImpl* controller) | 60 explicit WindowPropertyObserver(UserWindowControllerImpl* controller) |
57 : controller_(controller) {} | 61 : controller_(controller) {} |
58 ~WindowPropertyObserver() override {} | 62 ~WindowPropertyObserver() override {} |
59 | 63 |
60 private: | 64 private: |
61 // mus::WindowObserver: | 65 // mus::WindowObserver: |
62 void OnWindowSharedPropertyChanged( | 66 void OnWindowSharedPropertyChanged( |
63 mus::Window* window, | 67 ::mus::Window* window, |
64 const std::string& name, | 68 const std::string& name, |
65 const std::vector<uint8_t>* old_data, | 69 const std::vector<uint8_t>* old_data, |
66 const std::vector<uint8_t>* new_data) override { | 70 const std::vector<uint8_t>* new_data) override { |
67 if (!controller_->user_window_observer()) | 71 if (!controller_->user_window_observer()) |
68 return; | 72 return; |
69 if (name == mus::mojom::WindowManager::kWindowTitle_Property) { | 73 if (name == ::mus::mojom::WindowManager::kWindowTitle_Property) { |
70 controller_->user_window_observer()->OnUserWindowTitleChanged( | 74 controller_->user_window_observer()->OnUserWindowTitleChanged( |
71 window->GetLocalProperty(kUserWindowIdKey), | 75 window->GetLocalProperty(kUserWindowIdKey), |
72 mojo::String::From(GetWindowTitle(window))); | 76 mojo::String::From(GetWindowTitle(window))); |
73 } else if (name == mus::mojom::WindowManager::kWindowAppIcon_Property) { | 77 } else if (name == ::mus::mojom::WindowManager::kWindowAppIcon_Property) { |
74 controller_->user_window_observer()->OnUserWindowAppIconChanged( | 78 controller_->user_window_observer()->OnUserWindowAppIconChanged( |
75 window->GetLocalProperty(kUserWindowIdKey), | 79 window->GetLocalProperty(kUserWindowIdKey), |
76 new_data ? mojo::Array<uint8_t>::From(*new_data) | 80 new_data ? mojo::Array<uint8_t>::From(*new_data) |
77 : mojo::Array<uint8_t>()); | 81 : mojo::Array<uint8_t>()); |
78 } | 82 } |
79 } | 83 } |
80 | 84 |
81 UserWindowControllerImpl* controller_; | 85 UserWindowControllerImpl* controller_; |
82 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver); | 86 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver); |
83 }; | 87 }; |
84 | 88 |
85 UserWindowControllerImpl::UserWindowControllerImpl() | 89 UserWindowControllerImpl::UserWindowControllerImpl() |
86 : root_controller_(nullptr) {} | 90 : root_controller_(nullptr) {} |
87 | 91 |
88 UserWindowControllerImpl::~UserWindowControllerImpl() { | 92 UserWindowControllerImpl::~UserWindowControllerImpl() { |
89 if (!root_controller_) | 93 if (!root_controller_) |
90 return; | 94 return; |
91 | 95 |
92 mus::Window* user_container = GetUserWindowContainer(); | 96 ::mus::Window* user_container = GetUserWindowContainer(); |
93 if (!user_container) | 97 if (!user_container) |
94 return; | 98 return; |
95 | 99 |
96 RemoveObservers(user_container); | 100 RemoveObservers(user_container); |
97 } | 101 } |
98 | 102 |
99 void UserWindowControllerImpl::Initialize( | 103 void UserWindowControllerImpl::Initialize( |
100 RootWindowController* root_controller) { | 104 RootWindowController* root_controller) { |
101 DCHECK(root_controller); | 105 DCHECK(root_controller); |
102 DCHECK(!root_controller_); | 106 DCHECK(!root_controller_); |
103 root_controller_ = root_controller; | 107 root_controller_ = root_controller; |
104 GetUserWindowContainer()->AddObserver(this); | 108 GetUserWindowContainer()->AddObserver(this); |
105 GetUserWindowContainer()->window_tree()->AddObserver(this); | 109 GetUserWindowContainer()->window_tree()->AddObserver(this); |
106 window_property_observer_.reset(new WindowPropertyObserver(this)); | 110 window_property_observer_.reset(new WindowPropertyObserver(this)); |
107 for (mus::Window* window : GetUserWindowContainer()->children()) { | 111 for (::mus::Window* window : GetUserWindowContainer()->children()) { |
108 AssignIdIfNecessary(window); | 112 AssignIdIfNecessary(window); |
109 window->AddObserver(window_property_observer_.get()); | 113 window->AddObserver(window_property_observer_.get()); |
110 } | 114 } |
111 } | 115 } |
112 | 116 |
113 void UserWindowControllerImpl::AssignIdIfNecessary(mus::Window* window) { | 117 void UserWindowControllerImpl::AssignIdIfNecessary(::mus::Window* window) { |
114 if (window->GetLocalProperty(kUserWindowIdKey) == 0u) | 118 if (window->GetLocalProperty(kUserWindowIdKey) == 0u) |
115 window->SetLocalProperty(kUserWindowIdKey, next_id_++); | 119 window->SetLocalProperty(kUserWindowIdKey, next_id_++); |
116 } | 120 } |
117 | 121 |
118 void UserWindowControllerImpl::RemoveObservers(mus::Window* user_container) { | 122 void UserWindowControllerImpl::RemoveObservers(::mus::Window* user_container) { |
119 user_container->RemoveObserver(this); | 123 user_container->RemoveObserver(this); |
120 user_container->window_tree()->RemoveObserver(this); | 124 user_container->window_tree()->RemoveObserver(this); |
121 for (auto iter : user_container->children()) | 125 for (auto iter : user_container->children()) |
122 iter->RemoveObserver(window_property_observer_.get()); | 126 iter->RemoveObserver(window_property_observer_.get()); |
123 } | 127 } |
124 | 128 |
125 mus::Window* UserWindowControllerImpl::GetUserWindowById(uint32_t id) { | 129 ::mus::Window* UserWindowControllerImpl::GetUserWindowById(uint32_t id) { |
126 for (mus::Window* window : GetUserWindowContainer()->children()) { | 130 for (::mus::Window* window : GetUserWindowContainer()->children()) { |
127 if (window->GetLocalProperty(kUserWindowIdKey) == id) | 131 if (window->GetLocalProperty(kUserWindowIdKey) == id) |
128 return window; | 132 return window; |
129 } | 133 } |
130 return nullptr; | 134 return nullptr; |
131 } | 135 } |
132 | 136 |
133 mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { | 137 ::mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { |
134 return root_controller_->GetWindowForContainer( | 138 return root_controller_->GetWindowForContainer( |
135 ash::mojom::Container::USER_PRIVATE_WINDOWS); | 139 mojom::Container::USER_PRIVATE_WINDOWS); |
136 } | 140 } |
137 | 141 |
138 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { | 142 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { |
139 DCHECK(root_controller_); | 143 DCHECK(root_controller_); |
140 if (params.new_parent == GetUserWindowContainer()) { | 144 if (params.new_parent == GetUserWindowContainer()) { |
141 params.target->AddObserver(window_property_observer_.get()); | 145 params.target->AddObserver(window_property_observer_.get()); |
142 AssignIdIfNecessary(params.target); | 146 AssignIdIfNecessary(params.target); |
143 if (user_window_observer_) | 147 if (user_window_observer_) |
144 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); | 148 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); |
145 } else if (params.old_parent == GetUserWindowContainer()) { | 149 } else if (params.old_parent == GetUserWindowContainer()) { |
146 params.target->RemoveObserver(window_property_observer_.get()); | 150 params.target->RemoveObserver(window_property_observer_.get()); |
147 if (user_window_observer_) | 151 if (user_window_observer_) |
148 user_window_observer_->OnUserWindowRemoved( | 152 user_window_observer_->OnUserWindowRemoved( |
149 params.target->GetLocalProperty(kUserWindowIdKey)); | 153 params.target->GetLocalProperty(kUserWindowIdKey)); |
150 } | 154 } |
151 } | 155 } |
152 | 156 |
153 void UserWindowControllerImpl::OnWindowDestroying(mus::Window* window) { | 157 void UserWindowControllerImpl::OnWindowDestroying(::mus::Window* window) { |
154 if (window == GetUserWindowContainer()) | 158 if (window == GetUserWindowContainer()) |
155 RemoveObservers(window); | 159 RemoveObservers(window); |
156 } | 160 } |
157 | 161 |
158 void UserWindowControllerImpl::OnWindowTreeFocusChanged( | 162 void UserWindowControllerImpl::OnWindowTreeFocusChanged( |
159 mus::Window* gained_focus, | 163 ::mus::Window* gained_focus, |
160 mus::Window* lost_focus) { | 164 ::mus::Window* lost_focus) { |
161 if (!user_window_observer_) | 165 if (!user_window_observer_) |
162 return; | 166 return; |
163 | 167 |
164 // Treat focus in the user window hierarchy as focus of the top-level window. | 168 // Treat focus in the user window hierarchy as focus of the top-level window. |
165 gained_focus = GetTopLevelWindow(gained_focus, GetUserWindowContainer()); | 169 gained_focus = GetTopLevelWindow(gained_focus, GetUserWindowContainer()); |
166 lost_focus = GetTopLevelWindow(lost_focus, GetUserWindowContainer()); | 170 lost_focus = GetTopLevelWindow(lost_focus, GetUserWindowContainer()); |
167 if (gained_focus == lost_focus) | 171 if (gained_focus == lost_focus) |
168 return; | 172 return; |
169 | 173 |
170 if (lost_focus) { | 174 if (lost_focus) { |
171 user_window_observer_->OnUserWindowFocusChanged( | 175 user_window_observer_->OnUserWindowFocusChanged( |
172 lost_focus->GetLocalProperty(kUserWindowIdKey), false); | 176 lost_focus->GetLocalProperty(kUserWindowIdKey), false); |
173 } | 177 } |
174 if (gained_focus) { | 178 if (gained_focus) { |
175 user_window_observer_->OnUserWindowFocusChanged( | 179 user_window_observer_->OnUserWindowFocusChanged( |
176 gained_focus->GetLocalProperty(kUserWindowIdKey), true); | 180 gained_focus->GetLocalProperty(kUserWindowIdKey), true); |
177 } | 181 } |
178 } | 182 } |
179 | 183 |
180 void UserWindowControllerImpl::AddUserWindowObserver( | 184 void UserWindowControllerImpl::AddUserWindowObserver( |
181 ash::mojom::UserWindowObserverPtr observer) { | 185 mojom::UserWindowObserverPtr observer) { |
182 // TODO(msw): Support multiple observers. | 186 // TODO(msw): Support multiple observers. |
183 user_window_observer_ = std::move(observer); | 187 user_window_observer_ = std::move(observer); |
184 | 188 |
185 const mus::Window::Children& windows = GetUserWindowContainer()->children(); | 189 const ::mus::Window::Children& windows = GetUserWindowContainer()->children(); |
186 mojo::Array<ash::mojom::UserWindowPtr> user_windows = | 190 mojo::Array<mojom::UserWindowPtr> user_windows = |
187 mojo::Array<ash::mojom::UserWindowPtr>::New(windows.size()); | 191 mojo::Array<mojom::UserWindowPtr>::New(windows.size()); |
188 for (size_t i = 0; i < windows.size(); ++i) | 192 for (size_t i = 0; i < windows.size(); ++i) |
189 user_windows[i] = GetUserWindow(windows[i]); | 193 user_windows[i] = GetUserWindow(windows[i]); |
190 user_window_observer_->OnUserWindowObserverAdded(std::move(user_windows)); | 194 user_window_observer_->OnUserWindowObserverAdded(std::move(user_windows)); |
191 } | 195 } |
192 | 196 |
193 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { | 197 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { |
194 mus::Window* window = GetUserWindowById(window_id); | 198 ::mus::Window* window = GetUserWindowById(window_id); |
195 if (window) | 199 if (window) |
196 window->SetFocus(); | 200 window->SetFocus(); |
197 } | 201 } |
198 | 202 |
199 } // namespace wm | 203 } // namespace mus |
200 } // namespace mash | 204 } // namespace ash |
OLD | NEW |