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