From 62158fc19923e71a320668102265232da67f759d Mon Sep 17 00:00:00 2001 From: Jan Holthuis Date: Wed, 5 Jan 2022 12:39:44 +0100 Subject: QML: Add positiveModulo() JS helper function --- res/qml/Library.qml | 4 +--- res/qml/Mixxx/MathUtils.mjs | 14 ++++++++++++++ 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; +}; -- cgit v1.2.3