|
| 1 | +/* |
| 2 | + * Copyright 2024 elementary, Inc. (https://elementary.io) |
| 3 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + * |
| 5 | + * Authored by: Leonhard Kargl <[email protected]> |
| 6 | + */ |
| 7 | + |
| 8 | +public class Gala.PanBackend : Object { |
| 9 | + public signal bool on_gesture_detected (Gesture gesture); |
| 10 | + public signal void on_begin (double delta, uint64 time); |
| 11 | + public signal void on_update (double delta, uint64 time); |
| 12 | + public signal void on_end (double delta, uint64 time); |
| 13 | + |
| 14 | + public Clutter.Actor actor { get; construct; } |
| 15 | + public Utils.Size? travel_distances { get; construct; } |
| 16 | + |
| 17 | + private Clutter.PanAxis pan_axis; |
| 18 | + private Clutter.PanAction pan_action; |
| 19 | + |
| 20 | + private GestureDirection direction; |
| 21 | + |
| 22 | + private float origin_x; |
| 23 | + private float origin_y; |
| 24 | + |
| 25 | + public PanBackend (Clutter.Actor actor, Utils.Size? travel_distances) { |
| 26 | + Object (actor: actor, travel_distances: travel_distances); |
| 27 | + } |
| 28 | + |
| 29 | + construct { |
| 30 | + pan_action = new Clutter.PanAction () { |
| 31 | + n_touch_points = 1 |
| 32 | + }; |
| 33 | + |
| 34 | + actor.add_action_full ("pan-gesture", CAPTURE, pan_action); |
| 35 | + |
| 36 | + pan_action.gesture_begin.connect (on_gesture_begin); |
| 37 | + pan_action.pan.connect (on_pan); |
| 38 | + pan_action.gesture_end.connect (on_gesture_end); |
| 39 | + } |
| 40 | + |
| 41 | + private bool on_gesture_begin () { |
| 42 | + float x_coord, y_coord; |
| 43 | + pan_action.get_press_coords (0, out x_coord, out y_coord); |
| 44 | + |
| 45 | + origin_x = x_coord; |
| 46 | + origin_y = y_coord; |
| 47 | + |
| 48 | + var handled = on_gesture_detected (build_gesture ()); |
| 49 | + |
| 50 | + if (!handled) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + on_begin (0, pan_action.get_last_event (0).get_time ()); |
| 55 | + |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + private void on_gesture_end () { |
| 60 | + float x_coord, y_coord; |
| 61 | + pan_action.get_motion_coords (0, out x_coord, out y_coord); |
| 62 | + on_end (calculate_percentage (x_coord, y_coord), pan_action.get_last_event (0).get_time ()); |
| 63 | + |
| 64 | + direction = GestureDirection.UNKNOWN; |
| 65 | + } |
| 66 | + |
| 67 | + private bool on_pan (Clutter.PanAction pan_action, Clutter.Actor actor, bool interpolate) { |
| 68 | + uint64 time = pan_action.get_last_event (0).get_time (); |
| 69 | + |
| 70 | + float x_coord, y_coord; |
| 71 | + pan_action.get_motion_coords (0, out x_coord, out y_coord); |
| 72 | + |
| 73 | + on_update (calculate_percentage (x_coord, y_coord), time); |
| 74 | + |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + private double calculate_percentage (float current_x, float current_y) { |
| 79 | + float current, origin, size; |
| 80 | + if (pan_axis == X_AXIS) { |
| 81 | + current = direction == RIGHT ? float.max (current_x, origin_x) : float.min (current_x, origin_x); |
| 82 | + origin = origin_x; |
| 83 | + size = travel_distances != null ? travel_distances.width : actor.width; |
| 84 | + } else { |
| 85 | + current = direction == DOWN ? float.max (current_y, origin_y) : float.min (current_y, origin_y); |
| 86 | + origin = origin_y; |
| 87 | + size = travel_distances != null ? travel_distances.height : actor.height; |
| 88 | + } |
| 89 | + |
| 90 | + return (current - origin).abs () / size; |
| 91 | + } |
| 92 | + |
| 93 | + private Gesture build_gesture () { |
| 94 | + float delta_x, delta_y; |
| 95 | + ((Clutter.GestureAction) pan_action).get_motion_delta (0, out delta_x, out delta_y); |
| 96 | + |
| 97 | + pan_axis = delta_x.abs () > delta_y.abs () ? Clutter.PanAxis.X_AXIS : Clutter.PanAxis.Y_AXIS; |
| 98 | + |
| 99 | + if (pan_axis == X_AXIS) { |
| 100 | + direction = delta_x > 0 ? GestureDirection.RIGHT : GestureDirection.LEFT; |
| 101 | + } else { |
| 102 | + direction = delta_y > 0 ? GestureDirection.DOWN : GestureDirection.UP; |
| 103 | + } |
| 104 | + |
| 105 | + return new Gesture () { |
| 106 | + type = Clutter.EventType.TOUCHPAD_SWIPE, |
| 107 | + direction = direction, |
| 108 | + fingers = (int) pan_action.get_n_current_points (), |
| 109 | + performed_on_device_type = Clutter.InputDeviceType.TOUCHSCREEN_DEVICE, |
| 110 | + origin_x = origin_x, |
| 111 | + origin_y = origin_y |
| 112 | + }; |
| 113 | + } |
| 114 | +} |
0 commit comments