|
| 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, GestureBackend { |
| 9 | + public delegate float GetTravelDistance (); |
| 10 | + |
| 11 | + public WindowManager wm { get; construct; } |
| 12 | + public Clutter.Actor actor { get; construct; } |
| 13 | + |
| 14 | + private GetTravelDistance get_travel_distance_func; |
| 15 | + |
| 16 | + private ModalProxy? modal_proxy; |
| 17 | + |
| 18 | + private Clutter.PanAxis pan_axis; |
| 19 | + private Clutter.PanAction pan_action; |
| 20 | + |
| 21 | + private GestureDirection direction; |
| 22 | + |
| 23 | + private float origin_x; |
| 24 | + private float origin_y; |
| 25 | + |
| 26 | + private float current_x; |
| 27 | + private float current_y; |
| 28 | + |
| 29 | + private float last_x_coord; |
| 30 | + private float last_y_coord; |
| 31 | + private uint last_n_points; |
| 32 | + |
| 33 | + private float travel_distance; |
| 34 | + |
| 35 | + public PanBackend (WindowManager wm, Clutter.Actor actor, owned GetTravelDistance get_travel_distance_func) { |
| 36 | + Object (wm: wm, actor: actor); |
| 37 | + |
| 38 | + this.get_travel_distance_func = (owned) get_travel_distance_func; |
| 39 | + } |
| 40 | + |
| 41 | + construct { |
| 42 | + pan_action = new Clutter.PanAction () { |
| 43 | + n_touch_points = 1 |
| 44 | + }; |
| 45 | + |
| 46 | + actor.add_action_full ("pan-gesture", CAPTURE, pan_action); |
| 47 | + |
| 48 | + pan_action.gesture_begin.connect (on_gesture_begin); |
| 49 | + pan_action.pan.connect (on_pan); |
| 50 | + pan_action.gesture_end.connect (on_gesture_end); |
| 51 | + pan_action.gesture_cancel.connect (on_gesture_end); |
| 52 | + } |
| 53 | + |
| 54 | + ~PanBackend () { |
| 55 | + actor.remove_action (pan_action); |
| 56 | + } |
| 57 | + |
| 58 | + private bool on_gesture_begin () { |
| 59 | + float x_coord, y_coord; |
| 60 | + pan_action.get_press_coords (0, out x_coord, out y_coord); |
| 61 | + |
| 62 | + origin_x = current_x = x_coord; |
| 63 | + origin_y = current_y = y_coord; |
| 64 | + |
| 65 | + var time = pan_action.get_last_event (0).get_time (); |
| 66 | + |
| 67 | + var handled = on_gesture_detected (build_gesture (), time); |
| 68 | + |
| 69 | + if (!handled) { |
| 70 | + reset (); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + travel_distance = get_travel_distance_func (); |
| 75 | + |
| 76 | + on_begin (0, time); |
| 77 | + |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + public override void prepare_gesture_handling () { |
| 82 | + modal_proxy = wm.push_modal (actor); |
| 83 | + } |
| 84 | + |
| 85 | + private void on_gesture_end () { |
| 86 | + on_end (calculate_percentage (), Meta.CURRENT_TIME); |
| 87 | + |
| 88 | + reset (); |
| 89 | + } |
| 90 | + |
| 91 | + private void reset () { |
| 92 | + if (modal_proxy != null) { |
| 93 | + wm.pop_modal (modal_proxy); |
| 94 | + modal_proxy = null; |
| 95 | + } |
| 96 | + |
| 97 | + direction = GestureDirection.UNKNOWN; |
| 98 | + last_n_points = 0; |
| 99 | + last_x_coord = 0; |
| 100 | + last_y_coord = 0; |
| 101 | + } |
| 102 | + |
| 103 | + private bool on_pan (Clutter.PanAction pan_action, Clutter.Actor actor, bool interpolate) { |
| 104 | + var time = pan_action.get_last_event (0).get_time (); |
| 105 | + |
| 106 | + float x, y; |
| 107 | + pan_action.get_motion_coords (0, out x, out y); |
| 108 | + |
| 109 | + if (pan_action.get_n_current_points () == last_n_points) { |
| 110 | + current_x += x - last_x_coord; |
| 111 | + current_y += y - last_y_coord; |
| 112 | + } |
| 113 | + |
| 114 | + last_x_coord = x; |
| 115 | + last_y_coord = y; |
| 116 | + last_n_points = pan_action.get_n_current_points (); |
| 117 | + |
| 118 | + on_update (calculate_percentage (), time); |
| 119 | + |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + private double calculate_percentage () { |
| 124 | + float current, origin; |
| 125 | + if (pan_axis == X_AXIS) { |
| 126 | + current = direction == RIGHT ? float.max (current_x, origin_x) : float.min (current_x, origin_x); |
| 127 | + origin = origin_x; |
| 128 | + } else { |
| 129 | + current = direction == DOWN ? float.max (current_y, origin_y) : float.min (current_y, origin_y); |
| 130 | + origin = origin_y; |
| 131 | + } |
| 132 | + |
| 133 | + return (current - origin).abs () / travel_distance; |
| 134 | + } |
| 135 | + |
| 136 | + private Gesture build_gesture () { |
| 137 | + float delta_x, delta_y; |
| 138 | + ((Clutter.GestureAction) pan_action).get_motion_delta (0, out delta_x, out delta_y); |
| 139 | + |
| 140 | + pan_axis = delta_x.abs () > delta_y.abs () ? Clutter.PanAxis.X_AXIS : Clutter.PanAxis.Y_AXIS; |
| 141 | + |
| 142 | + if (pan_axis == X_AXIS) { |
| 143 | + direction = delta_x > 0 ? GestureDirection.RIGHT : GestureDirection.LEFT; |
| 144 | + } else { |
| 145 | + direction = delta_y > 0 ? GestureDirection.DOWN : GestureDirection.UP; |
| 146 | + } |
| 147 | + |
| 148 | + return new Gesture () { |
| 149 | + type = Clutter.EventType.TOUCHPAD_SWIPE, |
| 150 | + direction = direction, |
| 151 | + fingers = (int) pan_action.get_n_current_points (), |
| 152 | + performed_on_device_type = Clutter.InputDeviceType.TOUCHSCREEN_DEVICE, |
| 153 | + origin_x = origin_x, |
| 154 | + origin_y = origin_y |
| 155 | + }; |
| 156 | + } |
| 157 | +} |
0 commit comments