-
-
Notifications
You must be signed in to change notification settings - Fork 77
Implement a TouchpadBackend #2497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| /* | ||
| * Copyright 2025 elementary, Inc. (https://elementary.io) | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| * | ||
| * Authored by: Leonhard Kargl <[email protected]> | ||
| */ | ||
|
|
||
| private class Gala.TouchpadBackend : Object, GestureBackend { | ||
| private const int TOUCHPAD_BASE_HEIGHT = 300; | ||
| private const int TOUCHPAD_BASE_WIDTH = 400; | ||
| private const int DRAG_THRESHOLD_DISTANCE = 16; | ||
|
|
||
| private enum State { | ||
| NONE, | ||
| IGNORED, | ||
| IGNORED_HORIZONTAL, | ||
| IGNORED_VERTICAL, | ||
| ONGOING | ||
| } | ||
|
|
||
| public Clutter.Actor actor { get; construct; } | ||
| public string? id { get; construct; } | ||
|
|
||
| private static List<TouchpadBackend> instances = new List<TouchpadBackend> (); | ||
| private State state = NONE; | ||
| private GestureDirection direction = UNKNOWN; | ||
| private double distance_x = 0; | ||
| private double distance_y = 0; | ||
| private double distance = 0; | ||
|
|
||
| public TouchpadBackend (Clutter.Actor actor, string? id) { | ||
| Object (actor: actor, id: id); | ||
| } | ||
|
|
||
| ~TouchpadBackend () { | ||
| instances.remove (this); | ||
| } | ||
|
|
||
| construct { | ||
| actor.captured_event.connect (on_captured_event); | ||
|
|
||
| instances.append (this); | ||
| } | ||
|
|
||
| public override void cancel_gesture () { | ||
| state = IGNORED; | ||
| } | ||
|
|
||
| private bool on_captured_event (Clutter.Event event) { | ||
| return handle_event (event, true); | ||
| } | ||
|
|
||
| private bool handle_event (Clutter.Event event, bool main_handler) { | ||
| if (event.get_type () != TOUCHPAD_SWIPE) { | ||
| return Clutter.EVENT_PROPAGATE; | ||
| } | ||
|
|
||
| if (state == IGNORED) { | ||
| if (event.get_gesture_phase () == END || event.get_gesture_phase () == CANCEL) { | ||
| reset (); | ||
| } | ||
|
|
||
| return Clutter.EVENT_PROPAGATE; | ||
| } | ||
|
|
||
| double delta_x, delta_y; | ||
| event.get_gesture_motion_delta_unaccelerated (out delta_x, out delta_y); | ||
|
|
||
| if (state != ONGOING) { | ||
| distance_x += delta_x; | ||
| distance_y += delta_y; | ||
|
|
||
| Gesture? gesture = null; | ||
| State state_if_ignored = NONE; | ||
|
|
||
| var threshold = main_handler ? DRAG_THRESHOLD_DISTANCE : DRAG_THRESHOLD_DISTANCE * 4; | ||
|
|
||
| if (state != IGNORED_HORIZONTAL && distance_x.abs () >= threshold) { | ||
| gesture = new Gesture (); | ||
| gesture.direction = direction = distance_x > 0 ? GestureDirection.RIGHT : GestureDirection.LEFT; | ||
| state_if_ignored = IGNORED_HORIZONTAL; | ||
| } else if (state != IGNORED_VERTICAL && distance_y.abs () >= threshold) { | ||
| gesture = new Gesture (); | ||
| gesture.direction = direction = distance_y > 0 ? GestureDirection.DOWN : GestureDirection.UP; | ||
| state_if_ignored = IGNORED_VERTICAL; | ||
| } else { | ||
| return Clutter.EVENT_PROPAGATE; | ||
| } | ||
|
|
||
| gesture.type = event.get_type (); | ||
| gesture.fingers = (int) event.get_touchpad_gesture_finger_count (); | ||
| gesture.performed_on_device_type = event.get_device ().get_device_type (); | ||
|
|
||
| if (!on_gesture_detected (gesture, event.get_time ())) { | ||
| if (state == NONE) { | ||
| state = state_if_ignored; | ||
| } else { // Both directions were ignored, so stop trying | ||
| state = IGNORED; | ||
| } | ||
| return Clutter.EVENT_PROPAGATE; | ||
| } | ||
|
|
||
| state = ONGOING; | ||
| on_begin (0, event.get_time ()); | ||
| } else if (main_handler && id != null) { | ||
| foreach (var instance in instances) { | ||
| if (instance != this && instance.id == id) { | ||
| instance.handle_event (event, false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| distance += get_value_for_direction (delta_x, delta_y); | ||
|
|
||
| var percentage = get_percentage (distance); | ||
|
|
||
| switch (event.get_gesture_phase ()) { | ||
| case BEGIN: | ||
| // We don't rely on the begin phase because we delay activation until the drag threshold is reached | ||
| break; | ||
|
|
||
| case UPDATE: | ||
| on_update (percentage, event.get_time ()); | ||
| break; | ||
|
|
||
| case END: | ||
| case CANCEL: | ||
| on_end (percentage, event.get_time ()); | ||
| reset (); | ||
| break; | ||
| } | ||
|
|
||
| return Clutter.EVENT_STOP; | ||
| } | ||
|
|
||
| private void reset () { | ||
| state = NONE; | ||
| distance = 0; | ||
| direction = UNKNOWN; | ||
| distance_x = 0; | ||
| distance_y = 0; | ||
| } | ||
|
|
||
| private double get_percentage (double value) { | ||
| return value / (direction == LEFT || direction == RIGHT ? TOUCHPAD_BASE_WIDTH : TOUCHPAD_BASE_HEIGHT); | ||
| } | ||
|
|
||
| private double get_value_for_direction (double delta_x, double delta_y) { | ||
| if (direction == LEFT || direction == RIGHT) { | ||
| return direction == LEFT ? -delta_x : delta_x; | ||
| } else { | ||
| return direction == UP ? -delta_y : delta_y; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.