|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +private class Gala.TouchpadBackend : Object, GestureBackend { |
| 9 | + private const int TOUCHPAD_BASE_HEIGHT = 300; |
| 10 | + private const int TOUCHPAD_BASE_WIDTH = 400; |
| 11 | + private const int DRAG_THRESHOLD_DISTANCE = 16; |
| 12 | + |
| 13 | + private enum State { |
| 14 | + NONE, |
| 15 | + IGNORED, |
| 16 | + IGNORED_HORIZONTAL, |
| 17 | + IGNORED_VERTICAL, |
| 18 | + ONGOING |
| 19 | + } |
| 20 | + |
| 21 | + public Clutter.Actor actor { get; construct; } |
| 22 | + public string? id { get; construct; } |
| 23 | + |
| 24 | + private static List<TouchpadBackend> instances = new List<TouchpadBackend> (); |
| 25 | + private State state = NONE; |
| 26 | + private GestureDirection direction = UNKNOWN; |
| 27 | + private double distance_x = 0; |
| 28 | + private double distance_y = 0; |
| 29 | + private double distance = 0; |
| 30 | + |
| 31 | + public TouchpadBackend (Clutter.Actor actor, string? id) { |
| 32 | + Object (actor: actor, id: id); |
| 33 | + } |
| 34 | + |
| 35 | + ~TouchpadBackend () { |
| 36 | + instances.remove (this); |
| 37 | + } |
| 38 | + |
| 39 | + construct { |
| 40 | + actor.captured_event.connect (on_captured_event); |
| 41 | + |
| 42 | + instances.append (this); |
| 43 | + } |
| 44 | + |
| 45 | + public override void cancel_gesture () { |
| 46 | + state = IGNORED; |
| 47 | + } |
| 48 | + |
| 49 | + private bool on_captured_event (Clutter.Event event) { |
| 50 | + return handle_event (event, true); |
| 51 | + } |
| 52 | + |
| 53 | + private bool handle_event (Clutter.Event event, bool main_handler) { |
| 54 | + if (event.get_type () != TOUCHPAD_SWIPE) { |
| 55 | + return Clutter.EVENT_PROPAGATE; |
| 56 | + } |
| 57 | + |
| 58 | + if (state == IGNORED) { |
| 59 | + if (event.get_gesture_phase () == END || event.get_gesture_phase () == CANCEL) { |
| 60 | + reset (); |
| 61 | + } |
| 62 | + |
| 63 | + return Clutter.EVENT_PROPAGATE; |
| 64 | + } |
| 65 | + |
| 66 | + double delta_x, delta_y; |
| 67 | + event.get_gesture_motion_delta_unaccelerated (out delta_x, out delta_y); |
| 68 | + |
| 69 | + if (state != ONGOING) { |
| 70 | + distance_x += delta_x; |
| 71 | + distance_y += delta_y; |
| 72 | + |
| 73 | + Gesture? gesture = null; |
| 74 | + State state_if_ignored = NONE; |
| 75 | + |
| 76 | + var threshold = main_handler ? DRAG_THRESHOLD_DISTANCE : DRAG_THRESHOLD_DISTANCE * 4; |
| 77 | + |
| 78 | + if (state != IGNORED_HORIZONTAL && distance_x.abs () >= threshold) { |
| 79 | + gesture = new Gesture (); |
| 80 | + gesture.direction = direction = distance_x > 0 ? GestureDirection.RIGHT : GestureDirection.LEFT; |
| 81 | + state_if_ignored = IGNORED_HORIZONTAL; |
| 82 | + } else if (state != IGNORED_VERTICAL && distance_y.abs () >= threshold) { |
| 83 | + gesture = new Gesture (); |
| 84 | + gesture.direction = direction = distance_y > 0 ? GestureDirection.DOWN : GestureDirection.UP; |
| 85 | + state_if_ignored = IGNORED_VERTICAL; |
| 86 | + } else { |
| 87 | + return Clutter.EVENT_PROPAGATE; |
| 88 | + } |
| 89 | + |
| 90 | + gesture.type = event.get_type (); |
| 91 | + gesture.fingers = (int) event.get_touchpad_gesture_finger_count (); |
| 92 | + gesture.performed_on_device_type = event.get_device ().get_device_type (); |
| 93 | + |
| 94 | + if (!on_gesture_detected (gesture, event.get_time ())) { |
| 95 | + if (state == NONE) { |
| 96 | + state = state_if_ignored; |
| 97 | + } else { // Both directions were ignored, so stop trying |
| 98 | + state = IGNORED; |
| 99 | + } |
| 100 | + return Clutter.EVENT_PROPAGATE; |
| 101 | + } |
| 102 | + |
| 103 | + state = ONGOING; |
| 104 | + on_begin (0, event.get_time ()); |
| 105 | + } else if (main_handler && id != null) { |
| 106 | + foreach (var instance in instances) { |
| 107 | + if (instance != this && instance.id == id) { |
| 108 | + instance.handle_event (event, false); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + distance += get_value_for_direction (delta_x, delta_y); |
| 114 | + |
| 115 | + var percentage = get_percentage (distance); |
| 116 | + |
| 117 | + switch (event.get_gesture_phase ()) { |
| 118 | + case BEGIN: |
| 119 | + // We don't rely on the begin phase because we delay activation until the drag threshold is reached |
| 120 | + break; |
| 121 | + |
| 122 | + case UPDATE: |
| 123 | + on_update (percentage, event.get_time ()); |
| 124 | + break; |
| 125 | + |
| 126 | + case END: |
| 127 | + case CANCEL: |
| 128 | + on_end (percentage, event.get_time ()); |
| 129 | + reset (); |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + return Clutter.EVENT_STOP; |
| 134 | + } |
| 135 | + |
| 136 | + private void reset () { |
| 137 | + state = NONE; |
| 138 | + distance = 0; |
| 139 | + direction = UNKNOWN; |
| 140 | + distance_x = 0; |
| 141 | + distance_y = 0; |
| 142 | + } |
| 143 | + |
| 144 | + private double get_percentage (double value) { |
| 145 | + return value / (direction == LEFT || direction == RIGHT ? TOUCHPAD_BASE_WIDTH : TOUCHPAD_BASE_HEIGHT); |
| 146 | + } |
| 147 | + |
| 148 | + private double get_value_for_direction (double delta_x, double delta_y) { |
| 149 | + if (direction == LEFT || direction == RIGHT) { |
| 150 | + return direction == LEFT ? -delta_x : delta_x; |
| 151 | + } else { |
| 152 | + return direction == UP ? -delta_y : delta_y; |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments