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 // Handles acknowledgement of an input event, either immediately when a nested | |
27 // message loop starts, or upon destruction. | |
28 class EventAckHandler : public base::MessageLoop::NestingObserver { | |
29 public: | |
30 explicit EventAckHandler( | |
31 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback) | |
32 : ack_callback_(std::move(ack_callback)) { | |
33 DCHECK(ack_callback_); | |
34 base::MessageLoop::current()->AddNestingObserver(this); | |
35 } | |
36 | |
37 ~EventAckHandler() override { | |
38 base::MessageLoop::current()->RemoveNestingObserver(this); | |
39 if (ack_callback_) | |
Lei Zhang
2016/04/27 22:48:06
nit: should have braces.
James Cook
2016/04/27 23:31:17
Done.
| |
40 ack_callback_->Run(handled_ ? EventResult::HANDLED | |
41 : EventResult::UNHANDLED); | |
42 } | |
43 | |
44 void set_handled(bool handled) { handled_ = handled; } | |
45 | |
46 // base::MessageLoop::NestingObserver: | |
47 void OnBeginNestedMessageLoop() override { | |
48 // Acknowledge the event immediately if a nested message loop starts. | |
49 // Otherwise we appear unresponsive for the life of the nested message loop. | |
50 if (ack_callback_) { | |
51 ack_callback_->Run(EventResult::HANDLED); | |
52 ack_callback_.reset(); | |
53 } | |
54 } | |
55 | |
56 private: | |
57 std::unique_ptr<base::Callback<void(EventResult)>> ack_callback_; | |
58 bool handled_ = false; | |
59 | |
60 DISALLOW_COPY_AND_ASSIGN(EventAckHandler); | |
61 }; | |
62 | |
22 } // namespace | 63 } // namespace |
23 | 64 |
24 PlatformWindowMus::PlatformWindowMus(ui::PlatformWindowDelegate* delegate, | 65 PlatformWindowMus::PlatformWindowMus(ui::PlatformWindowDelegate* delegate, |
25 shell::Connector* connector, | 66 shell::Connector* connector, |
26 mus::Window* mus_window) | 67 mus::Window* mus_window) |
27 : delegate_(delegate), | 68 : delegate_(delegate), |
28 mus_window_(mus_window), | 69 mus_window_(mus_window), |
29 show_state_(mus::mojom::ShowState::RESTORED), | 70 show_state_(mus::mojom::ShowState::RESTORED), |
30 last_cursor_(mus::mojom::Cursor::CURSOR_NULL), | 71 last_cursor_(mus::mojom::Cursor::CURSOR_NULL), |
31 mus_window_destroyed_(false) { | 72 mus_window_destroyed_(false) { |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 } | 256 } |
216 delegate_->OnWindowStateChanged(state); | 257 delegate_->OnWindowStateChanged(state); |
217 } | 258 } |
218 | 259 |
219 void PlatformWindowMus::OnRequestClose(mus::Window* window) { | 260 void PlatformWindowMus::OnRequestClose(mus::Window* window) { |
220 delegate_->OnCloseRequest(); | 261 delegate_->OnCloseRequest(); |
221 } | 262 } |
222 | 263 |
223 void PlatformWindowMus::OnWindowInputEvent( | 264 void PlatformWindowMus::OnWindowInputEvent( |
224 mus::Window* view, | 265 mus::Window* view, |
225 const ui::Event& event, | 266 const ui::Event& event_in, |
226 std::unique_ptr<base::Callback<void(mus::mojom::EventResult)>>* | 267 std::unique_ptr<base::Callback<void(EventResult)>>* ack_callback) { |
227 ack_callback) { | 268 // 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 | 269 EventAckHandler ack_handler(std::move(*ack_callback)); |
229 // the callback now, otherwise we appear unresponsive for the life of the | 270 |
230 // nested message loop. | 271 std::unique_ptr<ui::Event> event = ui::Event::Clone(event_in); |
231 (*ack_callback)->Run(mus::mojom::EventResult::HANDLED); | 272 delegate_->DispatchEvent(event.get()); |
232 ack_callback->reset(); | 273 // NOTE: |this| may be deleted. |
233 // TODO(moshayedi): Avoid cloning after updating PlatformWindowDelegate to | 274 |
234 // accept constant pointers. | 275 ack_handler.set_handled(event->handled()); |
235 delegate_->DispatchEvent(ui::Event::Clone(event).get()); | 276 // |ack_handler| acks the event on destruction if necessary. |
236 } | 277 } |
237 | 278 |
238 } // namespace views | 279 } // namespace views |
OLD | NEW |