effects: Use chrono to specify the animation times

Makes both the API and its uses explicit in terms of what unit the times
are on.
This commit is contained in:
Aleix Pol 2024-03-05 19:15:55 +01:00 committed by Aleix Pol Gonzalez
parent f82542790c
commit 1d57c9a5af
20 changed files with 72 additions and 38 deletions

View file

@ -444,16 +444,16 @@ QPointF Effect::cursorPos()
return effects->cursorPos();
}
double Effect::animationTime(const KConfigGroup &cfg, const QString &key, int defaultTime)
double Effect::animationTime(const KConfigGroup &cfg, const QString &key, std::chrono::milliseconds defaultTime)
{
int time = cfg.readEntry(key, 0);
return time != 0 ? time : std::max(defaultTime * effects->animationTimeFactor(), 1.);
return time != 0 ? time : std::max(defaultTime.count() * effects->animationTimeFactor(), 1.);
}
double Effect::animationTime(int defaultTime)
double Effect::animationTime(std::chrono::milliseconds defaultTime)
{
// at least 1ms, otherwise 0ms times can break some things
return std::max(defaultTime * effects->animationTimeFactor(), 1.);
return std::max(defaultTime.count() * effects->animationTimeFactor(), 1.);
}
int Effect::requestedEffectChainPosition() const

View file

