Simplify PointerAccelerationProfiles by making reusedObject never null

This commit is contained in:
Albert Vaca Cintora
2025-12-06 21:29:48 +01:00
parent 4af419ed04
commit 0a28c6bb30
4 changed files with 32 additions and 50 deletions

View File

@@ -72,7 +72,7 @@ public class MousePadActivity
private MousePadGestureDetector mMousePadGestureDetector;
private PointerAccelerationProfile mPointerAccelerationProfile;
private PointerAccelerationProfile.MouseDelta mouseDelta; // to be reused on every touch move event
private PointerAccelerationProfile.MouseDelta mouseDelta = new PointerAccelerationProfile.MouseDelta(); // to be reused on every touch move event
private KeyListenerView keyListenerView;

View File

@@ -1,35 +0,0 @@
/*
* SPDX-FileCopyrightText: 2018 Chansol Yang <CosmicSubspace@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Plugins.MousePadPlugin;
/* Base class for a pointer acceleration profile. */
public abstract class PointerAccelerationProfile {
/* Class representing a mouse delta, a pair of floats.*/
static class MouseDelta {
public float x, y;
MouseDelta() {
this(0,0);
}
MouseDelta(float x, float y) {
this.x = x;
this.y = y;
}
}
/* Touch coordinate deltas are fed through this method. */
public abstract void touchMoved(float deltaX, float deltaY, long eventTime);
/* An acceleration profile should 'commit' the processed delta when this method is called.
* The value returned here will be directly sent to the desktop client.
*
* A MouseDelta object can be provided by the caller (or it can be null);
* if not null, subclasses should use and return this object, to reduce object allocations.*/
public abstract MouseDelta commitAcceleratedMouseDelta(MouseDelta reusedObject);
}

View File

@@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2018 Chansol Yang <CosmicSubspace@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
package org.kde.kdeconnect.Plugins.MousePadPlugin
/* Base class for a pointer acceleration profile. */
abstract class PointerAccelerationProfile {
/* Class representing a mouse delta, a pair of floats.*/
class MouseDelta(@JvmField var x: Float = 0f, @JvmField var y: Float = 0f)
/* Touch coordinate deltas are fed through this method. */
abstract fun touchMoved(deltaX: Float, deltaY: Float, eventTime: Long)
/* An acceleration profile should 'commit' the processed delta when this method is called.
* The value returned here will be directly sent to the desktop client.
*
* A MouseDelta object will be provided by the caller to reduce object allocations.*/
abstract fun commitAcceleratedMouseDelta(reusedObject: MouseDelta): MouseDelta
}

View File

@@ -7,6 +7,8 @@
package org.kde.kdeconnect.Plugins.MousePadPlugin;
import androidx.annotation.NonNull;
public class PointerAccelerationProfileFactory {
/* The simplest profile. Merely adds the mouse deltas without any processing. */
@@ -20,17 +22,14 @@ public class PointerAccelerationProfileFactory {
accumulatedY += deltaY;
}
@NonNull
@Override
public MouseDelta commitAcceleratedMouseDelta(MouseDelta reusedObject) {
MouseDelta result;
if (reusedObject == null) result = new MouseDelta();
else result = reusedObject;
result.x = accumulatedX;
result.y = accumulatedY;
reusedObject.x = accumulatedX;
reusedObject.y = accumulatedY;
accumulatedY = 0;
accumulatedX = 0;
return result;
return reusedObject;
}
}
@@ -110,21 +109,18 @@ public class PointerAccelerationProfileFactory {
* for the touch delta. ( mouse_delta = touch_delta * multiplier ) */
abstract float calculateMultiplier(float speed);
@NonNull
@Override
public MouseDelta commitAcceleratedMouseDelta(MouseDelta reusedObject) {
MouseDelta result;
if (reusedObject == null) result = new MouseDelta();
else result = reusedObject;
/* This makes sure that only the integer components of the deltas are sent,
* since the coordinates are converted to integers in the desktop client anyway.
* The leftover fractional part is stored and added later; this makes
* the cursor move much smoother in slow speeds. */
result.x = (int) accumulatedX;
result.y = (int) accumulatedY;
reusedObject.x = (int) accumulatedX;
reusedObject.y = (int) accumulatedY;
accumulatedY = accumulatedY % 1.0f;
accumulatedX = accumulatedX % 1.0f;
return result;
return reusedObject;
}
}