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