@ -860,18 +860,18 @@ public:
* @param defaultTime default animation time in milliseconds
*/
// return type is intentionally double so that one can divide using it without losing data
static double animationTime(const KConfigGroup &cfg, const QString &key, int defaultTime);
static double animationTime(const KConfigGroup &cfg, const QString &key, std::chrono::milliseconds defaultTime);
/**
* @overload Use this variant if the animation time is hardcoded and not configurable
* in the effect itself.
*/
static double animationTime(int defaultTime);
static double animationTime(std::chrono::milliseconds defaultTime);
/**
* @overload Use this variant if animation time is provided through a KConfigXT generated class
* having a property called "duration".
*/
template<typename T>
int animationTime(int defaultDuration);
int animationTime(std::chrono::milliseconds defaultDuration);
/**
* Linearly interpolates between @p x and @p y.
*
@ -897,9 +897,9 @@ public Q_SLOTS:
};
template<typename T>
int Effect::animationTime(int defaultDuration)
int Effect::animationTime(std::chrono::milliseconds defaultDuration)
{
return animationTime(T::duration() != 0 ? T::duration() : defaultDuration);
return animationTime(T::duration() != 0 ? std::chrono::milliseconds(T::duration()) : defaultDuration);
}
/**

View file

@ -14,6 +14,8 @@
#include <QDBusConnection>
#include <QTimer>
using namespace std::chrono_literals;
namespace KWin
{
BlendChanges::BlendChanges()
@ -36,7 +38,7 @@ bool BlendChanges::supported()
void KWin::BlendChanges::start(int delay)
{
int animationDuration = animationTime(400);
int animationDuration = animationTime(400ms);
if (!supported() || m_state != Off) {
return;

View file

@ -16,6 +16,8 @@
// KConfigSkeleton
#include "diminactiveconfig.h"
using namespace std::chrono_literals;
namespace KWin
{
@ -79,7 +81,7 @@ void DimInactiveEffect::reconfigure(ReconfigureFlags flags)
: nullptr;
m_fullScreenTransition.timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(250))));
std::chrono::milliseconds(static_cast<int>(animationTime(250ms))));
effects->addRepaintFull();
}
@ -207,7 +209,7 @@ void DimInactiveEffect::scheduleInTransition(EffectWindow *w)
{
TimeLine &timeLine = m_transitions[w];
timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(160))));
std::chrono::milliseconds(static_cast<int>(animationTime(160ms))));
if (timeLine.done()) {
// If the Out animation is still active, then we're trucating
// duration of the timeline(from 250ms to 160ms). If the timeline
@ -242,7 +244,7 @@ void DimInactiveEffect::scheduleOutTransition(EffectWindow *w)
{
TimeLine &timeLine = m_transitions[w];
timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(250))));
std::chrono::milliseconds(static_cast<int>(animationTime(250ms))));
if (timeLine.done()) {
timeLine.reset();
}

View file

@ -16,6 +16,8 @@
#include <cmath>
using namespace std::chrono_literals;
Q_LOGGING_CATEGORY(KWIN_FALLAPART, "kwin_effect_fallapart", QtWarningMsg)
static const QSet<QString> s_blacklist{
@ -64,7 +66,7 @@ void FallApartEffect::prePaintWindow(EffectWindow *w, WindowPrePaintData &data,
}
animationIt->lastPresentTime = presentTime;
animationIt->progress += time / animationTime(1000.);
animationIt->progress += time / animationTime(1s);
data.setTransformed();
}
effects->prePaintWindow(w, data, presentTime);

View file

@ -27,6 +27,8 @@
#include <cmath>
using namespace std::chrono_literals;
namespace KWin
{
@ -54,7 +56,7 @@ GlideEffect::~GlideEffect() = default;
void GlideEffect::reconfigure(ReconfigureFlags flags)
{
GlideConfig::self()->read();
m_duration = std::chrono::milliseconds(animationTime<GlideConfig>(160));
m_duration = std::chrono::milliseconds(animationTime<GlideConfig>(160ms));
m_inParams.edge = static_cast<RotationEdge>(GlideConfig::inRotationEdge());
m_inParams.angle.from = GlideConfig::inRotationAngle();

View file

@ -13,6 +13,8 @@
#include <QDBusConnection>
using namespace std::chrono_literals;
Q_LOGGING_CATEGORY(KWIN_HIGHLIGHTWINDOW, "kwin_effect_highlightwindow", QtWarningMsg)
namespace KWin
@ -20,7 +22,7 @@ namespace KWin
HighlightWindowEffect::HighlightWindowEffect()
: m_easingCurve(QEasingCurve::Linear)
, m_fadeDuration(animationTime(150))
, m_fadeDuration(animationTime(150ms))
, m_monitorWindow(nullptr)
{
#if KWIN_BUILD_X11

View file

@ -14,6 +14,8 @@
#include "kscreenconfig.h"
#include <QDebug>
using namespace std::chrono_literals;
/**
* How this effect works:
*
@ -79,7 +81,7 @@ void KscreenEffect::addScreen(Output *screen)
{
connect(screen, &Output::wakeUp, this, [this, screen] {
auto &state = m_waylandStates[screen];
state.m_timeLine.setDuration(std::chrono::milliseconds(animationTime<KscreenConfig>(250)));
state.m_timeLine.setDuration(std::chrono::milliseconds(animationTime<KscreenConfig>(250ms)));
setState(state, StateFadingIn);
});
connect(screen, &Output::aboutToTurnOff, this, [this, screen](std::chrono::milliseconds dimmingIn) {
@ -93,7 +95,7 @@ void KscreenEffect::reconfigure(ReconfigureFlags flags)
{
KscreenConfig::self()->read();
m_xcbState.m_timeLine.setDuration(
std::chrono::milliseconds(animationTime<KscreenConfig>(250)));
std::chrono::milliseconds(animationTime<KscreenConfig>(250ms)));
}
void KscreenEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime)

View file

@ -14,6 +14,8 @@
// KConfigSkeleton
#include "magiclampconfig.h"
using namespace std::chrono_literals;
namespace KWin
{
@ -43,9 +45,9 @@ void MagicLampEffect::reconfigure(ReconfigureFlags)
// TODO: Rename animationDuration to duration so we can use
// animationTime<MagicLampConfig>(250).
const int d = MagicLampConfig::animationDuration() != 0
? MagicLampConfig::animationDuration()
: 250;
const std::chrono::milliseconds d = MagicLampConfig::animationDuration() != 0
? std::chrono::milliseconds(MagicLampConfig::animationDuration())
: 250ms;
m_duration = std::chrono::milliseconds(static_cast<int>(animationTime(d)));
}

View file

@ -21,6 +21,8 @@
#include "opengl/glutils.h"
#include <KGlobalAccel>
using namespace std::chrono_literals;
namespace KWin
{
@ -90,7 +92,7 @@ void MagnifierEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::mill
const int time = m_lastPresentTime.count() ? (presentTime - m_lastPresentTime).count() : 0;
if (m_zoom != m_targetZoom) {
double diff = time / animationTime(500.0);
double diff = time / animationTime(500ms);
if (m_targetZoom > m_zoom) {
m_zoom = std::min(m_zoom * std::max(1 + diff, 1.2), m_targetZoom);
} else {

View file

@ -16,6 +16,8 @@
#include <QQuickItem>
#include <QTimer>
using namespace std::chrono_literals;
namespace KWin
{
@ -173,7 +175,7 @@ OverviewEffect::~OverviewEffect()
void OverviewEffect::reconfigure(ReconfigureFlags)
{
OverviewConfig::self()->read();
setAnimationDuration(animationTime(300));
setAnimationDuration(animationTime(300ms));
setFilterWindows(OverviewConfig::filterWindows());
for (const ElectricBorder &border : std::as_const(m_borderActivate)) {

View file

@ -18,6 +18,8 @@
#include <QDebug>
using namespace std::chrono_literals;
static void ensureResources()
{
// Must initialize resources manually because the effect is a static lib.
@ -94,7 +96,7 @@ void ScreenTransformEffect::addScreen(Output *screen)
auto &state = m_states[screen];
state.m_oldTransform = screen->transform();
state.m_oldGeometry = screen->geometry();
state.m_timeLine.setDuration(std::chrono::milliseconds(long(animationTime(250))));
state.m_timeLine.setDuration(std::chrono::milliseconds(long(animationTime(250ms))));
state.m_timeLine.setEasingCurve(QEasingCurve::InOutCubic);
state.m_angle = transformAngle(changeSet->transform.value(), state.m_oldTransform);
state.m_prev.texture = std::move(texture);

View file

@ -13,6 +13,8 @@
#include "scene/cursoritem.h"
#include "scene/workspacescene.h"
using namespace std::chrono_literals;
namespace KWin
{
@ -77,7 +79,7 @@ void ShakeCursorEffect::animateTo(qreal magnification)
m_scaleAnimation.setStartValue(m_currentMagnification);
m_scaleAnimation.setEndValue(magnification);
m_scaleAnimation.setDuration(animationTime(200));
m_scaleAnimation.setDuration(animationTime(200ms));
m_scaleAnimation.setEasingCurve(QEasingCurve::InOutCubic);
m_scaleAnimation.start();
@ -97,7 +99,7 @@ void ShakeCursorEffect::pointerEvent(MouseEvent *event)
if (m_shakeDetector.update(event)) {
inflate();
m_deflateTimer.start(animationTime(2000));
m_deflateTimer.start(animationTime(2000ms));
}
}

View file

@ -22,6 +22,8 @@
#include <QMatrix4x4>
#include <qmath.h>
using namespace std::chrono_literals;
namespace KWin
{
@ -39,10 +41,10 @@ void SheetEffect::reconfigure(ReconfigureFlags flags)
SheetConfig::self()->read();
// TODO: Rename AnimationTime config key to Duration.
const int d = animationTime(SheetConfig::animationTime() != 0
? SheetConfig::animationTime()
: 300);
m_duration = std::chrono::milliseconds(static_cast<int>(d));
const double d = animationTime(SheetConfig::animationTime() != 0
? std::chrono::milliseconds(SheetConfig::animationTime())
: 300ms);
m_duration = std::chrono::milliseconds(int(d));
}
void SheetEffect::prePaintWindow(EffectWindow *w, WindowPrePaintData &data, std::chrono::milliseconds presentTime)

View file

@ -25,6 +25,8 @@
Q_DECLARE_METATYPE(KWindowEffects::SlideFromLocation)
using namespace std::chrono_literals;
namespace KWin
{
@ -100,9 +102,9 @@ void SlidingPopupsEffect::reconfigure(ReconfigureFlags flags)
{
SlidingPopupsConfig::self()->read();
m_slideInDuration = std::chrono::milliseconds(
static_cast<int>(animationTime(SlidingPopupsConfig::slideInTime() != 0 ? SlidingPopupsConfig::slideInTime() : 150)));
static_cast<int>(animationTime(SlidingPopupsConfig::slideInTime() != 0 ? std::chrono::milliseconds(SlidingPopupsConfig::slideInTime()) : 150ms)));
m_slideOutDuration = std::chrono::milliseconds(
static_cast<int>(animationTime(SlidingPopupsConfig::slideOutTime() != 0 ? SlidingPopupsConfig::slideOutTime() : 250)));
static_cast<int>(animationTime(SlidingPopupsConfig::slideOutTime() != 0 ? std::chrono::milliseconds(SlidingPopupsConfig::slideOutTime()) : 250ms)));
auto animationIt = m_animations.begin();
while (animationIt != m_animations.end()) {

View file

@ -17,6 +17,8 @@
#include <QPainter>
using namespace std::chrono_literals;
namespace KWin
{
@ -82,7 +84,7 @@ SnapHelperEffect::~SnapHelperEffect()
void SnapHelperEffect::reconfigure(ReconfigureFlags flags)
{
m_animation.timeLine.setDuration(
std::chrono::milliseconds(static_cast<int>(animationTime(250))));
std::chrono::milliseconds(static_cast<int>(animationTime(250ms))));
}
void SnapHelperEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseconds presentTime)

View file

@ -13,6 +13,8 @@
#include <KGlobalAccel>
#include <KLocalizedString>
using namespace std::chrono_literals;
namespace KWin
{
@ -49,7 +51,7 @@ QVariantMap TilesEditorEffect::initialProperties(Output *screen)
void TilesEditorEffect::reconfigure(ReconfigureFlags)
{
setAnimationDuration(animationTime(200));
setAnimationDuration(animationTime(200ms));
}
void TilesEditorEffect::toggle()

View file

@ -16,6 +16,8 @@
#include <KGlobalAccel>
#include <KLocalizedString>
using namespace std::chrono_literals;
namespace KWin
{
@ -146,7 +148,7 @@ int WindowViewEffect::requestedEffectChainPosition() const
void WindowViewEffect::reconfigure(ReconfigureFlags)
{
WindowViewConfig::self()->read();
setAnimationDuration(animationTime(300));
setAnimationDuration(animationTime(300ms));
for (ElectricBorder border : std::as_const(m_borderActivate)) {
effects->unreserveElectricBorder(border, this);

View file

@ -32,6 +32,8 @@
#include "scene/cursoritem.h"
#include "scene/workspacescene.h"
using namespace std::chrono_literals;
namespace KWin
{
@ -234,9 +236,9 @@ void ZoomEffect::prePaintScreen(ScreenPrePaintData &data, std::chrono::milliseco
const float zoomDist = std::abs(target_zoom - source_zoom);
if (target_zoom > zoom) {
zoom = std::min(zoom + ((zoomDist * time) / animationTime(150 * zoomFactor)), target_zoom);
zoom = std::min(zoom + ((zoomDist * time) / animationTime(std::chrono::milliseconds(int(150 * zoomFactor)))), target_zoom);
} else {
zoom = std::max(zoom - ((zoomDist * time) / animationTime(150 * zoomFactor)), target_zoom);
zoom = std::max(zoom - ((zoomDist * time) / animationTime(std::chrono::milliseconds(int(150 * zoomFactor)))), target_zoom);
}
}

View file

@ -683,7 +683,7 @@ int ScriptedEffect::displayHeight() const
int ScriptedEffect::animationTime(int defaultTime) const
{
return Effect::animationTime(defaultTime);
return Effect::animationTime(std::chrono::milliseconds(defaultTime));
}
bool ScriptedEffect::registerScreenEdge(int edge, const QJSValue &callback)