Skip to content

Commit 933cb5d

Browse files
Support forward and back buttons (#164356)
Based on flutter/flutter#163500 by @2bndy5
1 parent 25b3a47 commit 933cb5d

File tree

4 files changed

+285
-91
lines changed

4 files changed

+285
-91
lines changed

engine/src/flutter/shell/platform/linux/fl_pointer_manager.cc

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ struct _FlPointerManager {
2727

2828
G_DEFINE_TYPE(FlPointerManager, fl_pointer_manager, G_TYPE_OBJECT);
2929

30+
// 8 corresponds to mouse back button on both x11 and wayland
31+
static constexpr guint kMouseButtonBack = 8;
32+
33+
// 9 corresponds to mouse forward button on both x11 and wayland
34+
static constexpr guint kMouseButtonForward = 9;
35+
36+
// Convert a GDK button ID into a Flutter button ID
37+
static gboolean get_mouse_button(guint gdk_button, int64_t* button) {
38+
switch (gdk_button) {
39+
case GDK_BUTTON_PRIMARY:
40+
*button = kFlutterPointerButtonMousePrimary;
41+
return TRUE;
42+
case GDK_BUTTON_MIDDLE:
43+
*button = kFlutterPointerButtonMouseMiddle;
44+
return TRUE;
45+
case GDK_BUTTON_SECONDARY:
46+
*button = kFlutterPointerButtonMouseSecondary;
47+
return TRUE;
48+
case kMouseButtonBack:
49+
*button = kFlutterPointerButtonMouseBack;
50+
return TRUE;
51+
case kMouseButtonForward:
52+
*button = kFlutterPointerButtonMouseForward;
53+
return TRUE;
54+
default:
55+
return FALSE;
56+
}
57+
}
58+
3059
// Generates a mouse pointer event if the pointer appears inside the window.
3160
static void ensure_pointer_added(FlPointerManager* self,
3261
guint event_time,
@@ -79,9 +108,14 @@ gboolean fl_pointer_manager_handle_button_press(
79108
FlutterPointerDeviceKind device_kind,
80109
gdouble x,
81110
gdouble y,
82-
int64_t button) {
111+
guint gdk_button) {
83112
g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
84113

114+
int64_t button;
115+
if (!get_mouse_button(gdk_button, &button)) {
116+
return FALSE;
117+
}
118+
85119
ensure_pointer_added(self, event_time, device_kind, x, y);
86120

87121
// Drop the event if Flutter already thinks the button is down.
@@ -112,9 +146,14 @@ gboolean fl_pointer_manager_handle_button_release(
112146
FlutterPointerDeviceKind device_kind,
113147
gdouble x,
114148
gdouble y,
115-
int64_t button) {
149+
guint gdk_button) {
116150
g_return_val_if_fail(FL_IS_POINTER_MANAGER(self), FALSE);
117151

152+
int64_t button;
153+
if (!get_mouse_button(gdk_button, &button)) {
154+
return FALSE;
155+
}
156+
118157
// Drop the event if Flutter already thinks the button is up.
119158
if ((self->button_state & button) == 0) {
120159
return FALSE;

engine/src/flutter/shell/platform/linux/fl_pointer_manager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ FlPointerManager* fl_pointer_manager_new(FlutterViewId view_id,
3535
* @device_kind: kind of device generating the event.
3636
* @x: x co-ordinate of event.
3737
* @y: y co-ordinate of event.
38-
* @button: button being pressed.
38+
* @gdk_button: button being pressed.
3939
*
4040
* Returns %TRUE if this event was handled.
4141
*/
@@ -45,7 +45,7 @@ gboolean fl_pointer_manager_handle_button_press(
4545
FlutterPointerDeviceKind device_kind,
4646
gdouble x,
4747
gdouble y,
48-
int64_t button);
48+
guint gdk_button);
4949

5050
/**
5151
* fl_pointer_manager_handle_button_release:
@@ -54,7 +54,7 @@ gboolean fl_pointer_manager_handle_button_press(
5454
* @device_kind: kind of device generating the event.
5555
* @x: x co-ordinate of event.
5656
* @y: y co-ordinate of event.
57-
* @button: button being released.
57+
* @gdk_button: button being released.
5858
*
5959
* Returns %TRUE if this event was handled.
6060
*/
@@ -64,7 +64,7 @@ gboolean fl_pointer_manager_handle_button_release(
6464
FlutterPointerDeviceKind device_kind,
6565
gdouble x,
6666
gdouble y,
67-
int64_t button);
67+
guint gdk_button);
6868

6969
/**
7070
* fl_pointer_manager_handle_motion:

0 commit comments

Comments
 (0)