Chromium Code Reviews| 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 "mash/wm/user_window_controller_impl.h" |
| 6 | 6 |
| 7 #include "components/mus/public/cpp/property_type_converters.h" | 7 #include "components/mus/public/cpp/property_type_converters.h" |
| 8 #include "components/mus/public/cpp/window.h" | 8 #include "components/mus/public/cpp/window.h" |
| 9 #include "components/mus/public/cpp/window_property.h" | 9 #include "components/mus/public/cpp/window_property.h" |
| 10 #include "components/mus/public/cpp/window_tree_connection.h" | 10 #include "components/mus/public/cpp/window_tree_connection.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver); | 82 DISALLOW_COPY_AND_ASSIGN(WindowPropertyObserver); |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 UserWindowControllerImpl::UserWindowControllerImpl() | 85 UserWindowControllerImpl::UserWindowControllerImpl() |
| 86 : root_controller_(nullptr) {} | 86 : root_controller_(nullptr) {} |
| 87 | 87 |
| 88 UserWindowControllerImpl::~UserWindowControllerImpl() { | 88 UserWindowControllerImpl::~UserWindowControllerImpl() { |
| 89 if (!root_controller_) | 89 if (!root_controller_) |
| 90 return; | 90 return; |
| 91 | 91 |
| 92 // TODO(msw): should really listen for user window container being destroyed | |
| 93 // and cleanup there. | |
| 94 mus::Window* user_container = GetUserWindowContainer(); | 92 mus::Window* user_container = GetUserWindowContainer(); |
| 95 if (!user_container) | 93 if (!user_container) |
|
msw
2016/05/18 17:36:56
optional nit: invert and nest the RemoveObservers
| |
| 96 return; | 94 return; |
| 97 | 95 |
| 98 user_container->RemoveObserver(this); | 96 RemoveObservers(user_container); |
| 99 for (auto iter : user_container->children()) | |
| 100 iter->RemoveObserver(window_property_observer_.get()); | |
| 101 } | 97 } |
| 102 | 98 |
| 103 void UserWindowControllerImpl::Initialize( | 99 void UserWindowControllerImpl::Initialize( |
| 104 RootWindowController* root_controller) { | 100 RootWindowController* root_controller) { |
| 105 DCHECK(root_controller); | 101 DCHECK(root_controller); |
| 106 DCHECK(!root_controller_); | 102 DCHECK(!root_controller_); |
| 107 root_controller_ = root_controller; | 103 root_controller_ = root_controller; |
| 108 GetUserWindowContainer()->AddObserver(this); | 104 GetUserWindowContainer()->AddObserver(this); |
| 109 GetUserWindowContainer()->connection()->AddObserver(this); | 105 GetUserWindowContainer()->connection()->AddObserver(this); |
| 110 window_property_observer_.reset(new WindowPropertyObserver(this)); | 106 window_property_observer_.reset(new WindowPropertyObserver(this)); |
| 111 for (mus::Window* window : GetUserWindowContainer()->children()) { | 107 for (mus::Window* window : GetUserWindowContainer()->children()) { |
| 112 AssignIdIfNecessary(window); | 108 AssignIdIfNecessary(window); |
| 113 window->AddObserver(window_property_observer_.get()); | 109 window->AddObserver(window_property_observer_.get()); |
| 114 } | 110 } |
| 115 } | 111 } |
| 116 | 112 |
| 117 void UserWindowControllerImpl::AssignIdIfNecessary(mus::Window* window) { | 113 void UserWindowControllerImpl::AssignIdIfNecessary(mus::Window* window) { |
| 118 if (window->GetLocalProperty(kUserWindowIdKey) == 0u) | 114 if (window->GetLocalProperty(kUserWindowIdKey) == 0u) |
| 119 window->SetLocalProperty(kUserWindowIdKey, next_id_++); | 115 window->SetLocalProperty(kUserWindowIdKey, next_id_++); |
| 120 } | 116 } |
| 121 | 117 |
| 118 void UserWindowControllerImpl::RemoveObservers(mus::Window* user_container) { | |
| 119 user_container->RemoveObserver(this); | |
| 120 user_container->connection()->RemoveObserver(this); | |
| 121 for (auto iter : user_container->children()) | |
| 122 iter->RemoveObserver(window_property_observer_.get()); | |
| 123 } | |
| 124 | |
| 122 mus::Window* UserWindowControllerImpl::GetUserWindowById(uint32_t id) { | 125 mus::Window* UserWindowControllerImpl::GetUserWindowById(uint32_t id) { |
| 123 for (mus::Window* window : GetUserWindowContainer()->children()) { | 126 for (mus::Window* window : GetUserWindowContainer()->children()) { |
| 124 if (window->GetLocalProperty(kUserWindowIdKey) == id) | 127 if (window->GetLocalProperty(kUserWindowIdKey) == id) |
| 125 return window; | 128 return window; |
| 126 } | 129 } |
| 127 return nullptr; | 130 return nullptr; |
| 128 } | 131 } |
| 129 | 132 |
| 130 mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { | 133 mus::Window* UserWindowControllerImpl::GetUserWindowContainer() const { |
| 131 return root_controller_->GetWindowForContainer( | 134 return root_controller_->GetWindowForContainer( |
| 132 mojom::Container::USER_PRIVATE_WINDOWS); | 135 mojom::Container::USER_PRIVATE_WINDOWS); |
| 133 } | 136 } |
| 134 | 137 |
| 135 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { | 138 void UserWindowControllerImpl::OnTreeChanging(const TreeChangeParams& params) { |
| 136 DCHECK(root_controller_); | 139 DCHECK(root_controller_); |
| 137 if (params.new_parent == GetUserWindowContainer()) { | 140 if (params.new_parent == GetUserWindowContainer()) { |
| 138 params.target->AddObserver(window_property_observer_.get()); | 141 params.target->AddObserver(window_property_observer_.get()); |
| 139 AssignIdIfNecessary(params.target); | 142 AssignIdIfNecessary(params.target); |
| 140 if (user_window_observer_) | 143 if (user_window_observer_) |
| 141 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); | 144 user_window_observer_->OnUserWindowAdded(GetUserWindow(params.target)); |
| 142 } else if (params.old_parent == GetUserWindowContainer()) { | 145 } else if (params.old_parent == GetUserWindowContainer()) { |
| 143 params.target->RemoveObserver(window_property_observer_.get()); | 146 params.target->RemoveObserver(window_property_observer_.get()); |
| 144 if (user_window_observer_) | 147 if (user_window_observer_) |
| 145 user_window_observer_->OnUserWindowRemoved( | 148 user_window_observer_->OnUserWindowRemoved( |
| 146 params.target->GetLocalProperty(kUserWindowIdKey)); | 149 params.target->GetLocalProperty(kUserWindowIdKey)); |
| 147 } | 150 } |
| 148 } | 151 } |
| 149 | 152 |
| 153 void UserWindowControllerImpl::OnWindowDestroying(mus::Window* window) { | |
| 154 if (window == GetUserWindowContainer()) | |
| 155 RemoveObservers(window); | |
| 156 } | |
| 157 | |
| 150 void UserWindowControllerImpl::OnWindowTreeFocusChanged( | 158 void UserWindowControllerImpl::OnWindowTreeFocusChanged( |
| 151 mus::Window* gained_focus, | 159 mus::Window* gained_focus, |
| 152 mus::Window* lost_focus) { | 160 mus::Window* lost_focus) { |
| 153 if (!user_window_observer_) | 161 if (!user_window_observer_) |
| 154 return; | 162 return; |
| 155 | 163 |
| 156 // Treat focus in the user window hierarchy as focus of the top-level window. | 164 // Treat focus in the user window hierarchy as focus of the top-level window. |
| 157 gained_focus = GetTopLevelWindow(gained_focus, GetUserWindowContainer()); | 165 gained_focus = GetTopLevelWindow(gained_focus, GetUserWindowContainer()); |
| 158 lost_focus = GetTopLevelWindow(lost_focus, GetUserWindowContainer()); | 166 lost_focus = GetTopLevelWindow(lost_focus, GetUserWindowContainer()); |
| 159 if (gained_focus == lost_focus) | 167 if (gained_focus == lost_focus) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 183 } | 191 } |
| 184 | 192 |
| 185 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { | 193 void UserWindowControllerImpl::FocusUserWindow(uint32_t window_id) { |
| 186 mus::Window* window = GetUserWindowById(window_id); | 194 mus::Window* window = GetUserWindowById(window_id); |
| 187 if (window) | 195 if (window) |
| 188 window->SetFocus(); | 196 window->SetFocus(); |
| 189 } | 197 } |
| 190 | 198 |
| 191 } // namespace wm | 199 } // namespace wm |
| 192 } // namespace mash | 200 } // namespace mash |
| OLD | NEW |