| Flutter Linux Embedder |

fl_scrolling_manager.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "flutter/shell/platform/linux/fl_scrolling_manager.h"
6 #include "flutter/shell/platform/embedder/embedder.h"
7 #include "flutter/shell/platform/linux/fl_engine_private.h"
8
9 static constexpr int kMicrosecondsPerMillisecond = 1000;
10
11 struct _FlScrollingManager {
12 GObject parent_instance;
13
14 GWeakRef engine;
15
16 FlutterViewId view_id;
17
18 gdouble last_x;
19 gdouble last_y;
20
21 gboolean pan_started;
22 gdouble pan_x;
23 gdouble pan_y;
24
25 gboolean zoom_started;
26 gboolean rotate_started;
27 gdouble scale;
28 gdouble rotation;
29 };
30
31 G_DEFINE_TYPE(FlScrollingManager, fl_scrolling_manager, G_TYPE_OBJECT);
32
33 static void fl_scrolling_manager_dispose(GObject* object) {
34 FlScrollingManager* self = FL_SCROLLING_MANAGER(object);
35
36 g_weak_ref_clear(&self->engine);
37
38 G_OBJECT_CLASS(fl_scrolling_manager_parent_class)->dispose(object);
39 }
40
41 static void fl_scrolling_manager_class_init(FlScrollingManagerClass* klass) {
42 G_OBJECT_CLASS(klass)->dispose = fl_scrolling_manager_dispose;
43 }
44
45 static void fl_scrolling_manager_init(FlScrollingManager* self) {}
46
47 FlScrollingManager* fl_scrolling_manager_new(FlEngine* engine,
48 FlutterViewId view_id) {
49 g_return_val_if_fail(FL_IS_ENGINE(engine), nullptr);
50
51 FlScrollingManager* self = FL_SCROLLING_MANAGER(
52 g_object_new(fl_scrolling_manager_get_type(), nullptr));
53
54 g_weak_ref_init(&self->engine, engine);
55 self->view_id = view_id;
56 self->pan_started = FALSE;
57 self->zoom_started = FALSE;
58 self->rotate_started = FALSE;
59
60 return self;
61 }
62
63 void fl_scrolling_manager_set_last_mouse_position(FlScrollingManager* self,
64 gdouble x,
65 gdouble y) {
66 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
67 self->last_x = x;
68 self->last_y = y;
69 }
70
71 void fl_scrolling_manager_handle_scroll_event(FlScrollingManager* self,
72 GdkEventScroll* scroll_event,
73 gint scale_factor) {
74 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
75
76 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
77 if (engine == nullptr) {
78 return;
79 }
80
81 GdkEvent* event = reinterpret_cast<GdkEvent*>(scroll_event);
82
83 guint event_time = gdk_event_get_time(event);
84 gdouble event_x = 0.0, event_y = 0.0;
85 gdk_event_get_coords(event, &event_x, &event_y);
86 gdouble scroll_delta_x = 0.0, scroll_delta_y = 0.0;
87 GdkScrollDirection event_direction = GDK_SCROLL_SMOOTH;
88 if (gdk_event_get_scroll_direction(event, &event_direction)) {
89 if (event_direction == GDK_SCROLL_UP) {
90 scroll_delta_x = 0;
91 scroll_delta_y = -1;
92 } else if (event_direction == GDK_SCROLL_DOWN) {
93 scroll_delta_x = 0;
94 scroll_delta_y = 1;
95 } else if (event_direction == GDK_SCROLL_LEFT) {
96 scroll_delta_x = -1;
97 scroll_delta_y = 0;
98 } else if (event_direction == GDK_SCROLL_RIGHT) {
99 scroll_delta_x = 1;
100 scroll_delta_y = 0;
101 }
102 } else {
103 gdk_event_get_scroll_deltas(event, &scroll_delta_x, &scroll_delta_y);
104 }
105
106 // The multiplier is taken from the Chromium source
107 // (ui/events/x/events_x_utils.cc).
108 const int kScrollOffsetMultiplier = 53;
109 scroll_delta_x *= kScrollOffsetMultiplier * scale_factor;
110 scroll_delta_y *= kScrollOffsetMultiplier * scale_factor;
111
112 if (gdk_device_get_source(gdk_event_get_source_device(event)) ==
113 GDK_SOURCE_TOUCHPAD) {
114 scroll_delta_x *= -1;
115 scroll_delta_y *= -1;
116 if (gdk_event_is_scroll_stop_event(event)) {
117 fl_engine_send_pointer_pan_zoom_event(
118 engine, self->view_id, event_time * kMicrosecondsPerMillisecond,
119 event_x * scale_factor, event_y * scale_factor, kPanZoomEnd,
120 self->pan_x, self->pan_y, 0, 0);
121 self->pan_started = FALSE;
122 } else {
123 if (!self->pan_started) {
124 self->pan_x = 0;
125 self->pan_y = 0;
126 fl_engine_send_pointer_pan_zoom_event(
127 engine, self->view_id, event_time * kMicrosecondsPerMillisecond,
128 event_x * scale_factor, event_y * scale_factor, kPanZoomStart, 0, 0,
129 0, 0);
130 self->pan_started = TRUE;
131 }
132 self->pan_x += scroll_delta_x;
133 self->pan_y += scroll_delta_y;
134 fl_engine_send_pointer_pan_zoom_event(
135 engine, self->view_id, event_time * kMicrosecondsPerMillisecond,
136 event_x * scale_factor, event_y * scale_factor, kPanZoomUpdate,
137 self->pan_x, self->pan_y, 1, 0);
138 }
139 } else {
140 self->last_x = event_x * scale_factor;
141 self->last_y = event_y * scale_factor;
142 fl_engine_send_mouse_pointer_event(
143 engine, self->view_id,
144 FlutterPointerPhase::kMove /* arbitrary value, phase will be ignored as
145 this is a discrete scroll event */
146 ,
147 event_time * kMicrosecondsPerMillisecond, event_x * scale_factor,
148 event_y * scale_factor, kFlutterPointerDeviceKindMouse, scroll_delta_x,
149 scroll_delta_y, 0);
150 }
151 }
152
153 void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager* self) {
154 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
155
156 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
157 if (engine == nullptr) {
158 return;
159 }
160
161 self->rotate_started = TRUE;
162 if (!self->zoom_started) {
163 self->scale = 1;
164 self->rotation = 0;
165 fl_engine_send_pointer_pan_zoom_event(
166 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
167 kPanZoomStart, 0, 0, 0, 0);
168 }
169 }
170
171 void fl_scrolling_manager_handle_rotation_update(FlScrollingManager* self,
172 gdouble rotation) {
173 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
174
175 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
176 if (engine == nullptr) {
177 return;
178 }
179
180 self->rotation = rotation;
181 fl_engine_send_pointer_pan_zoom_event(
182 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
183 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
184 }
185
186 void fl_scrolling_manager_handle_rotation_end(FlScrollingManager* self) {
187 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
188
189 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
190 if (engine == nullptr) {
191 return;
192 }
193
194 self->rotate_started = FALSE;
195 if (!self->zoom_started) {
196 fl_engine_send_pointer_pan_zoom_event(
197 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
198 kPanZoomEnd, 0, 0, 0, 0);
199 }
200 }
201
202 void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager* self) {
203 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
204
205 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
206 if (engine == nullptr) {
207 return;
208 }
209
210 self->zoom_started = TRUE;
211 if (!self->rotate_started) {
212 self->scale = 1;
213 self->rotation = 0;
214 fl_engine_send_pointer_pan_zoom_event(
215 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
216 kPanZoomStart, 0, 0, 0, 0);
217 }
218 }
219
220 void fl_scrolling_manager_handle_zoom_update(FlScrollingManager* self,
221 gdouble scale) {
222 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
223
224 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
225 if (engine == nullptr) {
226 return;
227 }
228
229 self->scale = scale;
230 fl_engine_send_pointer_pan_zoom_event(
231 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
232 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
233 }
234
235 void fl_scrolling_manager_handle_zoom_end(FlScrollingManager* self) {
236 g_return_if_fail(FL_IS_SCROLLING_MANAGER(self));
237
238 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
239 if (engine == nullptr) {
240 return;
241 }
242
243 self->zoom_started = FALSE;
244 if (!self->rotate_started) {
245 fl_engine_send_pointer_pan_zoom_event(
246 engine, self->view_id, g_get_real_time(), self->last_x, self->last_y,
247 kPanZoomEnd, 0, 0, 0, 0);
248 }
249 }
g_autoptr
g_autoptr(GMutexLocker) locker
TRUE
return TRUE
Definition: fl_compositor_software.cc:68
fl_engine_send_mouse_pointer_event
void fl_engine_send_mouse_pointer_event(FlEngine *self, FlutterViewId view_id, FlutterPointerPhase phase, size_t timestamp, double x, double y, FlutterPointerDeviceKind device_kind, double scroll_delta_x, double scroll_delta_y, int64_t buttons)
Definition: fl_engine.cc:1123
fl_engine_send_pointer_pan_zoom_event
void fl_engine_send_pointer_pan_zoom_event(FlEngine *self, FlutterViewId view_id, size_t timestamp, double x, double y, FlutterPointerPhase phase, double pan_x, double pan_y, double scale, double rotation)
Definition: fl_engine.cc:1287
fl_engine_private.h
fl_scrolling_manager_handle_rotation_begin
void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:153
fl_scrolling_manager_class_init
static void fl_scrolling_manager_class_init(FlScrollingManagerClass *klass)
Definition: fl_scrolling_manager.cc:41
G_DEFINE_TYPE
G_DEFINE_TYPE(FlScrollingManager, fl_scrolling_manager, G_TYPE_OBJECT)
fl_scrolling_manager_set_last_mouse_position
void fl_scrolling_manager_set_last_mouse_position(FlScrollingManager *self, gdouble x, gdouble y)
Definition: fl_scrolling_manager.cc:63
fl_scrolling_manager_handle_rotation_end
void fl_scrolling_manager_handle_rotation_end(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:186
fl_scrolling_manager_handle_scroll_event
void fl_scrolling_manager_handle_scroll_event(FlScrollingManager *self, GdkEventScroll *scroll_event, gint scale_factor)
Definition: fl_scrolling_manager.cc:71
fl_scrolling_manager_new
FlScrollingManager * fl_scrolling_manager_new(FlEngine *engine, FlutterViewId view_id)
Definition: fl_scrolling_manager.cc:47
fl_scrolling_manager_handle_zoom_update
void fl_scrolling_manager_handle_zoom_update(FlScrollingManager *self, gdouble scale)
Definition: fl_scrolling_manager.cc:220
kMicrosecondsPerMillisecond
static constexpr int kMicrosecondsPerMillisecond
Definition: fl_scrolling_manager.cc:9
fl_scrolling_manager_handle_rotation_update
void fl_scrolling_manager_handle_rotation_update(FlScrollingManager *self, gdouble rotation)
Definition: fl_scrolling_manager.cc:171
fl_scrolling_manager_dispose
static void fl_scrolling_manager_dispose(GObject *object)
Definition: fl_scrolling_manager.cc:33
fl_scrolling_manager_handle_zoom_begin
void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:202
fl_scrolling_manager_handle_zoom_end
void fl_scrolling_manager_handle_zoom_end(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:235
fl_scrolling_manager_init
static void fl_scrolling_manager_init(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:45
fl_scrolling_manager.h
view_id
G_BEGIN_DECLS FlutterViewId view_id
Definition: fl_view_accessible.h:49
_FlScrollingManager
Definition: fl_scrolling_manager.cc:11
_FlScrollingManager::zoom_started
gboolean zoom_started
Definition: fl_scrolling_manager.cc:25
_FlScrollingManager::view_id
FlutterViewId view_id
Definition: fl_scrolling_manager.cc:16
_FlScrollingManager::last_x
gdouble last_x
Definition: fl_scrolling_manager.cc:18
_FlScrollingManager::parent_instance
GObject parent_instance
Definition: fl_scrolling_manager.cc:12
_FlScrollingManager::rotation
gdouble rotation
Definition: fl_scrolling_manager.cc:28
_FlScrollingManager::rotate_started
gboolean rotate_started
Definition: fl_scrolling_manager.cc:26
_FlScrollingManager::pan_started
gboolean pan_started
Definition: fl_scrolling_manager.cc:21
_FlScrollingManager::engine
GWeakRef engine
Definition: fl_scrolling_manager.cc:14
_FlScrollingManager::pan_y
gdouble pan_y
Definition: fl_scrolling_manager.cc:23
_FlScrollingManager::scale
gdouble scale
Definition: fl_scrolling_manager.cc:27
_FlScrollingManager::last_y
gdouble last_y
Definition: fl_scrolling_manager.cc:19
_FlScrollingManager::pan_x
gdouble pan_x
Definition: fl_scrolling_manager.cc:22