OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "ui/aura/mus/window_port_mus.h" |
| 6 |
| 7 #include "ui/aura/client/aura_constants.h" |
| 8 #include "ui/aura/mus/property_converter.h" |
| 9 #include "ui/aura/mus/surface_id_handler.h" |
| 10 #include "ui/aura/mus/window_tree_client.h" |
| 11 #include "ui/aura/mus/window_tree_client_delegate.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_observer.h" |
| 14 #include "ui/aura/window_property.h" |
| 15 |
| 16 namespace aura { |
| 17 |
| 18 // static |
| 19 WindowMus* WindowMus::Get(Window* window) { |
| 20 return WindowPortMus::Get(window); |
| 21 } |
| 22 |
| 23 WindowPortMus::WindowPortMus(WindowTreeClient* client) |
| 24 : window_tree_client_(client) {} |
| 25 |
| 26 WindowPortMus::~WindowPortMus() { |
| 27 if (surface_info_) |
| 28 SetSurfaceIdFromServer(nullptr); |
| 29 |
| 30 window_tree_client_->OnWindowMusDestroyed(this); |
| 31 } |
| 32 |
| 33 // static |
| 34 WindowPortMus* WindowPortMus::Get(Window* window) { |
| 35 return static_cast<WindowPortMus*>(WindowPort::Get(window)); |
| 36 } |
| 37 |
| 38 void WindowPortMus::SetTextInputState(mojo::TextInputStatePtr state) { |
| 39 window_tree_client_->SetWindowTextInputState(this, std::move(state)); |
| 40 } |
| 41 |
| 42 void WindowPortMus::SetImeVisibility(bool visible, |
| 43 mojo::TextInputStatePtr state) { |
| 44 window_tree_client_->SetImeVisibility(this, visible, std::move(state)); |
| 45 } |
| 46 |
| 47 void WindowPortMus::SetPredefinedCursor(ui::mojom::Cursor cursor_id) { |
| 48 window_tree_client_->SetPredefinedCursor(this, predefined_cursor_, cursor_id); |
| 49 predefined_cursor_ = cursor_id; |
| 50 } |
| 51 |
| 52 WindowPortMus::ServerChangeIdType WindowPortMus::ScheduleChange( |
| 53 const ServerChangeType type, |
| 54 const ServerChangeData& data) { |
| 55 ServerChange change; |
| 56 change.type = type; |
| 57 change.server_change_id = next_server_change_id_++; |
| 58 change.data = data; |
| 59 server_changes_.push_back(change); |
| 60 return change.server_change_id; |
| 61 } |
| 62 |
| 63 void WindowPortMus::RemoveChangeById(ServerChangeIdType change_id) { |
| 64 for (auto iter = server_changes_.rbegin(); iter != server_changes_.rend(); |
| 65 ++iter) { |
| 66 if (iter->server_change_id == change_id) { |
| 67 server_changes_.erase(--(iter.base())); |
| 68 return; |
| 69 } |
| 70 } |
| 71 } |
| 72 |
| 73 bool WindowPortMus::RemoveChangeByTypeAndData(const ServerChangeType type, |
| 74 const ServerChangeData& data) { |
| 75 for (auto iter = server_changes_.begin(); iter != server_changes_.end(); |
| 76 ++iter) { |
| 77 if (iter->type != type) |
| 78 continue; |
| 79 |
| 80 switch (type) { |
| 81 case ServerChangeType::ADD: |
| 82 case ServerChangeType::REMOVE: |
| 83 case ServerChangeType::REORDER: |
| 84 if (iter->data.child_id == data.child_id) |
| 85 break; |
| 86 continue; |
| 87 case ServerChangeType::BOUNDS: |
| 88 if (iter->data.bounds == data.bounds) |
| 89 break; |
| 90 continue; |
| 91 case ServerChangeType::PROPERTY: |
| 92 if (iter->data.property_name == data.property_name) |
| 93 break; |
| 94 continue; |
| 95 case ServerChangeType::VISIBLE: |
| 96 if (iter->data.visible == data.visible) |
| 97 break; |
| 98 continue; |
| 99 } |
| 100 server_changes_.erase(iter); |
| 101 return true; |
| 102 } |
| 103 return false; |
| 104 } |
| 105 |
| 106 PropertyConverter* WindowPortMus::GetPropertyConverter() { |
| 107 return window_tree_client_->delegate_->GetPropertyConverter(); |
| 108 } |
| 109 |
| 110 Window* WindowPortMus::GetWindow() { |
| 111 return window_; |
| 112 } |
| 113 |
| 114 void WindowPortMus::AddChildFromServer(WindowMus* window) { |
| 115 DCHECK(has_server_window()); |
| 116 ServerChangeData data; |
| 117 data.child_id = window->server_id(); |
| 118 ScopedServerChange change(this, ServerChangeType::ADD, data); |
| 119 window_->AddChild(window->GetWindow()); |
| 120 } |
| 121 |
| 122 void WindowPortMus::RemoveChildFromServer(WindowMus* child) { |
| 123 DCHECK(has_server_window()); |
| 124 ServerChangeData data; |
| 125 data.child_id = child->server_id(); |
| 126 ScopedServerChange change(this, ServerChangeType::REMOVE, data); |
| 127 window_->RemoveChild(child->GetWindow()); |
| 128 } |
| 129 |
| 130 void WindowPortMus::ReorderFromServer(WindowMus* child, |
| 131 WindowMus* relative, |
| 132 ui::mojom::OrderDirection direction) { |
| 133 DCHECK(has_server_window()); |
| 134 // Keying off solely the id isn't entirely accurate, in so far as if Window |
| 135 // does some other reordering then the server and client are out of sync. |
| 136 // But we assume only one client can make changes to a particular window at |
| 137 // a time, so this should be ok. |
| 138 ServerChangeData data; |
| 139 data.child_id = child->server_id(); |
| 140 ScopedServerChange change(this, ServerChangeType::REORDER, data); |
| 141 if (direction == ui::mojom::OrderDirection::BELOW) |
| 142 window_->StackChildBelow(child->GetWindow(), relative->GetWindow()); |
| 143 else |
| 144 window_->StackChildAbove(child->GetWindow(), relative->GetWindow()); |
| 145 } |
| 146 |
| 147 void WindowPortMus::SetBoundsFromServer(const gfx::Rect& bounds) { |
| 148 DCHECK(has_server_window()); |
| 149 ServerChangeData data; |
| 150 data.bounds = bounds; |
| 151 ScopedServerChange change(this, ServerChangeType::BOUNDS, data); |
| 152 window_->SetBounds(bounds); |
| 153 } |
| 154 |
| 155 void WindowPortMus::SetVisibleFromServer(bool visible) { |
| 156 DCHECK(has_server_window()); |
| 157 ServerChangeData data; |
| 158 data.visible = visible; |
| 159 ScopedServerChange change(this, ServerChangeType::VISIBLE, data); |
| 160 if (visible) |
| 161 window_->Show(); |
| 162 else |
| 163 window_->Hide(); |
| 164 } |
| 165 |
| 166 void WindowPortMus::SetOpacityFromServer(float opacity) { |
| 167 // TODO(sky): this may not be necessary anymore. |
| 168 DCHECK(has_server_window()); |
| 169 // Changes to opacity don't make it back to the server. |
| 170 window_->layer()->SetOpacity(opacity); |
| 171 } |
| 172 |
| 173 void WindowPortMus::SetPredefinedCursorFromServer(ui::mojom::Cursor cursor) { |
| 174 // As this does nothing more than set the cursor we don't need to use |
| 175 // ServerChange. |
| 176 predefined_cursor_ = cursor; |
| 177 } |
| 178 |
| 179 void WindowPortMus::SetPropertyFromServer( |
| 180 const std::string& property_name, |
| 181 const std::vector<uint8_t>* property_data) { |
| 182 ServerChangeData data; |
| 183 data.property_name = property_name; |
| 184 ScopedServerChange change(this, ServerChangeType::PROPERTY, data); |
| 185 GetPropertyConverter()->SetPropertyFromTransportValue(window_, property_name, |
| 186 property_data); |
| 187 } |
| 188 |
| 189 void WindowPortMus::SetSurfaceIdFromServer( |
| 190 std::unique_ptr<SurfaceInfo> surface_info) { |
| 191 if (surface_info_) { |
| 192 const cc::SurfaceId& existing_surface_id = surface_info_->surface_id; |
| 193 cc::SurfaceId new_surface_id = |
| 194 surface_info ? surface_info->surface_id : cc::SurfaceId(); |
| 195 if (!existing_surface_id.is_null() && |
| 196 existing_surface_id != new_surface_id) { |
| 197 // Return the existing surface sequence. |
| 198 window_tree_client_->OnWindowMusSurfaceDetached( |
| 199 this, surface_info_->surface_sequence); |
| 200 } |
| 201 } |
| 202 WindowPortMus* parent = Get(window_->parent()); |
| 203 if (parent && parent->surface_id_handler_) { |
| 204 parent->surface_id_handler_->OnChildWindowSurfaceChanged(window_, |
| 205 &surface_info); |
| 206 } |
| 207 surface_info_ = std::move(surface_info); |
| 208 } |
| 209 |
| 210 void WindowPortMus::NotifyEmbeddedAppDisconnected() { |
| 211 for (WindowObserver& observer : *GetObservers(window_)) |
| 212 observer.OnEmbeddedAppDisconnected(window_); |
| 213 } |
| 214 |
| 215 std::unique_ptr<WindowPortInitData> WindowPortMus::OnPreInit(Window* window) { |
| 216 window_ = window; |
| 217 return window_tree_client_->OnWindowMusCreated(this); |
| 218 } |
| 219 |
| 220 void WindowPortMus::OnPostInit(std::unique_ptr<WindowPortInitData> init_data) { |
| 221 window_tree_client_->OnWindowMusInitDone(this, std::move(init_data)); |
| 222 } |
| 223 |
| 224 void WindowPortMus::OnDeviceScaleFactorChanged(float device_scale_factor) {} |
| 225 |
| 226 void WindowPortMus::OnWillAddChild(Window* child) { |
| 227 if (!has_server_window()) |
| 228 return; |
| 229 |
| 230 ServerChangeData change_data; |
| 231 change_data.child_id = Get(child)->server_id(); |
| 232 if (!RemoveChangeByTypeAndData(ServerChangeType::ADD, change_data)) |
| 233 window_tree_client_->OnWindowMusAddChild(this, Get(child)); |
| 234 } |
| 235 |
| 236 void WindowPortMus::OnWillRemoveChild(Window* child) { |
| 237 if (!has_server_window()) |
| 238 return; |
| 239 |
| 240 ServerChangeData change_data; |
| 241 change_data.child_id = Get(child)->server_id(); |
| 242 if (!RemoveChangeByTypeAndData(ServerChangeType::REMOVE, change_data)) |
| 243 window_tree_client_->OnWindowMusRemoveChild(this, Get(child)); |
| 244 } |
| 245 |
| 246 void WindowPortMus::OnWillMoveChild(size_t current_index, size_t dest_index) { |
| 247 if (!has_server_window()) |
| 248 return; |
| 249 |
| 250 ServerChangeData change_data; |
| 251 change_data.child_id = Get(window_->children()[current_index])->server_id(); |
| 252 if (!RemoveChangeByTypeAndData(ServerChangeType::REORDER, change_data)) |
| 253 window_tree_client_->OnWindowMusMoveChild(this, current_index, dest_index); |
| 254 } |
| 255 |
| 256 void WindowPortMus::OnVisibilityChanged(bool visible) { |
| 257 if (!has_server_window()) |
| 258 return; |
| 259 |
| 260 ServerChangeData change_data; |
| 261 change_data.visible = visible; |
| 262 if (!RemoveChangeByTypeAndData(ServerChangeType::VISIBLE, change_data)) |
| 263 window_tree_client_->OnWindowMusSetVisible(this, visible); |
| 264 } |
| 265 |
| 266 void WindowPortMus::OnDidChangeBounds(const gfx::Rect& old_bounds, |
| 267 const gfx::Rect& new_bounds) { |
| 268 if (!has_server_window()) |
| 269 return; |
| 270 |
| 271 ServerChangeData change_data; |
| 272 change_data.bounds = new_bounds; |
| 273 if (!RemoveChangeByTypeAndData(ServerChangeType::BOUNDS, change_data)) |
| 274 window_tree_client_->OnWindowMusBoundsChanged(this, old_bounds, new_bounds); |
| 275 } |
| 276 |
| 277 std::unique_ptr<WindowPortPropertyData> WindowPortMus::OnWillChangeProperty( |
| 278 const void* key) { |
| 279 if (!has_server_window()) |
| 280 return nullptr; |
| 281 |
| 282 return window_tree_client_->OnWindowMusWillChangeProperty(this, key); |
| 283 } |
| 284 |
| 285 void WindowPortMus::OnPropertyChanged( |
| 286 const void* key, |
| 287 std::unique_ptr<WindowPortPropertyData> data) { |
| 288 if (!has_server_window()) |
| 289 return; |
| 290 |
| 291 ServerChangeData change_data; |
| 292 change_data.property_name = |
| 293 GetPropertyConverter()->GetTransportNameForPropertyKey(key); |
| 294 // TODO(sky): investigate to see if we need to compare data. In particular do |
| 295 // we ever have a case where changing a property cascades into changing the |
| 296 // same property? |
| 297 if (!RemoveChangeByTypeAndData(ServerChangeType::PROPERTY, change_data)) |
| 298 window_tree_client_->OnWindowMusPropertyChanged(this, key, std::move(data)); |
| 299 } |
| 300 |
| 301 } // namespace aura |
OLD | NEW |