| 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
7 static constexpr int kMicrosecondsPerMillisecond = 1000;
8
9 struct _FlScrollingManager {
10 GObject parent_instance;
11
12 FlScrollingViewDelegate* view_delegate;
13
14 gdouble last_x;
15 gdouble last_y;
16
17 gboolean pan_started;
18 gdouble pan_x;
19 gdouble pan_y;
20
21 gboolean zoom_started;
22 gboolean rotate_started;
23 gdouble scale;
24 gdouble rotation;
25 };
26
27 G_DEFINE_TYPE(FlScrollingManager, fl_scrolling_manager, G_TYPE_OBJECT);
28
29 static void fl_scrolling_manager_dispose(GObject* object);
30
31 static void fl_scrolling_manager_class_init(FlScrollingManagerClass* klass) {
32 G_OBJECT_CLASS(klass)->dispose = fl_scrolling_manager_dispose;
33 }
34
35 static void fl_scrolling_manager_init(FlScrollingManager* self) {}
36
37 static void fl_scrolling_manager_dispose(GObject* object) {
38 G_OBJECT_CLASS(fl_scrolling_manager_parent_class)->dispose(object);
39 }
40
41 FlScrollingManager* fl_scrolling_manager_new(
42 FlScrollingViewDelegate* view_delegate) {
43 g_return_val_if_fail(FL_IS_SCROLLING_VIEW_DELEGATE(view_delegate), nullptr);
44
45 FlScrollingManager* self = FL_SCROLLING_MANAGER(
46 g_object_new(fl_scrolling_manager_get_type(), nullptr));
47
48 self->view_delegate = view_delegate;
49 g_object_add_weak_pointer(
50 G_OBJECT(view_delegate),
51 reinterpret_cast<gpointer*>(&(self->view_delegate)));
52
53 self->pan_started = false;
54 self->zoom_started = false;
55 self->rotate_started = false;
56
57 return self;
58 }
59
60 void fl_scrolling_manager_set_last_mouse_position(FlScrollingManager* self,
61 gdouble x,
62 gdouble y) {
63 self->last_x = x;
64 self->last_y = y;
65 }
66
67 void fl_scrolling_manager_handle_scroll_event(FlScrollingManager* self,
68 GdkEventScroll* scroll_event,
69 gint scale_factor) {
70 GdkEvent* event = reinterpret_cast<GdkEvent*>(scroll_event);
71
72 guint event_time = gdk_event_get_time(event);
73 gdouble event_x = 0.0, event_y = 0.0;
74 gdk_event_get_coords(event, &event_x, &event_y);
75 gdouble scroll_delta_x = 0.0, scroll_delta_y = 0.0;
76 GdkScrollDirection event_direction = GDK_SCROLL_SMOOTH;
77 if (gdk_event_get_scroll_direction(event, &event_direction)) {
78 if (event_direction == GDK_SCROLL_UP) {
79 scroll_delta_x = 0;
80 scroll_delta_y = -1;
81 } else if (event_direction == GDK_SCROLL_DOWN) {
82 scroll_delta_x = 0;
83 scroll_delta_y = 1;
84 } else if (event_direction == GDK_SCROLL_LEFT) {
85 scroll_delta_x = -1;
86 scroll_delta_y = 0;
87 } else if (event_direction == GDK_SCROLL_RIGHT) {
88 scroll_delta_x = 1;
89 scroll_delta_y = 0;
90 }
91 } else {
92 gdk_event_get_scroll_deltas(event, &scroll_delta_x, &scroll_delta_y);
93 }
94
95 // The multiplier is taken from the Chromium source
96 // (ui/events/x/events_x_utils.cc).
97 const int kScrollOffsetMultiplier = 53;
98 scroll_delta_x *= kScrollOffsetMultiplier * scale_factor;
99 scroll_delta_y *= kScrollOffsetMultiplier * scale_factor;
100
101 if (gdk_device_get_source(gdk_event_get_source_device(event)) ==
102 GDK_SOURCE_TOUCHPAD) {
103 scroll_delta_x *= -1;
104 scroll_delta_y *= -1;
105 if (gdk_event_is_scroll_stop_event(event)) {
106 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
107 self->view_delegate, event_time * kMicrosecondsPerMillisecond,
108 event_x * scale_factor, event_y * scale_factor, kPanZoomEnd,
109 self->pan_x, self->pan_y, 0, 0);
110 self->pan_started = FALSE;
111 } else {
112 if (!self->pan_started) {
113 self->pan_x = 0;
114 self->pan_y = 0;
115 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
116 self->view_delegate, event_time * kMicrosecondsPerMillisecond,
117 event_x * scale_factor, event_y * scale_factor, kPanZoomStart, 0, 0,
118 0, 0);
119 self->pan_started = TRUE;
120 }
121 self->pan_x += scroll_delta_x;
122 self->pan_y += scroll_delta_y;
123 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
124 self->view_delegate, event_time * kMicrosecondsPerMillisecond,
125 event_x * scale_factor, event_y * scale_factor, kPanZoomUpdate,
126 self->pan_x, self->pan_y, 1, 0);
127 }
128 } else {
129 self->last_x = event_x * scale_factor;
130 self->last_y = event_y * scale_factor;
131 fl_scrolling_view_delegate_send_mouse_pointer_event(
132 self->view_delegate,
133 FlutterPointerPhase::kMove /* arbitrary value, phase will be ignored as
134 this is a discrete scroll event */
135 ,
136 event_time * kMicrosecondsPerMillisecond, event_x * scale_factor,
137 event_y * scale_factor, scroll_delta_x, scroll_delta_y, 0);
138 }
139 }
140
141 void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager* self) {
142 self->rotate_started = true;
143 if (!self->zoom_started) {
144 self->scale = 1;
145 self->rotation = 0;
146 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
147 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
148 kPanZoomStart, 0, 0, 0, 0);
149 }
150 }
151
152 void fl_scrolling_manager_handle_rotation_update(FlScrollingManager* self,
153 gdouble rotation) {
154 self->rotation = rotation;
155 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
156 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
157 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
158 }
159 void fl_scrolling_manager_handle_rotation_end(FlScrollingManager* self) {
160 self->rotate_started = false;
161 if (!self->zoom_started) {
162 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
163 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
164 kPanZoomEnd, 0, 0, 0, 0);
165 }
166 }
167
168 void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager* self) {
169 self->zoom_started = true;
170 if (!self->rotate_started) {
171 self->scale = 1;
172 self->rotation = 0;
173 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
174 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
175 kPanZoomStart, 0, 0, 0, 0);
176 }
177 }
178 void fl_scrolling_manager_handle_zoom_update(FlScrollingManager* self,
179 gdouble scale) {
180 self->scale = scale;
181 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
182 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
183 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
184 }
185 void fl_scrolling_manager_handle_zoom_end(FlScrollingManager* self) {
186 self->zoom_started = false;
187 if (!self->rotate_started) {
188 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
189 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
190 kPanZoomEnd, 0, 0, 0, 0);
191 }
192 }
event
FlKeyEvent * event
Definition: fl_key_channel_responder.cc:118
_FlScrollingManager
Definition: fl_scrolling_manager.cc:9
fl_scrolling_manager_handle_zoom_end
void fl_scrolling_manager_handle_zoom_end(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:185
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:67
_FlScrollingManager::pan_x
gdouble pan_x
Definition: fl_scrolling_manager.cc:18
_FlScrollingManager::pan_y
gdouble pan_y
Definition: fl_scrolling_manager.cc:19
_FlScrollingManager::zoom_started
gboolean zoom_started
Definition: fl_scrolling_manager.cc:21
fl_scrolling_manager_class_init
static void fl_scrolling_manager_class_init(FlScrollingManagerClass *klass)
Definition: fl_scrolling_manager.cc:31
G_DEFINE_TYPE
G_DEFINE_TYPE(FlScrollingManager, fl_scrolling_manager, G_TYPE_OBJECT)
fl_scrolling_manager_dispose
static void fl_scrolling_manager_dispose(GObject *object)
Definition: fl_scrolling_manager.cc:37
fl_scrolling_manager_new
FlScrollingManager * fl_scrolling_manager_new(FlScrollingViewDelegate *view_delegate)
Definition: fl_scrolling_manager.cc:41
_FlScrollingManager::scale
gdouble scale
Definition: fl_scrolling_manager.cc:23
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:60
_FlScrollingManager::pan_started
gboolean pan_started
Definition: fl_scrolling_manager.cc:17
_FlScrollingManager::rotation
gdouble rotation
Definition: fl_scrolling_manager.cc:24
fl_scrolling_manager.h
TRUE
return TRUE
Definition: fl_pixel_buffer_texture_test.cc:53
fl_scrolling_manager_handle_zoom_update
void fl_scrolling_manager_handle_zoom_update(FlScrollingManager *self, gdouble scale)
Definition: fl_scrolling_manager.cc:178
_FlScrollingManager::view_delegate
FlScrollingViewDelegate * view_delegate
Definition: fl_scrolling_manager.cc:12
g_object_add_weak_pointer
g_object_add_weak_pointer(G_OBJECT(self), reinterpret_cast< gpointer * >(&self->engine))
fl_scrolling_manager_init
static void fl_scrolling_manager_init(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:35
fl_scrolling_manager_handle_rotation_update
void fl_scrolling_manager_handle_rotation_update(FlScrollingManager *self, gdouble rotation)
Definition: fl_scrolling_manager.cc:152
_FlScrollingManager::parent_instance
GObject parent_instance
Definition: fl_scrolling_manager.cc:10
fl_scrolling_view_delegate_send_mouse_pointer_event
void fl_scrolling_view_delegate_send_mouse_pointer_event(FlScrollingViewDelegate *self, FlutterPointerPhase phase, size_t timestamp, double x, double y, double scroll_delta_x, double scroll_delta_y, int64_t buttons)
Definition: fl_scrolling_view_delegate.cc:14
_FlScrollingManager::last_x
gdouble last_x
Definition: fl_scrolling_manager.cc:14
self
GdkEvent FlView * self
Definition: fl_view.cc:100
_FlScrollingManager::rotate_started
gboolean rotate_started
Definition: fl_scrolling_manager.cc:22
kMicrosecondsPerMillisecond
static constexpr int kMicrosecondsPerMillisecond
Definition: fl_scrolling_manager.cc:7
fl_scrolling_manager_handle_rotation_end
void fl_scrolling_manager_handle_rotation_end(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:159
fl_scrolling_manager_handle_rotation_begin
void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:141
_FlScrollingManager::last_y
gdouble last_y
Definition: fl_scrolling_manager.cc:15
fl_scrolling_manager_handle_zoom_begin
void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:168
fl_scrolling_view_delegate_send_pointer_pan_zoom_event
void fl_scrolling_view_delegate_send_pointer_pan_zoom_event(FlScrollingViewDelegate *self, size_t timestamp, double x, double y, FlutterPointerPhase phase, double pan_x, double pan_y, double scale, double rotation)
Definition: fl_scrolling_view_delegate.cc:28