Skip to content

Commit a163cfa

Browse files
committed
Pass a size for exact finger tracking
1 parent c5af5b9 commit a163cfa

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

lib/Utils.vala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,31 @@
1717

1818
namespace Gala {
1919
public class Utils {
20+
/**
21+
* Represents a size as an object in order for it to be updatable.
22+
* Additionally it has some utilities like {@link Gala.Utils.Size.actor_tracking}.
23+
*/
24+
public class Size {
25+
public float width;
26+
public float height;
27+
28+
public Size (float width, float height) {
29+
this.width = width;
30+
this.height = height;
31+
}
32+
33+
/**
34+
* Constructs a new size that will keep in sync with the width and height of an actor.
35+
*/
36+
public Size.actor_tracking (Clutter.Actor actor) {
37+
actor.notify["width"].connect ((obj, pspec) => width = ((Clutter.Actor) obj).width);
38+
actor.notify["height"].connect ((obj, pspec) => height = ((Clutter.Actor) obj).height);
39+
40+
width = actor.width;
41+
height = actor.height;
42+
}
43+
}
44+
2045
private struct CachedIcon {
2146
public Gdk.Pixbuf icon;
2247
public int icon_size;

src/Gestures/GestureTracker.vala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,15 @@ public class Gala.GestureTracker : Object {
147147
/**
148148
* Allow to receive pan gestures.
149149
* @param actor Clutter actor that will receive the events.
150+
* @param travel_distances {@link Utils.Size} with width and height. The given size wil be
151+
* used to calculate the percentage. It should be set to the amount something will travel (e.g.
152+
* when moving an actor) based on the gesture to allow exact finger tracking. It can also be used
153+
* to calculate the raw pixels the finger travelled at a given time with percentage * corresponding distance
154+
* (height for {@link GestureDirection.UP} or DOWN, width for LEFT or RIGHT). If set to null the size of the
155+
* actor will be used. If the values change those changes will apply.
150156
*/
151-
public void enable_pan (Clutter.Actor actor) {
152-
pan_backend = new PanBackend (actor);
157+
public void enable_pan (Clutter.Actor actor, Utils.Size? travel_distances) {
158+
pan_backend = new PanBackend (actor, travel_distances);
153159
pan_backend.on_gesture_detected.connect (gesture_detected);
154160
pan_backend.on_begin.connect (gesture_begin);
155161
pan_backend.on_update.connect (gesture_update);

src/Gestures/PanBackend.vala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class Gala.PanBackend : Object {
55
public signal void on_end (double delta, uint64 time);
66

77
public Clutter.Actor actor { get; construct; }
8-
public GestureSettings settings { get; construct; }
8+
public Utils.Size? travel_distances { get; construct; }
99

1010
private Clutter.PanAxis pan_axis;
1111
private Clutter.PanAction pan_action;
@@ -15,8 +15,8 @@ public class Gala.PanBackend : Object {
1515
private float origin_x;
1616
private float origin_y;
1717

18-
public PanBackend (Clutter.Actor actor) {
19-
Object (actor: actor);
18+
public PanBackend (Clutter.Actor actor, Utils.Size? travel_distances) {
19+
Object (actor: actor, travel_distances: travel_distances);
2020
}
2121

2222
construct {
@@ -69,16 +69,18 @@ public class Gala.PanBackend : Object {
6969
}
7070

7171
private double calculate_percentage (float current_x, float current_y) {
72-
float current, origin;
72+
float current, origin, size;
7373
if (pan_axis == X_AXIS) {
7474
current = current_x;
7575
origin = origin_x;
76+
size = travel_distances != null ? travel_distances.width : actor.width;
7677
} else {
7778
current = current_y;
7879
origin = origin_y;
80+
size = travel_distances != null ? travel_distances.height : actor.height;
7981
}
8082

81-
return (current - origin).abs () / actor.get_width ();
83+
return (current - origin).abs () / size;
8284
}
8385

8486
private Gesture build_gesture () {

0 commit comments

Comments
 (0)