summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Holthuis <jholthuis@mixxx.org>2022-01-05 12:39:44 +0100
committerJan Holthuis <jholthuis@mixxx.org>2022-01-11 19:53:42 +0100
commit62158fc19923e71a320668102265232da67f759d (patch)
tree3203d9bcaacada58806e11277ddc8345bdb47760
parent0bc7711524cd1e3b5b5d967a439ab95b3ace4f75 (diff)
QML: Add positiveModulo() JS helper function
-rw-r--r--res/qml/Library.qml4
-rw-r--r--res/qml/Mixxx/MathUtils.mjs14
2 files changed, 15 insertions, 3 deletions
diff --git a/res/qml/Library.qml b/res/qml/Library.qml
index 7b88561f25..d6589927ef 100644
--- a/res/qml/Library.qml
+++ b/res/qml/Library.qml
@@ -28,9 +28,7 @@ Item {
if (rowCount == 0)
return ;
- let newIndex = currentIndex = (currentIndex + value) % rowCount;
- while (newIndex < 0)newIndex += rowCount
- currentIndex = newIndex;
+ currentIndex = Mixxx.MathUtils.positiveModulo(currentIndex + value, rowCount);
}
function loadSelectedTrackIntoNextAvailableDeck(play) {
diff --git a/res/qml/Mixxx/MathUtils.mjs b/res/qml/Mixxx/MathUtils.mjs
index e7b500b7b4..5e69447ba9 100644
--- a/res/qml/Mixxx/MathUtils.mjs
+++ b/res/qml/Mixxx/MathUtils.mjs
@@ -7,3 +7,17 @@
export const clamp = function(value, min, max) {
return Math.max(Math.min(value, max), min);
};
+
+/**
+ * @param {number} x Value
+ * @param {number} m Modulus
+ * @returns {number} Result of y where y = x modulo m and y > 0
+ */
+export const positiveModulo = function(x, m) {
+ console.assert(m > 0);
+ let result = x % m;
+ while (result < 0) {
+ result += m;
+ }
+ return result;
+};