OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/app/linux/blimp_display_manager.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "blimp/client/app/compositor/browser_compositor.h" | |
9 #include "blimp/client/public/compositor/compositor_dependencies.h" | |
10 #include "blimp/client/public/contents/blimp_contents.h" | |
11 #include "blimp/client/public/contents/blimp_contents_view.h" | |
12 #include "blimp/client/public/contents/blimp_navigation_controller.h" | |
13 #include "ui/events/event.h" | |
14 #include "ui/events/gesture_detection/motion_event_generic.h" | |
15 #include "ui/events/gestures/motion_event_aura.h" | |
16 #include "ui/events/platform/platform_event_source.h" | |
17 #include "ui/gfx/geometry/dip_util.h" | |
18 #include "ui/gfx/geometry/size.h" | |
19 #include "ui/platform_window/platform_window.h" | |
20 #include "ui/platform_window/x11/x11_window.h" | |
21 | |
22 namespace blimp { | |
23 namespace { | |
24 constexpr int kPointer1Id = 0; | |
25 constexpr int kPointer2Id = 1; | |
26 constexpr int kZoomOffsetMultiplier = 4; | |
27 } // namespace | |
28 | |
29 namespace client { | |
30 | |
31 BlimpDisplayManager::BlimpDisplayManager( | |
32 BlimpDisplayManagerDelegate* delegate, | |
33 CompositorDependencies* compositor_dependencies) | |
34 : device_pixel_ratio_(1.f), | |
35 delegate_(delegate), | |
36 platform_event_source_(ui::PlatformEventSource::CreateDefault()), | |
37 platform_window_(new ui::X11Window(this)) { | |
38 compositor_ = base::MakeUnique<BrowserCompositor>(compositor_dependencies); | |
39 } | |
40 | |
41 BlimpDisplayManager::~BlimpDisplayManager() = default; | |
42 | |
43 void BlimpDisplayManager::SetWindowSize(const gfx::Size& window_size) { | |
44 platform_window_->SetBounds( | |
45 gfx::ConvertRectToPixel(device_pixel_ratio_, gfx::Rect(window_size))); | |
46 } | |
47 | |
48 void BlimpDisplayManager::SetBlimpContents( | |
49 std::unique_ptr<BlimpContents> contents) { | |
50 contents_ = std::move(contents); | |
51 compositor_->SetContentLayer(contents_->GetView()->GetLayer()); | |
52 platform_window_->Show(); | |
53 } | |
54 | |
55 void BlimpDisplayManager::OnBoundsChanged(const gfx::Rect& new_bounds) { | |
56 compositor_->SetSize(new_bounds.size()); | |
57 if (contents_) { | |
58 contents_->GetView()->SetSizeAndScale(new_bounds.size(), | |
59 device_pixel_ratio_); | |
60 } | |
61 } | |
62 | |
63 void BlimpDisplayManager::DispatchEvent(ui::Event* event) { | |
64 if (event->IsMouseWheelEvent()) { | |
65 DispatchMouseWheelEvent(event->AsMouseWheelEvent()); | |
66 } else if (event->IsMouseEvent()) { | |
67 DispatchMouseEvent(event->AsMouseEvent()); | |
68 } | |
69 } | |
70 | |
71 void BlimpDisplayManager::DispatchMotionEventAura( | |
72 ui::MotionEventAura* touch_event_stream, | |
73 ui::EventType event_type, | |
74 int pointer_id, | |
75 int pointer_x, | |
76 int pointer_y) { | |
77 DCHECK(contents_); | |
78 | |
79 touch_event_stream->OnTouch( | |
80 ui::TouchEvent(event_type, gfx::Point(pointer_x, pointer_y), pointer_id, | |
81 base::TimeTicks::Now())); | |
82 contents_->GetView()->OnTouchEvent(*touch_event_stream); | |
83 } | |
84 | |
85 void BlimpDisplayManager::DispatchMouseWheelEvent( | |
86 ui::MouseWheelEvent* wheel_event) { | |
87 // In order to better simulate the Android experience on the Linux client | |
88 // we convert mousewheel scrolling events into pinch/zoom events. | |
89 bool zoom_out = wheel_event->y_offset() < 0; | |
90 Zoom(wheel_event->x(), wheel_event->y(), wheel_event->y_offset(), zoom_out); | |
91 } | |
92 | |
93 void BlimpDisplayManager::Zoom(int pointer_x, | |
94 int pointer_y, | |
95 int y_offset, | |
96 bool zoom_out) { | |
97 int pinch_distance = abs(y_offset * kZoomOffsetMultiplier); | |
98 int pointer2_y = pointer_y; | |
99 if (zoom_out) { | |
100 // Pointers start apart when zooming out. | |
101 pointer2_y -= pinch_distance; | |
102 } | |
103 | |
104 ui::MotionEventAura touch_event_stream; | |
105 | |
106 // Place two pointers. | |
107 DispatchMotionEventAura(&touch_event_stream, ui::ET_TOUCH_PRESSED, | |
108 kPointer1Id, pointer_x, pointer_y); | |
109 DispatchMotionEventAura(&touch_event_stream, ui::ET_TOUCH_PRESSED, | |
110 kPointer2Id, pointer_x, pointer2_y); | |
111 | |
112 // Pinch pointers. Need to simulate gradually in order for the event to be | |
113 // properly processed. | |
114 for (int pointer2_y_end = pointer2_y + pinch_distance; | |
115 pointer2_y < pointer2_y_end; ++pointer2_y) { | |
116 DispatchMotionEventAura(&touch_event_stream, ui::ET_TOUCH_MOVED, | |
117 kPointer2Id, pointer_x, pointer2_y); | |
118 } | |
119 | |
120 // Remove pointers. | |
121 DispatchMotionEventAura(&touch_event_stream, ui::ET_TOUCH_RELEASED, | |
122 kPointer1Id, pointer_x, pointer_y); | |
123 DispatchMotionEventAura(&touch_event_stream, ui::ET_TOUCH_RELEASED, | |
124 kPointer2Id, pointer_x, pointer2_y); | |
125 } | |
126 | |
127 ui::MotionEvent::Action MouseEventToAction(ui::MouseEvent* mouse_event) { | |
128 switch (mouse_event->type()) { | |
129 case ui::ET_MOUSE_PRESSED: | |
130 return ui::MotionEvent::ACTION_DOWN; | |
131 case ui::ET_MOUSE_RELEASED: | |
132 return ui::MotionEvent::ACTION_UP; | |
133 case ui::ET_MOUSE_DRAGGED: | |
134 return ui::MotionEvent::ACTION_MOVE; | |
135 default: | |
136 return ui::MotionEvent::ACTION_NONE; | |
137 } | |
138 } | |
139 | |
140 void BlimpDisplayManager::DispatchMouseEvent(ui::MouseEvent* mouse_event) { | |
141 ui::MotionEvent::Action action = MouseEventToAction(mouse_event); | |
142 | |
143 if (action != ui::MotionEvent::ACTION_NONE && | |
144 mouse_event->IsOnlyLeftMouseButton()) { | |
145 DCHECK(contents_); | |
146 | |
147 ui::PointerProperties mouse_properties(mouse_event->x(), mouse_event->y(), | |
148 0); | |
149 ui::MotionEventGeneric motion_event(action, mouse_event->time_stamp(), | |
150 mouse_properties); | |
151 contents_->GetView()->OnTouchEvent(motion_event); | |
152 } | |
153 } | |
154 | |
155 void BlimpDisplayManager::OnCloseRequest() { | |
156 DCHECK(contents_); | |
157 | |
158 contents_->Hide(); | |
159 compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
160 platform_window_->Close(); | |
161 } | |
162 | |
163 void BlimpDisplayManager::OnClosed() { | |
164 if (delegate_) | |
165 delegate_->OnClosed(); | |
166 } | |
167 | |
168 void BlimpDisplayManager::OnAcceleratedWidgetAvailable( | |
169 gfx::AcceleratedWidget widget, | |
170 float device_pixel_ratio) { | |
171 DCHECK(contents_); | |
172 | |
173 device_pixel_ratio_ = device_pixel_ratio; | |
174 contents_->GetView()->SetSizeAndScale(platform_window_->GetBounds().size(), | |
175 device_pixel_ratio_); | |
176 | |
177 if (widget != gfx::kNullAcceleratedWidget) { | |
178 contents_->Show(); | |
179 compositor_->SetAcceleratedWidget(widget); | |
180 } | |
181 } | |
182 | |
183 void BlimpDisplayManager::OnAcceleratedWidgetDestroyed() { | |
184 DCHECK(contents_); | |
185 contents_->Hide(); | |
186 compositor_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
187 } | |
188 | |
189 } // namespace client | |
190 } // namespace blimp | |
OLD | NEW |