| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #pragma once |
| 3 | #include <QEasingCurve> |
| 4 | #include <KConfigGroup> |
| 5 | #include <QString> |
| 6 | #include <algorithm> |
| 7 | |
| 8 | namespace ClickTween { |
| 9 | struct Curve { const char *name; QEasingCurve::Type type; }; |
| 10 | inline constexpr Curve curves[] = { |
| 11 | {"Linear", QEasingCurve::Linear}, |
| 12 | #define FAMILY(name) {"easeIn" #name, QEasingCurve::In##name}, {"easeOut" #name, QEasingCurve::Out##name}, {"easeInOut" #name, QEasingCurve::InOut##name}, |
| 13 | FAMILY(Sine) FAMILY(Quad) FAMILY(Cubic) FAMILY(Quart) FAMILY(Quint) |
| 14 | FAMILY(Expo) FAMILY(Circ) FAMILY(Back) FAMILY(Elastic) FAMILY(Bounce) |
| 15 | #undef FAMILY |
| 16 | }; |
| 17 | inline QEasingCurve::Type curveType(const QString &name) { |
| 18 | for (const auto &curve : curves) { |
| 19 | if (name == QLatin1String(curve.name)) return curve.type; |
| 20 | } |
| 21 | return QEasingCurve::OutCubic; |
| 22 | } |
| 23 | inline QString curveName(const QString &name) { |
| 24 | for (const auto &curve : curves) { |
| 25 | if (name == QLatin1String(curve.name)) return name; |
| 26 | } |
| 27 | return QStringLiteral("easeOutCubic"); |
| 28 | } |
| 29 | struct Settings { |
| 30 | int clickTime = 150; |
| 31 | int releaseTime = 720; |
| 32 | QString clickEasing = QStringLiteral("easeOutBack"); |
| 33 | QString releaseEasing = QStringLiteral("easeOutElastic"); |
| 34 | void read(const KConfigGroup &group) { |
| 35 | const Settings defaults; |
| 36 | clickTime = std::clamp(group.readEntry("ClickTweenTime", defaults.clickTime), 0, 5000); |
| 37 | releaseTime = std::clamp(group.readEntry("ReleaseTweenTime", defaults.releaseTime), 0, 5000); |
| 38 | clickEasing = curveName(group.readEntry("ClickEasing", defaults.clickEasing)); |
| 39 | releaseEasing = curveName(group.readEntry("ReleaseEasing", defaults.releaseEasing)); |
| 40 | } |
| 41 | }; |
| 42 | } |