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