| 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* event,
69 gint scale_factor) {
70 gdouble scroll_delta_x = 0.0, scroll_delta_y = 0.0;
71 if (event->direction == GDK_SCROLL_SMOOTH) {
72 scroll_delta_x = event->delta_x;
73 scroll_delta_y = event->delta_y;
74 } else if (event->direction == GDK_SCROLL_UP) {
75 scroll_delta_y = -1;
76 } else if (event->direction == GDK_SCROLL_DOWN) {
77 scroll_delta_y = 1;
78 } else if (event->direction == GDK_SCROLL_LEFT) {
79 scroll_delta_x = -1;
80 } else if (event->direction == GDK_SCROLL_RIGHT) {
81 scroll_delta_x = 1;
82 }
83
84 // The multiplier is taken from the Chromium source
85 // (ui/events/x/events_x_utils.cc).
86 const int kScrollOffsetMultiplier = 53;
87 scroll_delta_x *= kScrollOffsetMultiplier * scale_factor;
88 scroll_delta_y *= kScrollOffsetMultiplier * scale_factor;
89
90 if (gdk_device_get_source(gdk_event_get_source_device(
91 reinterpret_cast<GdkEvent*>(event))) == GDK_SOURCE_TOUCHPAD) {
92 scroll_delta_x *= -1;
93 scroll_delta_y *= -1;
94 if (event->is_stop) {
95 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
96 self->view_delegate, event->time * kMicrosecondsPerMillisecond,
97 event->x * scale_factor, event->y * scale_factor, kPanZoomEnd,
98 self->pan_x, self->pan_y, 0, 0);
99 self->pan_started = FALSE;
100 } else {
101 if (!self->pan_started) {
102 self->pan_x = 0;
103 self->pan_y = 0;
104 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
105 self->view_delegate, event->time * kMicrosecondsPerMillisecond,
106 event->x * scale_factor, event->y * scale_factor, kPanZoomStart, 0,
107 0, 0, 0);
108 self->pan_started = TRUE;
109 }
110 self->pan_x += scroll_delta_x;
111 self->pan_y += scroll_delta_y;
112 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
113 self->view_delegate, event->time * kMicrosecondsPerMillisecond,
114 event->x * scale_factor, event->y * scale_factor, kPanZoomUpdate,
115 self->pan_x, self->pan_y, 1, 0);
116 }
117 } else {
118 self->last_x = event->x * scale_factor;
119 self->last_y = event->y * scale_factor;
120 fl_scrolling_view_delegate_send_mouse_pointer_event(
121 self->view_delegate,
122 FlutterPointerPhase::kMove /* arbitrary value, phase will be ignored as
123 this is a discrete scroll event */
124 ,
125 event->time * kMicrosecondsPerMillisecond, event->x * scale_factor,
126 event->y * scale_factor, scroll_delta_x, scroll_delta_y, 0);
127 }
128 }
129
130 void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager* self) {
131 self->rotate_started = true;
132 if (!self->zoom_started) {
133 self->scale = 1;
134 self->rotation = 0;
135 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
136 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
137 kPanZoomStart, 0, 0, 0, 0);
138 }
139 }
140
141 void fl_scrolling_manager_handle_rotation_update(FlScrollingManager* self,
142 gdouble rotation) {
143 self->rotation = rotation;
144 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
145 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
146 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
147 }
148 void fl_scrolling_manager_handle_rotation_end(FlScrollingManager* self) {
149 self->rotate_started = false;
150 if (!self->zoom_started) {
151 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
152 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
153 kPanZoomEnd, 0, 0, 0, 0);
154 }
155 }
156
157 void fl_scrolling_manager_handle_zoom_begin(FlScrollingManager* self) {
158 self->zoom_started = true;
159 if (!self->rotate_started) {
160 self->scale = 1;
161 self->rotation = 0;
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 kPanZoomStart, 0, 0, 0, 0);
165 }
166 }
167 void fl_scrolling_manager_handle_zoom_update(FlScrollingManager* self,
168 gdouble scale) {
169 self->scale = scale;
170 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
171 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
172 kPanZoomUpdate, 0, 0, self->scale, self->rotation);
173 }
174 void fl_scrolling_manager_handle_zoom_end(FlScrollingManager* self) {
175 self->zoom_started = false;
176 if (!self->rotate_started) {
177 fl_scrolling_view_delegate_send_pointer_pan_zoom_event(
178 self->view_delegate, g_get_real_time(), self->last_x, self->last_y,
179 kPanZoomEnd, 0, 0, 0, 0);
180 }
181 }
_FlKeyEvent::time
guint32 time
Definition: fl_key_event.h:33
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:174
_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:167
_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:141
_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
fl_scrolling_manager_handle_scroll_event
void fl_scrolling_manager_handle_scroll_event(FlScrollingManager *self, GdkEventScroll *event, gint scale_factor)
Definition: fl_scrolling_manager.cc:67
_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:148
fl_scrolling_manager_handle_rotation_begin
void fl_scrolling_manager_handle_rotation_begin(FlScrollingManager *self)
Definition: fl_scrolling_manager.cc:130
_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:157
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