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 "ui/views/mus/platform_window_mus.h" | 5 #include "ui/views/mus/platform_window_mus.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | |
7 #include "build/build_config.h" | 8 #include "build/build_config.h" |
8 #include "components/bitmap_uploader/bitmap_uploader.h" | 9 #include "components/bitmap_uploader/bitmap_uploader.h" |
9 #include "components/mus/public/cpp/property_type_converters.h" | 10 #include "components/mus/public/cpp/property_type_converters.h" |
10 #include "components/mus/public/cpp/window_property.h" | 11 #include "components/mus/public/cpp/window_property.h" |
11 #include "components/mus/public/interfaces/window_manager.mojom.h" | 12 #include "components/mus/public/interfaces/window_manager.mojom.h" |
12 #include "mojo/converters/input_events/input_events_type_converters.h" | 13 #include "mojo/converters/input_events/input_events_type_converters.h" |
13 #include "ui/base/view_prop.h" | 14 #include "ui/base/view_prop.h" |
14 #include "ui/platform_window/platform_window_delegate.h" | 15 #include "ui/platform_window/platform_window_delegate.h" |
15 #include "ui/views/mus/window_manager_connection.h" | 16 #include "ui/views/mus/window_manager_connection.h" |
16 | 17 |
18 using mus::mojom::EventResult; | |
19 | |
17 namespace views { | 20 namespace views { |
18 | 21 |
19 namespace { | 22 namespace { |
23 | |
20 static uint32_t accelerated_widget_count = 1; | 24 static uint32_t accelerated_widget_count = 1; |
21 | 25 |
26 // Acknowledges an input event immediately if a nested message loop starts. | |
27 // Needed because otherwise we appear unresponsive for the life of the nested | |
28 // message loop. | |
29 class EventAckNestingObserver : public base::MessageLoop::NestingObserver { | |
30 public: | |
31 explicit EventAckNestingObserver( | |
32 std::unique_ptr<base::Callback<void(EventResult)>>* ack_callback) | |
sky
2016/04/27 17:49:48
Did you consider making this take a std::unique_pt
James Cook
2016/04/27 18:11:14
Done.
| |
33 : ack_callback_(ack_callback) { | |
34 DCHECK(ack_callback_); | |
35 } | |
36 ~EventAckNestingObserver() override {} | |
37 | |
38 // base::MessageLoop::NestingObserver: | |
39 void OnBeginNestedMessageLoop() override { | |
40 if (*ack_callback_) { | |
41 (*ack_callback_)->Run(EventResult::HANDLED); | |
42 ack_callback_->reset(); // Signal that the event was ack'd. | |
43 } | |
44 } | |
45 | |
46 private: | |
47 std::unique_ptr<base::Callback<void(EventResult)>>* ack_callback_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(EventAckNestingObserver); | |
50 }; | |
51 | |
22 } // namespace | 52 } // namespace |
23 | 53 |
24 PlatformWindowMus::PlatformWindowMus(ui::PlatformWindowDelegate* delegate, | 54 PlatformWindowMus::PlatformWindowMus(ui::PlatformWindowDelegate* delegate, |
25 shell::Connector* connector, | 55 shell::Connector* connector, |
26 mus::Window* mus_window) | 56 mus::Window* mus_window) |
27 : delegate_(delegate), | 57 : delegate_(delegate), |
28 mus_window_(mus_window), | 58 mus_window_(mus_window), |
29 show_state_(mus::mojom::ShowState::RESTORED), | 59 show_state_(mus::mojom::ShowState::RESTORED), |
30 last_cursor_(mus::mojom::Cursor::CURSOR_NULL), | 60 last_cursor_(mus::mojom::Cursor::CURSOR_NULL), |
31 mus_window_destroyed_(false) { | 61 mus_window_destroyed_(false) { |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 245 } |
216 delegate_->OnWindowStateChanged(state); | 246 delegate_->OnWindowStateChanged(state); |
217 } | 247 } |
218 | 248 |
219 void PlatformWindowMus::OnRequestClose(mus::Window* window) { | 249 void PlatformWindowMus::OnRequestClose(mus::Window* window) { |
220 delegate_->OnCloseRequest(); | 250 delegate_->OnCloseRequest(); |
221 } | 251 } |
222 | 252 |
223 void PlatformWindowMus::OnWindowInputEvent( | 253 void PlatformWindowMus::OnWindowInputEvent( |
224 mus::Window* view, | 254 mus::Window* view, |
225 const ui::Event& event, | 255 const ui::Event& event_in, |
226 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* | 256 std::unique_ptr<base::Callback<void(EventResult)>>* ack_callback_in) { |
227 ack_callback) { | 257 // Take ownership of the callback, indicating that we will handle it. |
228 // It's possible dispatching the event will spin a nested message loop. Ack | 258 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback = |
229 // the callback now, otherwise we appear unresponsive for the life of the | 259 std::move(*ack_callback_in); |
230 // nested message loop. | 260 |
231 (*ack_callback)->Run(mus::mojom::EventResult::HANDLED); | 261 // If dispatching the event creates a nested message loop we need to ack the |
232 ack_callback->reset(); | 262 // event immediately. There might be multiple nested message loops; each one |
233 // TODO(moshayedi): Avoid cloning after updating PlatformWindowDelegate to | 263 // needs its own observer. |
234 // accept constant pointers. | 264 EventAckNestingObserver nesting_observer(&ack_callback); |
235 delegate_->DispatchEvent(ui::Event::Clone(event).get()); | 265 base::MessageLoop::current()->AddNestingObserver(&nesting_observer); |
266 | |
267 std::unique_ptr<ui::Event> event = ui::Event::Clone(event_in); | |
268 delegate_->DispatchEvent(event.get()); | |
269 // NOTE: |this| may be deleted. | |
270 | |
271 base::MessageLoop::current()->RemoveNestingObserver(&nesting_observer); | |
272 | |
273 // If the callback wasn't already called by the message loop observer then | |
274 // ack it with the handled state. | |
275 if (ack_callback) | |
276 ack_callback->Run(event->handled() ? EventResult::HANDLED | |
277 : EventResult::UNHANDLED); | |
236 } | 278 } |
237 | 279 |
238 } // namespace views | 280 } // namespace views |
OLD | NEW |