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 "services/ui/input_manager/input_dispatcher_impl.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "mojo/services/geometry/cpp/geometry_util.h" | |
11 #include "mojo/services/gfx/composition/cpp/formatting.h" | |
12 #include "mojo/services/ui/views/cpp/formatting.h" | |
13 #include "services/ui/input_manager/input_associate.h" | |
14 | |
15 namespace input_manager { | |
16 namespace { | |
17 void TransformEvent(const mojo::Transform& transform, mojo::Event* event) { | |
18 if (!event->pointer_data) | |
19 return; | |
20 mojo::Point point; | |
21 point.x = event->pointer_data->x; | |
22 point.y = event->pointer_data->y; | |
23 point = TransformPoint(transform, point); | |
24 event->pointer_data->x = point.x; | |
25 event->pointer_data->y = point.y; | |
26 } | |
27 } // namespace | |
28 | |
29 InputDispatcherImpl::InputDispatcherImpl( | |
30 InputAssociate* associate, | |
31 mojo::ui::ViewTreeTokenPtr view_tree_token, | |
32 mojo::InterfaceRequest<mojo::ui::InputDispatcher> request) | |
33 : associate_(associate), | |
34 view_tree_token_(view_tree_token.Pass()), | |
35 hit_tester_( | |
36 new mojo::ui::ViewTreeHitTesterClient(associate_->inspector(), | |
37 view_tree_token_.Clone())), | |
38 binding_(this, request.Pass()), | |
39 weak_factory_(this) { | |
40 DCHECK(associate_); | |
41 DCHECK(view_tree_token_); | |
42 | |
43 binding_.set_connection_error_handler( | |
44 base::Bind(&InputAssociate::OnInputDispatcherDied, | |
45 base::Unretained(associate_), base::Unretained(this))); | |
46 } | |
47 | |
48 InputDispatcherImpl::~InputDispatcherImpl() {} | |
49 | |
50 void InputDispatcherImpl::DispatchEvent(mojo::EventPtr event) { | |
51 DCHECK(event); | |
52 | |
53 pending_events_.push(event.Pass()); | |
54 if (pending_events_.size() == 1u) | |
55 ProcessNextEvent(); | |
56 } | |
57 | |
58 void InputDispatcherImpl::ProcessNextEvent() { | |
59 DCHECK(!pending_events_.empty()); | |
60 | |
61 const mojo::Event* event = pending_events_.front().get(); | |
62 if (event->action != mojo::EventType::POINTER_DOWN) { | |
63 DeliverNextEvent(); | |
64 return; | |
65 } | |
66 | |
67 DCHECK(event->pointer_data); | |
68 auto point = mojo::Point::New(); | |
69 point->x = event->pointer_data->x; | |
70 point->y = event->pointer_data->y; | |
71 DVLOG(1) << "HitTest: point=" << point; | |
72 hit_tester_->HitTest(point.Pass(), | |
73 base::Bind(&InputDispatcherImpl::OnHitTestResult, | |
74 weak_factory_.GetWeakPtr())); | |
75 } | |
76 | |
77 void InputDispatcherImpl::DeliverNextEvent() { | |
78 DCHECK(!pending_events_.empty()); | |
79 | |
80 mojo::EventPtr event = pending_events_.front().Pass(); | |
81 pending_events_.pop(); | |
82 | |
83 if (focused_view_token_) { | |
84 TransformEvent(*focused_view_transform_, event.get()); | |
85 associate_->DeliverEvent(focused_view_token_.get(), event.Pass()); | |
86 } | |
87 | |
88 if (!pending_events_.empty()) { | |
89 base::MessageLoop::current()->PostTask( | |
90 FROM_HERE, base::Bind(&InputDispatcherImpl::ProcessNextEvent, | |
91 weak_factory_.GetWeakPtr())); | |
abarth
2016/03/09 04:17:37
Interesting.
jeffbrown
2016/03/09 19:38:50
This was to prevent reentrance when called from On
| |
92 } | |
93 } | |
94 | |
95 void InputDispatcherImpl::OnHitTestResult( | |
96 scoped_ptr<mojo::ui::ResolvedHits> resolved_hits) { | |
97 DVLOG(1) << "OnHitTestResult: resolved_hits=" << resolved_hits.get(); | |
98 | |
99 // TODO(jeffbrown): Flesh out the input protocol so it makes sense to | |
100 // look at more than the first hit. | |
101 focused_view_token_.reset(); | |
102 focused_view_transform_.reset(); | |
103 if (resolved_hits && resolved_hits->result()->root) { | |
104 mojo::gfx::composition::HitTestResultPtr result = | |
105 resolved_hits->TakeResult(); | |
106 const mojo::gfx::composition::SceneHit* scene = result->root.get(); | |
107 for (;;) { | |
108 DCHECK(scene->hits.size()); | |
109 if (scene->hits[0]->is_node()) { | |
110 auto it = resolved_hits->map().find(scene->scene_token->value); | |
111 if (it != resolved_hits->map().end()) { | |
112 focused_view_token_ = it->second.Clone(); | |
113 focused_view_transform_ = | |
114 scene->hits[0]->get_node()->transform.Pass(); | |
115 } | |
116 break; | |
117 } | |
118 DCHECK(scene->hits[0]->is_scene()); | |
119 scene = scene->hits[0]->get_scene().get(); | |
abarth
2016/03/09 04:17:37
I see, you're just delivering to the most specific
jeffbrown
2016/03/09 19:38:50
Acknowledged.
| |
120 } | |
121 } | |
122 | |
123 DVLOG(1) << "OnHitTestResult: focused_view_token_=" << focused_view_token_ | |
124 << ", focused_view_transform_=" << focused_view_transform_; | |
125 | |
126 DeliverNextEvent(); | |
127 } | |
128 | |
129 } // namespace input_manager | |
OLD | NEW |